This article was translated to English, and was originally published for deviceplus.jp.
Device Plus has introduced a variety of applications and examples of Arduino, but basic knowledge is still important no matter what you make!
In this article, we bring you the “key” basics of Arduino electronics through enabling Arduino to adjust LED brightness.
You can easily switch an LED on and off between HIGH (5V) and LOW (0V) states by connecting it to Arduino’s digital output terminals. However, since digital output can only be output in either of two states, you cannot adjust controls such as brightness.
Instead, for this purpose, you can use the “PWM” output. The PWM can be used to adjust the brightness of the LED by repeatedly switching between HIGH and LOW states.
In this article, we will learn how to use the PWM to adjust the brightness of the LED. We will also learn how to write a program that uses the PWM output to gradually light up the LED.
・Brightness cannot be digitally adjusted
・Changing LED brightness using digital output
・Adjusting brightness with PWM
・Making LED blink gradually
As we explained last time, the LED brightness changes depending on the flowing current. Comparing a 330Ω resistor to a 10kΩ resistor connected to adjust the amount of current connected to the LED, for example, more current flows through 330Ω with the smaller resistance, which causes the LED to light up more brightly. Alternatively, the current that flows also changes if you change the power supply voltage connected to the LED.
If 5V and 3.3V are applied, 5V makes the LED light brighter. If you want to illuminate the LED even brighter, you either “reduce the resistance” or “increase the voltage”, as explained in the previous formula for the relationship between the current flowing through the LED and the resistance.
However, Arduino’s digital output has only two states, HIGH (5V) or LOW (0V), meaning that the voltage and resistance values cannot be changed even if the circuit is connected as-is. Thus, the LED brightness cannot be adjusted. Although there are only two output states, HIGH and LOW, the LED brightness can be changed with some ingenuity.
Let’s change the LED brightness using only the digital output. The brightness can be adjusted by making the LED blink. Actually operate the LED step by step to see how the brightness shifts. Connect the LED to pin 5 of Arduino, as shown below:
Next, create the program as shown below and write it to Arduino. The LED should blink at one-second intervals.
In the program, the LED lights up at “digitalWrite( LED_PIN, HIGH )” (line 11) with HIGH output, and then waits for the duration specified by “delay (ON_TIME)” (line 12). The duration is specified in milliseconds; if you specify 1000, the LED will stay lit for one second.
Then, the LED is turned off at “digitalWrite( LED_PIN, LOW )” (line 14) with LOW output, and then kept off for the duration specified by “delay( OFF_TIME )” (line 15). The LED blinks by repeating this program.
Next, let’s shorten the light-on and -off durations. The light-on duration can be changed with “const int ON_TIME” (line 3), and the light-off duration can be changed with “const int OFF_TIME” (line 4). Change both of the values to “500” and write the program to check the LED status. The blinking speed should increase. As you reduce the values to “250”, “100”, “75”, “50”, and so on, the blinking should become faster. With the value of around “10”, the LED appears to light up continuously. If it blinks too fast, the human eye cannot keep up with it, causing the light to appear to be constantly on.
Next, let’s change the light-on and -off durations. Set both “ON_TIME” and “OFF_TIME” to “10”. Next, sequentially change the “ON_TIME” value to “9”, “8”, “7” … “1”, and check the lighting state of the LED. As you decrease the value, you can see that the LED gets darker.
The LED looks darker because the lighting duration is shortened, and the amount of luminescence is reduced. In other words, you can adjust the brightness of the LED by changing the “lighting duration”. This method lets you control the LED brightness using Arduino digital output only equipped with on and off functions.
As described above, you can control the brightness by adjusting the ratio of HIGH and LOW, making the LED blink in a short cycle. However, it is not practical to create your own program to control the blinking duration. If it takes time to process other programs, the blinking interval will shift, varying the brightness.
Arduino provides PWM (Pulse Width Modulation) that can periodically output HIGH and LOW. The PWM function, periodically switching between HIGH and LOW at a specified rate, can be used to adjust the LED brightness as described earlier.
However, the pins that can be used for PWM are fixed in Arduino. PWM output is only available with pins that have a “~” mark next to the number (that is, 3, 5, 6, 9, 10, and 11). Note that the other pins do not support PWM output.
PWM has the advantage of stable output without affecting program operation, since the PWM is generated on an Arduino microcomputer.
Now, connect the LED to pin 5 to try the operation. Connect the LED in advance as in the circuit shown earlier.
Next, write a program as shown below and transfer it to Arduino. The LED should light a bit darker.
For output with PWM, set the target pin to the output mode using “pinMode()” (line 6). The actual output occurs with “analogWrite()” (line 10). Specify the target pin number, and then set the HIGH ratio in the range of 0 to 255. “0” always outputs LOW, and “255” always outputs HIGH. “127” equally outputs both HIGH and LOW.
In this program, you can specify the PWM ratio in “const int DUTY” (line 3). Change the value to see how the brightness shifts.
Output using PWM broadens the way the LED lights up. Now, let’s change the PWM gradually to realize the effect that the LED brightens gradually.
Write a program as shown below
The above program stores the PWM ratio in the “i” variable used in the loop() function so that it can increment the value to make the LED gradually become brighter.
The while processing (line 14) increases the value in increments specified by STEP until i reaches 255. The incremented value is output by analogWrite() (line 15) to change the brightness of the LED. In addition, it is directed to wait for the duration specified by WAITTIME each time the PWM output is changed (line 16).
When the PWM ratio reaches 255, it is decreased until it reaches 0 to darken the LED gradually (lines 21 to 25). You can modify the blinking speed by changing the value of WAITTIME (line 3) or STEP (line 4).
This time we’ve learned how to control the brightness of the LED. See you in another article!