What is PWM anyway?
PWM Pulse Width Modulation is a type of signals that change its duty cycle (i.e. the high to low times ratio).
While PWM signals appear as on the oscilloscope as ON/OFF signals of high frequency, they seems to devices such as lamps, LEDs and motors as only a weekend voltage signal.
That is, if the peak of the PWM signals is 5 Volts and the duty cycle is 0.5. This means that the High to the period of the signal is 0.5.
This means that the signal is only ON for half of the time.
If you try to operate a lamp, LED or a motor with this PWM signal, it seems as if you power this device with only 2.5 Volts without feeling that this voltage is going rapidly from high to low.
In this setup, you can to dim a lamp or LED and you can also control a DC motor speed and that’s only by changing the PWM signal duty cycle.
Since this change in the duty cycle appear to the devices as a change in analog voltage it can be thought of as an analog output of Arduino.
There are certain PWM modules inside Arduino chip those you can control using analogWrite() function.
The analogWrite() function can be used to change duty cycle of PWM signal using integer as its argument.
In the following code, you can see that we used the analogRead() function to read the analg voltage from the potentiometer the converted that input signal to control LED intensity using analogWrite() function.
Here is the Arduino code that does the job.
int ledPin = 9; // LED connected to digital pin 9 int analogPin = 3; // potentiometer connected to analog pin 3 int val = 0; // variable to store the read value void setup() { pinMode(ledPin, OUTPUT); // sets the pin as output } void loop() { val = analogRead(analogPin); // read the input pin analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255 }
Check our books on Amazon we created on our way to find happiness.
No comments:
Post a Comment