Modulo 2 A – Plataforma Arduino
Modulo 2 A – Plataforma Arduino
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)
15 current boards
Compatible Boards
Shields
Wave Shield
Touchscreen Shield
More Shields…
XBee Shield
APRS Shield
Modules
Bluetooth Module
GPS Module
Temperature &
Humidity Sensor
RFID Module
Sensors and
Modules
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
• 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
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
• Excel Demonstration
Topic 3: Analog Output
• Command:
analogWrite(pin,value)
• 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
// characters
// buffer
void setup() {
void loop() {
if (Serial.available()>0) {
// buffer
// = is ASCII character 61
// - is ASCII character 45
// + is ASCII character 43
Serial.write(myByte);
Serial.print('\n');
// to myByte
Serial.print('\n');
}
Serial-to-USB chip---what does it do?
Serial (TTL):