Index
Definition
Pulse Width Modulation (PWM) is a common technique used in microcontrollers to generate analog signals using digital hardware. PWM is a method of encoding a message into a pulsing signal. In the context of microcontrollers, PWM is used to control the power supplied to an electrical device or to generate analog signals.
PWM signals are a series of on/off pulses, where the on time (duty cycle) of the signal can be adjusted to produce an analog voltage. By rapidly switching the output between high and low states, the average voltage over time can be varied, which is how the analog output is simulated.
The idea behind PWM is to vary the duty cycle of a periodic signal while keeping the frequency constant. The duty cycle is the ratio of the pulse duration to the total period of the signal. By changing the duty cycle, we can control the amount of power delivered to an electrical device.
To generate a PWM signal, the microcontroller sets a fixed frequency, typically in the range of a few hundred Hz to a few kHz, and then varies the duty cycle of the signal. The microcontroller can generate PWM signals using hardware timers or specialized PWM modules.
The advantage of PWM is that it allows us to control the amount of power delivered to an electrical device with high precision, without dissipating excess power as heat. PWM is commonly used to control the speed of motors, the brightness of LEDs, and the voltage supplied to other electrical devices.
PWM in Arduino
In Arduino, PWM output is available on several pins, which are marked with a ~ symbol. These pins are capable of outputting a PWM signal, and the duty cycle of the signal can be controlled using the analogWrite() function. The analogWrite() function takes two arguments – the pin number and the duty cycle value (ranging from 0 to 255).
The frequency of the PWM signal in Arduino is fixed and depends on the timer used. For most Arduino boards, the frequency is approximately 490 Hz. However, on the Arduino Due board, the frequency is much higher (around 1 kHz) and can be adjusted using the analogWriteFrequency() function.
PWM is implemented in a similar way in different Arduino boards.
- Arduino UNO: has six PWM outputs on digital pins 3, 5, 6, 9, 10, and 11, which can be used for analog output with the analogWrite() function. The analogWrite() function allows the user to control the output voltage by setting the duty cycle of the PWM signal.
2. Arduino Mega: has a total of 15 PWM outputs on digital pins 2 to 13 and 44 to 46. These PWM outputs can be used with the analogWrite() function to generate a variable voltage output.
3. Arduino Nano: also has six PWM outputs on digital pins 3, 5, 6, 9, 10, and 11, which can be used with the analogWrite() function.
Example
Here’s an example code of using PWM on Arduino to control the brightness of an LED:
int ledPin = 9; // LED connected to digital pin 9
void setup() {
pinMode(ledPin, OUTPUT); // set the pin as output
}
void loop() {
for (int brightness = 0; brightness <= 255; brightness++) {
analogWrite(ledPin, brightness); // set the brightness of the LED using PWM
delay(10); // wait for 10 milliseconds
}
for (int brightness = 255; brightness >= 0; brightness--) {
analogWrite(ledPin, brightness); // set the brightness of the LED using PWM
delay(10); // wait for 10 milliseconds
}
}
Explanation
Here is how the code works:
- Define a variable
ledPin
and assign it the value of 9, which is the pin number that the LED is connected to. - In the setup function, set the pin mode of
ledPin
to OUTPUT, which means it can be used to output a signal. - In the loop function, use a for loop to increase the brightness of the LED from 0 to 255. For each brightness value, use the
analogWrite
function to set the brightness of the LED, and wait for 10 milliseconds using the “delay” function. - Use another for loop to decrease the brightness of the LED from 255 to 0. For each brightness value, use the
analogWrite
function to set the brightness of the LED, and wait for 10 milliseconds using the “delay” function. - The
loop
function runs continuously, so the LED will continuously fade in and out in a smooth manner due to the use of PWM.
Inbuilt Arduino PWM functions
Arduino has several inbuilt functions for generating PWM (Pulse Width Modulation) signals on its digital pins. These functions are:
analogWrite(pin, value)
: This function generates a PWM signal on the specified digital pin. Thepin
parameter specifies the digital pin number, and thevalue
parameter specifies the duty cycle of the PWM signal. The value can range from 0 (fully off) to 255 (fully on).tone(pin, frequency)
: This function generates a square wave on the specified digital pin at the specified frequency. Thepin
parameter specifies the digital pin number, and thefrequency
parameter specifies the frequency of the square wave in Hertz.noTone(pin)
: This function stops the generation of the square wave on the specified digital pin.pulseIn(pin, value)
: This function measures the duration of a pulse on the specified digital pin. Thepin
parameter specifies the digital pin number, and thevalue
parameter specifies the state of the pulse (HIGH or LOW) to measure the duration of. The function returns the duration of the pulse in microseconds.pulseIn(pin, value, timeout)
: This is an overloaded version of thepulseIn()
function that includes atimeout
parameter. Thetimeout
parameter specifies the maximum duration in microseconds to wait for a pulse. If a pulse is not detected within the specified timeout, the function returns 0.analogWriteResolution(bits)
: This function sets the resolution (in bits) for the PWM signals generated by theanalogWrite()
function. By default, the resolution is set to 8 bits (i.e., 0-255). However, you can set the resolution to a higher value (e.g., 10 bits or 12 bits) to get more precise control over the PWM signal.analogWriteFrequency(pin, frequency)
: This function sets the frequency (in Hertz) for the PWM signals generated by theanalogWrite()
function on the specified pin. The frequency can range from 1Hz to 65,535Hz.
Note: that not all Arduino boards support all of these functions. The availability of these functions may depend on the specific board you are using.