PWM with Arduino
PWM in Arduino has a wide range of applications used to control analog devices using digital signals. Arduino digital pins output can be categorized in two voltage levels either High which is 5V or Low which denotes 0V. Using PWM in Arduino we can generate a signal having constant frequency but with variable width of pulse. Most common example of PWM use in Arduino is controlling the brightness of an LED and controlling the speed of a motor.
Pulse Width Modulation signal have following two characteristics:
- Frequency: PWM signal frequency denotes how fast one cycle will be completed. Alternatively, the frequency of PWM decides how fast the output signal will switch between High and Low state.
- Duty Cycle: It describes the amount of time for which an output signal remains in high state as a percentage of total amount of time required to complete one cycle.
PWM Pins on Arduino Uno
Arduino Uno has a total of 14 digital input output pins, out of these digital pins 6 PWM pins are available on Arduino Uno board. On Arduino Uno digital I/O pins 3, 5, 6, 9, 10 and 11 are PWM pins. Number of PWM pins varies from one board to another.
Counter speed in Arduino determines the frequency of PWM signals. In Arduino Uno counter clock is equal to system clock divided by prescalers value. Three prescalers store the value of the Counter register. These three prescalers are known as: CS02, CS01, and CS00. As the total number of PWM pins are 6 so three counter registers are used in Arduino Uno having separate prescalers to control PWM pins.
Timer/Counter Registers | PWM Pins |
---|---|
TCCR0B | Controls Pin 6 and 5 |
TCCR1B | Controls Pin 9 and 10 |
TCCR2B | Controls Pin 11 and 3 |
Each of these three registers can configure three different frequency ranges for PWM signals. Normally by default an Arduino Uno have following frequencies for PWM pins:
Arduino Pins | PWM Frequency |
---|---|
5 and 6 | 980MHz |
9, 10,11 and 3 | 500MHz |
How to Use PWM Pins in Arduino
Digital pins on Arduino can be configured using pinMode(), digitalRead() and digitalWrite(). Here the pinMode() function sets a pin as input and output. When we configure digital pins as input digitalRead() function is used while setting a pin as output digitalWrite() function is used.
analogWrite()
To configure PWM pins we use analogWrite() function. This function writes an analog value to a digital pin. It can set PWM signal duty cycle. When analogWrite function is called on a specific pin a steady square wave with defined duty cycle is generated. This square wave will remain there until we call a new analogWrite() function for that pin or write a new value using digitalRead() or digitalWrite() function.
Syntax
The analogWrite() function takes two arguments:
- Pin: Pin whose value is to be set.
- Value: It describes the duty cycle between 0 which is Low state and 255 which is High or on state.
Another argument which is optional in case of PWM is frequency. If this is not specified by default it is 500Hz.
The analogWrite() value defines the duty cycle for PWM signals:
- analogWrite(0) means a PWM signal having 0% duty cycle.
- analogWrite(127) means a PWM signal having 50% duty cycle.
- analogWrite(255) means a PWM signal having 100% duty cycle.
Conclusion
PWM in Arduino is a technique or method to control analog devices using digital signals. All the Arduino boards have PWM pins on board. 6 PWM pins are present in Uno out of total 14 digital pins. Here we discussed how we can configure these pins using analogWrite() function in Arduino Uno.