I was looking at my Arduino Uno and I noticed that symbol by digital pins
3. What do these mean? Does this affect the way it works? Can I not use these pins for certain situations?
Relax. Don't worry. These pins are called
PWM and are the same as the other pins, except they have an "added bonus."
Some uses from Arduino's Website:
- Dimming an LED
- Simulates an analog output. The output is still digitally toggling from 0V to 5V. However, low-pass filter (capacitor and resistor) to simulate analog voltages.
- Generating audio signals.
- Providing variable speed control for motors.
- Generating a modulated signal, for example to drive an infrared LED for a remote control.
How it works:
The PWM pins are controlled by on-chip timers which toggle the pins automatically at a rate of about 490Hz. The "Pulse Width Modulation" (PWM) is how long the pin stays on or off for a single cycle of that frequency. This can dim a LED by giving the illusion it is at half the brightness as before, where it is really flashing very quickly.
When there is a 25% duty cycle, it is on one-forth of the time. If you used for a LED, it would appear about 1/4th as bright [give or take]. (Note: as some people pointed out this isn't truly proportional but let's leave it this way for simplicity. EX: 25% isn't always 1/4th the brightness.)
(If you are really electrical savvy, you could probably add a capacitor to make it also an analog output.)
How to use these pins to output:
First, you need to define the pin as output. Then, you use
to start it. The
ledPin is the PWM pin that you want to start PWM and
128 should be replaced with a number between
0% duty cycle (turns the pin completely off) and
100% duty cycle. (turns the pin on completely)
Why can't I just turn the light on and off really fast in my code?:
Technically, you can, however, there are some problems:
- It may not be as precise as using the hardwired circuits with the Arduino
- Its simpler just to type instruction instead of having lots of "if" statements
It's not really going to make that much of a difference if the Arduino's sole purpose is to generate PWM signals. However, if you put any delays longer than 50 MS in the main loop, it will mess up the timing. With the software approach you would want to eliminate any "delay" functions since the Arduino only runs on one thread (it can only do one thing at one time). If you know what you're doing, it won't make that much of a difference dimming the light, but if you have an extra pin with PWM, you're just wasting your time with a software approach.
As others have pointed out:
You still need a resistor for your circuits to limit current and voltage. You cannot skip this.
A common misconception about the PWM pins is that when using PWM, it is not necessary to use current limiting resistors (with LEDs, for example).
This is not true. The PWM pins should be thought of as digital pins, which are turned on and off automatically. This means they go to the same HIGH voltage as any of the other pins.
When using LEDs with PWM, you must still using current limiting resistors. When using motors with PWM, you should (almost always) be using a transistor.