In this article, we will learn what is PWM? PWM signals can be used to control motor speed or brightness of led, driving servo motors or to change the amount of power going to any devices. Let’s start with a description of waveforms.
Here a wave form of 10Volt looks like
You can see 10V and 0V on above figure.
If we will create a waveform 10V half of time and 0V half of the time, you can see a waveform something like bellow.
Since it is 50-50, we call this wave form of 50% duty cycle, so if you measure this voltage, the average voltage will be 5V.
So formula for Duty cycle is (D)= Ton /(Ton+Toff)
Ton – On or High time period (10V in our case)
Toff – Off or Low time period
So if we change the duty cycle to 20%, voltage will be 2V and wave form will be like bellow,
So changing the width of the pulse we can change the average voltage. This technique is called Pulse Width Modulation (PWM).
We can generate PWM signal using many different ways, but in our electronics lab we generally use function generator for experiment purpose. Function generator are use to generate different kind of signals.
In this tutorial we will create a PWM signal using Arduino.
In your Arduino connect one LED to Pin 9, Open your Arduino IDE, copy this bellow code, compile and upload to Arduino.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } |
Note: Please remember all pins in Arduino are not able to generate PWM signal, only 3,5,6,9,10,11 pin are able to generate the PWM signal. Whereas in above code we have taken pin 9 as PWM output.
analogWrite(), this function is used to generate an analog signal or a PWM signal in Arduino.
We hope now you got a little idea about PWM. If you don’t have an Arduino board, you can also generate PWM signal using a simple 555 Timer IC. You can get tons of tutorial and circuit over the internet about 555 Timer PWM circuit.
Let us know about your next PWM based project or you have any doubt in comment section bellow.
Don’t Forget to subscribe for more tutorials like this
[subscribe2]