Battery voltage detection

1. Experimental purpose

Use the voltage detection function on the microROS control board to learn the function of ESP32 to read ADC values.

 

2. Hardware connection

As shown in the figure below, the battery voltage detection circuit has been integrated on the microROS control board, so there is no need to connect other external devices. You only need to connect the type-C data cable to the computer and the microROS control board as a firmware flash function.

image-20240111113845152

 

3. Core code analysis

The virtual machine path corresponding to the program source code is as follows

First, initialize the battery voltage detection ADC channel. The hardware GPIO connected to the voltage detection is GPIO3, and the corresponding ADC channel is ADC1 channel 2.

Since the value read by the ADC needs to be converted before the GPIO voltage value can be obtained, the ADC voltage conversion function needs to be initialized.

Create a new battery voltage detection task and convert the ADC value read from ADC1 channel 2 into a GPIO voltage value.

Calculate the battery terminal voltage based on the GPIO voltage.

Read the battery voltage, unit: V.

Call the Battery_Init function in app_main to initialize the battery voltage ADC interface, and then print the current battery voltage value every 500 milliseconds in the loop.

 

4. Compile, download and flash firmware

Use a Type-C data cable to connect the virtual machine/computer and the microROS control board. If the system pops up, choose to connect to the virtual machine.

Activate the ESP-IDF development environment. Note that every time you open a new terminal, you need to activate the ESP-IDF development environment before compiling the firmware.

Enter the project directory

Compile, flash, and open the serial port simulator

If you need to exit the serial port simulator, press Ctrl+].

 

5. Experimental results

The serial port simulator prints the "hello yahboom" greeting. And continue to print the current battery voltage value, for example, the current battery voltage is 7.05V.

image-20240111141215200