Chapter 6 ADC Test

ADC

ADC is an electronic integrated circuit used to convert analog signals such as voltages to digital or binary form consisting of 1s and 0s. The range of our ADC on Raspberry Pi Pico (W) is 10 bits, which means the resolution is 2^10=1024, and it represents a range (at 3.3V) will be divided equally to 1024 parts. The rage of analog values corresponds to ADC values. So the more bits the ADC has, the denser the partition of analog will be and the greater the precision of the resulting conversion.

../../../_images/Chapter06_00.png

Subsection 1: the analog in rang of 0V—3.3/1023 V corresponds to digital 0;

Subsection 2: the analog in rang of 3.3/1023 V—2*3.3 /1023V corresponds to digital 1;

The following analog will be divided accordingly.

The conversion formula is as follows:

\[\boldsymbol{\text{ADC Value}} \boldsymbol{=} \frac{\boldsymbol{\text{Analog Voltage}}}{{3.3}} \boldsymbol{\times} {1023}\]

Schematic

As we can see, the car reads the voltage of the batteries through GPIO27 of Raspberry Pi Pico (W).

../../../_images/Chapter06_01.png

The voltage acquisition range of GPIO27 on Raspberry Pi Pico (W) is 0-3.3V, while the car is powered by two 18650-lithium batteries, whose voltage is 8.4V when they are fully charged, which exceeds the acquisition range of Raspberry Pi Pico (W). Therefore, after passing through the voltage divider circuit composed of R3 and R4, the voltage at A0 will be about 1/4 of the battery voltage, 8.4/4=2.1V, which is within the voltage collection range of GPIO27.

Sketch

In this section, we will use GPIO27 of Raspberry Pi Pico (W) to read the voltage value of the batteries and print it on serial monitor. Open “Sketch_04.1_Battery_level” folder in “Freenove Three-wheeled omniwheel Car Kit for Raspberry Pi pico \Sketches” and then double-click “Sketch_04.1_Battery_level.ino”.

../../../_images/Chapter06_02.png

Code

After downloading the code, we can see current battery level be printed on serial monitor every 1 second.

../../../_images/Chapter06_03.png

Code Explanation

Define the ADC sampling pin.

1const int Battery_ADC = 27;// Define the ADC sampling pin

Set the pin to input mode.

1pinMode(Battery_ADC,INPUT);// Enables the ADC sampling pin to be input mode

Initialize the pico serial port and set the baud rate to 9600.

1Serial.begin(9600);      // Set the serial port baud rate to 115200

Obtain the battery voltage every 1 second and print on Serial Monitor.

1void loop() {
2  // put your main code here, to run repeatedly:
3  Battery = analogRead(Battery_ADC)*3.3/1023*4;// Get the sampled value and calculate the real-time voltage value
4  Serial.print("Battery:");
5  Serial.print(Battery);// Print voltage
6  Serial.println("V");
7  delay(1000);// Delay 1 second
8}

Reference

int analogRead(uint8_t pin);

The analogRead function is used to read an analog input. Upon a successful read, it returns an integer value in the range of 0 to 1023.

Parameters:

pin: The analog input pin number to read from.