- Ask/Aim:
- Principle:
- Practice:
- Linkage:
- Exercise/Experiment:
01 radio track
1. Problem/goal
There are five ADC channels designed on the main control board of the wireless track smart car, which are used to measure external voltages. Among them, ADC0 is used to measure the internal power supply voltage. The other four ADC1 to ADC4 are used to measure the voltage of the external electromagnetic field sensor. The following is a small experiment to read and display the values of these five analog voltages, and observe their changes. Lay the foundation for better control of smart cars later.
▲ 图1.1.1 ESP32控制板
2. Basic principles
First of all, recognize the position of these AD ports on the circuit board. The four-way AD input port is located on the 6-pin socket on the left side of the circuit board, including four analog signal lines and two power lines of VCC and GND. This port can be directly connected to an external inductive sensor. Another analog output port is located on the power divider resistor inside the circuit board. This is an OLED screen for displaying ADC conversion results.
Let's take a look at the identification of these ports on the schematic diagram, which are the detection ports corresponding to ADC1 to ADC4. The internal ADC0 responds to the voltage of the power supply through two divider resistors. Let's compare the ports of ADC0 to ADC4 connected to ESP32. Finding these ports can provide corresponding port parameters for subsequent programming. ADC0 here, ADC1,2,3,4 below. The ESP32 ports corresponding to the following four ADCs are relatively clear, corresponding to IO34, 35, 32 and 33 respectively. For the port corresponding to ADC0, you can know that it corresponds to IO36 by consulting the ESP32 data sheet. These parameters need to be used during programming.
3. Application examples
1. Program design
Let's take a look at the experimental procedure first. This procedure is a modification of the given test procedure. Let's take a look at the initialization routine in the first half of the program. The first two sentences are the internal modules of MicroPython required by import, including ADC, Pin, SPI port, and time is the module used for timing. Next is the OLED driver file module. This module needs to be stored in the file area inside ESP32 through Thonny software in advance. Next is to define five ADC channels. The latter four, from ADC1 to ADC4, already exist in the given sample program. The above two lines define the ADC0 port, which is added later. We manually added to the sample file. Declaring the ADC port includes two steps, indicating the port corresponding to the ADC. This can be found from the schematic diagram just now. The second part is to set the parameters of the ADC. In this case, the attenuation factor of the ADC input signal is set. Corresponding to attenuation of 11 dB, the effective ADC input voltage range can reach 3.3V. It should be noted that the number of bits converted by the ADC of ESP32 is 12 bits, and the voltage output corresponding to 3.3V is 0xfff, which is 4095 .
Next, look at the following program. The middle part of the code defines the interface of the OLED LCD screen, and this part has not been modified. Just use the code in the original sample program directly. Five global variables are defined here to store the conversion results of five ADCs. Note that val0 is added later. These variables are initialized to 0 and are used to pass data between the main loop and the display subroutine. Next is the subroutine for the OLED to display the ADC result. In the program, the values corresponding to the five global variables are displayed on the OLED screen. The code here has been adjusted so that the five-row ADC results can be evenly displayed on the OLED screen. For this part of the code, I won't do too much interpretation here, and you can analyze it yourself. Next comes the final part of the program. Use the show member function of the oled object to refresh the screen. The following is the loop main program. The conversion value of the external five-channel analog voltage is read by calling the read function of the ADC object, displayed on Thonny’s Shell window through the print function, and the Show function defined above is called to display the result on the OLED screen. Finally, use time to delay for 0.5 seconds. This is the code for the entire experimental program. Next, test it with Thonny software.
2. Running results
Since this program needs to use the OLED display, it is necessary to store the OLED096 file in the ESP32 microcontroller. Next, copy the Test ADC program to the Thonny program editing area, store it in ESXP32, and then allow it to be changed. It can be seen that the modified program displays five ADC values on the OLED screen, among which ADC0 corresponds to the value after power voltage division. This value is relatively stable. The values of the first four channels are suspended, so the voltage jump is relatively large. If you touch the ADC port with your finger, you can see that the values of the first four channels change greatly. The value corresponding to ADC0 still maintains the original reading.
4. Practice/Experiment
Since there are not many electronic devices that you may use at hand, this test will stop here. Through this program, everyone is familiar with the process of reading external voltage. These values can be used for subsequent car model motion control, including direction and speed. Finally, the car model can better perceive the environment and adapt to the environment. You can also think about it, what other interesting things can you do with these ADC channels?
02 Lecture information
1. Lecture video
How to conduct ADC experiment on ESP32 control board?
1. Test procedure
from machine import ADC ,Pin , SoftSPI
import time
from LQ_OLED096 import OLED096_SPI
adc0 = ADC(Pin(36))
adc0.atten(ADC.ATTN_11DB) # Config input range as 3.3V
adc1 = ADC(Pin(33))
adc1.atten(ADC.ATTN_11DB) # 这里配置测量量程为3.3V
adc2 = ADC(Pin(32))
adc2.atten(ADC.ATTN_11DB) # 这里配置测量量程为3.3V
adc3 = ADC(Pin(34))
adc3.atten(ADC.ATTN_11DB) # 这里配置测量量程为3.3V
adc4 = ADC(Pin(35))
adc4.atten(ADC.ATTN_11DB) # 这里配置测量量程为3.3V
spi=SoftSPI(baudrate=100000,polarity=1,phase=0,sck=Pin(18),mosi=Pin(23),miso=Pin(19)) #创建软件模拟spi
dc=Pin(15,Pin.OUT)
res=Pin(13,Pin.OUT)
cs=Pin(19,Pin.OUT)
oled=OLED096_SPI(128,64,spi,dc,res,cs) #创建oled对象
val1 =val2 = val3 =val4 = 0
val0 = 0
def Show(): #显示
txt = ""
oled.fill(0) #清屏
txt = (" ADC_Test ")
oled.text(txt, 6*0, 8*0 ,1)
txt = ("ADC1: %d ") % (val1)
oled.text(txt, 6*0, 10*1+6 ,1)
txt = ("ADC2: %d ") % (val2)
oled.text(txt, 6*0, 10*2+6 ,1)
txt = ("ADC3: %d ") % (val3)
oled.text(txt, 6*0, 10*3+6 ,1)
txt = ("ADC4: %d ") % (val4)
oled.text(txt, 6*0, 10*4+6 ,1)
txt = ("ADC0: %d ") % (val0)
oled.text(txt, 6*0, 10*5+6 ,1)
oled.show()
while True:
val1 = adc1.read() # 0-4095
val2 = adc2.read() # 0-4095
val3 = adc3.read() # 0-4095
val4 = adc4.read() # 0-4095
val0 = adc0.read() # 0-4095
print(val1,val2,val3,val4,val0)
Show()
time.sleep(0.5)
2. Lecture materials
▲ 图2.2.1 电路板测试
- Tsinghua cloud disk data download : https://cloud.tsinghua.edu.cn/d/7471180bdb0f4d1ebbc7/
- Wireless track smart car school competition | How to learn wireless track car models?
- Basic Python learning website Learn Python : https://www.learnpython.org/
■ Links to related literature:
- Tsinghua cloud disk data download
- Wireless track smart car school competition | How to learn wireless track car models?
- Basic Python learning website Learn Python
● Links to related diagrams: