Arduino Lesson 3
PWM LED Brightness Control
Intro
Pulse Width Modulation(PWM) is a kind of method of analog signal level for
digital encoding, We have to use high resolution counter, by using square wave duty
ratio to encodes a specific analog signal level as computer unable to output analog
voltage, only 0 or 5 v digital voltage value . PWM signal are still numbers. Because in
any given moment, amplitude of the dc power supply is either full 5v (ON), or 0v
(OFF). Voltage or current source is a pass (ON) or OFF (OFF) to repeat pulse sequence
added to the simulative load. DC supply addedto load when it is on. DC supply stop
when it is off. As long as enough bandwidth, any analog value can use PWM coding.
The output voltage value is calculated through the on and off time
Output voltage = (turn on time/pulse time) * maximum voltage value
PWM can be used on lots of applications such as adjust lighting brightness, motor
speed, sound making etc.
Here are the PWM basic parameters.
1、Variations in pulse width (minimum/maximum)
2、pulse cycle (1 seconds pulse frequency number reciprocal)
3、voltage level (for example: 0 v to 5 v)
Connection
Arduino has 6 PWM interface, they are #3、#5、#6、#9、#10、#11,Last lessons we
have done the experiment by using push button to control led,which means Digital
signal control interface,and we do the experiment with potentiometer. Now we use
potentiometer to control the led.
Parts required:
Potentiometer x 1
Red 5mm LED x 1
220ΩResistor x 1
Breadboard x 1
Jumper wires x 1 Bundle
Connect the Potentiometer ( the analog input) to analog interface, LED interface to
PWM interface, so through different PWM signal led will have different brightness.
Connect them up as below.
During programming, we will use analogWrite(PWM interface,analogue value)
function,in this experiment the signal of analogue value is assigned to the PWM
interface to make the led brightness change, then read on the screen where shows
the analogue [Link] means we just have one more step(assign analogue value
to PWM interface) than the analogue value reading experiment.
Program for reference.
int potpin=0;//Define analogue interface.#0
int ledpin=11;//Define digital interface #11.(PWM output) int
val=0;// temporary storage the Variable value from the sensor
void setup()
pinMode(ledpin,OUTPUT);// Define digital interface #11 as output
[Link](9600);//Setup Baud rate as 9600
// Note: analogue interface default setup is input.
void loop()
val=analogRead(potpin);// Read analogue value from sensor and
assign to val
[Link](val);//Show val variable.
analogWrite(ledpin,val/4);// Turn on LED and set up
brightness(PWM [Link] value 255)
delay(10);//Delay 0.01 sec.
}
Download the program, the values on screen will keep changing and the LED
brightness change at the mean time when you rotate the potentiometer knob.