How to use ADC on Raspberry Pi Pico in detail with MicroPython example

Learn how Raspberry Pi Pico ADC works and how to read Analog values on Raspberry Pi Pico in detail with a MicroPython example.


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 image fromt back

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.

Raspberry Pi Pico pinout diagram with ADC highlightedAs you can see from the above pinout diagram there are

  • 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.

Required components:

  • Raspberry Pi Pico
  • 10k Potentiometer
  • Few Connecting wires

Schematic diagram connecting Raspberry Pi Pico with Potentiometer

Schematic diagram connecting Raspberry Pi Pico with PotentiometerFrom 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.

Before getting into coding download Thonny IDE or uPyCraft IDE you can download from its links.

In this example we are using Thonny IDE so after downloading install it, open it and copy the below and paste in it workspace.

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

Raspberry Pi Pico adc values output

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.

This uses the conversion factor which is dividing the max input voltage with max sample rate. To get the voltage value multiply the conversion factor with ADC reading and print it to show the voltage values on the shell monitor. When the Knob of the potentiometer is rotated the voltage values on the shell monitor changes accordingly from 0 to 3.3v as shown in the below image.

output as voltage on the raspberry pi pico with potentiometer

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.

CircuitSchools Staff

We at CircuitSchools publish on basics of electronics and electric components and everything related to evolution of electrical technology with complete analysis of development boards and modules along with latest projects with innovative ideas.

Related Articles

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button