Introduction to Arduino Paco Abad May 20 th, 2011 WGM #21 Outline What is Arduino? Where to start Types Shields Alternatives Know your board Installing and using the IDE Digital output Serial communication 2 1
What is Arduino? Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. Arduino can sense the environment by receiving input from a variety of sensors and can affect its surroundings by controlling lights, motors, and other actuators. 22 3 Where to start: http://arduino.cc http://www.ladyada.net/learn/arduino(this presentation is based on their tutorials) Google: arduino (5.550.000 hits on April 2011) 4 2
Types of Arduinos Nano Uno Lilypad Mega 2560 Pro BT Mini Fio 5 Shields Shields are boards that sit on top of the Arduino to extend its functionality MIDI Shield microsd 2Gb Module for Arduino Communication Shield (XB, BT, RFID) http://www.cooking-hacks.com 6 3
Shields Shields are boards that sit on top of the Arduino to extend its functionality Motor Driver Shield CAN-BUS Shield Ethernet Shield http://www.cooking-hacks.com 7 Shields Shields are boards that sit on top of the Arduino to extend its functionality Relay Shield Music Shield Input Shield http://www.cooking-hacks.com 8 4
Alternatives TI LaunchPad http://www.ti.com/launchpad 4.30$ C Basic Stamp 2 http://parallax.com 49$ Basic 9 Netduino http://netduino.com 34.95$ C# Bare controllers Picaxe Propeller PIC AVR Alternatives 10 5
What do I need to start? http://arduino.cc/en/main/software 11 Know your board (between 7 and 12V) 12 6
Know your board Microcontroller: ATmega168 / ATmega328 OperatingVoltage: 5V Input Voltage(recommended): 7-12V Input Voltage(limits): 6-20V Digital I/O Pins: 14 (of which6 providepwm output) AnalogInput Pins: 6 DC Currentper I/O Pin: 40 ma DC Currentfor3.3V Pin: 50 ma Flash Memory: 16 KB (ATmega168) or 32 KB (ATmega328) of which 2 KB usedbybootloader SRAM: 1 KB (ATmega168) or 2 KB (ATmega328) EEPROM: 512 bytes (ATmega168) or1 KB (ATmega328) Clock Speed: 16 MHz 13 Options: Power your board USB If Diecimila, select the power type Wall-wart: 2.1mm, center-positive - + Battery 14 7
Options: Take power from your board Voltage input (coming from the wall-wart) Ground (two pins) 5 V (from the regulator, or from the USB) 3.3 V (50mA maximum) 15 Talk to/listen to your board Options: 14 digital input/output pins. 5V, 40mA max, pull up resistor 6 PWM output pins 6 analog input pins. 0-5V, 10 bits (1024 levels) 16 8
Installing the IDE http://arduino.cc/en/guide/windows Download and unzip your IDE from http://arduino.cc/en/main/software Connect the Arduino to a USB port and install the driver (for Windows, in folder arduino\drivers\ftdi USB Drivers) Execute the IDE: 17 Select your board Installing the IDE 18 9
Select your port Installing the IDE You can find out which port to use in the Device Manager: 19 Arduino: The IDE Tab manager Source code Editor Results window 20 10
The toolbar Verify the program (compile) Stop the serial monitor New source code file (sketch) Open sketch Save sketch [Compile and] upload the program Open the serial monitor 21 The serial monitor Computer Arduino Arduino Computer Communication speed 22 11
Digital output: Hello world Open the Blink example File\Examples\1. Basics\Blink 23 Digital output: Hello world /* Blink Turns on an LED on for one second, then off for one second, repeatedly. This example code is in the public domain. */ void setup() { // initialize the digital pin as an output. // Pin 13 has an LED connected on most Arduino boards: pinmode(13, OUTPUT); } Mandatory function: executed once, when Arduino boots or resets void loop() { digitalwrite(13, HIGH); delay(1000); digitalwrite(13, LOW); delay(1000); } // set the LED on // wait for a second // set the LED off // wait for a second Mandatory function: executed over and over while Arduino is powered 24 12
Digital output: Hello world Compile it and upload it to the board 25 Digital output: Hello world In initialization, we have to declare how we will use the digital pins: pinmode(pin, INPUT OUTPUT) The pins are input by default We can turn on and off output pins with: digitalwrite(pin, HIGH LOW) We can tell Arduino to take a nap delay(ms) 26 13
Digital output: Exercise 1 Change the delays to turn on the LED during 10 ms and turn it off during 10 ms. What happens? Wave it And if you turn it on for 10 ms and turn it off for 5 ms? 27 Digital output: PWM Side note: we have discovered PWM! wikipedia.org 28 14
Digital output: Exercise 2 Connect another LED with a 1kΩ resistor to the pin 12 (and ground) and simulate a strobe light 29 Serial communications Arduino communicates with the O.S. through USB, using a virtual serial port Arduino shows when it receives or sends serial data, and give access to the TX (transmission) and RX (reception) lines (digital pins 1 and 0) 30 15
Serial communications: output /* * Hello World! * * This is the Hello World! for Arduino. * It shows how to send data to the computer */ void setup() { // run once, when the sketch starts Serial.begin(9600); // set up Serial library at 9600 bps // prints hello with ending line break Serial.println("Hello world!"); } void loop() // run over and over again { // do nothing! } 31 Serial communications: output When the Serial monitor starts, the board resets (you can also use the button in the board) Serial.println("Hello world!") Serial.begin(9600) 32 16
Serial communications: output Serial.println(val) val: an expression of any type Serial.println(val, format) format: DEC, HEX, OCT, BIN, BYTE Serial.print(val) Serial.print(val, format) 33 Serial communications: input int incomingbyte = 0; void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { incomingbyte = Serial.read(); Serial.print("I received: "); Serial.println(incomingByte, DEC); } } 34 17
Serial communications: input Serial.available() Returns the number of available bytes Serial.read() Returns the next byte, or -1 if there is none available 35 Serial communications: input Exercise: Create a serial cypherer: when arduino receives a character between a and z, it returns the following character (next(z) == a). When it receives something else, it outputs the same character 36 18
The simplest input device is the switch There are many types of switch 37 Before using the Arduino, let s check that all the components work 38 19
/* File\Examples\1. Basics\DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */ void setup() { Serial.begin(9600); pinmode(2, INPUT); } void loop() { int sensorvalue = digitalread(2); Serial.println(sensorValue, DEC); } Using a cable, connect pin 2 to GND orto 5V 39 pinmode(2, INPUT); digitalread(pin) pin: the pin to read from Returns the state of the pin (HIGH or LOW) 40 20
A floating input (an unconnected input pin) will return noise (random highs and lows) 41 To read the state of a pushbutton, we may use the following circuit: Where is the problem? 42 21
The actual circuit we use uses a resistor, to pull-down or pull-up the pin, when the switch is open pull-down pull-up 43 Arduino provides a switchable pull-up resistor for digital input pins. It is activated with: pinmode(2, INPUT); digitalwrite(2, HIGH); // pull-up So the circuit is simplified to: 44 22
With a pull-up resistor, when the pushbutton is not pressed the pin reads HIGH, and viceversa 45 Exercise Write a program to activate the LED in the board (pin 13) when the button is pressed 46 23
Exercise Write a program to toggle on and off the LED in the board when the button is pressed 47 Contact bounce 48 24
Contact bounce occurs in state changes, not when the button is being pressed down or when it is not pressed There are several techniques to debounce a switch, but we will use just a delay: int val, val2; val = digitalread(switchpin); delay(10); val2 = digitalread(switchpin); if (val!= val2) return; // Bounce // val contains the settled value 49 25