1. Arduino learning notebook -- Button experience (volume 1)
1. Arduino learning notebook -- Button experience
www.juvtamll.com ( a company supply PCB Prototyping, PCBA service, sell modules.)
The button is a commonly used key, normally used to connect or disconnect the circuit, so as to control the the motor or other equipment.
The buttons have a variety of appearance. This experiment is used for this kind of miniature keypad, 6mm, as shown below.
Look at the back of the button, it has 4 pins.
When the button not be pushed, No. 1 and 2 are connected, 3 and 4 are connected.
When the button is pressed down, all the pins will be connected.
We use button to control the led.
The general situation is install the button series in the led circuit, however, it’s too simple.
We use the indirect way, the button judge output voltage from the circuit. If the voltage is greater than 4.88 V, output high level to the LED circuit, whereas output low level. Using logical judgment to light or off the LED, this control method is used widely.
The connection method is shown below.
A part of button is connected to 5V pin, the other side is connected to analog pin 5. LED long pins in series 220 Ω resistance connecting pin 7, short pins to GND.
Upload the following code to the arduino control panel to see the effect.
1. int key = 7; / / set the digital IO pin of the LED
2. void setup ()
4. pinMode (key, the OUTPUT); / / set digital IO pin to output mode
6. void loop ()
8. int I; / / define variable
9. while (1)
11. i = analogRead (5); / / read the voltage from analog pin 5
12. if (i > 1000) / / if the voltage value is greater than 1000 (i.e. 4.88 V)
13. digitalWrite (key, HIGH); / / set the seventh pin for high level and light led
15. digitalWrite (key, LOW); / / set the seventh pin for low level and extinguish led lights
The experiment uses analogRead() function to read the values of the analog interface.
Divide the input voltage of 0-5v into 1024 copies, each of which is approximately 0.0049 V and its value is between 0 and 1023.
If the value is greater than 1000, output high level to LED, the corresponding voltage will be greater than 4.88 V.
The scope and resolution of the analogRead () command can be modified by using the analogReference () command.
At the beginning of this experiment, the standard of judgment was 512, which is 2.5 V.