Course Code: MDP 454, Course Name:, Second Semester 2014 Arduino
What is Arduino? Microcontroller Platform Okay but what s a Microcontroller? Tiny, self-contained computers in an IC Often contain peripherals Different packages available Vast array of size and power available
Why Arduino? Students, hobbyist, artists & designers open source hardware Open Source Physical Computing Platform open source free to inspect & modify community wiki, forums, tutorials
What can it do? Input Sensors (buttons, light sensors, ultrasound, ) Digital Input (Serial, SPI, I2C) Output LEDs & Lasers LCD/OLED/Touch Displays Speakers Motors, Servos, Steppers Control and Communication Drive other machinery Directly or using a communication protocol
Sensors
Arduino hardware Arduino models differ in design but all share some basic functionality AVR Microcontroller Atmel AVR Mega168 or AVR Mega 328 Powered either directly or via USB power Communications Serial (older models) USB (most models) Connections to a USB interface (smaller models) Pins for various functions
Digital pins Pins 0-13 (GPIO pins) Can output 40 ma Either input our output (default to input)
Digital pins Switching without Pull-up and pull-down resistors
Digital pins Switching with Pull-up and pull-down resistors Pull-down Pull-up
Digital pins What happens if we Digital Read a pin that is not connected? 20K internal pull-up resistor Serial pins 0 (RX) and 1 (TX). Connected to FTDI USB-to-TTL Pin 13 is different (input) (RES + LED) Analogue pins A0-A5 can be used as digital pins 14-19 (GPIO) Read up to 5V. Don t fry your pin!
Pulse Width Modulation (PWM) PWM used to create analogue output on digital pins Simulate voltage between 5V and 0V using square waves (signal switching between on and off) Pins 3, 5, 6, 9, 10, and 11
Analogue pins Pins A0-A5 (signal switching between on and off) 10 bit resolution (from 0-5V to int 0-1023) 100 microseconds (0.0001 s) to read an analog input, maximum reading rate is about 10,000 times a second.
Other pins ICSP (In Circuit Serial Programming) directly program AVR micro-controllers I2C / TWI (Two-wire interface) communication Inter IC control Arduino analog input A4 = I2C SDA (serial data line) Arduino analog input A5 = I2C SCL (serial clock line) External interrupts Event-handling without polling Interrupt 0 (on digital pin 2) and 1 (on digital pin 3) Analogue reference (Set analogue reference for ADC)
Arduino Environment
The Arduino Environment
Board Type
Serial (COM) port
The Environment
Parts of the code
Comments Comments can be anywhere
Comments Comments can be anywhere Comments created with // or /* and */
Comments Comments can be anywhere Comments created with // or /* and */ Comments do not affect code
Comments Comments can be anywhere Comments created with // or /* and */ Comments do not affect code You may not need comments, but think about the community!
Operators The equals sign = is used to assign a value == is used to compare values
Operators And & Or && is and is or
Variables Basic variable types: Boolean Integer Character String
Declaring variables Boolean: boolean variablename;
Declaring variables Boolean: boolean variablename; Integer: int variablename;
Declaring variables Boolean: boolean variablename; Integer: int variablename; Character: char variablename;
Declaring variables Boolean: boolean variablename; Integer: int variablename; Character: char variablename; String: stringname [ ];
Assigning variables Boolean: variablename = true; or variablename = false;
Assigning variables Boolean: variablename = true; or variablename = false; Integer: variablename = 32767; or variablename = -32768;
Assigning variables Boolean: variablename = true; or variablename = false; Integer: variablename = 32767; or variablename = -32768; Character: variablename = A ; or stringname = SparkFun ;
Variable Scope
Setup void setup ( ) { } The setup function comes before the loop function and is necessary for all Arduino sketches
Setup void setup ( ) { } The setup header will never change, everything else that occurs in setup happens inside the curly brackets
Setup Outputs are declare in setup, this is done by using the pinmode function This particular example declares digital pin # 13 as an output, remember to use CAPS
Setup Serial communication also begins in setup This particular example declares Serial communication at a baud rate of 9600. More on Serial later...
Setup You can also create internal pullup resistors in setup, to do so digitalwrite the pin HIGH This takes the place of the pullup resistors currently on your circuit 7 buttons
Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } You can designate an interrupt function to Arduino pins # 2 and 3 This is a way around the linear processing of Arduino
Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } Interrupt: the number of the interrupt, 0 or 1, corresponding to Arduino pins # 2 and 3 respectively Function: the function to call when the interrupt occurs Mode: defines when the interrupt should be triggered
Setup Setup, Interrupts void setup ( ) { attachinterrupt (interrupt, function, mode) } LOW whenever pin state is low CHANGE whenever pin changes value RISING whenever pin goes from low to high FALLING whenever pin goes from low to high Don t forget to CAPITALIZE
If statements if ( this is true ) { do this; }
If statements if ( this is true ) { do this; }
If statements if ( this is true ) { do this; }
If statements if ( this is true ) { do this; }
Else else { do this; }
Basic Repetition loop For while
Basic Repetition void loop ( ) { }
Basic Repetition void loop ( ) { }
Basic Repetition void loop ( ) { } The void in the header is what the function will return (or spit out) when it happens, in this case it returns nothing so it is void
Basic Repetition void loop ( ) { } The loop in the header is what the function is called, sometimes you make the name up, sometimes (like loop) the function already has a name
Basic Repetition void loop ( ) { } The ( ) in the header is where you declare any variables that you are passing (or sending) the function, the loop function is never passed any variables
Basic Repetition void loop ( ) { }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here //this could be anything }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition for (int count = 0; count<10; count++) { //for action code goes here }
Basic Repetition while ( count<10 ) { //while action code goes here }
Basic Repetition while ( count<10 ) { //while action code goes here //should include a way to change count //variable so the computer is not stuck //inside the while loop forever }
Basic Repetition while ( count<10 ) { //looks basically like a for loop //except the variable is declared before //and incremented inside the while //loop }
Basic Repetition Or maybe: while ( digitalread(buttonpin)==1 ) { //instead of changing a variable //you just read a pin so the computer //exits when you press a button //or a sensor is tripped }
Arduino Programming Structure
Arduino Software Layout Declare variables at top Initialize setup() run once at beginning, set pins Running loop() run repeatedly, after setup()
Arduino Software Layout int ledpin = 13; led connected to control pin 13 Global Variables int asensor = 0; setup sensor 'asensor' on analog pin 0 int statepin = LOW; use this to hold the state of a pin
Arduino Software Layout setup() pinmode() set a pin as input or output serial.begin() setup to `talk' to the computer
Arduino Software Layout setup() pinmode(ledpin, Output); set the pin `ledpin' as an output serial.begin(9600); talk to the computer at 9600 baud rate
Arduino Software Layout loop() digitalwrite() set a digital pin high/low digitalread() read a digital pin s state analogread() read an analog pin analogwrite() write an analog PWM value delay() wait an amount of time millis() get the current tim
Input / Output 14 Digital IO (pins 0-13) 6 Analog In (pins 0-5) 6 Analog Out (pins 3, 5, 6, 9, 10, 11)
Digital? Analog? Digital only has two values: on/off Analog has many (infinite) values Computers don t really do analog - use quantization instead
Digital? Analog? Many states (Analog) or just two (HIGH/LOW - Digital) Number of states (or bins ) is resolution Common computer resolutions: 8-bit = 256 states 16-bit = 65,536 states 32-bit = 4,294,967,296 states
I/O to/from What? Sensors LDR / IR Switch / Potentiometer / Joystick / Piezo Accelerometer Ultrasonic Indicators LED / Lamps Buzzers Actuators Solenoid Stepper Motor Other Circuits Prototype shields Special ICs
Hello World! void setup() { Run Arduino IDE // start serial port at 9600 bps: Serial.begin(9600); } Install latest Arduino IDE from arduino.cc Write the code on the left into the editor Compile / Verify the code by clicking the play button void loop() Before uploading your sketch, check the board and the serial port are correct for your Arduino and for { your computer Serial.print("Hello World!\n\r"); // wait 2sec for next reading: delay(2000); Menu -> Tools -> Board Menu -> Tools -> Serial Port } Upload the code from the computer to the Arduino using the upload button
LED Blink Turn Led on and off according to the byte coming from the serial port, if a zero byte then the Led off else the Led on.
LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { while (Serial.available()>0) { inbyte = Serial.read(); } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off } }
LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ Initialise int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { while (Serial.available()>0) { inbyte = Serial.read(); } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off } } some of the variables
LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } void loop() { Setup LED pin and while (Serial.available()>0) { inbyte = Serial.read(); serial connection } } if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the LED on } else { digitalwrite(ledpin, LOW); // sets the LED off }
LED Blink /* Blinking LED --- * turns on and off a light emitting diode(led) connected to a digital * pin, based on data coming over serial */ int ledpin = 13; // LED connected to digital pin 13 int inbyte = 0; void setup() { pinmode(ledpin, OUTPUT); // sets the digital pin as output Serial.begin(19200); // initiate serial communication } Loop - Reading the void loop() { serial for info, when } while (Serial.available()>0) { inbyte = Serial.read(); something is received } turn if (inbyte>0) { digitalwrite(ledpin, HIGH); // sets the } else { digitalwrite(ledpin, LOW); // sets the LED off } theled on LED on
Digital Input/ Output Write Arduino code that uses to turn led on (on pin 13) when the button status is high (on pin 7) and led off when the button status is low; Also send this status via serial port with baud rate 19600.
Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } }
Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } } Initialise some of the variables
Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ intledpin = 13; // choose the pin for the LED int inpin = 7; // choose the input pin (button) int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton as input Serial.begin(19200); // start serial communication to computer } void loop() { buttonval = digitalread(inpin); // read the pin and get the button's state if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } } Setup LED pin, switch pin and serial connection
Digital Input/ Output /* Digital reading, turns on and off a light emitting diode (LED) connected to digital * pin 13, when pressing a pushbutton attached to pin 7. It illustrates the concept of * Active-Low, which consists in connecting buttons using a 1K to 10K pull-up resistor. */ int inpin = 7; // choose the input pin (button) button for info, when intledpin = 13; // choose the pin for the LED int buttonval = 0; // variable for reading the pin status void setup() { pinmode(ledpin, OUTPUT); // set LED as output pinmode(inpin, INPUT); // set pushbutton asthe LED on and input } Serial.begin(19200); // start serial communication to computer void loop() { } buttonval = digitalread(inpin); // read the pin and get if (buttonval == HIGH) { // check if the input is HIGH (button released) digitalwrite(ledpin, LOW); // turn LED OFF Serial.write('0'); // Button off (0) sent to computer } else { digitalwrite(ledpin, HIGH); // turn LED ON Serial.write('1'); // Button on (1) sent to computer } Loop - Reading the button is press turn signal the computer change Of the button's state
Thank You For Your Attention! Questions?