Lab7: Introduction To Arduino
Lab7: Introduction To Arduino
to Arduino
ENGG1100 Engineering Design I
Study this document before coming to the lab
Demonstrate your results of the following
exercises to a TA before the lab ends.
Part (b) of Exercise 7.1 (page 28)
Part (b) of Exercise 7.2 (page 33)
Exercise 7.3 (page 41)
Exercise 7.4d (page 52)
Overview
• Theory
• Introduction to Arduino
• Hardware system structure
• Programming structure
• Practice
• Experiment1: LED control
• Experiment1: Input/output functions
• Experiment2: Pulse width modulation (PWM)
• Experiment3: Finite State machines (FSM)
10/3/2014 2
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Introduction to Arduino
• Arduino is a computation tool for sensing and
controlling signals
• It is more convenient and cost effective than using
a personal computer PC.
• It's an open-source system in terms of hardware
and software.
• You can download the Integrated Development
Environment (IDE) for your own OS from http://
arduino.cc/en/Main/Software
• Follow the instruction to install the IDE
10/3/2014 3
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Arduino UNO
• Microcontroller is based on 14 digital input/output (I/O) pins
13,12,… ………2,1,0
ATmega328
• If needed , download Arduino Integrated
D
evelopment Environment IDE http://ardui
no.cc/en/Main/Software#toc1
10/3/2014 4
Start to use
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
the Arduino
IDE
• To start Arduino IDE,
click Start Menu All
Programs Arduino
Select Board
10/3/2014 6
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Select Port
10/3/2014 7
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Arduino IDE
Tool Bar Serial monitor,
Can use this to issue
commands to the
board, and read
outputs from the
board.
This programming Area
is called “Sketch”.
Status Message
10/3/2014 8
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Toolbar
• Verify
• Checks code for errors
• Upload
• Compiles and uploads code to the Arduino I/O board
• New
• Creates a new sketch
• Open
• Open sketch
• Save
• Save sketch
• Serial Monitor
• Display serial data being sent from the Arduino board
10/3/2014 9
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Arduino Code
To run a program in Arduino, your sketch should
contain two methods
void setup()
{
// initialization of variables, pin modes, libraries
// run once after each power up or reset
}
void loop()
{
// loops the content consecutively
// allowing the program to change and respond
}
10/3/2014 10
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
10/3/2014 11
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
10/3/2014 12
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Meaning of INPUT,
OUTPUT, INPUT_PULLUP
HIGH(5V) or LOW(0V)
• INPUT: Arduino
• OUTPUT:
HIGH(5V) or LOW (0V) Arduino
High(5V))
• INPUT_PULLUP:
1KΩ
When the pin is not Arduino
connect to HIGH(5V) or LOW)
or
anything, it is HIGH not_connected_to_anything
10/3/2014 14
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Basic Function(step2a) –
digitalWrite()
• digitalWrite() is used to write a HIGH or a LOW
value to a digital pin
• Syntax
digitalWrite(pin, value) // comment
• pin: the number of the pin whose value you wish to set
• value: HIGH (5 V) or LOW (Ground)
• Example:
• digitalWrite(pin, value) // comment
• E.g
• digitalWrite(1, HIGH)//set pin1 to HIGH
10/3/2014 15
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Basic Function(step2b) –
digitalRead()
• digitalWrite() is used to read the value from a
specified digital pin, either HIGH or LOW
• Syntax
digitalRead(pin)
• pin: the number of the pin whose mode you want to
read (integer)
• Example:
• digitalRead(pin)// read the state of the
• // it can be “HIGH” or
“LOW”
10/3/2014 16
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
• Syntax
delay(ms)
• ms: the number of milliseconds to pause (unsigned long)
10/3/2014 17
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
10/3/2014 18
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
10/3/2014 19
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
More Functions
• Please visit http://arduino.cc/en/Reference/ for
Arduino Language Reference
10/3/2014 21
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Experiment 7.1:
Blinks an LED
Turns on/off an Active-LOW LED
10/3/2014 22
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
24
10/3/2014 25
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
LED7 is ON for
a second and
OFF for three
second
10/3/2014 27
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Exercise 7.1
a) Write a program to turn on and off each of the
LEDs (LED0,… LED7) one at a time. Such that,
LED0 is turn on for a second and off for one
second, then it will be the turn for LED1 and so
on till LED7 is selected. After that, start again with
LED0.
b) Why all LEDs light up at the beginning? Change
your program to solve this problem?
Advanced exercise (to be done in your spare time if
you are interested): Display the state of the LED using
the serial monitor. See
http://arduino.cc/en/Serial/Print#.UxFaulPYFCs
10/3/2014 28
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
• void loop() {
• //to be filled by students
• }
10/3/2014 29
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Experiment 7.2:
Get Input
Use an input value to control an LED
10/3/2014 30
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
void loop() {
inputState = digitalRead(inputPin); //get status
if (inputState == LOW) { // GND is connected
digitalWrite(ledPin, HIGH); // turn off LED
}
else {
digitalWrite(ledPin, LOW); //turn on LED
}
}
10/3/2014 31
demo (7.2) Expected
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Input/output
13 12 11 10 9 8 7 6 5 4 3 2 1 0
Exercise 7.2
a) Explain what you see after running demo72.ino
b) Modify the code so that the input pin is 3 (instead
of 2) and output LED is 6 (instead of 7). Run the
code to verify your result.
Advanced exercise (to be done in your spare time if
you are interested): Use the keyboard of your PC to
control the on and off of the LEDS. See
http://arduino.cc/en/Serial/read#.UxFdTPmSxIE
10/3/2014 33
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Experiment 7.3:
Pulse Width
Modulation (PWM)
Use PWM to control the intensity an LED
10/3/2014 34
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
PWM in Arduino
• The green lines represents
a regular time period i.e.
inverse of PWM frequency
• Arduino’s default PWM
frequency is approximately
500 Hz i.e. a period is 2 ms
• Use analogWrite() to
control the pulse width
• Varying LED’s brightness
• Varying motor’s speed
• Only pin 3, 5, 6, 9, 10, and
11 can be used for PWM Courtesy of Arduino.cc
10/3/2014 36
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
PWM outputs
13 12 11 10 9 8 7 6 5 4 3 2 1 0
analogRead()
• analogRead() is used to read the value from the
specified analog pin
• The input voltage between 0 V and 5 V will be mapped
into integer values between 0 and 1023 i.e. 4.9 mV/unit
• Syntax
return = analogRead(pin)
• pin: the number of the analog input pin to read from 0 V
to 5 V
• return: integer from 0 to 1023
10/3/2014 38
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
analogWrite()
• analogWrite() is used to set the duty cycle of a
PWM pulse
• After a call to analogWrite(), the pin will generate a
steady square wave of the specified duty cycle until the
next call to analogWrite(), digitalRead() or digitalWrite()
on the same pin
• Syntax
analogWrite(pin, value)
• pin: the pin to write to
• value: the duty cycle between 0 (always OFF) and 255
(always ON)
10/3/2014 39
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
void loop() {
int dtwait = 1000;
analogWrite (ledPin, 255-0); //LED OFF,when value=255,LED=off
delay (dtwait);
analogWrite (ledPin, 255-100); // a dimmer LED
delay (dtwait);
analogWrite (ledPin, 255-255); // full bright LED, when value=0
delay (dtwait);
}
10/3/2014 40
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Exercise 7.3
• Write the program to control the intensity of LED5
continuously and repeatedly from dark to full and
dark again within a period of five seconds.
• Hint: Change intensity every 0.5 seconds, put the
statements inside: Void Loop( ) { Your code }
• Advanced exercise (to be done in your spare time if
you are interested): Use the 7 LEDS as an intensity
ramp: intensity changes from low to high for LED0
to LED7 at the beginning and then gradually reverse
the pattern continuously and repeatedly at 1Hz.
(Hints: may need to use for())
10/3/2014 41
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Experiment 7.4:
Finite State
Machine (FSM)
Use FSM to control a system
10/3/2014 42
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Logic Control
• Logic control is an essential part to develop an
intelligence device
10/3/2014 43
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
A FSM demo74a.ino
(FSM with no input)
Transition condition:
• delay 1 second
Transition condition:
delay 2 seconds
10/3/2014 44
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
//demo74a.ino •
•
void loop() {
switch(state) {
•
case STATE1:
• //demo74a.ino • delay(1000);
•
• #define STATE1 1 state=STATE2;
• break;
• #define STATE2 2 • case STATE2:
• pinMode (3, OUTPUT); • case STATE_END: // turn off the LEDs, this state is
not used here
• default:
• state=STATE_END;
• break;
• } }
10/3/2014 45
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Demo 7.4b:
Two-State FSM with input
• When a magnetic strip is detected, the LED is ON
• When there is no magnetic strip, the LED is OFF
State Transition Table State Diagram
Input Current State Next State
Magnetic strip is ON ON
detected
Magnetic strip is OFF ON
detected
No magnetic strip ON OFF
is detected
No magnetic strip OFF OFF
is detected
10/3/2014 46
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
10/3/2014 47
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
• void loop() {
• //demo74b.ino • switch(state) {
• #define STATE1 1 • case STATE1:
• case STATE_END:
• pinMode (magnetic, INPUT);
• digitalWrite(ledPin_S1, HIGH); // LEDOFF
• pinMode (ledPin_S1, • digitalWrite(ledPin_S2, HIGH); // LED OFF
OUTPUT); pinMode break;
(ledPin_S2, OUTPUT); • default:
• } • state=STATE_END;
• break; }}
10/3/2014 48
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Exercise 7.4c
• Write a FSM program that controls two LEDs
through two input signals, the specification is
shown in flow diagram 7.4c in the next slide
• Use inputs
• pin0 for sensor1, and
• pin1 for sensor2.
• The values of input signals (sensor 1 and sensor 2) can be
either GND (LOW) or 5V (HIGH)
• Use outputs
• pin5 for LED1 and
• pin6 for LED2
10/3/2014 49
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Exercise 7.4c
State Diagram 7.4c
10/3/2014 50
Introd. | Arduino | basic func. | exp71-LED | exp72-Input | exp73-PWM | exp74-FSM
Hints
• //hint for ans74.c
• void loop() {
• #define STATE1 1
• switch(state) {
• #define STATE2 2
• case STATE1:
• #define STATE3 3
• digitalWrite(led1, LOW);
• #define STATE4 4
• digitalWrite(led2, LOW);
• #define STATE_END 100
• if ((digitalRead(sensor1) == LOW) &&
(digitalRead(sensor2) == HIGH)) state=STATE2;
• int sensor1 = 0; • else if ((digitalRead(sensor1) == HIGH) &&
• int sensor2 = 1; (digitalRead(sensor2) == LOW)) state=STATE3;
Exercise 7.4d
• Improve the previous exercise with the following
conditions
• When both LEDs are ON, they should be in full
brightness
• When either LED is lighted up, the brightness of that LED
should be as low as 10 %
• Hint use: analogWrite(led1, 255-25); //will give 10% LED light
10/3/2014 52
Summary
• Learned the use of
• the Arduino microcontroller
• digital input / outputs functions
• some basic functions
• if-then-else and switch-case control statements in
programs
• Finite state machines
10/3/2014 53