Tag Archives: code

Seg 5: Arduino code

(6) Arduino code

void setup()

{

Serial.begin(115200);//set baud value

}

void loop()

{

int n=analogRead(A0);//define A0 as the received port

float vol=n*(5.0/1023*100);//voltage signal to temperature

upload_sensor(vol);//sub-function

delay(5000);

}

void upload_sensor(float vol)

{

// send the HTTP PUT request:

char buf[200];

memset(buf,0,200);

int ret;

ret=sprintf(buf,”GET /upload.php?uid=ycf&ps=ycf&sensor_name=arduino&data=”);

//设置协议

Serial.print(buf); //stored at buf temporally

Serial.print(vol);//use temperature data vol

Serial.println(” HTTP/1.1″); //HTTP protocol name

Serial.println(“Host: api.cduino.com”);//set server website

Serial.println(“Connection: close”);//data transmission is finished, and connection is closed.

Serial.println();

}

Then you can download the above code to the Arduino control board. Note that, there is a switch. You should let the switch on the outside (O) when downloading the Arduino code. This is because the serial connection may affect the data download. At the same time, you can track the data transmission on the monitor at the Arduino soft-platform, which is shown in the red label. The temperature is 25.90, and this data has been transmitted to the remote server cloud by wifi networks.

Seg 5: Schematic diagram and experiment of LM35

  1. Schematic circuit

By the connection diagram shown in Figure 8-2, this experimental schematics is easily draw in Figure 8-3. Note that, we should design the circuit by the pin connection of temperature sensor.


Figure 8-3 Schematic circuit


8-4 Circuit diagram

  1. Code

Run Program 8.

01 // Program 8: use sensor to measure your environmental temperature
02 
03 void setup() {
04  
05   Serial.begin(9600);         //set the baud for serial communication
06 }
07  
08 void loop() {
09  
10   int n = analogRead(A0);    //read the voltage from A0
11  
12   float vol = n * (5.0 / 1023.0*100);   //computation of temperature
13  
14   Serial.println(vol);                   //output temperature
15   delay(2000);                           //wait for 2s for display
16 }

Open the serial monitor on the Arduino software platform, we can see the Celsius temperature sensed by the temperature sensor LM35. Note that, the baud of monitor should be the same as the above code, which can be seen in Figure 8-5.


Figure 8-5 the temperature value on the Arduino monitor

From Figure 8-5, we know that the environmental temperature is about 24.44 in this experiment.

  1. Key points and summaries
  2. We should know the features about the temperature sensor. For example, in such experiment, the temperature of LM35 is linearly proportional to the voltage. By this relationship, we can get the formula about the temperature and voltage.
  3. We should the connection about the sensor logs. As for LM35 in this experiment, let the full-face having characters facing us. Then the left is connected to 5V port on the Arduino board, the right leg is a GND port, which should connect to the GND port on Arduino board, and the middle leg should connect to the analog port on the Arduino board, which can output the concrete value about temperature.
  4. The output temperature value can be seen on the serial monitor. But, the baud for the monitor should be unanimous with the baud set in the experimental code.

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 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.