What Is Arduino?: Arduino Web Site: WWW - Arduino.cc
What Is Arduino?: Arduino Web Site: WWW - Arduino.cc
LilyPad Arduino
Ethernet shield
Xbee shield
Instructor: Dr. Yu.Vlasov (Not to scale) 3
Arduino Duemilanove Microcontroller
("Duemilanove" means 2009 in Italian)
Reset button
Microcontroller
chip
Power connector
Power Analog
pins Input
pins
Instructor: Dr. Yu.Vlasov 5
Arduino Board Schematic
It has:
• 14 digital input/output pins (of which 6 can be used as PWM (Pulse
Width Modulation) outputs)
• 6 analog inputs
• a 16 MHz crystal oscillator
• a USB connection
• a power jack
• an ICSP (In-Circuit Serial Programming) header
• a reset button.
Instructor: Dr. Yu.Vlasov 7
Digital or Analog?
USB connector
Power connector
SRAM 2 KB (ATmega328)
EEPROM 1 KB (ATmega328)
Clock Speed 16 MHz
Instructor: Dr. Yu.Vlasov 11
Using the breadboard
(Socket board)
The bread board has many strips of metal The metal strips are laid out as shown in
(copper usually) which run underneath the orange. The long top and bottom row of
board. holes are usually used for power supply
connections.
UPLOAD BUTTON
INPUT AREA
STATUS BAR
PROGRAM NOTIFICATION AREA
Instructor: Dr. Yu.Vlasov 16
Arduino software
void loop()
{
}
Instructor: Dr. Yu.Vlasov 20
Arduino “Language”
int inputVariable = 0;
y = y + 3;
x = x - 7;
i = j * 6;
r = r / 5;
int a = 5;
int b = 10; Run this program.
int c = 20;
void setup()
{
What do you see on the
Serial.begin(9600); // set up Serial library at 9600 bps
Serial Monitor?
Serial.println("Here is some math: ");
Serial.print("a = ");
Serial.println(a);
Replace format “int” with
Serial.print("b = ");
Serial.println(b);
“float”
Serial.print("c = ");
Serial.println(c);
Example:
x = 2; // x = 2
x += 4; // x now contains 6
x -= 3; // x now contains 3
x *= 10; // x now contains 30
x /= 2; // x now contains 15
x == y (x is equal to y)
x != y (x is not equal to y)
x < y (x is less than y)
x > y (x is greater than y)
x <= y (x is less than or equal to y)
x >= y (x is greater than or equal to y)
if (inputPin == HIGH)
{
doThingA;
}
else
{
doThingB;
}
void setup()
{
initialization
// no setup needed
} condition
increment
void loop()
{
for (int i=0; i <= 255; i++)
{
analogWrite(PWMpin, i); statement
delay(10);
}
}
Instructor: Dr. Yu.Vlasov 34
“switch ... case”
switch...case controls the flow of programs by allowing
programmers to specify different code that should be executed in
various conditions.
When a case statement is found whose value matches that of the
variable, the code in that case statement is run.
Example:
switch (var)
{
case label:
// statements
break;
case label:
// statements
break;
default:
// statements
Instructor: Dr. Yu.Vlasov
} 35
“while” loop
while loops will loop continuously, and infinitely, until the
expression inside the parenthesis, () becomes false. Something
must change the tested variable, or the while loop will never exit.
This could be in your code, such as an incremented variable, or
an external condition, such as testing a sensor.
while(someVariable ?? value)
{
doSomething;
}
Example:
const int button2Pin = 2;
buttonState = LOW;
while(buttonState == LOW)
{
buttonState = digitalRead(button2Pin);
}
Example:
do
{
x = readSensor();
delay(10);
}
while (x < 100); // loops if x < 100
/* Blink
Turns an LED ON and OFF repeatedly.
There is already an LED on the board connected to pin 13.
*/
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(1000); // wait for a second
}
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output
}
void loop()
{
digitalWrite(ledPin, HIGH); // set the LED on
delay(ON); // wait for a second
digitalWrite(ledPin, LOW); // set the LED off
delay(OFF); // wait for a second
}
const int ledPin = 13; // the number of the LED pin. Constants won't change.
int ledState = LOW; // ledState used to set the LED. Variables will change
long previousMillis = 0; // will store last time LED was updated. Variables will change
long interval = 1000; // interval at which to blink (milliseconds)
void setup() {
pinMode(ledPin, OUTPUT); // set the digital pin as output:
}
void loop()
{
// check to see if it's time to blink the LED; that is, is the difference between the
current time and last time we blinked the LED bigger than the interval at which we want to
blink the LED.
if (millis() - previousMillis > interval)
{
previousMillis = millis(); // save the last time you blinked the LED
digitalWrite(ledPin, ledState); // set the LED with the ledState of the variable
}
}
LED
220 Ohm
220 Ohm
220 Ohm
void loop()
{
digitalWrite(led9Pin, HIGH); // set the LED on
delay(ON); // wait for time “ON” (ms)
digitalWrite(led9Pin, LOW); // set the LED off
delay(OFF); // wait for time “OFF” (ms)
15 KOhm
220 Ohm
Push
button
+5V
void setup()
{
pinMode(led13Pin, OUTPUT);
pinMode(button2Pin, INPUT);
}
void loop()
{
buttonState = digitalRead(button2Pin);
if (buttonState == HIGH)
{
digitalWrite(led13Pin, LOW);
}
else
{
digitalWrite(led13Pin, HIGH);
}
}
LED
15 KOhm
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
void setup()
{ // initialize digital pins
pinMode(led9Pin, OUTPUT);
pinMode(led10Pin, OUTPUT);
pinMode(led11Pin, OUTPUT);
pinMode(led13Pin, OUTPUT);
pinMode(button2Pin, INPUT);
}
void loop()
{
buttonState = digitalRead(button2Pin);
if (buttonState == HIGH)
{
digitalWrite(led13Pin, LOW);
analogWrite(pin, value)
Where:
pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255
(always on).
Instructor: Dr. Yu.Vlasov 49
Example 07a PWM. LED fading
/* Fading
This example shows how to fade an LED using the analogWrite() function.
The circuit: LED attached from digital pin 9 to ground.
*/
void setup()
{
// nothing happens in setup
}
void loop()
{
for(int fadeValue = 0 ; fadeValue <= 255; fadeValue++) // fade in from min to max in
increments
{
analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255)
delay(2); // wait for x milliseconds to see the dimming effect
}
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue--) // fade out from max to min in
increments
{
analogWrite(ledPin, fadeValue); // sets the value (range from 0 to 255)
delay(2); // wait for x milliseconds to see the dimming effect
}
}
LED
15 KOhm
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
+5V
void setup() { // turn on LED to signal the start of the calibration period:
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
while (millis() < 5000) { // calibrate during the first five seconds
sensorValue = analogRead(sensorPin);
void loop() {
sensorValue = analogRead(sensorPin); // read the sensor:
sensorValue = map(sensorValue, sensorMin, sensorMax, 0, 255); // apply the calibration to the sensor reading
sensorValue = constrain(sensorValue, 0, 255); // in case the sensor value is outside the range seen during calibration
analogWrite(ledPin, sensorValue); // fade the LED using the calibrated value:
}
GND
LED
15 KOhm
+5V
220 Ohm
220 Ohm
220 Ohm
220 Ohm
Push
button
+5V
void setup()
{
Serial.begin(9600); // initialize serial communication
}
void loop()
{
// establish variables for duration of the ping,
// and the distance result in inches and centimeters:
long duration, inches, cm;
// The same pin is used to read the signal from the PING))): a HIGH
// pulse whose duration is the time (in microseconds) from the sending
// of the ping to the reception of its echo off of an object.
pinMode(pingPin, INPUT);
duration = pulseIn(pingPin, HIGH); // Reads a pulse (either HIGH or LOW) on a pin.
Instructor: Dr. Yu.Vlasov 54
Example 08 PING sensor
(continued)
Serial.print(inches);
Serial.print("in, ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
delay(100);
}
+5V
void setup()
{
Serial.begin(9600); // start serial communication
}
void loop()
{
for(i = 0; i<=7; i++)
{ // gets 8 samples of temperature
samples[i] = ( 5.0 * analogRead(pin) * 100.0) / 1024.0;
tempc = tempc + samples[i];
delay(10);
}
tempc = tempc/8; // better precision
tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
if(tempc > maxi) {maxi = tempc;} // set max temperature
if(tempc < mini) {mini = tempc;} // set min temperature
Serial.print(tempc,DEC);
Serial.print(" Celsius, ");
Serial.print(tempf,DEC);
Serial.print(" fahrenheit -> ");
Serial.print(maxi,DEC);
Serial.print(" Max, ");
Serial.print(mini,DEC);
Serial.println(" Min");
tempc = 0;
delay(1000); // delay before loop
}
4 3 2 1
+5V
IR
LED
/* Sharp LM35DZ IR light detector with built-in signal processing sircuit for light modulation
Circuit:
Leg 1 is connected to Vcc (+5V)
Leg 2 is connected to digital pin 6
Leg 3 is connected to ground
Leg 4 is connected to negative terminal of IR LED
Positive terminal of IR LED is connected to Vcc (+5V)
*/
void setup()
{
pinMode(IR_SENSOR_PIN, INPUT);
Serial.begin(9600);
}
void loop()
{
IR_SENSOR_STATE = digitalRead(IR_SENSOR_PIN);
if (IR_SENSOR_STATE == LOW)
{
Serial.println("No object");
digitalWrite(ledPin, LOW);
}
else
{
Serial.println("Object detected");
digitalWrite(ledPin, HIGH);
}
delay(100);
}
• Start of program
• Measure temperature
- Is temperature < 100 F ?
• Yes, Turn on heat
- Is temperature > 102 F ?
• Yes, Turn on cooling fan
• Go back to start.
Measure
Temperature
No
Temp. Yes
Energize
> 102 Fan
No
Go back to start
Pseudo-Code Flowchart
Start: Main
Is button 1 pressed?
• Yes, Go sound Alarm
False True
• No, Go back to start Button 1
Pressed
Alarm: Speaker
• Sound speaker
2000Hz for
1 second
1. http://www.arduino.cc/en/Tutorial/HomePage
2. http://www.arduino.cc/en/Guide/HomePage
3. http://www.ladyada.net/learn/arduino/index.html -- an introduction to programming,
input / output, communication, etc. using Arduino
4. http://arduino.cc/en/Reference/HomePage -- Programming Language Reference
5. http://todbot.com/blog/spookyarduino/
6. http://dma.ucla.edu/senselab/topics/tags/arduino
7. http://www.freeduino.org/
8. http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl -- Arduino forum
PING
LED
15 KOhm
SIG
220 Ohm
220 Ohm
220 Ohm
GND
220 Ohm
Push
button
+5V
+5V