In this lesson, let’s try something interesting –gradually changing the luminance of an LED through programming. Since the pulsing light looks like breathing, we give it a magical name - breathing LED. We’ll accomplish this effect with pulse width modulation (PWM)
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Components
Step 2: Principle
Pulse width modulation, or PWM, is a technique for getting analog results with digital means. Digital control is used to create a square wave, a signal switched between on and off. This on-off pattern can simulate voltages in between full on (5 Volts) and off (0 Volts) by changing the portion of the time the signal spends on versus the time that the signal spends off. The duration of "on time" is called pulse width. To get varying analog values, you change, or modulate, that width. If you repeat this on-off pattern fast enough with some device, an LED for example, it would be like this: the signal is a steady voltage between 0 and 5V controlling the brightness of the LED. (See the PWM description on the official website of Arduino).
In the graphic below, the green lines represent a regular time period. This duration or period is the inverse of the PWM frequency. In other words, with Arduino PWM frequency at about 500Hz, the green lines would measure 2 milliseconds each.
A call to analogWrite() is on a scale of 0 - 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.
You will find that the smaller the PWM value is, the smaller the value will be after being converted into voltage. Then the LED becomes dimmer accordingly. Therefore, we can control the brightness of the LED by controlling the PWM value.
Step 3: The Schematic Diagram
Step 4: Procedures
By programming, we can use the analogWrite() function to write different values to pin 9. The luminance of the LED will change based on that. On the SunFounder Uno board, pin 3, 5, 6, 9, 10, and 11 are the pins of PWM (with “~“ marked). You can connect any of these pins.
Build the circuit.
Download the code from https://github.com/primerobotics/Arduino
Upload the sketch to the Arduino Uno board
Click the Upload icon to upload the code to the control board.
If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.
Here you should see the LED gets brighter and brighter, then slowly dimmer, and again brighter and dimmer repeatedly, just like breathing.