Tag Archives: Arduino

Seg 7: Key points and summaries


Figure 7-6 Arduino photon control LED


Figure 7-7 Arduino serial monitor

If we let the outside is much stronger, the value of photoresistor would decrease rapidly. This would let the value of the distribution voltage decreases rapidly, which cannot trigger the Port 8 as a high voltage level on the Arduino board. Therefore, the LED light connected to Port 8 CANNOT be lightened, as seen in Figure 7-8. We can change the strength of the light by using our mobile phone.


Figure 7-8 LED is dark by photoresistor

7.5 Key points and summaries

1) The value of photoresistor would be changed to be small along with the stronger outside light. So the value of voltage is so small that it cannot trigger the Port connected to LED light a high voltage level, which leads LED to be dark.

2) By utilizing the principle of distribution voltage, the photoresistor can control the LED light or dark with Arduino board.

3) Generally speaking, the values of light and dark photoresistor are given out in the product description.

4) Photoresistor is widely applied to the automatic control.

Seg 1: How to use Arduino make a controllable LED light?

7.1 Problem: How to use Arduino make a controllable LED light?

In this experiment, we will use an Arduino board to make a controllable LED light, which let LED light bright and/or dark by the strength of light. This experiment is very usable. For example, when night is coming, we should let the street lamp light, and the lamp should be dark in the daytime for the energy saving. So, with respect to this experiment, we can make a light controllable lamp in our home. Similarly, we can also make voice controllable LED light.

7.2 The required materials

The required materials are also very simple in this example. On the basis of experiment, we just use photoresistor to replace the nixie tube, which are shown in Table 7-1.

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

Photoresistor 1 control optional

7

Bread board Connection optional

 

Seg 6: Experiment and code


Figure 6-8 Experimental circuits

byte seven_seg_digits[10][8] = { // set the code array by the number and pins

{ 1,1,1,1,1,1,1,0 }, // = 0

{ 0,0,1,1,1,0,0,0 }, // = 1

{ 1,1,0,1,1,1,0,1 }, // = 2

{ 0,1,1,1,1,1,0,1 }, // = 3

{ 0,0,1,1,1,0,1,1 }, // = 4

{ 0,1,1,1,0,1,1,1 }, // = 5

{ 1,1,1,1,0,1,1,1 }, // = 6

{ 0,0,1,1,1,1,0,0 }, // = 7

{ 1,1,1,1,1,1,1,1 }, // = 8

{ 0,1,1,1,1,1,1,1 } // = 9

};

void setup() { //set 4-11 as OUTPUT

pinMode(4, OUTPUT);

pinMode(5, OUTPUT);

pinMode(6, OUTPUT);

pinMode(7, OUTPUT);

pinMode(8, OUTPUT);

pinMode(9, OUTPUT);

pinMode(10, OUTPUT);

pinMode(11, OUTPUT);

}

void sevenSegWrite(byte digit) { //set the digital array by the order of the ports 4-11 on arduino

byte pin = 4;

for (byte segCount = 0; segCount < 8; ++segCount) {

digitalWrite(pin, seven_seg_digits[digit][segCount]);

++pin;

}

}

void loop() { //display the digital number by reverse order

for (byte count = 10; count > 0; –count) {

delay(1000);

sevenSegWrite(count – 1);

}

delay(2000);

}

6-5 Key points and summaries

  1. Nixie tube can be divided into the common anode and cathode. The judgments may have Arduino used directly and multimeter.
  2. We should know the order of the pins by digital number and letter. This can help us to get the code array for each digital number.
  3. When encoding, we should connect the pins of nixie tube and the ports on the Arduino. It will be difficult to connect each other with error.

Seg 4: Order of the pins for the nixie tube and coding

  1. Order of the pins of the nixie tube

Similarly, why do we know the order the pin? If we do not know the order of the pins, we do not know how to connect to the pins of nixietube. For example, if we want to display the digital number 5, how can we connect the dupon lines from the pins of nixie tube to the ports on the Arduino? To connect to the Arduino board rightly, we must know the connect rules.

  1. re-know the nixie tube

We firstly know the pins of nixie tube from the followings in Figure 6-6.


Figure 6-6 Pins connection of nixie tube

  1. As shown on the left subfigure of Figure 6-6, the digital number is labeled from the lower left by the counterclockwise, and the number of point is 5. All of the number is 1~10.
  2. The letter label is marked from up to down by the clockwise. It is a~h, respectively, where the number of point is “h” as the last location.
  3. Digital number 3 and 8 is the common port, which is the common anode or cathode. Figure 6-6 is the common cathode nixie tube; that is, all of the negative pins are connected a common port 3 or 8.
    1. Coding of nixie tube

What is coding? To make the nixie tube display the designed digital number, we should let the LED to be conduction. For example, we want to display number 5 in Figure 6-6, how? Firstly, the polarity is must be right. In such experiment, the nixie tube is the common cathode. Thus, the common cathode should be connected to the GND port on the Arduino. Other pins must be corresponding to the ports on the Arduino to display the right digital number. This is coding. For example, in the letter view, if let the pins of a,c,d,f,g set as a high voltage level (or 1), and other pins is set as low level, (or 0), then, the corresponding segments would be light and digital number 5 can be displayed on the nixie tube. Since the decimal point “h” would not affect the display of digital number, it can be conducted or not. In this experiment, the decimal point is conducted. From the digital view, pins 2,4,5,7,9,10 are conducted, and the other remaining ones are not. Therefore, if we use binary code to express this segment, it can be coded as {0,1,1,1,0,1,1,1}. That is, there are two segments dark among of the eight segments in the nixie tube. Then, digital number “5″ can be displayed correctly. By this coding method, we can get other digital number coding, as shown in Table 6-2.

Table 6-2 Digital number coding scheme for the common cathode nixie tube

Arduino ports

Nixietube pins

0

1

2

3

4

5

6

7

8

9

4

1(e)

1

0

1

0

0

0

1

0

1

0

5

2(d)

1

0

1

1

0

1

1

0

1

1

6

4(c)

1

1

0

1

1

1

1

1

1

1

7

5(Dp)

1

1

1

1

1

1

1

1

1

1

8

6(b)

1

1

1

1

1

0

0

1

1

1

9

7(a)

1

0

1

1

0

1

1

1

1

1

10

9(f)

1

0

0

0

1

1

1

0

1

1

11

10(g)

0

0

1

1

1

1

1

0

1

1

Note that, to avoid the error of connection to Arduino, or convenience, we had better encode the digital number by one-defined rule. For example, in this experiment, let the digital ports on Arduino board correspond to the pins of nixietube for small to big (1~10). Certainly, there are many encoding schemes by following different ones’ habits. But the effect of display and the code is the same.

Seg 3: Ways to judgment of the polarity of the nixie tube

  1. The judgment of the polarity of the nixie tube

Before giving the method, we ask ourselves a question. Why do we judge the positive and negative polarity of nixie tube? Since the nixie tube is similar to LED, to make LED bright, the polarity must be right. This can make LED switching into conduction.


Figure 6-3 Schematic diagram of Arduino measures the polarity

There are two general methods to judge the polarity of nixietube shown in the following.

First method: use directly the ports on the Arduino (heuristics)

  1. According to the schematic diagram in Figure 6-3, we finish the connection of circuits, as shown in Figure 6-4, where, the voltage is provided by the 5V port on the Arduino. In such experiment, the value of resistor is R=220Ω, and the voltage is 5V.
  2. Since we don’t know the COM port in nixietube whether anode or cathode, we don’t know how to connect the polarity of power. But, we can know the common pin of nixietube. So, we can let the negative connect to the COM port (i.e., 3 and 8 pin), then the positive can be connect to the redundant pins randomly. At this time, if the segment is bright, then it shows that the polarity is right. That is, the COM port is negative, and the other pins are positive. Then the segments for the nixie tube can be lightened. Moreover, this is a common cathode nixie tube. Therefore, generally speaking, if the segment is not bright, it shows that this is a common anode tube. However, to make sure it is common anode, we can change the order of the connection of negative and positive till some segment is light. Note that, a current-limited resistor must be connected to avoid the damage of LED. In Figure 6-4, the positive (in hand) is connected to the 5V port on the Arduino board, while the GND is connected to the COM port of nixie tube by a mother dupont line. After connection, the 10th segment is light, which in fact is corresponding to the pin 10 of nixie tube. This shows that the polarity is right, and the LED is conducted. Thus, we know that the negative is com, i.e., common cathode.


Figure 6-4 Using Arduino to measure the polarity of nixietube

Second method: Using the multimeter

In addition, we can also use a multimeter to measure the polarity of nixie tube. Its principle is also can be referred to Figure 6-3. In fact, the main principle is that the multimeter can be viewed as a power for the nixie tube, as shown in Figure 6-5.


Figure 6-5 Using multimeter to measure the polarity of nixietube

The measuring method is very simple. After the simple setting of the multimeter, as shown in the special marked label in Figure 6-5, the multimeter must be turned to the diode. The black pen is connected to the COM port (pin 3 or 8) of nixie tube, and the red pen is touched to the other remaining ports randomly. Then, if the segment is bright, it shows that the polarity is right. So, this is a common cathode nixie tube. In other cases, we changing the connection order of the pen. But, in this experiment, since the voltage is not very high, so the brightness is not enough.

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 2: Experiment, principle, and code

5.4 Experimental principle

The principle is simple in this experiment, which is summarized as follows. The buzzer can make a sound, since the current on the buzzer is powered on the electromagnetic coil, which can make coil generate magnetic field. Then it can drive vibrating diaphragm to generate a sound. This is the reason why the buzzer must need the current. Its schematic principle is drawn in Figure 5-3. In our such experiment, port 9 on the Arduino board can provide a voltage for the passive buzzer. Then it can generate a sound. It is worth noting that, the rated voltage for the buzzer is 5V, while the Arduino’s max voltage is 5V as well. Therefore, the circuit cannot connect serially a resistor. If connected, then the buzzer doesn’t work.

5.5 Experiment and code

In fact, the circuit connection of such experiment is very simple, which is seen in Figure 5-3. After run Program 5, Arduino can generate alarm sound.


Figure 5-3 Experimental circuit

01 // Program 5: how to use Arduino and buzzer generate alarm sound
02 
03 void setup()
04 {
05 }
06  
07 void loop()
08 {
09 for(int i=200;i<=800;i++)   // frequency from 200Hz to 800Hz
10 {
11   pinMode(4,OUTPUT);
12   tone(4,i);       //output frequency at port 4, i.e., generate a sound
13  delay(5);       //generate a sound for 5ms 
14 }
15 delay(4000);           //the highest frequency lasts for 4ms
16 for(int i=800;i>=200;i)
17 {
18   pinMode(4,OUTPUT);
19   tone(4,i);
20  delay(10);
21 }
22 }

Here, a new Arduino function “tone” is introducted. Its syntax is:

tone(pin, frequency)

tone(pin, frequency, duration)

where,

pinwhich is defined on the Arduino board

frequency: the voice frequency, unit is Hz, unsigned int

durationvoice lasting time, unit is ms (optional), unsigned long

5.6 Key points and summaries

1) The buzzer can be divided into active and passive. The active one can make a sound by directly connecting power, while the passive one can generate a sound only for the changing frequency. Therefore, the buzzer can make a sound on the Arduino board by PWM technique. But, it cannot connect serially a resistor.

2) The principle of buzzer is that, when the current passes through the electromagnetic coil, it will generate a magnetic field, which can drive the vibrating diaphragm make a sound periodically.

Seg 1: Buzzer introduction

5.1 Problem presentation: how to use Arduino and buzzer to imitate alarm song

This sample is similar to the fourth experiment, which are all use PWM technique. This is because voice is an analog signal. So, if you still use the code in experiment 4, you can listen the voice.

5.2 The required materials

The required materials are very simple. It is just change the LED into buzzer, and the resistor doesn’t be used. Since the voltage is also 5V, the buzzer might not make a sound if a resistor increases. The required materials are shown in Table 5-1.

Table 5-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

buzzer 1 LED blink optional

7

Bread board Connection optional

The real material could be seen in Figure 5-3.

5.3 Elements introduction

Buzzer can be divided into piezoelectric and electromagnetic ones by its structure. The electromagnetic buzzer is composed of oscillator, the electromagnetic coil, magnets, diaphragm and shell vibration. After power on, the oscillator in buzzer can generate the audio signal current, which then make the electromagnetic coil generate the magnetic field. So, Vibrating diaphragm can periodically make a sound under the interaction of electromagnetic coil and magnet.

Additionally, buzzer can be divided into active and passive ones by the signal source. As for the active buzzer, if the rated voltage is put, then the periodic frequency signal can be generated at the inside of oscillator. The periodic signal can drive the buzzer voice. While as for the passive buzzer, it can be viewed as a horn. If and only if a changing electronic signal is added onto the buzzer, it can make a sound. Their model can be shown in Figure 5-1.



  1. Passive buzzer (b) active buzzer

 

Figure 5-1 Buzzer

We can make the difference between the passive and active buzzers by the following three methods.

  1. From the shapes of buzzer shown in Figure 5-1 (a)(b), the two buzzers are so similar to each other in shape that it is difficult to recognize them. But, if careful, we know that the height of the two buzzers is different. The passive is 8mm in Figure 5-1(a), while the active is 9mm in Figure 5-1(b).
  2. if the pins are above, the green board is passive buzzer, while the other is active one, whose board is closed by the black sheet.
  3. the more scientific method is by using multimeter with resistance Rx1. Let the black pen in multimeter connect the “+” pin of buzzer, and the red pen touch to and fro the other pin. If we listen the “ka…ka” sound and the resistance value is only 8Ω or 16Ω, then this buzzer is passive; if we listen the continuous sound and the resistance value is more than hundreds of ohm (Ω), then this buzzer is active.

Note that, the active buzzer can make a sound continuously by connected a rated power, which is labeled on the new version buzzer; while the passive is similar to an electromagnetic speaker, which must be connected an audio circuit, the buzzer can make sound. Additionally, as shown in Figure 5-2(a), a symbol “+” is labeled on the passive buzzer. But during the experiment, the passive buzzer can make a sound regardless of the positive or negative polarity on the Arduino board. Generally speaking, we had better do the corresponding experiment by the label.


Figure 5-2 Circuit schematics

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