KEMBAR78
Day 9 - Analog Output (PWM) With Arduino Using Pictoblox | PDF | Electricity | Telecommunications Engineering
0% found this document useful (0 votes)
8 views37 pages

Day 9 - Analog Output (PWM) With Arduino Using Pictoblox

This document provides a comprehensive guide on using Pulse Width Modulation (PWM) with an Arduino Uno to control the brightness of an LED via a potentiometer. It covers the basics of PWM, the necessary components, circuit diagrams, and step-by-step coding instructions using Pictoblox. The project culminates in creating an adjustable LED breathing lamp, demonstrating practical applications of PWM in controlling light intensity.

Uploaded by

m-13344851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views37 pages

Day 9 - Analog Output (PWM) With Arduino Using Pictoblox

This document provides a comprehensive guide on using Pulse Width Modulation (PWM) with an Arduino Uno to control the brightness of an LED via a potentiometer. It covers the basics of PWM, the necessary components, circuit diagrams, and step-by-step coding instructions using Pictoblox. The project culminates in creating an adjustable LED breathing lamp, demonstrating practical applications of PWM in controlling light intensity.

Uploaded by

m-13344851
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

Day 9

Analog Output (PWM) with Arduino using Pictoblox

1
Objectives
• Learn what is analog-like signal (PWM) and its usage
• Learn how to create analog-like output using Arduino Uno board.
• Learn how to use analog input to control intensity of output using
analog (PWM) signal.
• Build a LED Breathing Lamp, rate adjustable by a potentiometer.

2
Analog Output by Arduino
Have you noticed that by only using digital signal for LED, you can only
control its ON and OFF state, but never the brightness?
Actually, you can use a special type of digital signal that behaves like
analog signal to control the brightness of LED (or speed of motor, in
future chapters), and Arduino board is capable of this!
Virtually ALL microcontrollers around the world is digital device,
including Arduino boards, but the good thing is most Arduino boards,
specifically its microcontroller chip can generate analog-like output by
performing Pulse-Width-Modulation (PWM).

3
What is PWM?
Pulse Width Modulation, known as PWM in short.

It is a type of digital signal that uses high-speed switching method to


reduce the power carried through the channel. This signal is constantly
switching at high frequency, when it used to operate low frequency
devices such as LED, it will exhibits an average or steady state voltage
that approximates an analog type signal.

4
How PWM signal looks like?

With this signal, you can output certain voltage level between 0v (Fully OFF) and 5v (Fully ON). 5
Type of PWM Signal
Pulse Width Modulation (PWM)
This is the type of analog signal
commonly used for controlling
intensity of operation, such as
the proportion of ON-time for
controlling LED brightness, speed
of motor, etc.

6
Type of PWM Signal
Servo Positioning PWM
This is the type of PWM signal
commonly used for driving analog
servo motors, to tell precisely which
angle the servo shaft should go to.
Servo motors uses different type of
PWM signal called Pulse Positioning
Modulation (PPM), operates at
specific frequency and has pulse
width between 1.0 – 2.0ms.

7
What can PWM signal do?
PWM signal can be used to control brightness of LED, here is a
corresponding value for the brightness, PWM and percentage.

Brightness Percentage PWM Value

Fully OFF 0 0

Quarterly
25 63
ON

Half ON 50 127

Almost
75 191
Fully ON

Fully ON 100 255


8
How to make Arduino output PWM?
In Pictoblox, you can use the code below to make Arduino board create
PWM signal
𝑅𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛 = 2𝑛
𝐹𝑜𝑟 8 − 𝑏𝑖𝑡 𝑟𝑒𝑠𝑜𝑙𝑢𝑡𝑖𝑜𝑛, 𝑙𝑒𝑡 𝑛 = 8
𝑀𝑎𝑥. 𝑟𝑎𝑛𝑔𝑒 𝑜𝑓 𝑣𝑎𝑙𝑢𝑒 = 2𝑛 − 1

28 − 1 = 255
The range of value for PWM is 255, defined by the resolution of the
microcontroller. In ATMEGA328 chip, it is 8-bit resolution, so the value
of PWM is between 0 and 255.

9
PWM in Arduino Uno
The pins in Arduino Uno board
that is capable of creating PWM
signal is located among the
digital input-output pins, with
the curly symbol ~

PWM capable pin in Uno are


• 3, 5, 6, 9, 10, 11

10
Hands-on
Output PWM to control LED Brightness

11
Introduction
In this hands-on, you will learn to use a potentiometer to control
brightness of LED.

12
Component Required
• Arduino Uno Board
• USB Data Cable
• Jumper Wire
• Breadboard
• Resistor 220R
• LED (Any Color)
• Potentiometer

13
Breadboard Diagram Pin 11 ~

220R

GND

A0
5V
14
Circuit Diagram

15
Pictoblox Code

16
Code Explanation (INPUT)
First, read the ADC value input by the potentiometer at Arduino Uno
pin A0 by building this block.

Then save the value read into ADC0 variable.

17
Code Explanation (PROCESS)
Then, convert the value of ADC0 into PWM range by using this block.

This block converts the ADC0 value range from 0 to 1023, into PWM
value range from 0 to 255, then save it into variable PWM.

18
Code Explanation (OUTPUT)
Lastly, place the PWM value to the output device, which is connected
to Arduino Uno pin 11 (has PWM function) by using this block.

This block puts the value stored in variable PWM to the digital output
pin 11, creating a varying brightness effect on LED.

19
Code Explanation (CYCLE)
Last but not least, add a simple delay of 50ms (0.05s) at the end of
program to make the program run at a predictable speed, using this
block

Changing this value can make the program goes faster or slower,
decreasing this will make program respond faster but it will decrease
accuracy of certain sensors.

20
Results
Once done building the program, upload it into your Arduino Uno
board. Then, you may play with the potentiometer and observe the
brightness of the LED.

0% OFF 30% ON 100% ON 21


Project
Adjustable LED Breathing Lamp using Arduino

22
Introduction
In this project, you will learn to control the timing rate of fading or
“breathing” of LED lamp using a potentiometer.

23
Breadboard Diagram Pin 11 ~

220R

GND

A0
5V
24
Circuit Diagram

25
Pictoblox Code
In this project, you
may make your own
Block (Function) and
describe the functions
of it using blocks, to
shorten (simplify) the
program length.

To make a block, go to
My Block section,
create a block with a
meaningful name.

26
Code Explanation (FUNCTION)
For setting up a series of constants in this
project, you can do the following to describe
the constants.

• Step : brightness increase step, set to 5 for


faster change in brightness.
• Increase and Decrease : the direction trend of
LED brightness change.
• PWM Min and Max : the range of PWM value
for Arduino Uno
• Timing Min and Max : the range of timing
delay, in milliseconds. Increasing the max can
make the LED “breath slower”
27
Code Explanation (FUNCTION)
This block describes the process of
changing brightness based on the
direction of brightness change.

For changing the brightness of LED,


IF direction is in Increase trend,
THEN step up the PWM by 5.
IF direction is in Decrease trend,
THEN step down the PWM by 5.

28
Code Explanation (FUNCTION)
This block describes the process of limiting
the PWM value to prevent it from being
increased more than 255 (out of PWM
maximum range), and decreased less than 0
(into negative number).

IF PWM value is more than PWM max,


THEN set it to be at PWM max. and reverse
the brightness changing trend to Decrease.
IF PWM value is less than PWM min,
THEN set it to be at PWM min. and forward
the brightness changing trend to Increase.
29
Code Explanation (SETUP)
In this project, these are the
only variables that will change
over time, namely the PWM
value used for controlling the
LED output brightness, set to
fully OFF at first, and the Fade
Direction which is the brightness
change trend, set to forward in
the beginning.

30
Code Explanation (INPUT)
First, read the ADC value input by the potentiometer at Arduino Uno
pin A0 by building this block.

Then save the value read into ADC0 variable.

31
Code Explanation (PROCESS)
This block can make the Arduino Uno to convert the value in ADC0
range from 0 to 1023, to the Maximum timing and Minimum timing
constants we have set before this in the function describing block.

Note that the position of resulting variables are reversed to make it


follow our normal logic, which is increase timing when ADC0 is lower
(rotating potentiometer knob to left), and decrease timing when ADC0
is higher (rotating potentiometer knob to right).

32
Code Explanation (PROCESS)
In the My Block section, you can add these 2
blocks as shown here, to make Arduino
board to step up/down the LED brightness,
then check the brightness PWM value and
trend if it hits the limit.

33
Code Explanation (OUTPUT)
Lastly, place the PWM value to the output device, which is connected
to Arduino Uno pin 11 (has PWM function) by using this block.

This block puts the value stored in variable PWM to the digital output
pin 11, creating a varying brightness effect on LED.

34
Code Explanation (CYCLE)
This is the part where you do the control in this program, as the LED
“breathing” action is already logically fixed.

This block makes Arduino board to delay for the timing set by the
converted result of potentiometer, divide it by 1000 to get milliseconds
delay to make this breathing speed observable!

35
Results
After done building the code, upload it into your Arduino board. Then,
you may play with the potentiometer to observe the “breathing” of LED
brightness and its rate of change.

Turn pot to left, decrease rate of breathing Turn pot to right, increase rate of breathing
36
Conclusion
Now you should know how to use analog output (PWM) signal to
control intensity of things such as brightness of LED (in future, speed of
DC motor).

Congratulations, you have went through the hardship of making


awesome things!

End of Contents

37

You might also like