Using the Raspberry Pi Pico [4] – A/D conversion with Micro Python


The Raspberry Pi Pico has four analog inputs. In this article we will use this feature to measure the analog input voltages of the terminals and to measure the internal temperature.
ADC in Raspberry Pi Pico
The RP2040 has an internal A/D converter, which is connected to the external input pins GPIO[26] to GPIO[29] and the internal temperature sensor as shown in the figure below. The A/D converter is a 12-bit SAR type with a sampling rate of 500kS/s when using a 48MHz clock.
ADC in Raspberry Pi Pico

A/D conversion of an external analog voltage input

Hardware connections
Below is the connection between the I/O pins of the Raspberry Pi Pico. 3.3V supply voltage is divided by a variable resistor, and the voltage between the tap and GND is input to the ADC0 pin.
Schematics

Connection
Software (Micro Python code)
As before, we will use Micro Python. Referring to the documentation of the MicroPython library and checking the examples of the ADC class used to interface to the analog-to-digital converter.
# Create an ADC object for the specified pin (ADC0)

ad = machine.ADC(0)

# Read the analog value
voltage = ad.read_u16() * 3.3 / (65535)
from machine import ADC, Pin
import time, math

ad = machine.ADC(0)

while True:
    voltage = ad.read_u16() * 3.3 / (65535)
    print(voltage)
    time.sleep(0.1)

Using the internal temperature sensor

Hardware Connection
No additional circuitry is required to use the RP2040’s internal temperature sensor.
Software (Micro Python code)
The temperature sensor is connected to the ADC(4) inside the RP2040, which is used to read the voltage value. The conversion from voltage to temperature follows the description in the datasheet.
from machine import ADC, Pin
import time, math

# Connect the temperature sensor to the ADC(4)

ad = machine.ADC(4)

while True:
    voltage = ad.read_u16() * 3.3 / (65535)
    temp = 27 - (voltage - 0.706)/0.001721    # Vbe=[0.706V], Slope= 1.721[mV/degree]
    print(temp)
    time.sleep(0.1)

References

MicroPython library documentation (JAPANESE site)

Leave a Reply

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