HOW to Use Five-key AD Button Module with ESP32
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.
Components Required
|
|
|
|
|
---|---|---|---|---|
ESP32 Board*1 |
ESP32 Expansion Board*1 |
keyestudio 5-Channel AD Button Module*1 |
3P Dupont Wire*1 |
Micro USB Cable*1 |
Connection Diagram
Test Code
# Import Pin and ADC modules.
from machine import ADC,Pin
import time
# Turn on and configure the ADC with the range of 0-3.3V
adc=ADC(Pin(34))
adc.atten(ADC.ATTN_11DB)
adc.width(ADC.WIDTH_12BIT)
while True:
adcvalue = adc.read()
print(adcvalue, end = '')
if adcvalue <= 500:
print(" no key is pressed")
elif adcvalue <= 1000:
print(" SW5 is pressed")
elif adcvalue <= 2000:
print(" SW4 is pressed")
elif adcvalue <= 3000:
print(" SW3 is pressed")
elif adcvalue <= 4000:
print(" SW2 is pressed")
else:
print(" SW1 is pressed")
time.sleep(0.5)
Code Explanation
We assign the read analog value to the variable val, and the shell displays the value of val, (our default setting is 9600, which can be changed). We judge the read analog value. When the analog value is lower than 6000, we judge that the button is not pressed. When the analog value is between 6000 and 20000, we judge that the button SW5 is pressed. Between 20000 and 32000, we judge that the button SW4 is pressed.
when the analog value is between 32000 and 45000, we judge that the button SW3 is pressed. When the analog value is between 45000 and 59000, we judge that the button SW2 is pressed. Press. Otherwise, when the analog value is above 59000, we judge that the button SW1 is pressed. If we only use a fixed value, there will inevitably be errors, so we use the interval to judge.
Test Result
Connect the wires according to the experimental wiring diagram and power on. Click “Run current script”, the code starts executing. when the button is pressed, the shell prints out the corresponding information, as shown in the figure below. Press “Ctrl+C”or click
“Stop/Restart backend”to exit the program.