Basic of Arduino III
By the end of this class…
We will learn to control the intensity of an LED
2
What components do we need today?
▫ Arduino UNO and its cable - 1
▫ Breadboard - 1
▫ 1k Resistor -1
▫ LED – 1
▫ Male to Male Jumper Wire - 4
3
How do we achieve it?
▫ Pulse Width Modulation (PWM)
▫ analogWrite()
▫ For loop
4
1. Digital and Analog
The differences
Digital Signal Analog Signal
▫ Only exists in two states ▫ Take any value across a continuous
▫ Denote the states using 1 for range
HIGH and 0 for LOW ▫ Infinite
6
▫ The microcontroller on Arduino is
digital
▫ To read analog signal, we have analog-
to-digital converter (ADC) for the
analog input pins
▫ To write analog signal, Arduino does
not have a digital-to-analog converter
(DAC)
▫ How?
7
2. PWM
Mimic analog signal
Pulse Width Modulation
(PWM)
▫ Digital signal which the output
appears like analog result
▫ Consists of square waves on and
off rapidly
▫ Output is the average value
9
Pulse Width Modulation
(PWM)
▫ Duty cycle = (ON time/Period)%
▫ Period = ON time + OFF time
▫ Higher duty cycle, higher average
value
10
Pulse Width Modulation
(PWM)
▫ High frequency
▫ In controlling brightness of LED,
high frequency ensure we cannot
detect the blinking
▫ In controlling the speed of motor,
frequency needs to be high enough
to have enough power
11
How to
connect
PWM and LED
12
Connection with PWM ~pin
▫ PWM signal will be generated by ~ pin
▫ In Arduino UNO, pin 3, 5, 6, 9, 10, 11
(with ~ sign) are PWM pins
▫ Pin 3, 9, 10, 11: 490Hz
▫ Pin 5, 6: 980Hz
▫ It is possible to alter the frequency by
manipulating the timer
13
Connection with PWM ~pin
14
3. analogWrite()
To write analog output
Reading Input and Writing Output
▫ Two types of pins:
▫ Digital pins: Only read or write two value, HIGH (0V) or LOW (5V)
▫ Analog pins: Read or write value from 0,1,2,3 … 100, ... till 255(output) or 1023(input)
digitalWrite() analogWrite() digitalRead() analogRead()
▫ Write a value(HIGH ▫ Write an analog ▫ Read the ▫ Read the value(0-
or LOW) to a digital value (0-255) to pin value(HIGH or 1023) at analog pin
pin (only digital pin that LOW) at any pin (A0 to A5 only for
▫ For example has ~, PIN ▫ For example UNO)
digitalWrite(13, 3,5,6,9,10,11) digitalRead(10); ▫ For example
HIGH); ▫ Used for changing analogRead(A0);
▫ Used to turn the brightness of
something on LED or drive a
completely or turn motor
something ▫ For example
completely off. analogWrite(9,255);
16
To determine for analog value for
analogWrite()
▫ Maps 0 - 255 to 0 - 5V
▫ If you want 3V,
○ Analog value = (255/5V) * 3V = 153
▫ Analog value is controlling the duty cycle
▫ Maximum analog value 255 is due to the 8 bit PWM register
on microcontroller
17
4. For Loop
Repeat
Coding without for loop
void setup() {
pinMode(11, OUTPUT);
}
void loop() {
analogWrite(11, 0);
delay(10);
analogWrite(11, 1);
delay(10);
………………
analogWrite(11, 255);
delay(10);
analogWrite(11, 254);
delay(10);
…
analogWrite(11, 0);
delay(10); 19
What is a For Loop
▫ For us to do same thing repeatedly
20
Coding with for loop Can be any PWM pins that are
identified with a "~" sign, like ~3,
~5, ~6, ~9, ~10 and ~11.
void setup() {
pinMode(11, OUTPUT); analogWrite() only works on pwm
} pins
void loop() {
for(int fadevalue=0; fadevalue<256; The analogWrite function has
fadevalue+=1) nothing to do with the analog pins
{ Why?
analogWrite(11, fadevalue); 1. Arduino works on digital only.
delay(10); 2. Have to read into PWM
}//lighting up
for(int fadevalue=256; fadevalue>0; fadevalue-
=1) How fast you want to count
{
analogWrite(11, fadevalue); If starts from 0 and want to do something 5
delay(10); things, meaning fadevalue < 5.
Initialize counter Stop condition or when you
}//fading you
or where If starts from 1 and want to do something 5
want to start counting for like want to stop counting or how
} things, meaning fadevalue < 6 or you can do
maybe start count for 0 or 1 ? much you want this to repeat. fadevalue <= 5. 21
Summary
▫ Arduino concept
▫ Pulse width modulation (PWM) - generate analog output
▫ Arduino Programming Basic
▫ analogWrite() – Changing brightness of LED
▫ Programming Concept
▫ For loop – To simplify our code
22
Thanks!
Any questions?
You can find us at @ rec_um
23