Tag Archives: experiment

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 2: Schematics and Experiments

3.3 Schematics for experiment

When button is pressed down, a high voltage level can be obtained. Then it will trigger Arduino to control the blink of LED. Certainly, we can let the LED blink when the button is pressed down. Its schematic is shown in Figure 3-4, which can be viewed as extension from the first experiment by adding a button.


Figure 3-4 Experiment principle diagram

And its real circuit is shown in Figure 3-5.


Figure 3-5 Circuit block

After the circuit is prepared by Figure 3-5, Program 3 can be run, which is also presented as follows.

01 //Program 3: How to control the blink of LED by Arduino and button
02 
03 int led8=8;// set pin 8 as the digital port for LED on the Arduino board
04 void setup()
05 {
06  pinMode(led8,OUTPUT);// set digital port 8 as OUTPUT
07 }
08 void loop()
09 {
10    int i;// define i as the received voltage
11    i=analogRead(0);//read analog port 0 to port i
12    if(i>512)// set the voltage threshold as i>512(i.e., 2.5V)
13      {digitalWrite(led8,HIGH);// then set port 8 as a high level, which can make led blink
14      delay(2000);}//make LED blink for 2s
15    else
16      digitalWrite(led8,LOW);// if i<512, let port 8 to be a low level, which make led extinguish 
17  
18 }

OK, at this time, when you push down the button, LED can be lighted. But, unfortunately, if you unpress the button, an expected result happens. That is, the LED is still lighting, even if the button loosed. Furthermore, if you hold an end of dupont line by your hand, the LED is still light, as shown in Figure 3-6. But why?


Figure 3-6 Holding an end of dupont, LED is still light

It turns out that, there exists interference around one’s body or environment to some extent. Therefore, even if the button doesn’t be pressed, LED can light. Then, the interface will affect our such experiment by a voltage level, which can trigger the Arduino board to control the state of LED. Certainly, the interface would be different from difference of body and environment. So, when the button is pressed down, because of the flexibility of button, the button cannot trigger the Arduino board with a high voltage level quickly and steadily. There exist some jitters for some time. The length of time is different and determined by the mechanical features. In general, the time is 5~10ms. This is a very important parameter to design a control system. The phenomenon is shown in Figure 3-7. Before or after pressing the button, there exist some jitters. So, we must delete these jitters to avoid the error decision.


Figure 3-7 Jitters appearance

To avoid doing an error choice, we must remove the appearance. There are many ways to remove jitters from hardware and software. It is worth noting that, even if the voltage threshold is set as a value bigger than 512 (512 is a pwm value. In the next sections, we will introduce its principle) in Program 3, the jitter appears as well. What’s more, during the process of our experiment, when i=1000, the LED would light in case the button doesn’t push down. So, we must remove the jitter according to the principle.


Figure 3-8 schematic after adding a bias resistor