(3) Another way to learn the basic components of button control LED lights

Publisher:JoyfulMelodyLatest update time:2023-01-30 Source: zhihu Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

animated cover

Code:

void setup(){
  pinMode(2, OUTPUT);
  pinMode(1,INPUT);}void loop(){
  if(digitalRead(1))
  {
  	  digitalWrite(2, LOW);
  }
  else
  {
  	  digitalWrite(2, HIGH);
  }
  delay(10); // Wait for 10 millisecond(s)}

button

Buttons are an input device that we use a lot, and they are an important way for microcontrollers to obtain external signals. There are also many variations of buttons in actual circuits. No matter how they change, as long as they meet the requirements of the button signals, we basically They can all be handled in the same way as pressing the keys, with little difference.

Real touch switch

Mechanical Dimensions

Internal circuit principle

This kind of switch is also called a tact switch. It is widely used in electronic products. It is also a switch that we often come into contact with when learning microcontrollers and embedded systems.

We usually use a switch to feel that this thing is either off or on, just these two actions, but the actual process of pressing and rebounding of the switch is like this

Ideal waveform and actual waveform

Next, let’s implement the button control LED light on and off experiment:

Experimental materials: 6*6 touch buttons, 5mm red LED light, resistor (270Ω), adjustable power supply

animated cover

Experimental results: When the light touch button is pressed, the LED light turns on, and when the light touch button is released, the LED light goes out.

think? ? : Can we use MCU to accept touch button signals to control LED lights? Use a button to turn the LED light on and off. Even if you release the button, the LED light will still be on? The answer is yes, let’s do it together

The experimental schematic diagram is as follows:

The components included are: a 6*6 touch button, a 5mm red LED light, a 1/4W/270Ω in-line resistor, a 1/4W/10K in-line resistor, an arduino uno development board, and a Type USB cable. , connect some wires

Button control LED light schematic diagram

Earlier we learned how to control the LED light to turn on and off, so we won’t go into details here. Next, we learn how to detect the buttons through the microcontroller, that is, the Arduino. Before that, we first learn a little bit about the microcontroller.

High-impedance state: The Arduino pin is in a high-impedance state by default. This literal meaning is easy to understand. It means that the resistance is very large, usually at the MΩ level, so it is very unstable in this state and any interference The signal will cause false triggering, which is why I connected a pull-down resistor in the schematic diagram.

Pull-up: Connect a resistor between the microcontroller pin and the power supply, usually 10K

Pull-up resistor

Pulldown: Connect a resistor between the microcontroller pin and ground. Generally, 10K~47K can be selected.

Pull-down resistor

I am using the default method here, so I connected a pull-down resistor, so that when the button is not pressed, the input pin 1 of the microcontroller is low level, and when the button is pressed, it is connected to the power supply and is high level, as shown below The meaning of the code is that when pin 1 is detected to be low level, the LED light pin is set to high level, and the LED light does not light up; when pin 1 is detected to be high level, the LED light pin is set to Low level, the LED light is on.

void loop(){
  if(digitalRead(1)) //If input pin 1 is high level {
  	  digitalWrite(2, LOW); // LED light on 
  }
  else // Otherwise the input pin is low {
  	  digitalWrite(2, HIGH); // LED light off }
  delay(10); // Wait for 10 millisecond(s) // Detect key presses in a 10ms period}

Experimental phenomena:

animated cover

In order to see the effect more intuitively, we add two voltmeters to measure the voltage of pin 1 and pin 2 respectively, so that we can more intuitively see the voltage change effect of the LED light and button.

animated cover

In the above example, we found that the LED light went out immediately after the button was released. So, can we press the button to keep the LED light on without changing the circuit, and then press it again to keep it out? Our wise ancestors have already thought of this for us. Let’s try to change the program.

code show as below:

Code principle: First detect whether the button is pressed. If pressed, delay 20ms to eliminate jitter, then detect whether it is pressed. If pressed, flip the status of the LED light, digitalWrite(2,!digitalRead( 2));, the principle of flipping is to read the current status of pin 2 and then invert it.

void setup(){
  pinMode(2, OUTPUT);
  pinMode(1,INPUT);}void loop(){
  if(1==digitalRead(1))
  {
    delay(20);
  	if(1==digitalRead(1))
    {
     	digitalWrite(2, !digitalRead(2)); 
    }
  }
  delay(10); // Wait for 10 millisecond(s)}

Button effect:

animated cover

In the above experiment, we basically realized the status maintenance of the button control LED light. Careful friends may find that the control is very smooth. You can use your brain to further optimize the code to achieve the effect you want, such as the state machine processing method.


Summarize:

1. Buttons are a type of human-computer interaction device that we use a lot in our lives. The feel of the buttons will directly affect the experience.

2. Through the buttons, we can actually further derive the variants of the buttons. The principles are the same, but the uses are different.

Self-locking switch

Micro Switch

Limit switch

3. Furthermore, if the signal output by one of our circuit modules is also this high and low button level signal, it can also be treated as a button.

Thermal infrared sensing module

Photoelectric switch

Electromagnetic induction switch

4. The same circuit with different codes can achieve different effects. This is also the fun of programming. As we accumulate basic knowledge later, we will further discover the fun of programming.


Reference address:(3) Another way to learn the basic components of button control LED lights

Previous article:PWM video code analysis and explanation
Next article:(2) Another way to learn basic components of LED lights

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号