Write an embedded C program for PWM
Pulse Width Modulation, or PWM, is a method that uses digital signals to achieve analog results. In simple terms, it's like turning a light switch on and off rapidly to control the brightness of a light bulb.
Imagine you have a switch that you can flip between two positions: on and off. With PWM, we quickly switch between these two states. The time the switch is in the "on" position is called the pulse width. By adjusting how long the switch stays on compared to off, we can simulate different levels of voltage between the minimum (0 Volts) and the maximum (Vcc, which is 5 volts on an Arduino UNO board).
For example, if you want to control the brightness of an LED, you can use PWM. If you turn the LED on and off very fast with a certain pattern, it appears as if the LED is receiving a steady, in-between voltage. This allows you to make the LED dimmer or brighter by adjusting the pulse width.
In the context of Arduino, the green lines in the diagram represent a regular time period. The duration of each green line is the inverse of the PWM frequency. So, if the PWM frequency is around 500Hz, each green line would last about 2 milliseconds.
When you use the `analogWrite()` function in Arduino, you're specifying the duty cycle on a scale from 0 to 255. A duty cycle of 255 means the signal is always on (100% duty cycle), while a duty cycle of 127 means the signal is on half the time (50% duty cycle). This varying duty cycle allows you to control the brightness or intensity of devices like LEDs.
Code:
voidsetup ()
{
pinMode(9, OUTPUT);
}
voidloop () {
analogWrite (9,64);
delay (1000);
analogWrite (9,127);
delay (1000);
analogWrite (9,19);
delay (1000);
analogWrite (9,255);
delay (1000);
analogWrite (9,0);
delay (1000);
}
0 Comments
if you have any doubt, plz let me know