# Category Archives: Action 1: Arduino controls the Blink of LED

How to control the blink of LED

# Seg 1: Arduino an nixie tube

6.1 Problem presentation: how to use Arduino to control nixietube for displaying the digital number 0,1,2,3,4,5,6,7,8,9.

In fact, Arduino is used to control nixie tube to show the digital number, which is the same to the control of many LEDs to display. By the different constitution of display of LEDs, we can get the different digital number. So, the key problem is to understand the polarity of nixietube or LED. This a very important problem in such experiment.

6.2 The required materials

The required materials is very similar to the experiment 5. Only the buzzer is altered into digital display, which can be shown in Figure 6-8.

Table 6-1: Materials

 The required materials Number Name Quality Function Note 1 Arduino software 1 suit Provide ide New ver 1.05 2 Arduino UNO board 1 Control board Many 3 USB data line 1 Connect board distribution 4 Dupont line 2 Connect elements optional 5 Nixie tube 1 display optional 7 Bread board Connection optional

# 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. 04   05 void setup()  {  06   07   pinMode(9, OUTPUT);// set port 9 as OUTPUT 08 }  09   10 void loop()  {  11   12   analogWrite(9, brightness);//write brightness to port 9 13   14   brightness = brightness + fadeAmount;//change brightness used in the next cycle 15   16   if (brightness == 0 || brightness == 255) { 17     fadeAmount = -fadeAmount ; //transform in high level (5V) and low level (0V) 18   }      19   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: Arduino controls many LEDs

2.4 Experimental platform

According to Figure 2-2, we start to set up the experimental platform, as shown Figure 2-2. Oh, my god, there are so much LEDs, dupont lines, and resistors, that we are confused by the complex connection. But can we rightly connect the electrical elements? In fact, it is not difficult if we can find a technique. Then, it will be very simple to set up our such experiment.

1. As for one LED, the long les is located left side for positive, and the other is located right side for negative. if all of these LEDs are connected by this method, it is not a problem.
2. One end of dupont line is connected to negative polarity (by the equal voltage for each column in the brandboard). Another end is connected to GND. All of these ends are connected the same line in the narrow band (the equal voltage for each row in the narrow band).
3. The current-limited resistors are connected to the polarities of LEDs. Similarly, they are located randomly in the same column. But for the good-looking, they had better be connected unifiedly the different columns at the same line in the brandband. It will look like neat, as shown in Figure 2-2.
4. One end is connected to resistor at the same column, and the other is connected to the pins on the Arduino board, as shown in Figure 2-2.

OK, when you have a few techniques to connect these electronic elements on the Arduino board, you may finish this connection of this experiment successfully at a time. The circuit is not complex, but it needs your carefulness.

Wow, when we insert USB line into computer and Arduino board, the LED is blinking repeatedly at pin 8 on the Arduino board. But other LEDs are honest, and not blinking. Why? Yes, maybe, you have got it. Do you remember the previous experiment? We let LED blink located at pin 8 on the Arduino board. When the code has been burnt into the Arduino board, it can be saved in the board forever. It does not disappear with the power off of Arduino board. If we repower on the Arduino board, the LED will blink at pin 8. But, if we burn a new program into the Arduino board, the board will execute the new code.

Figure 2-3 Start to write the letter V

Figure 2-4 The letter V

2.5 Solution for code

On the basis of the last experiment on the Arduino IDE, we can give out the code in this experiment, which is exhibited in Program 2.

 01 //Program 2: Arduino controls many LEDs 02 //define 9 pins for LEDs 03 int led4 = 4; 04 int led5 = 5; 05 int led6 = 6; 06 int led7 = 7; 07 int led8 = 8; 08 int led9 = 9; 09 int led10 = 10; 10 int led11 = 11; 11 int led12 = 12; 12 13 //initialize the 9 pins as OUTPUT 14 void setup() 15 { 16    for(int i=4;i<=12;i++)//define 4-12 as OUTPUT 17    pinMode(i,OUTPUT);//set i as OUTPUT 18 } 19 20 void loop() 21 { 22  mode();//display of 9 LEDs 23 24 } 25 //subfunction for mode() 26 void mode() 27 { 28  unsigned char j; 29  //let 4-12 LEDs blink one by one 30  for(j=12;j>=4;j–) 31  { 32    digitalWrite(j,HIGH); 33    delay(500); 34  } 35  //let 4-12 LEDs go out 36  for(j=12;j>=4;j–) 37  { 38    digitalWrite(j,LOW); 39  //delay(500); 40  } 41  //let 4-12 LEDs bright 42  for(j=4;j<=12;j++) 43  { 44    digitalWrite(j,HIGH); 45  } 46 }

2.6 Key points

1) Be familiar with the polarity of LED and the features of breadboard.

2) After finishing a experiment, we should summarize some experiences from the process of experiment.

3) Arduino can realize your idea. Do it yourself.

# Seg 7: Key points for Action one

1. The interfaces are USB-A and USB-B for the USB data line used to connect to Arduino board.
2. The general rule for the polarity of LED: long leg is corresponding to positive, and short is for negative. If the two legs are the same length, for the inside chip, the big one is for negative, and another is for positive.
3. To prevent LED be damaged, a current-limited resistance must be connected serially.
4. Breadboard can be viewed as two parts: narrow and broadband. The middle part is called as broadband, and the two-sides are narrow band, where each row has the same voltage in the narrow one, and each column has the same voltage for the broadband. This point can help us to set up the circuits exactly.
5. Before the experiment, we should analyze the principle of circuit, and then draw the schematics. By the help of schematics, the real circuit can be set up for the experiment.
6. A simple sample provided by the Arduino platform can be used to test the built environment whether is right or not.
7. During the process of experiment, we should choose exactly the type of Arduino board and the number of port connected to Arduino.
8. Especially, Arduino software is sensitive to the letter case of the key words, system functions and the constants.
9. Digital signal is usually discrete, and its value is only HIGH or LOW.
10. The unite of the value in the function delay() is microsencods (ms).

# Seg 6: Software environment

1.3.2 Software environment

Before doing our experiment, we should carry out some simple settings. Firstly, when USB data line is inserted into computer, the driver program will be installed automatically (note that, OS is windows 7 in this experiment). To determine the settings and/or hardware (like Arduino board) whether it is right or not, a simple method is to choose a sample carried by Arduino software to have a test. Here, this test is a simple way to make a single LED blink.

Open Arduino software, then choose File→Examples→01.Basics→Blink, as shown in Figure 1-10.

Figure 1-10. A simple test program

Choose a shortcut menu “Upload” on the Arduino platform, as shown in Figure 1-11.

Figure 1-11. Code is uploaded to Arduino board

3) Choose COM port

Choose COM port, here, COM4 is chosen, as shown in Figure 1-12. But why is COM4, and not COM3? To answer this question, we can check which port is corresponding to the USB serial port on the Arduino mainboard. Firstly, we select “computer” (or my computer)→Right click mouse, select the last submenu “property”→”device manager”→double click “COM and LPT”, then we can find the PORT connected with Arduino board is COM4.

Figure 1-12. Select COM4

Naturally, we can choose the type of Arduino board and port on the software platform, i.e, choose Tools→Board→select “Arduino Uno” (in this experiment, we choose Arduino Uno, which is printed and can be seen on the Arduino mainboard. Then, choose Tools→Serial Port→COM4, why choose COM4? Please return the start of this step.)

1. Test.

Another way to blink the LED is similar to the first method. It is just different the pin connected LED. Let us start the second way to blink LED, which is connected to pin 8 on the Arduino board (certainly, we can choose randomly the pin on the board).

Referring to Figure 1-9, we can also blink a LED. But, one question may be presented for us. Why do we choose a so much complex way to blink a LED. Yes. This is a very good question. Since this way is very flexible. For example, we can choose any pin on the Arduino board to blink a LED, and we can also alter the colorful LEDs. The first way can do this? In this experiment, we choose randomly pin 8 to finish this experiment. When we already set up the circuits by Figure 1-9, we can start write the code for this experiment, which can be seen in Program1.

 01 /* 02   Program1: Blink a single LED, which is blink repeatedly by one frequency. 03 */ 04 05 /* LED is connected to pin 8 on the Arduino board, and it has a name led8, since it is also a variable.*/ 06 07 int led8 = 8; 08 09 // the setup block is used to initialize some parameters. 10 void setup() { 11 12   pinMode(led8, OUTPUT); /* initialize the digital pin as an output. Note that, the capital letters can’t be changed. */ 13 } 14 15 // the loop block make the LED blink repeatedly, till power off the Arduino board. 16 void loop() { 17   digitalWrite(led8, HIGH);   // Light up LED. i.e., give led8 a high voltage level. 18   delay(1000);               // make LED bright for 1s=1000ms. 19   digitalWrite(led8, LOW);    // turn the LED off by making led8 LOW 20   delay(1000);               // make LED dim for 1s=1000ms. 21 } 22 /* Note that, in Arduino software platform, int and void are keywords, setup(), loop(), digitalWrite(), and delay() are the system functions. OUTPUT and LOW can be understood the constant values. Important! The capital letters cannot be changed for all of these words. In other words, Arduino is sensitive to the letter case. */

Then, we can upload the above code to Arduino board under the Arduino IDE software. After a while, “Done uploading” will appear at the state bar. At the same time, the LED in pin 8 on the Arduino board is blinking successfully to you repeatedly with the frequency 1s.

Figure 1-14. Program1 is uploaded to Arduino board

6) Solution for Code

From this experiment, we have:

1. This program can be divided into 3 big blocks roughly.
1. Definition part: all variables must be defined before used. Similarly, pin 8 must also be defined, which make Arduino know it. Grammar is already introduced in the part “Basic Grammar of Arduino“.
2. Initialized part: all the initialized sentences are located here by using “void setup() {…}”. Here, using pinMode sentence to initialize led8 as a OUTPUT port.
3. Loop part: this is realized by using “void loop() {…}”. Note that, in this block, all code sentences is run repeatedly till power off the Arduino board. For example, digitalWrite(led8, HIGH) sends a high voltage level to led8, which is corresponding to pin 8 on the Arduino board. After led8 is at high voltage level for 1s (delay (1000)), digitalWrite(led8, LOW) sends a low voltage level to led8 for 1s. This is the whole process. In the next, the whole process will be repeated.
2. Relevant instructions: int, void are the key words, and loop(), digitalWrite(), delay(), pinMode() are the system functions for the realization of some functions. OUTPUT, HIGH, and LOW are the system constants. led8 is the user-defined variable. Especially, the key words and constants are the system-defined. They are sensitive to the letter case. We cannot change randomly the letter case. In addition, as for digital signal, its value has only two types of values: HIGH/LOW, which is similar to 0/1 in computer science.

# Seg 5: Experiment envoriments

1. Exprimental enviroment

In the section, we will descripe the software and hardware platform for this expriment.

1.3.1 Hardware

For clearly express our ideas by the experimental realization, we generally draw a schematics. Thus, we can set up our experimental environment by the schematics. Here, we firstly give out the schematics of this experiment by using fritzing software (which can be downloaded from http://fritzing.org/download/), which is shown in Figure 1-7. Note that, to ensure the exactness of the schematics, we usually draw the schematics by the direction of work current, which can avoid error or miss some elements.

Figure 1-7. Schematics of blink of single LED

Additionally, we must have the required materials from Figure 1-7, and which are also shown in Figure 1-8.

Figure 1-8. The required real materials

By following Figure 1-7, the circuit in this experiment can be easily set up, which is shown in Figure 1-9.

Figure 1-9. Real environment in this experiment

# Seg 4: The required materials: LED and breadboard

1. Light Emitting Diode (LED)

LED is a solid state semiconductor, which can transform electronical energy into visible light. Its priciple is very complex. In the later, we will introduce its priciples in the later electronical element parts. Usually, the pins of LEDs may be long or short, as shown in Figure 1-5.

So, there is a problem: how to choose the positvie or negative? In general, the two pins of led are either long or short. Therefore, the long pin of LED is connected to positive end, and the short pin is corresponding to the negative end. If the longth of the two pins are the same, what can be do? We can do our choice by the following rules.

Figure 1-5(a) LED pins Figure 1-5(b) LED structure

1. Geneal rule: the long pin is positive, and the short one is corresponding to negative.
2. Chip judgement: if the longth of the two pins are the same, then we can choose the plarity by the inside chip of LED, as shown in Figure 1-5(b). That is, the big chip is corresponding to the negative and the other is positive.
3. Multimeter: suppose red pen connects “+”, and the other is “-”. When we use multimeter to measure LED, we can’t get the result by low resistance. So, we can use R*10K. Then, if the value of resistance is small, the black pen is corresponding to the positve, and otherwise, the black pen is negative.
1. Current limited resistance 220

Fisrstly, we ask ourself a question: why do we need to choose a current limited resistance? If we use a current limited resistance, how about the results. If the resistance is necessary, how to choose its value? This is a very good question.

Before answer these questions, we must know the parameters and characteristics about LED.

Some important parameters

By looking for the relavent resources, we know that the general LED voltage paparameters: the positive saturation voltage pressure is 1.6~2.1V, and the positive work current is 5~20mA. As for the LED, the maximum of reverse voltage pressure is a very important parameters, and its value is 5V. But, in our such exprement, the providable voltage is 5V by the Arduino control board. This may lead to damage LED.

Some important characteristics

1. Maximum of allowable power consumption: it means the maximum, which equals the product of positive voltage pressure and current on the LED. If more than this value, LED will be hot, and even damaged.
2. Maximum of positive direct current: the allowable maximum of current added on LED. Similarly, if more than this value, LED will be damaged, or even brokendown.
3. Maximum of reverse voltage pressure: the allowable maximum of reverse voltage pressure. If more than this value, the LED will be damaged, or even brokendown.

In additon, LED could be effected by the work enviroment tempreture. When lower or higher some tempreture value, LED can not work normally. Moreover, LED has another important characteristics V-I. That is, if the voltage pressure lowers some threshold value, LED can not emit because of its small work current. If more than some threshold value, the positive work current increase fastly, and then LED emits light. So, in the later exprement, we would control the brightness level of LED by Arduino board.

Now, we will eazily answer the above-mentioned questions. Since the providable voltage id 5V by Arduino board, the voltage pressure will more than its limit value. So, by introducing a current-limited resistance, we can control the voltage (including reverse voltage), work current, or power consumption. Next, let’s compute the value of current-limited resistance.

To make LED work normally, we must let it work at a normal work voltage and current, and this must compute the value of current-limited resistance. Assume that Arduino board can provide a total voltage being 5V. As for LED, its normal work voltage is set as U=1.7V, and work current I=15mA. Then, the value of current limited is R=(5-U)/I=(5-1.7)/0.015=220Ω.

Bread board is designed exclusively for the stainless electronic circuit experiment. By using it, we can set up easily and fastly the all kinds of required circuits. Its use is very easy, which is shown in Figure 1-6. The bread board can be devided into the upper, middle and lower three parts. The upper and lower parts is called as narrow part, and the middle is named as broadband part. During the use, as for the narrow part, the voltage value is the same in each row, and in each collumn, the voltage value is the same for the middle broadband part. This will help us to build a circuit fastly and exactly.

Figure 1-6. Structure of bread board

# Seg 3: The required material: USB data and dupont lines

1. USB data line

Generally, the USB data line is taken with Arduino control board. We do not buy it especially. Its main function is used to connect to Arduino development board, and then our codes can be burned to the Arduino board.

Figure 1-2. all kinds of USB interfaces.

In general, the USB data line can be divided into USB-A and USB-B. But, if you check its classifications, you will found there are many types of definitions and names about the USB lines, just like Figure 1-2. Many more types USB interfaces could also be referred in USB official website www.usb.org. In this experiment, we just use and introduce the following interfaces, which can be seen in Figure 1-3.

Figure 1-3(a) USB-A Plug Figure 1-3(b) USB-A Receptacle

Figure 1-3(c) USB-B Plug Figure 1-3(d) USB-B Receptacle

In addition, if let LED connect other port, like 8 in Arduino board, one LED and 2 dupont lines need to be added to this experiment.

1. Dupont line

It is generated by the United States dupont company to sew thread with special utility. Later, this concept is used to the electronical industry. Figrue 1-4 is illustrated the dupont lines, which can be used to the extension of pins. So, by the use of the dupont lines, we can fastly and reliablely to connect the eletronical elements with stainless during experiments.

Figure 1-4. Dupont lines.