Sub topic 12
Limits of PWM and Driving Larger Loads
This chapter assumes that you know Arduino sketches, pinMode(), digitalWrite(), basic breadboard wiring, Ohm’s law, and the earlier PWM concepts of duty cycle and frequency. The goal is to understand when an Arduino pin can drive a load directly and when you need a separate switching circuit.
PWM Is Not a Universal Analog Output
PWM works well when the load responds to average power. An LED appears dimmer because the eye averages rapid flashes. A DC motor responds partly to average voltage because its mechanical system cannot speed up and slow down at the PWM rate.
PWM does not create a steady analog voltage. The pin still switches between approximately 0 V and 5 V. The duty cycle controls how long the signal stays high during each period.
Some loads do not respond well to this rapid switching:
- A small incandescent lamp usually averages the power because its filament is slow to cool.
- An LED responds well, but very low PWM frequencies can produce visible flicker.
- A motor may make audible noise or behave unevenly at some frequencies.
- Audio circuits can turn PWM into unwanted sound.
- Devices that require a smooth, regulated voltage need a proper analog or power-conversion circuit.
A higher PWM frequency can reduce visible flicker, but it does not automatically make every load work better. Switching losses, electrical noise, and the limits of the timer hardware also matter.
Arduino Uno PWM Limits
On an Arduino Uno, analogWrite() is available on pins marked with a tilde (~):
| PWM pins | Timer | Approximate default frequency |
|---|---|---|
| 3 and 11 | Timer2 | 490 Hz |
| 5 and 6 | Timer0 | 980 Hz |
| 9 and 10 | Timer1 | 490 Hz |
The default PWM resolution is 8 bits. Values from 0 through 255 represent duty cycles from 0% through 100%.
These pins share hardware timers with other Arduino features. For example, Timer0 supports millis(), micros(), and delay(). Timer1 is used by some libraries, including common servo libraries. Changing a timer's settings can therefore change timing functions or another device's behavior.
The default frequency is fast enough for many LEDs, but it may be too low for some cameras, optical sensors, or quiet motor control. Before changing timer registers, check which library and timing features your project uses.
Directly Driving an LED
An Arduino pin can usually drive one small indicator LED through a current-limiting resistor. The resistor limits current according to Ohm’s law:
\[ R = \frac{V\text{pin} - V\text{LED}}{I} \]
For a 5 V Uno, a red LED with a forward voltage near 2 V and a target current of 10 mA gives:
\[ R = \frac{5 - 2}{0.010} = 300\ \Omega \]
A nearby standard value, such as 330 Ω, is a reasonable choice.
The ATmega328P output pins have an absolute maximum of 40 mA, but that is not a normal operating target. Keep the current lower, commonly around 20 mA or less, and also observe the total current limits for the microcontroller and board. PWM changes the average current, but the pin still carries the full current during each high part of the cycle.
Switching a Larger Load
An LED strip, lamp, relay, or motor can require more current than an Arduino pin should provide. Do not connect such a load directly to the pin. Use the pin as a control signal for a transistor or logic-level MOSFET.
A typical low-side MOSFET circuit works like this:
Connect the external supply ground to the Arduino ground so the gate voltage has a shared reference. Choose a MOSFET specified for the Arduino's gate voltage. A MOSFET designed only for a 10 V gate drive may not turn on fully at 5 V.
For a motor, relay, or solenoid, add a flyback diode across the load. The diode absorbs the voltage spike created when the inductive load is switched off. Use a motor driver or H-bridge when you need direction control, braking, or current limiting.
The external supply must provide the load's voltage and current. Do not assume that the Arduino's USB connection or onboard regulator can power a high-current load.
Choosing Frequency and Hardware
When PWM causes flicker, noise, or unstable motor behavior, consider these questions:
- Is the PWM frequency appropriate for the load?
- Does a library already use the timer attached to the selected pin?
- Can the load and switching device handle the switching losses?
- Is the power supply sized for startup and peak current?
- Does the circuit need filtering, shielding, or a dedicated driver?
A capacitor or filter can smooth PWM for a circuit that expects a voltage, but it changes the circuit from direct PWM switching into filtered power conversion. Design that filter for the load instead of adding a capacitor blindly.
Exercise
Why should a high-current LED strip use a MOSFET instead of connecting directly to an Arduino PWM pin, and what extra connection is normally required?
Show answer
The strip needs more current than the pin should supply, so the MOSFET carries the load current while the pin controls its gate. The strip's power-supply ground and the Arduino ground must normally be connected together.