Arduino Lecture
Arduino Lecture
A0A5
6 Analog inputs, they can also
be used as digital I/O pins
To start Arduino IDE,
click Start Menu All
Programs Arduino
Status Message
void loop() {
// put your main code here, to run
repeatedly:
}
setup : It is called only when the Arduino is
powered on or reset. It is used to initialize
variables and pin modes
Syntax
delay(ms)
◦ ms: the number of milliseconds to pause (unsigned
long)
Syntax
IF(condition1){
// do stuff if condition1 is true
}ELSE IF (condition2){
// do stuff only if condition1 is false
// and conition2 is true
}ELSE{
// do stuff when both condition1 and
// condition2 are false
}
Syntax
analogWrite(2,128);
// These constants won't change. They're used to give names to the pins used:
const int analogInPin = A0; // Analog input pin that the potentiometer is attached to
const int analogOutPin = 9; // Analog output pin that the LED is attached to
int sensorValue = 0; // value read from the pot
int outputValue = 0; // value output to the PWM (analog out)
void setup() {
// initialize serial communications at 9600 bps:
Serial.begin(9600);
}
void loop() {
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(analogOutPin, outputValue);
// print the results to the serial monitor:
Serial.print("sensor = " );
Serial.print(sensorValue);
Serial.print("\t output = ");
Serial.println(outputValue);
// wait 2 milliseconds before the next loop
// for the analog-to-digital converter to settle
// after the last reading:
delay(2);
}
Turns on/off an Active-LOW LED
14 digital input/output (I/O) pins
13,12,… ………2,1,0
Success
Connection:
LED will be
Connect to ON
PC through
USB Cable
//setup pinMode
void setup() {
for (int x=0; x<8; x++)
{
pinMode(x, OUTPUT);
}
}
void loop() {
//to be filled by students
}
Use an input value to control an LED
Use an integer variable “InputPin” to hold the pin number: 2
Use an integer variable “ledPin” to hold the led number: 7
//demo2.ino
int inputPin = 2; // the number of the input pin
int ledPin = 7; // the number of the LED pin
int inputState = 0; //variable for reading the input status
void setup () {
pinMode (ledPin, OUTPUT); //assign the pin as an output
pinMode (inputPin, INPUT); //assign the pin as an input
}
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
}
}
Use PWM to control the intensity an LED
PWM is a modulation technique that obtains
analog results by digital means
The duration of “ON” is called the pulse_width
◦ Create an analog output by changing the
pulse_width for a fixed frequency signal
Can be used to control the speed of a motor
◦ The longer the switch is ON compared to the OFF
periods, the higher the power supplied to the load
Advantage: easy to use and implement, low
power loss.
• 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
Only pins 3, 5, 6, 9, 10, and 11 can be used for
PWM
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
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)
//demo73.ino
int ledPin = 3; // must be one of 3, 5, 6, 9, 10, or 11 for PWM
void setup () {
pinMode (ledPin, OUTPUT); // assign the pin as an output
}
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);
}
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 }
Transition
condition: delay
2 seconds
void loop() {
switch(state) {
case STATE1:
//demo74a.ino digitalWrite(3, HIGH); // LED OFF
#define STATE1 1 digitalWrite(4, LOW); // LED OFF
#define STATE2 2 delay(1000);
#define STATE_END 100 state=STATE2;
unsigned char state=1; //init. to break;
state1
case STATE2:
void setup() {
digitalWrite(3, LOW); // LED ON
pinMode (3, OUTPUT);
digitalWrite(4, HIGH); // LED OFF
pinMode (4, OUTPUT);
delay(2000);
}
state=STATE1;
break;
case STATE_END: // turn off the LEDs,
this state is not used here
digitalWrite(3, HIGH); // LED
OFF
digitalWrite(4, HIGH); // LED
OFF
break;
default:
state=STATE_END;
break;
} }
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 Next
State State
Magnetic strip is ON ON
detected
Magnetic strip is OFF ON
detected
No magnetic ON OFF
strip is detected
No magnetic OFF OFF
strip is detected
Circuit
◦ Connect one leg of a magnetic sensor to GND and
another leg to pin 7
Code demo4b.ino
◦ When a magnet is near the magnetic switch sensor
(if you don’t have a magnetic switch, connect Pin7
to ground to simulate the effect), then
LED5=ON,LED6=OFF
◦ When a magnet is NOT near the magnetic switch
sensor (or leave pin7 unconnected), then
LED5=OFF,LED6=ON
Try demo4b.ino
void loop() {
//demo4b.ino
switch(state) {
#define STATE1 1
case STATE1:
#define STATE2 2
digitalWrite(ledPin_S1, HIGH);
#define STATE_END 100
// LED OFF
int magnetic = 7;
digitalWrite(ledPin_S2, LOW);
int ledPin_S1 = 5; // LED ON
int ledPin_S2 = 6; if (digitalRead(magnetic) ==
unsigned char state=1; LOW)
void setup() { state=STATE2; break;
pinMode (magnetic, INPUT); case STATE2:
pinMode (ledPin_S1, OUTPUT); digitalWrite(ledPin_S1, LOW);
pinMode (ledPin_S2, OUTPUT); // LED ON
} digitalWrite(ledPin_S2,
HIGH); // LED OFF
if (digitalRead(magnetic) ==
HIGH)
state=STATE1; break;
case STATE_END:
digitalWrite(ledPin_S1,
HIGH); // LEDOFF
digitalWrite(ledPin_S2,
HIGH); // LED OFF break;
default:
state=STATE_END;
break; }}
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
State Diagram 4c
//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;
int led1 = 5; else if ((digitalRead(sensor1) == HIGH) &&
int led2 = 6; (digitalRead(sensor2) == HIGH)) state=STATE4;
unsigned char state=4; break;
void setup() { // …………To be filled in by students
pinMode (sensor1, INPUT);
pinMode (sensor2, INPUT);
pinMode (led1, OUTPUT);
pinMode (led2, OUTPUT);
}
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
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