Which Button is Pressed?
Description
When we talked about analog and digital sensors earlier, we talked about the single-channel key module. When we press the key, it outputs a low level, and when we release the key, it outputs a high level. We can only read these two digital signals. In fact, the key module ADC acquisition can also be performed. In this kit, a DIY electronic building block five-way AD button module is included.
We can judge which key is pressed through the analog value. In the experiment, we print out the key press information in the shell.
Working Principle
Let’s look at the schematic diagram, when we do not press the key, the OUT of S output to the signal end is pulled down by R1. At this time, we read the low level 0V. When we press the key SW1, the OUT of the output to the signal end S is directly connected to the VCC. At this time, we read the high level 3.3V(the figure is marked as a 12-bit ADC(0~4095) and VCC is 5V. The principle is the same. Here we have VCC of 3.3V and ADC mapped to 12 bits), which is an analog value of 4095.
Next,when we press the key SW2, the OUT terminal voltage of the signal we read is the voltage between R2 and R1, namely VCC*R1/(R2+R1), which is about 2.64V, and the analog value is about 3276.
When we press the key SW3, the OUT terminal voltage of the signal we read is the voltage between R2+R3 and R1, namely VCC*R1/(R3+R2+R1), which is about 1.99V, and the analog value is about 2469.
When we press the key SW4, the OUT terminal voltage of the signal we read is the voltage between R2+R3+R4 and R1, namely VCC*R1/(R4+R3+R2+R1), about 1.31V, and the analog value is about 1626.
Similarly, when we press the key SW5, the OUT terminal voltage of the signal we read is the voltage between R2+R3+R4+R5 and R1, namely VCC*R1/(R5+R4+R3+R2+R1), which is about 0.68V, and the analog value is about 844. R4+R3+R2+R1), which is about 0.68V, and the analog value is about 844.
Components Required
Connection Diagram
Test Code
//**********************************************************************************
/*
* Description : Read the value of Five AD Keys
* Auther : http//www.keyestudio.com
*/
int val = 0;
int ADkey = 34; //Define five AD keys connected to GPIO36
void setup() {
Serial.begin(9600); //Set baud rate to 9600
}
void loop() {
val = analogRead(ADkey); //Read the simulated value of the AD key and assign it to the variable val
Serial.print(val); //A newline prints the variable val
if (val <= 500) { //Val is less than or equal to 500 when no button is pressed
Serial.println(" no key is pressed");
} else if (val <= 1000) { //When key 5 is pressed,val is between 500 and 1000
Serial.println(" SW5 is pressed");
} else if (val <= 2000) { //When pressed,val is between 1000 and 2000
Serial.println(" SW4 is pressed");
} else if (val <= 3000) { //When pressed,val is between 2000 and 3000
Serial.println(" SW3 is pressed");
} else if (val <= 4000) { //When key 2 is pressed,val is between 3000 and 4000
Serial.println(" SW2 is pressed");
} else { //When key 1 is pressed,val is greater than 4000
Serial.println(" SW1 is pressed");
}
}
//**********************************************************************************
Code Explanation
We assign the read analog value to the variable val, and the serial monitor displays the value of val, (we set to 9600).
When the analog value is in the range of 500 and 1000, the button SW5 is pressed; when the analog value is in the 1000 and 2000, the button SW4 is pressed; when the analog value is between 2000 and 3000, the button SW3 is pressed; when the analog value is between 3000 and 4000, the button SW2 is pressed. When the analog value is above 4000, we judge that the button SW1 is pressed.
Test Result
Connect the wires according to the experimental wiring diagram, compile and upload the code to the ESP32. After uploading successfully,we will use a USB cable to power on. Open the serial monitor and set baud rate to 9600; when the button is pressed, the serial monitor prints out the corresponding information, as shown in the figure below.