Modulo 2 A –
Plataforma Arduino
Raúl Melo
Universidad Nacional – Sede Palmira
2016
Que es?
Codigo Abierto
• Open Source Hardware
• Open Source Bootloader
• Open Source Development Kit
• Community Driven Support
Terminologia
• I/O Board - main microcontroller
• Shield - add-on boards
• Sketch - the program
• Sensor - components (thermistors, etc.)
• Modules - serial data (GPS module, etc.)
Que se puede hacer?
Digital IO (LEDs, switches)
Analog IO (resistive sensor data)
Serial Connection (Sensors, GPS, etc)
Program from your computer
Your limit is only your creativity!
Arduino Uno Close Up
• The pins are in three groups:
• Invented in 2010
• 14 digital pins
• 6 analog pins
• power
Arduino I/O Boards
More Boards
15 current boards
Compatible Boards
These are two of MANY!
Compatible Boards
Shiel
ds
Datalogging Shield
Shields
Wave Shield
Touchscreen Shield
More Shields…
XBee Shield
Ethernet Shield Wifi Shield
And more shields…
Even more shields!
APRS Shield
Modules
Bluetooth Module
GPS Module
Temperature &
Humidity Sensor
RFID Module
Sensors and
Modules
Gas Sensor Temp & Humidity
Fingerprint Scanner
Flex Sensor
Geiger Counter
Sensor
s Photoresistor, infared, force sensitive resistor, Hall effect,
Piezo, tilt sensor..
Sketches
Includes
Globals
void setup()
void loop()
Amateur Radio Applications
Applications - APRS Trackduino
Argent Data
Other Examples
• Weather Stations
• Remote Antenna Switches
Getting Started
Sparkfun Getting Started in Arduino Kit ($95)
Sparkfun Starter Kit for Arduino ($60)
Adafruit Experimentation Kit ($85)
Adafruit Starter Pack ($65)
OR
Arduino Board (Uno $20 or Mega $35) +
Breadboard, wires, components, etc.
With Starter Sets
Book is open source!
Other Open Source Hardware
Raspberry Pi
TI Launchpad
Arduino Types
• Many different versions
• Number of input/output channels
• Form factor
• Processor
• Leonardo
• Due
• Micro
• LilyPad
• Esplora
• Uno
Leonardo
• Compared to the Uno, a slight upgrade.
• Built in USB compatibility
• Bugs?
Presents to
PC as a mouse
or keyboard
Due
• Much faster processor, many more pins
• Operates on 3.3 volts
• Similar to the Mega
Micro
• When size matters: Micro, Nano, Mini
• Includes all functionality of the Leonardo
• Easily usable on a breadboard
LilyPad
• LilyPad is popular for clothing-based projects.
Esplora
• Game controller
• Includes joystick, four buttons, linear potentiometer (slider),
microphone, light sensor, temperature sensor, three-axis
accelerometer.
• Not the standard set of IO pins.
Arduino Compiler
• Download current compiler from:
arduino.cc/en/Main/software
• Arrogantly refers to itself as an IDE (Ha!).
• Run the software installer.
• Written in Java, it is fairly slow.
Visit
playground.arduino.cc/Main/ Devel
opmentTools
for alternatives to the base
Configuring the Arduino
Compiler
• Defaults to COM1, will probably need to change the COM port setting
(my work PC uses 7).
• Appears in Device Manager (Win7) under Ports as a Comm port.
Arduino Program Development
• Based on C++ without 80% of the instructions.
• A handful of new commands.
• Programs are called 'sketches'.
• Sketches need two functions:
• void setup( )
• void loop( )
• setup( ) runs first and once.
• loop( ) runs over and over, until power is lost or a new sketch is
loaded.
Arduino C
• Arduino sketches are centered around the pins on an Arduino board.
• Arduino sketches always loop.
• void loop( ) {} is equivalent to while(1) { }
• The pins can be thought of as global variables.
Arduino C Specific Functions
• pinMode(pin, mode)
Designates the specified pin for input or output
• digitalWrite(pin, value)
Sends a voltage level to the designated pin
• digitalRead(pin)
Reads the current voltage level from the designated pin
• analog versions of above
• analogRead's range is 0 to 1023
• serial commands
• print, println, write
Compiler Features
• Numerous sample
sketches are included in
the compiler
• Located under File,
Examples
• Once a sketch is written,
it is uploaded by clicking
on File, Upload, or by
pressing <Ctrl> U
Arduino C is Derived from C++
These programs blink an LED on pin
13
• avr-libc • Arduino C
#include <avr/io.h> void setup( ) {
#include <util/delay.h> pinMode(13, OUTPUT);
}
int main(void) {
while (1) { void loop( ) {
PORTB = 0x20; digitalWrite(13, HIGH);
_delay_ms(1000); delay(1000);
PORTB = 0x00; digitalWrite(13, LOW);
_delay_ms(1000); delay(1000);
} }
return 1;
}
Basic Electric Circuit
• Every circuit (electric or electronic) must have at least a power source
and a load.
• The simplest circuit is a light.
• Plug in the light, and it lights up.
• Unplug it, the light goes out.
• Electricity flows from the power source, through the load (the light)
and then back to the power source.
Blink Sketch
void setup( ) { Connected to Connected to
one end of
pinMode(13, OUTPUT); the circuit
other end of
the circuit
}
void loop( ) {
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
delay(1000);
}
4 LED Blink Sketch
void setup( ) { void loop( ) {
pinMode(1, OUTPUT); digitalWrite(1, HIGH);
delay (200);
pinMode(3, OUTPUT);
digitalWrite(1, LOW);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT); digitalWrite(3, HIGH);
} delay (200);
digitalWrite(3, LOW);
digitalWrite(5, HIGH);
delay (200);
digitalWrite(5, LOW);
digitalWrite(7, HIGH);
delay (200);
digitalWrite(7, LOW);
Inputs
• Digital inputs will come to the Arduino as either on or off (HIGH or
LOW, respectively).
• HIGH is 5VDC.
• LOW is 0VDC.
• Analog inputs will come to the Arduino as a range of numbers, based
upon the electrical characteristics of the circuit.
• 0 to 1023
• .0049 V per digit (4.9 mV)
• Read time is 100 microseconds (10,000 a second)
Analog Input
• A potentiometer (variable
resistor) is connected to analog
pin 0 to an Arduino.
• Values presented to pin 0 will
vary depending upon the
resistance of the potentiometer.
Analog Input-Application
• The variable resistor can be replaced with a sensor.
• For example, a photo resistor.
• Depending upon the light level at the photo resistor:
• Turn on a light
• Increase or decrease the brightness of an LED (or an LED array)
• Most sensors are simply variable resistors, but vary their resistance
based on some physical characteristic.
Sensors
• Sensors can be both binary or a range.
• Usually, sensors that measure a range of values vary their resistance
to reflect their detection.
• Arduinos can only sense voltages, not resistances.
• Sensors that only vary their resistances require a circuit called a
voltage divider to provide the Arduino a voltage.
Common Sensors
• Dials on a radio are • Infrared sensor & light
simply potentiometers • Hall effect sensor and
• Temperature magnet
• Light • Ball tilt sensor (for
• Angle measuring orientation)
• Switches • Force
• did the user throw a
switch or push a button?
• Accelerometer
(measures motion and
tilt)
Topic 1: Meet Arduino
Uno
Getting Started
• Check out: http://arduino.cc/en/Guide/HomePage
1. Download & install the Arduino environment (IDE)
(not needed in lab)
2. Connect the board to your computer via the USB cable
3. If needed, install the drivers (not needed in lab)
4. Launch the Arduino IDE
5. Select your board
6. Select your serial port
7. Open the blink example
8. Upload the program
Arduino IDE
See: http://arduino.cc/en/Guide/Environment for more information
Select Serial Port and Board
todbot.com/blog/bionicarduino
Input/Output
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Topic 2: Digital
Input/Output
1
• Digital IO is binary valued—
it’s either on or off, 1 or 0
0
• Internally, all
microprocessors are digital,
why?
Arduino Digital
I/0
www.mikroe.com/chapters/view/1
pinMode(pin, mode)
Sets pin to either INPUT or OUTPUT
digitalRead(pin)
Reads HIGH or LOW from a pin
digitalWrite(pin, value)
Writes HIGH or LOW to a pin
Electronic stuff
Output pins can provide 40 mA of current
Writing HIGH to an input pin installs a 20KΩ pullup
Our First Program
IO Pins
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
In-class Exercise 1: Digital
IO
• Use a push-button to turn ON/Off LED
Topic 3: Analog Input
• Think about music stored on a CD---an analog signal
captured on digital media
• Sample rate
• Word length
Arduino Analog Input
Image credit: Tod Kurt
• Resolution: the number of different voltage levels (i.e., states) used
to discretize an input signal
• Resolution values range from 256 states (8 bits) to 4,294,967,296
states (32 bits)
• The Arduino uses 1024 states (10 bits)
• Smallest measurable voltage change is 5V/1024 or 4.8 mV
How does ADC work?
• Excel Demonstration
Topic 3: Analog Output
• Can a digital devise produce analog output?
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
• Analog output can be simulated using
pulse width modulation (PWM)
Pulse Width Modulation
• Can’t use digital pins
to directly supply say
2.5V, but can pulse
the output on and off
really fast to produce
the same effect
• The on-off pulsing
happens so quickly,
the connected output
device “sees” the
result as a reduction
in the voltage
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
PWM Duty Cycle
output voltage = (on_time / cycle_time) * 5V
Image credit: Tod Kurt
Fixed cycle length; constant
number of cycles/sec
PMW Pins
• Command:
analogWrite(pin,value)
• value is duty cycle:
between 0 and 255
• Examples:
analogWrite(9, 128)
for a 50% duty cycle
analogWrite(11, 64)
for a 25% duty cycle
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
In-class Exercise 2: Analog
IO
Part 1:
A light theremin
In-class Exercise 2: Analog
IO
Part 2: Add an LED
• Add a 330 ohm resistor and an LED to pin 9
• Using the analogWrite() command, set the
intensity of the LED as a function of the value of
prReading
Topic 4: Serial
Communication
Image from http://www.ladyada.net/learn/arduino/lesson4.html
todbot.com/blog/bionicarduino
Serial Communication
• Compiling turns your program into
binary data (ones and zeros)
• Uploading sends the bits through
USB cable to the Arduino
• The two LEDs near the USB
connector blink when data is
transmitted
• RX blinks when the Arduino is
receiving data
• TX blinks when the Arduino is
transmitting data
todbot.com/blog/bionicarduino
Open the Serial Monitor and Upload
the Program
Some Commands
• Serial.begin()
- e.g., Serial.begin(9600)
• Serial.print() or Serial.println()
- e.g., Serial.print(value)
• Serial.read()
• Serial.available()
• Serial.write()
• Serial.parseInt()
/ Program Name: Serial Echo
// note: serial bytes are read as ASCII
// (American Standard Code for Information Interchange)
// characters
byte myByte; // a variable to store a byte read from the serial
// buffer
void setup() {
Serial.begin(9600); // begin serial communications at 9600 bps
void loop() {
if (Serial.available()>0) {
while(Serial.available()>0){ // while bytes remain in the serial
// buffer
myByte = Serial.read(); // read in the current byte
// = is ASCII character 61
// 0-9 are ASCII characters 48 to 57
// - is ASCII character 45
// + is ASCII character 43
Serial.print("ASCII Character Value of Byte Read: \n");
Serial.write(myByte);
Serial.print('\n');
Serial.print("ASCII Decimal Value of Byte Read: \n");
Serial.print(myByte); // prints ASCII character corresponding
// to myByte
Serial.print('\n');
}
Serial-to-USB chip---what does it do?
The LilyPad and Fio Arduino require an external USB to
TTY connector, such as an FTDI “cable”.
In the Arduino Leonardo a single microcontroller runs the
Arduino programs and handles the USB connection.
Image from Theory and Practice of Tangible User Interfaces at UC Berkley
Two different communication
protocols
Serial (TTL):
Image from http://www.fiz-ix.com/2013/02/introduction-to-arduino-serial-communication/
USB Protocol
Image from http://en.wikipedia.org/wiki/USB
• Much more complicated
In-class Exercise 3: Serial
Communication
Modify your program from in-class
exercise 2-part 2 to control the
intensity of the LED attached to pin 9
based on keyboard input.
Use the Serial.parseInt() method to
read numeric keyboard input as an
integer.
An input of 9 should produce full
intensity and an input of 0 should turn
the LED off.