Category Archives: Action 4: From bright to dim for LED by Arduino and PWM

Problem presentation: how to control LED from bright to dim by Arduino

Seg 4: Experiment and code

4.4 Experiment and code

This experimental schematic is very simple, and similar to the example one. Note that, the port connected with LED must be one of the ports 3,5,6,9,10,11 on Arduino board. Certainly, if you use other method, it is maybe different. Its schematic and circuits can be seen in Figure 4-4, 4-5.

Figure 4-4 Experimental schematic

Figure 4-5 Experimental circuit

Code solution

01 / Program 4: Arduino and PWM for continuous variations from bright to dim for LED
02 int brightness = 0;    // define the int variation brightness, which is used to change the brightness of LED.
03 int fadeAmount = 5;    //define variation fadeAmount used as a increasement or decreasement.
05 void setup()  { 
07   pinMode(9, OUTPUT);// set port 9 as OUTPUT
08 } 
10 void loop()  { 
12   analogWrite(9, brightness);//write brightness to port 9
14   brightness = brightness + fadeAmount;//change brightness used in the next cycle
16   if (brightness == 0 || brightness == 255{
17     fadeAmount = -fadeAmount ; //transform in high level (5V) and low level (0V)
18   }     
20   delay(50); //delay 50ms, make the value brightness+fadeAmount lasts 50ms
21 }

4.5 Key points to summarize

1) Although digital signal is superior than analog signal. But in some applications, it is a must to use analog signal, like motor, music, and so on.

2) The scope of parameter “value” in the Arduino sentence analogWrite(pin,value) is 0~255. We should understand the relationships between duty cycle and the output voltage.

3) There are two kinds of ports on the Arduino board, in which, only some ports can be used to output the analog signal.

Seg 3: Experimental principle


Experimental principle: on the Arduino board, there only exist digital output ports. In these ports, digital signals can be output. Therefore, in our previous experiments, digital signals can be output in any digital port, and LED can be changed between the two cases: brightness and dark. That is, there exist only two values: high and low voltage level, or 0V and 5V. However, if the Arduino board is used to control the motor run, what can we do? Certainly, we cannot simply use the previous methods, because the voltage is only 0V or 5V. If we use it, the motor would run, and the next time, would stop for a while. But, if we can give the motor a stable continuous voltage signal, the motor can run continuously. Happily, the smart Arduino have rich interfaces. In some interfaces, Arduino can let them output the continuous signal. The ports are 3,5,6,9,10,11 for the Arduino UNO, which are marked by the label “~” on the board. The signals from these ports are used PWM technique. For example, in our such experiment, port 9 is digital, but it has PWM. So it can output a continuous signal. When the digital signal is added into port 9, the signal can be changed into a continuous signal by PWM. This signal can also be used to control the motor. Note that, this signal is discrete, but the internal is so small that we cannot feel it a discrete signal. We just think it is a continuous signal. This point is similar to film, which is discrete, and composed by many pictures (it is said as data frame from image processing). But the internals is very small between the pictures. So we cannot feel the fact.

Theoretically, we can get any concrete voltage value between 0~5V on the Arduino board by PWM technique, but not only 0V and 5V. The fact is realized by the Arduino sentence analogWrite(pin,value), which means that a anlog voltage signal can be output from port pin, where the parameter pin is denoted by the number of port; in such experiment, the port is 9. Another parameter is value, which is a digital signal. Its scope is 0~255. Different value denotes different duty cycle. For example, in Figure 4-3, analogWrite(9,255) means that a continuous 5V voltage signal is output from port 9. Next, how about analogWrite(9,64)? Its voltage value can be computed by U=64/255*5V=1.25V, where the first term 64/255 is denoted by duty cycle 25%. But, can we obtain any voltage value? For example, if we want to get a voltage value U=1.8V, the parameter “value” in Arduino sentence “analogWrite(pin,value)” is computed as value=U*255/5=51U. Therefore, by U=1.8 instituting into the formula, we can get value=91.8. In other words, by the sentence analogWrite(9,91.8), a 1.8V voltage signal is output at port 9.

In addition, the duty cycle is denoted by the ratio of busy and idle. As shown in Figure 4-3, busy means that there exist a voltage and its cure is high. While, idle means that there is no signal, and its curve is low. It is worth noting that, in the runtine “analogWrite(pin,value)”, the scope of value is 0~255, and the voltage is 5V/255 each unit; but the scope of value is 0~1024 in “analogRead(pin)”, and the voltage is 5V/1024 each unit.

Figrue 4-3 Duty cycle

Seg 2: Relevant Concepts

4.3 Experimental principle

Before presenting the experiment principle, some relevant concepts should be given out firstly.

Digital signal: its means that its amplitude is discrete, and limited into a range, like binary code used widely in computer science. The digital signal has a strong anti-interference ability, and can be easily processed by digital signal processing. Nowadays, there are many digital signals, such as mobile signal, information handling by computer, and so on.

Analog signal: its ware changes continuously. Theoretically, we can get any of the value from the analog signal. Since it is interfered easily by the other signals, it is difficult to handling. Thus, generally, the analog signal should firstly be transformed into the digital signal for the convenient signal processing. The difference could be shown in Figure 4-1.

Figure 4-1 Difference of digital and analog signals

PWM is the abbreviation of Pulse Width Modulation. It means that we can equivalently get the required signal wave on the basis of a series pulse width, as shown in Figure 4-2. We can achieve a sine signal wave by a series of pulse signal by the different width. In fact, this principle can be illustrated by the equivalent area from mathematical integral. For example, the area of the first pulse is equivalent to the area surrounded by sine signal wave. So, we can change the duty cycle to get the voltage signal wave. Please image it. If we want to get a direct current voltage wave, the width of each pulse should be equal. In addition, PWM technique has been widely applied to the motor speed control, valve control, and so on. For example, PWM has been used to electronic cars.

Figure 4-2 Sine wave by PWM

Seg 1: How to control LED from bright to dim by Arduino

4.1 Problem presentation: how to control LED from bright to dim by Arduino

In the previous examples, Arduino is just used to control the brightness and/or dark for LED. That is, there are only two voltage levels: high and/or low from the voltage level, and there are only two digital value: 0 and/or 1 from the digital signal. But, Arduino doesn’t control the continuous variation from bright to dim, or visa. Moreover, the applied requirements exist in our usual lives, such as dancing hall, or concern. For the lighting effects, sometimes, we need a gradual change for the LED lights from bright to dim. This can be realized by giving a continuous voltage to a LED light. Therefore, in our such experiment, by utilizing the PWM (Pulse Width Modulation) technique, Arduino can transmit a continuous voltage signal to control LED, and the PWM technique is also widely used in the continuous operation of steering gear, music play, and power control in the communication.

4.2 The required materials

The required materials are very simple in this sample, which is similar to the experiment one. It is different from the connected port on the Arduino, as shown in Figure 4-1.

Table 4-1: Materials

The required materials


Name Quality Function Note


Arduino software 1 suit Provide ide New ver 1.05


Arduino UNO board 1 Control board Many


USB data line 1 Connect board distribution


Dupont line 2 Connect elements optional


Light-emitting diode (led) 1 LED blink optional


Resistance 220Ω Current limiting optional


Bread board Connection optional

The required materials can be referred in the following Figure 4-5.