PULSE WIDTH MODULATION (PWM) is a technique in which we create an illusion of an LED being on at different brightness levels by continuously turning the LED on and off at around 500 cycles per second. The brightness we perceive is determined by the amount of time the digital output pin is on versus the amount of time it is off i.e., every time the LED is on or off. Because our eyes cannot see flickers faster than 50 cycles per second, therefore the LED appears to have a constant brightness. We can assume the PWM as an alternative rather than just turning the LEDs on or off rapidly.
With PWM, we also come across with a term known as duty cycle (the longer the pin is lit as compared to unlit in each cycle), the greater the received brightness of the LED connected to the digital output pin. The amount of time per cycle that the light is on increases with the duty cycle.
For PWM implementation we can't use any of the Arduino's digital pins, we are only allowed to use the digital pins 3, 5, 6, 9, 10, and 11. They are marked on the Arduino board with a tilde (~) sign, as you can see below.
To create a PWM signal, we have to make use of anlogWrite(x, y) function, where x is the digital pin, and y is a value for a duty cycle between 0 and 255, where 0 indicates 0% duty cycle and 255 indicates 100% duty cycles.
We will do an experiment for the demonstration of PWM and for better understanding.
Material Required: Arduino Uno board, LED, one 220k ohm resistor, breadboard, several connecting wires.
Procedure:
- First of all, connect the LED to the breadboard.
- Now connect one end of a resistor to the negative terminal of the LED (short leg).
- Afterward, connect the positive terminal of the LED to the Arduino's digital pin 3 (yellow wire).
- Now connect the resistor's other end (the one which is left, not connect in the second step) to the GND (black wire) of Arduino. Now the connection is over.
- Finally, after the connection upload the sketch in the Arduino board.
//Demonstrating PWM (Pulse Width Modulation)
int d=2;
void setup()
{
pinMode(3,OUTPUT);
}
void loop()
{
for(int a=0;a<256;a++)
{
analogWrite(3,a);
delay(d);
}
for(int a=256;a>0;a--)
{
analogWrite(3,a);
delay(d);
}
delay(200);
I hope this was helpful to you.
Thank You!
No comments:
Post a Comment
សូមអរគុណ!