Introduction To Arduino
Introduction To Arduino
MICROCONTROLLER
Hands-on Research in Complex Systems
Shanghai Jiao Tong University
June 17 29, 2012
Instructor: Thomas E. Murphy (University of Maryland)
Assisted by: Hien Dao (UMD), Caitlin Williams (UMD) and
(SJTU)
What is a Microcontroller
(C, MCU)
Used in:
Cellphones,
Toys
Household appliances
Cars
Cameras
So what is Arduino?
Its a movement, not a microcontroller:
Founded by Massimo Banzi and David
Cuartielles in 2005
Based on Wiring Platform, which dates to
2003
Open-source hardware platform
Open source development environment
Easy-to learn language and libraries (based
on Wiring language)
Integrated development environment (based
on Processing programming environment)
Available for Windows / Mac / Linux
Arduino Uno
Arduino Leonardo
Arduino LilyPad
Arduino Mega
Arduino Nano
Arduino Mini
Arduino Mini Pro
Arduino BT
Arduino-like Systems
Cortino (ARM)
Xduino (ARM)
LeafLabs Maple
(ARM)
BeagleBoard (Linux)
Wiring Board
(Arduino
predecessor)
14 digital inputs/outputs
(6 PWM outputs)
Power
indicator
Reset
Button
USB connection
16 MHz clock
Voltage regulator
AC/DC adapter
jack
ATmega328P
DC voltage
supply
(IN/OUT)
6 analog
inputs
Compiler
Hardware Interface
Uploading programs
Communicating with
Arduino via USB
Serial Monitor
Program area
Save
New
Messages /
Errors
Open
Arduino Reference
voidloop()
Will be executed
repeatedly
voidsetup(){
//putyoursetupcodehere,torunonce:
}
voidloop(){
//putyourmaincodehere,torunrepeatedly:
}
voidsetup(){
//initializethedigitalpinasanoutput.
//Pin13hasanLEDconnectedonmostArduino boards:
pinMode(13,OUTPUT);
}
voidloop(){
digitalWrite(13,HIGH);//settheLEDon
delay(1000);//waitforasecond
digitalWrite(13,LOW);//settheLEDoff
delay(1000);//waitforasecond
}
http://www.wikipedia.org/
Notes:
Resistor is needed to limit current
Resistor and LED may be
interchanged
(but polarity of LED is important)
Pin 13 is special: has built-in
resistor and LED
Change program and upload
Connected together
300 mils
Connected together
Experimenting
Change the blink rate
how fast can the LED blink (before you can
no longer perceive the blinking?)
Activity 2: Seven-Segment
Display
Write a that program that counts from 0 to
9 and displays the result on a sevensegment LED display
Consider writing a function:
voidwriteDigit(int n)
that writes a single digit
ABCDEFG
07E
030
06D
079
033
05B
05F
070
07F
07B
A
on
off
on
on
off
on
on
on
on
on
B
on
on
on
on
on
off
off
on
on
on
C
on
on
off
on
on
on
on
on
on
on
D
on
off
on
on
off
on
on
off
on
on
E
on
off
on
off
off
off
on
off
on
off
F
on
off
off
off
on
on
on
off
on
on
G
off
off
on
on
on
on
on
off
on
on
Useful:
bitRead(x,n)
Get the value of the nth bit of an integer x
Example:
bitRead(0x7E,7);
//returns1 (seetableabove)
Serial.begin(baud)
Initialize serial port for communication (and sets baud
rate)
Note: Serial.end() command
is usually unnecessary, unless
Example:
Serial.print(val),Serial.print(val,fmt)
Prints data to the serial port
Examples:
Serial.begin(9600); //9600baud
Serial.print(Hi);
//printastring
Serial.print(78);//workswithnumbers,too
Serial.print(variable); //workswithvariables
Serial.print(78,BIN);
//willprint1001110
Serial.println(val)
Same as Serial.print(), but with line-feed
Serial Monitor:
letter=Serial.read()
letters=Serial.readBytesUntil(character,buffer,length)
number=Serial.parseInt()
number=Serial.parseFloat()
//returns5
//returns10
//returns1
Aside: Potentiometers
(variable resistors, rheostats)
Activity 6 Arduino
Thermometer
Time functions
#include<Time.h>
setTime(hr,min,sec,day,month,yr)
hour(),minute(),day(),month(),year()
Analog Output?
Most microcontrollers have
only digital outputs
Pulse-width Modulation:
Analog variables can be
represented by the dutycycle (or pulse-width) of a
digital signal
http://arduino.cc/en/Tutorial/PWM
analogWrite(pin,val)
set the PWM fraction:
val =0: always off
val =255: always on
Servomotors
Standard servo:
http://www.parallax.com/
Continuous-rotation servo:
duty cycle sets speed and/or direction
Logic-level MOSFET
(requires only 5 V)
Acts like a voltagecontrolled switch
Works with PWM!
Electromechanically
-actuated switch
Provides electrical
isolation
Typically few ms
response time
Note: Arduino cannot supply
enough current to drive relay coil
http://en.wikipedia.org/
MISO (12)
MOSI (11)
SS (10)
SPI pins:
SCK = serial clock
MISO = master in, slave out
MOSI = master out slave in
SS = slave select
I2C pins:
SDA = data line
SCL = clock line
SDA (A4)
SCL (A5)
EXPLANATION
Wire.begin()
Wire.endTransmission(address)
Wire.beginTransmission(B1100000);//Byte1(Initiatecommunication)
Wire.write(B01000000);//Byte2(commandandpowerdownmode)
Wire.write(data>>4);//Byte3(sendbitsD11..D4)
Wire.write((data&B00001111)<<4);//Byte4(sendbitsD3..D0)
Wire.endTransmission();
Remember: you must include the Wire library at the top:
#include<Wire.h>
and you must also use Wire.begin() in setup
EXPLANATION
Wire.begin()
Wire.begin(address)
Wire.beginTransmission(address)
Wire.write(byte)
Wire.write(bytes,length)
Wire.endTransmission(address)
Wire.requestFrom(address,quantity)
Wire.requestFrom(address,quantity,stop)
Wire.available()
Wire.read()
http://www.sparkfun.com/
Note: the I2C bus requires pullup resistors on SCL and SDA
(provided on the board)
EXPLANATION
SPI.begin()
byteIn =SPI.transfer(byteOut)
EXPLANATION
SPI.begin()
SPI.end()
SPI.setBitOrder(order)
SPI.setClockDivider(divider)
SPI.setDataMode(mode)
SPI.transfer(byte)
Features:
six independent, 3wiper potentiometers
8-bit precision
(256 possible levels)
Available in 10k,
50k and 100k
Programmed
through SPI interface
Note: same as SS
(slave select)
MISO (12)
MOSI (11)
SS (10)
digitalWrite(SS,LOW);//holdSSpinlowtoselectchip
SPI.transfer(potnumber); //determinewhichpot(0..5)
SPI.transfer(wipervalue);//transfer8bitwipersetting
digitalWrite(SS,HIGH);
//deselectthechip