Category Archives: Action 3: Arduino, Button, and LED

How to use Arduino controller and button to control the blink of LED

Seg 3: hardware and software for removing jitters

The first method: remove jitter from hardware

Firstly, we should get a thorough understanding of the reasons of generating jitters. From Figure 3-5, 3-6, we know that, even if the analog port A0 is cut, there exists voltage to trigger LED light. This shows that there exists some interference around us. If the interference is expressed as voltage, its value may be bigger than 512. Therefore, according to Figure 3-3, a bias resistor can be paralleled between ports 1, 2 and 3, 4. In this way, if the button doesn’t be pressed down, and if there are some interference existing as a voltage level, the voltage will be consumed on the bias resistor. Thus, the LED does not light, if the button doesn’t be push down. By adding a bias resistor (i.e., pulldown resistor), the circuit is changed, as shown in Figure 3-8. Evidently, from this Figure, the extra voltage has been already consumed on the bias resistor. Equivently, the extra voltage is already masked. At this time, if the button doesn’t be pressed down, the LED light doesn’t light. When the circuit is prepared by Figure 3-9, the LED light would light after the code in Program 3 is burn into the Arduino board. Everything is the same as expected.


Figure 3-9 Circuit after adding a bias resistor

Appendix: pulldown resistor (bias resistor)

As shown in Figure 3-10, the bias resistor is connected to GND in the above, so its name is called as pulldown resistor, which means the voltage in location A is pulled down to a low voltage level (GND). Its main function is to make the circuit generate a stable voltage with other resistors and driven circuit.


Figure 3-10 Pulldown resistor

The second method: remove jitter from software

Principle: as the above-mentioned description, once the button is pressed down, there is a delay 5~10ms because of the jitter. So, we can let the trigger delay 5~10ms to make the jitter disappeared in this time. Then, we can make a decision by the voltage after this time. This way better fits for many buttons, since many pulldown resistors are very complex to be arranged. However, during our experiment, if the pulldown resistor doesn’t be connected to the circuit, the LED is still lighting for any long time. Generally, we can combine software and hardware to remove the jitters. In the following appendix is attached here.

Appendix: the code removed jitters by software

01 /*
02 Program 3.1: Remove the jitters by software and hardware
03  */
04  
05 int Button=3; 
06 int LED=13; 
07 boolean onoff=LOW; 
08 void setup()
09 {
10   pinMode(Button,INPUT); 
11   pinMode(LED,OUTPUT); 
12 }
13 void loop(){
14   if(digitalRead(Button)==LOW
15   {
16     delay(10); 
17     if(digitalRead(Button)==HIGH
18     {
19       digitalWrite(LED,onoff);  
20       onoff=(!onoff); 
21       delay(10);  
22       while(digitalRead(Button)==HIGH
23       {
24         delay(1);
25       }
26     }
27   }
28 }

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

Seg 1: How to use Arduino controller and button to control the blink of LED

3.1 Problem description: How to use Arduino controller and button to control the blink of LED

The previous two examples are relatively simple. When the corresponding codes are burn into the Arduino board, then the LED could blink. But it cannot interact with people. So, in our such experiment, by adding a new material, button, to control the blink of LED.

3.2 The required material

Table 3-1 the required material

Num

Name Qua Function Note

1

Arduino software 1 IDE Version 1.05

2

Arduino UNO board 1 board

3

USB line 1 burn

4

Dupont lines many Connection elements

5

LED 1 LED

6

Resistor (10, 200Ω) 2 Current limited

7

Breadboard board 1 connection

8

button 1 onoff

Before doing this experiment, we firstly introduce the relevant properties of botton.

Button

Button is an usual device in the design embedded system. By button, some instructions and data can be utilized to control the on/off states, which can control the run states of some devices. For example, in this experiment, by button, the high/low level is generated to control the blink of LED. But, there are many kinds of switches, such as a single switch in kitchen, double switches in bedroom, voice control switch in corridor, and so on. In our such experiment, we mainly use small/or miniature switches, as shown in Figure 3-1.


Figure 3-1 Miniature buttons

And the size of the miniature switch in this experiment is about 6*6*5mm and has four legs, which is shown in Figure 3-2.


Figure 3-2 Miniature button in this experiment

Note that, the two logs divided by a deep ditch is the same side. Its principle is also given out in Figure 3-2. If the button is pushed down, the four logs 1,2,3,4 would connected each other. Then this can trigger a high level to light the LED. But if you relax the button, 1,2,3,4 would be off


Figure 3-3 Schematics of button