How to use ADC in Raspberry Pi Pico | ADC Example Code
Use ADC in Raspberry Pi Pico with ADC Example Code for MicroPython. RP2040 has 12 Bit Analog to Digital Converter pins GP26, GP27 & GP28
- Suitable forB.Tech/Diploma RPi Pico students
- Build time1-2 weeks
- LevelIntermediate
- Available atAjvika Labs, Kakinada
About this project
Overview
In this tutorial, we will learn how to use
ADC in Raspberry Pi Pico
with
ADC Example Code
using
MicroPython
. An analog to digital converter (ADC) is a circuit that converts a continuous
voltage value (analog)
to a
binary value (digital)
that can be understood by a digital device which could then be used for
digital computation
The Raspberry Pi Pico is built using an
RP2040
microcontroller
. The board exposes
multi-function GPIO pins from a total of
36 GPIO pins
. Out of 36 GPIO Pins, there are 4 ADC pins but only 3 are usable.
The
ADC
in Raspberry Pi Pico is
12bits
, which is 4 times better than the 10 bits ADC of the Arduino. We will write a
MicroPython code
to learn how we can use the ADC pin value with any
analog sensors
. A potentiometer is the best tool to vary the input Analog Voltage. But before jumping directly into the ADC guide, it is recommended to go through Raspberry Pi Pico
Getting Started Tutorial
What is Analog to Digital Converter (ADC)?
An
Analog to Digital Converter (ADC)
is a very useful feature that converts an
analog voltage
on a pin to a
digital number
. By converting from the analog world to the digital world, we can begin to use electronics to interface with the analog world around us.
In real-life applications,
ADC
is a system that converts an
analog signal
, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an input analog
voltage
or
current
to a digital number representing the
magnitude
of the voltage or current.
The ADC is perfect for these sensors. With the ADC, you can sense environmental parameters like light, sound, distance, gravity, acceleration, rotation, smell, gases, other particles.
How does an ADC work in a Microcontroller?
Most
microcontrollers
nowadays have
built-in ADC
converters. It is also possible to connect an external ADC converter to any type of microcontroller. ADC converters are usually
10 or 12 bits
, having
1024
to
4096
quantization levels. A
16 bits ADC
has 65536 quantization levels. A
Raspberry Pi Pico
has
12 Bits ADC
with a quantization level of
4096
The ADC conversion process must be started by the user program and it may take several hundreds of
microseconds
for a conversion to complete. ADC converters usually generate
interrupts
when a conversion is complete so that the user program can read the converted data as quickly as possible.
ADC in Raspberry Pi Pico
The Raspberry Pi Pico supports four
12-bit SAR
based
analog to digital converters
. Out of the 4, you can only use 3 analog channels. The 4th analog channel is internally connected to the
internal temperature sensor
. You can measure the temperature using build-in temperature by reading the analog value of ADC4. The following table shows that the input signal for
ADC0, ADC1
, and
ADC2
can be connected with
GP26, GP27
&
GP28
pins respectively.
ADC Module
GPIO Pins
ADC0
GP26
ADC1
GP27
ADC2
GP28
You can perform the
A/D conversion
in polling, interrupt & FIFO with DMA mode. The ADC conversion speed per sample is
2μs
that is
500kS/s
. The
RP2040 microcontroller
operates on a
48MHZ clock frequency
which comes from USB PLL. So, its ADC takes a
96 CPU clock cycle
to perform one conversion. Therefore, the sampling frequency is
(96 x 1 / 48MHz)
=
2 μs
per sample
(500kS/s)
You may use a
ADC Calculator
to manually calculate the ADC Value.
Raspberry Pi Pico ADC Example Code
Now let us learn how to use ADC of Raspberry Pi Pico. We will use a
10K Potentiometer
to vary the Analog input voltage. We will map the analog voltage from
0 to 3.3V
with
12 Bit
or 16 Bit ADC. The connection diagram is given below.
Connect Pin 1 & Pin 3 of Potentiometer to
3.3V
Pin &
GND
Pin of the Raspberry Pi Pico. Connect the Pin 2 of the Potentiometer to
GP28
of Raspberry Pie Pico.
Now it’s time to write a code and check the Analog Reading. To do that you can use
Thonny IDE
or go with
uPyCraft IDE
. Copy the following code and hit ‘
Download & Run Button
‘.
import
machine
import
utime
analog_value
=
machine
ADC
(
)
while
True
:
reading
=
analog_value
read_u16
(
)
(
"ADC: "
reading
)
utime
sleep
(
0.2
)
In this code part, I define the
ADC input
connected at pin 28. Then to create an
infinite loop
I make a
while true
. then I read the
ADC value
with the read
u16 function
. Then using print I can show the value on the
shell monitor
When you rotate the potentiometer and the value goes from
0 to 65536
and that’s weird because the value should go from
0 to 4096
. This is supposed to be a
12bit
ADC and not
16bit
. Anyway, that’s how we can read analog values with the ADC on the raspberry Pi Pico.
If you want to add more analog pins to the Raspberry Pi Pico and read the ADC value more precisely, you may use the ADS1115 16-Bit ADC Module for your projects. You can follow the following Guide here:
ADS1115 16-Bit ADC Module with Raspberry Pi Pico
Video Tutorial & Guide
Raspberry Pi Pico Complete Guide| Pinout+Features+ADC+I2C+OLED+Internal Temperature Sensor+DHT11
Watch this video on YouTube
In this tutorial, we will learn how to use
ADC in Raspberry Pi Pico
with
ADC Example Code
using
MicroPython
. An analog to digital converter (ADC) is a circuit that converts a continuous
voltage value (analog)
to a
binary value (digital)
that can be understood by a digital device which could then be used for
digital computation
The Raspberry Pi Pico is built using an
RP2040
microcontroller
. The board exposes
multi-function GPIO pins from a total of
36 GPIO pins
. Out of 36 GPIO Pins, there are 4 ADC pins but only 3 are usable.
The
ADC
in Raspberry Pi Pico is
12bits
, which is 4 times better than the 10 bits ADC of the Arduino. We will write a
MicroPython code
to learn how we can use the ADC pin value with any
analog sensors
. A potentiometer is the best tool to vary the input Analog Voltage. But before jumping directly into the ADC guide, it is recommended to go through Raspberry Pi Pico
Getting Started Tutorial
What is Analog to Digital Converter (ADC)?
An
Analog to Digital Converter (ADC)
is a very useful feature that converts an
analog voltage
on a pin to a
digital number
. By converting from the analog world to the digital world, we can begin to use electronics to interface with the analog world around us.
In real-life applications,
ADC
is a system that converts an
analog signal
, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an input analog
voltage
or
current
to a digital number representing the
magnitude
of the voltage or current.
The ADC is perfect for these sensors. With the ADC, you can sense environmental parameters like light, sound, distance, gravity, acceleration, rotation, smell, gases, other particles.
How does an ADC work in a Microcontroller?
Most
microcontrollers
nowadays have
built-in ADC
converters. It is also possible to connect an external ADC converter to any type of microcontroller. ADC converters are usually
10 or 12 bits
, having
1024
to
4096
quantization levels. A
16 bits ADC
has 65536 quantization levels. A
Raspberry Pi Pico
has
12 Bits ADC
with a quantization level of
4096
The ADC conversion process must be started by the user program and it may take several hundreds of
microseconds
for a conversion to complete. ADC converters usually generate
interrupts
when a conversion is complete so that the user program can read the converted data as quickly as possible.
ADC in Raspberry Pi Pico
The Raspberry Pi Pico supports four
12-bit SAR
based
analog to digital converters
. Out of the 4, you can only use 3 analog channels. The 4th analog channel is internally connected to the
internal temperature sensor
. You can measure the temperature using build-in temperature by reading the analog value of ADC4. The following table shows that the input signal for
ADC0, ADC1
, and
ADC2
can be connected with
GP26, GP27
&
GP28
pins respectively.
ADC Module
GPIO Pins
ADC0
GP26
ADC1
GP27
ADC2
GP28
You can perform the
A/D conversion
in polling, interrupt & FIFO with DMA mode. The ADC conversion speed per sample is
2μs
that is
500kS/s
. The
RP2040 microcontroller
operates on a
48MHZ clock frequency
which comes from USB PLL. So, its ADC takes a
96 CPU clock cycle
to perform one conversion. Therefore, the sampling frequency is
(96 x 1 / 48MHz)
=
2 μs
per sample
(500kS/s)
You may use a
ADC Calculator
to manually calculate the ADC Value.
Raspberry Pi Pico ADC Example Code
Now let us learn how to use ADC of Raspberry Pi Pico. We will use a
10K Potentiometer
to vary the Analog input voltage. We will map the analog voltage from
0 to 3.3V
with
12 Bit
or 16 Bit ADC. The connection diagram is given below.
Connect Pin 1 & Pin 3 of Potentiometer to
3.3V
Pin &
GND
Pin of the Raspberry Pi Pico. Connect the Pin 2 of the Potentiometer to
GP28
of Raspberry Pie Pico.
Now it’s time to write a code and check the Analog Reading. To do that you can use
Thonny IDE
or go with
uPyCraft IDE
. Copy the following code and hit ‘
Download & Run Button
‘.
import
machine
import
utime
analog_value
=
machine
ADC
(
)
while
True
:
reading
=
analog_value
read_u16
(
)
(
"ADC: "
reading
)
utime
sleep
(
0.2
)
In this code part, I define the
ADC input
connected at pin 28. Then to create an
infinite loop
I make a
while true
. then I read the
ADC value
with the read
u16 function
. Then using print I can show the value on the
shell monitor
When you rotate the potentiometer and the value goes from
0 to 65536
and that’s weird because the value should go from
0 to 4096
. This is supposed to be a
12bit
ADC and not
16bit
. Anyway, that’s how we can read analog values with the ADC on the raspberry Pi Pico.
If you want to add more analog pins to the Raspberry Pi Pico and read the ADC value more precisely, you may use the ADS1115 16-Bit ADC Module for your projects. You can follow the following Guide here:
ADS1115 16-Bit ADC Module with Raspberry Pi Pico
Video Tutorial & Guide
Raspberry Pi Pico Complete Guide| Pinout+Features+ADC+I2C+OLED+Internal Temperature Sensor+DHT11
Watch this video on YouTube
Technologies used
Hardware components
- Arduino
- Raspberry Pi
- DHT11
- OLED
Software & tools
- Thonny
- MicroPython
What you get
- Working project / hardware kit, tested before delivery
- Complete source code with explanation
- Project demo and guidance for your viva
- Project report, PPT and IEEE paper support (on request)
Delivery & Payment
Delivery takes a minimum of two weeks from the date your inquiry is confirmed.
Payment is in two parts:
• 50% advance to start the project
• Remaining 50% before delivery
Note:
• Documentation and PPT are not included by default
• If you need documentation/PPT, it can be arranged for extra time and cost
We'll confirm the exact timeline and cost with you after reviewing your requirements.