Table of Contents
In this detailed tutorial let’s learn about Raspberry Pi Pico ADC, How it works, what are its uses, Pinout, and finally how to read the ADC value of it with a MicroPython example code.
What is ADC ( Analog to digital converter or signal converter)?
An ADC is a system that can transform analog signals to digital signals. Which in detail converting the analog signal which is voltages from the sensors or microcontrollers pins and converting them into digital binary numbers.
Almost every environmental parameters like temperature, sound, pressure, speed, light, etc is analog and measuring them with digital computers and processors is not possible. Thus an intermediate integrated circuit ADC which converts the analog signal(continuous form) to digital binary numbers(discrete form) in the form of 1 and 0 is need to acquire, analyze and process the data in order to communicate with digital processors like microcontrollers and microprocessors.
One of the main advantages of ADC is high data acquisition rate even at multiplexed inputs. ADC converters have higher sample rate which means how fast the signal is converted from analog to digital, and high bit resolution means how accurate the ADC is converting the signal from analog to digital.
How Raspberry Pi Pico ADC works?
Raspberry Pi Pico comes with a new RP2040 microcontroller chip with dual core ARM processor with frequencies upto 133MHz. There are total 30 GPIO pins out of which 4 are for analog inputs with ADC converters, from 4 only 3 works as analog inputs as the 4th one is connected to the onboard temperature sensor which measures temperature by converting the analog values to digital output.
Most of the microcontrollers have inbuild ADC, even if they are not present we can connect an external ADC. Raspberry Pi Pico has 3 ADC channels with 12 bit resolution. which should have a sampling rate between 0-4095 but as we are programming the Raspberry Pi Pico with MicroPython we get 16 bit resolution with a sampling rate of 65536(0-65535) because MicroPython ADC library scaled it from 12 bit to 16 bit.
Therefore when we connect any analog input to one of the Analog pin on Raspberry Pi Pico the analog signal is passed to SAR ADC (Successive Approximation Register Analogue to Digital Converter) which is a combination of digital controller, and analogue circuit. To perform conversion ADC need 48MHz clock frequency which could could be obtained from USB PLL. To capture a single sample ADC takes 96 clock cycles. So a single samples converts in (96 x 1 / 48MHz) = 2 μseconds per sample (500kS/s).
Analog inputs Raspberry Pi Pico Pinout
To make it clear we highlighted Analog input pins with red rectangle in which ADC0, ADC1,ADC2 with GP26, GP27, GP28 respectively are inputs and AGND is ground and ADC_VREF as the reference voltage.
- 30 multi-function General Purpose IO (4 can be used for ADC)(1.8-3.3V IO fixed Voltage)
- 2 × UART, 2 × I2C, 2 × SPI, 16 × PWM channels
Also read: MicroPython: SunLight Tracker system
Raspberry Pi Pico ADC MicroPython code to read the analog values
Here we are using an example code in MicroPython to read the values of the potentiometer which varies the analog input voltage by converting them into digital values.
- Raspberry Pi Pico
- 10k Potentiometer
- Few Connecting wires
Schematic diagram connecting Raspberry Pi Pico with Potentiometer
From the above circuit diagram you can see the potentiometer is powered from the Pico board through VCC and GND. The data pin from the middle of the potentiometer is connected to GP28(ADC2) of Pico board.
That’s it for connection, now let’s get in to the coding section and upload the MicroPython code to pico board through following process.
Install MicroPython on Pico board by downloading rp2_xxx_xxx.uf2 file from Link and connect the picoboard through microUSB to PC by holding the Bootset button the board to get the file explorer popup on the pc. now simply copy and paste the .uf2 file in the file explorer window of pico storage.
In this example we are using Thonny IDE so after downloading install it, open it and copy the below and paste in it workspace.
import machine import Pin, ADC
potentiometer_value = machine.ADC(28)
potreading = potentiometer_value.read_u16()
In the above code we created an infinite while loop to read the analog values from potentiometer at a time interval of 0.1 (100 microseconds).
Connect the Pico board to the PC and click on run current script (F5) icon.
After successful run, you can see the ADC values are printed on Shell monitor
When you rotate the knob of potentiometer the ADC value changes from 0 at minimum to 65535 at maximum rotation. As we are using MicroPython code the 12 bit ADC of Raspberry Pico Pico is scaled to 16 bit resolution.
To display the voltage values instead of integer conversion use the below code.
from machine import Pin, ADC
POT_Value = ADC(26)
conversion_factor = 3.3/(65536)
#Main Application loop
print(POT_Value.read_u16() * conversion_factor)
If you found this article helpful, please share it with your friends and if you have any doubts or got any errors don’t feel shy to drop your issue in the comment section below.