Arduino
Arduino
PhilRobotics
Objectives
Know what is a microcontroller Learn the capabilities of a microcontroller Understand how microcontroller execute instructions
Objectives
Learn how to program a microcontroller using Arduino
Configure a pin (Input, Output, Analog, PWM) Write Output State Read Input State Read Analog Input Use PWM
Microcontrollers
History of Microcontrollers
1971
Intel 4004 was released First microprocessor Followed by 8008 and then 8080
1975
Intel 8048 was released Intel President, Luke J. Valenter First Microcontroller
Microcontrollers
History of Microcontrollers
1976
Zilog, founded by Federico Faggin Z80 Gameboy, TI Calculators
1979
Decided to shift to embedded space Introduced Z8
Microcontrollers
History of Microcontrollers
1993
Microchip PIC16x84 was released First with EEPROM program memory
1993
Atmel First microcontroller with Flash memory
Microcontrollers
History of Microcontrollers
At present
Several companies followed the use of flash memory
Microcontrollers
Abstract Definition
Microcontrollers
Abstract Definition
Popular Microcontrollers
PIC16F84a
Developed by Microchip Widely used by hobbyist in the late 90s Very basic function Later replaced by pin compatible PIC16F628a
Popular Microcontrollers
PIC16F877a
Developed by Microchip Greater pin count than PIC16F84a Has ADC, PWM, UART and is self programmable
Popular Microcontrollers
PIC18F2550
Developed by Microchip With USB connectivity Used on Pinguino (Arduino equivalent for PIC)
Popular Microcontrollers
AtMega168
Atmega168
Microcontroller Developed by Atmel 8-bit Microcontroller 28pins 16Kbytes Flash Memory
Self-Programmable
Atmega168
Atmega168
Atmega168
Atmega168
Atmega168
Atmega168
Atmega168
Basic Setup
AtMega168
Basic Setup
Breadboard
Basic Setup
Supply
Basic Setup
Supply
Basic Setup
Crystal Oscillator
Basic Setup
Crystal Oscillator
Basic Setup
Resonator Bypass
Basic Setup
Resonator Bypass
Basic Setup
Reset Pullup
Basic Setup
Reset Pullup
Basic Setup
Filter Capacitor
Basic Setup
Filter Capacitor
Basic Setup
Bypass Capacitor
Basic Setup
Bypass Capacitor
Basic Setup
Reset Switch
Basic Setup
Reset Switch
Basic Setup
ADC Supply
Atmega168
Specifications
Pins
23 General Purpose IOs
Outputs
Outputs
Outputs
Outputs
Outputs
Outputs
Inputs
Inputs
Inputs
Atmega168
Specifications
Pins
23 General Purpose IOs 6 Channel 10bit ADC
ADC
ADC
ADC
ADC
Atmega168
Specifications
Pins
23 General Purpose IOs 6 Channel 10bit ADC 6 PWM Channels
Von Neuman
Hardvard
Programming Basic
Challenge I
Instruct the robot to draw a 10cm x 10cm yellow square starting 5cm from top and 5cm from the left side of the paper.
Programming Basic
Challenge II
Instruct the robot to draw that same square mentioned on Challenge I 10 times more.
Programming Basic
Challenge III
Instruct the robot to draw that same square for sets 10 pieces of paper forever
Who is Arduino?
What is Arduino
A project which began in Ivrea (Italy) in 2005
Massimo Banzi David Cuartielles
What is Arduino
Open Source Hardware Based on Wiring Open Source Software Based on Processing Multi-patform Programmed via USB
What is Arduino
named the project after a local bar
italian masculine first name "strong friend" English equivalent is "Hardwin"
What is Arduino
What is Arduino
Arduino
Arduino
Gizduino
Device Driver
Gizduino uses prolific chip
http://www.prolific.com.tw/eng/downloads.asp?id=31
Arduino Programming
Setup and Loop
void setup() { } void loop() { }
Arduino Programming
Setup()
will only run once, after each powerup or reset of the Arduino board
Arduino Programming
Setup()
int LEDPin; void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } void loop() { }
Arduino Programming
Setup()
int LEDPin; void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } void loop() { }
Arduino Programming
Loop()
Arduino Programming
Loop()
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Loop()
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Functions
a small set / group of instructions designed to operate on its given input
Arduino Programming
Functions
return type function name parameter list
int { }
Arduino Programming
Function name
Unique identifier for a function Inputs to a function Output of a function Describes what a function do
Arduino Programming
Calling a Functions
void loop() { result = get_Sum(2,3); } int get_Sum(int Var1, int Var2) { return Var1 + Var2; }
will give us a result of 5
Arduino Programming
Arduino Built-in Functions
Predefined functions to easily configure Arduino
Pin Operation Peripheral Operation
shortcuts
Arduino Programming
pinMode(pin, mode)
Configures a digital pin to behave either as an input or an output pin: the pin number whose mode you wish to set mode: either INPUT or OUTPUT
Example
pinMode(13, OUTPUT);
Arduino Programming
digitalWrite(pin, value)
Write a HIGH or a LOW value to a digital pin pin: the pin number whose value you wish to set value: either HIGH or LOW
Example
digitalWrite(13, HIGH);
Arduino Programming
digitalRead(pin)
Reads the value from a specified digital pin pin: the digital pin number you want to read returns either HIGH or LOW
Example
result = digitalRead(13);
Arduino Programming
analogRead(pin)
Reads the value from a specified analog pin pin: the analog pin number you want to read returns an integer (0 to 1023)corresponding to pin voltage
Example
result = analogRead(13);
result = pinV * 1023/Vref
Arduino Programming
analogWrite(pin, value)
Writes an analog value (PWM wave) to a pin pin will generate a steady square wave of the specified duty cycle 490 Hz
Arduino Programming
analogWrite(pin, value)
Can be used to vary
Brightness of LED Speed of the motor Low Pass Filter needed
Output a sound
Arduino Programming
analogWrite(pin, value)
pin: the pin to write to value: the duty cycle: between 0 (always off) and 255 (always on)
Duty_Cycle = value / 255 * 100% ~50% = 100 / 255 * 100%
Example
analogWrite(9, 127);
Arduino Programming
analogWrite(pin, value)
Arduino Programming
delay(value)
Pauses the program for the amount of time value: the number of milliseconds to pause
will pause for 1000ms, equivalent to 1sec pause
Example
delay(1000);
Arduino Programming
Blinking LED Example
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Comments
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Comments
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Comments
Starts with characters // Text after the start marker are ignored by the compiler This is for the programmer (person)
Arduino Programming
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Variables
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Variables
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Variables
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Variables
memory location in RAM that carries a value name is chosen to uniquely identify a variable the type defines
how much memory is used and how it is stored
Arduino Programming
Data Types
Type boolean char unsigned char byte int unsigned int word long unsigned long boolean character same as byte occupies 1 byte / 8bits integer, occupies 2 bytes / 16bit unsigned integer, occupies 2 bytes / 16bit same as unsigned int long integer, 4bytes / 32bits Description Sample true, false A-Z, a-z, CR, LF 0 -255 0 - 255 -32,768 to 32,767 0 to 65,535 0 to 65,535 -2,147,483,648 to 2,147,483,647 0 to 4,294,967,295
Arduino Programming
Declaring a Variables
int
VariableName1;
Arduino Programming
Declaring a Variables
type int name VariableName1;
Arduino Programming
Declaring a Variables
type int byte name VariableName1; VariableName2
Arduino Programming
Declaring a Variables
type int byte name VariableName1; VariableName2
Arduino Programming
Declaring a Variables
type int byte int name VariableName1; VariableName2 // not terminated by semicolon VariableName3, VariableName4, VariableName5;
Arduino Programming
Declaring a Variables
type int byte int name VariableName1; VariableName2 // not terminated by semicolon VariableName3, VariableName4, VariableName5;
Arduino Programming
Declaring a Variables
data type int byte int void name VariableName1; VariableName2 // not terminated by semicolon VariableName3, VariableName4, VariableName5; Variavle6;
Arduino Programming
Declaring a Variables
data type int byte int void name VariableName1; VariableName2 // not terminated by semicolon VariableName3, VariableName4, VariableName5; Variavle6; // there is no void type
Arduino Programming
Naming a Variable
int
Arduino Programming
Naming a Variable
int
Arduino Programming
Naming a Variable
int byte
Arduino Programming
Naming a Variable
int byte int
Arduino Programming
Naming a Variable
int byte int
Arduino Programming
Naming a Variable
int byte int
int
Serial,Number;
Arduino Programming
Naming a Variable
int byte int
int
Serial,Number;
Arduino Programming
Assigning Values
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Assigning Values
int LEDPin; // setup initializes the LED pin as output // and initially turned off void setup() { LEDPin = 13; pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Assigning Values
int LEDPin = 13; // setup initializes the LED pin as output // and initially turned off void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } // loop checks the button pin each time, void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); }
Arduino Programming
Decision Making
if statement used to compare
variable to a variable
if(variable1 > variable2) { function(); }
variable to a constant
Arduino Programming
Decision Making
execute the statement enclosed between { } if condition is satisfied
Arduino Programming
Decision Making
Comparison operator
Less than < Greater than > Equal == Not equal != Less than or equal <= Greater than or equal >=
Arduino Programming
Decision Making
if/else statement execute the statement enclosed between { } if condition is satisfied execute the statement enclosed between { } after else when not
Arduino Programming
Decision Making
Nested if/else statement
if(variable1 > variable2) { function1(); } else if(variable1 < variable2) { function2(); }
Arduino Programming
Decision Making
Nested if/else statement
if(variable1 > variable2) { function1(); } else if(variable1 < variable2) { function2(); } else { function3(); }
Arduino Programming
Blinking LED Example
int LEDPin = 13; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); } void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); } // set pin 13 output // Set initially to off
Arduino Programming
Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } void loop() { delay(500); digitalWrite(LEDPin, HIGH); delay(500); digitalWrite(LEDPin, LOW); } // set pin 13 output // Set initially to off // set pin 12 input
PinState = digitalRead(SWITCHPin);
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState, ledState; unsigned long previousMillis; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } // set pin 13 output // Set initially to off // set pin 12 input
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LEDPin, ledState); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState, LEDState; unsigned long previousMillis; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } // set pin 13 output // Set initially to off // set pin 12 input
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LEDPin, ledState); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState, LEDState; unsigned long previousMillis; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } // set pin 13 output // Set initially to off // set pin 12 input
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LEDPin, ledState); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState, LEDState; unsigned long previousMillis; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } // set pin 13 output // Set initially to off // set pin 12 input
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LEDPin, ledState); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Decision Making
Logical / boolean operator
Logic AND && Logic OR || Logic Not or Inversion !
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState, LEDState; unsigned long previousMillis; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); } // set pin 13 output // Set initially to off // set pin 12 input
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (ledState == LOW) { ledState = HIGH; } else { ledState = LOW; } digitalWrite(LEDPin, ledState); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState; unsigned long previousMillis; boolean LEDOn; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); }
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // check if >= 500mS // initialize prev with current
// if the LED is off turn it on and vice-versa: if (LEDOn) { LEDOn = false; } else { LEDOn = true; } digitalWrite(LEDPin, LEDOn); } PinState = digitalRead(SWITCHPin); } // Turn LED on/off
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12, PinState; unsigned long previousMillis; boolean LEDOn, BlinkEnabled = TRUE; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); }
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // if the if (LEDOn { LEDOn = } else { LEDOn = } } PinState = digitalRead(SWITCHPin); } // check if >= 500mS // initialize prev with current
digitalWrite(LEDPin, LEDOn);
Arduino Programming
Better Blinking LED Example
int LEDPin = 13, SWITCHPin = 12; unsigned long previousMillis; boolean LEDOn, BlinkEnabled; void setup() { pinMode(LEDPin, OUTPUT); digitalWrite(LEDPin, LOW); pinMode(SWITCHPin, INPUT); }
void loop() { unsigned long currentMillis = millis(); if(currentMillis - previousMillis >= 500) { previousMillis = currentMillis; // if the if (LEDOn { LEDOn = } else { LEDOn = } } BlinkEnabled = !digitalRead(SWITCHPin); } // check if >= 500mS // initialize prev with current
digitalWrite(LEDPin, LEDOn);
Arduino Programming
Looping
Starting Count
for loop execute expressions enclosed between {} continuously as long as a condition is satisfied
Limit Increment
Arduino Programming
Libraries
#include <Servo.h> Servo myservo; // create servo object to control a servo // a maximum of eight servo objects can be created // variable to store the servo position int pos = 0;
void loop() { for(pos = 0; pos < 180; pos += 1) { myservo.write(pos); 'pos' delay(15); position } for(pos = 180; pos>=1; pos-=1) { myservo.write(pos); 'pos' delay(15); position
// goes from 0 degrees to 180 degrees // in steps of 1 degree // tell servo to go to position in variable // waits 15ms for the servo to reach the
// goes from 180 degrees to 0 degrees // tell servo to go to position in variable // waits 15ms for the servo to reach the
Reference
www.arduin.cc Arduino Programming Notebook Getting Started with Arduino 3rd Edition
Beyond Arduino
Beyond Arduino
Processing
Beyond Arduino
Toolduino
Beyond Arduino
Mobile Processing
Beyond Arduino
S4A (Scratch for Arduino)
Beyond Arduino
P-Bot
Beyond Arduino
P-Bot
Arduino