Getting Started with the ADC Class
This guide explains how to use the ADC class to read analog signals from a Raspberry Pi Pico or Pico W and convert them to voltage or real-world values.
Initialization
1. Create an ADC instance
Parameters
- pin: GPIO pin number connected to the analog signal.
- vref: Reference voltage for conversion (default 3.3V).
- scale: Multiplier for real-world conversion (default 1.0).
- offset: Offset added after scaling (default 0.0).
Calibration
The scale and offset values can be used to calibrate the ADC to match your specific application.
Better to use the calibration function adc.calibrate(real voltge) instead.This requires a known real-world value to be provided.
Reading Values
2. Raw ADC Reading
- Returns a value between 0 and 65535.
3. Voltage Measurement
- Converts the raw reading into volts.
Equation: Calculating Voltage
The equation for voltage conversion is: $$ Voltage = \frac{\text{raw}}{65535} \cdot V_{\text{ref}} $$
the raw value is the raw reading from the ADC as an integer between 0 and 65535 (read_u16), and Vref is the reference voltage in volts.
4. Real-World Value
- Applies scaling and offset to get real-world units (like temperature or light intensity).
Sampling
5. Collect Multiple Samples
n: number of samplestype:raw,voltage, orrealdelay: seconds between samples
6. Asynchronous Sampling
7. Mean Value
- Use
async_meanfor asynchronous calculation.
Pin Connection Check
8. Check if Pin is Connected
- Returns a boolean and a dictionary with details.
- Detects floating, short-to-GND, or short-to-VCC conditions.
9. Asynchronous Check
Notes & Tips
- Use
voltage()for direct voltage readings. - Use
real()for calibrated real-world measurements. - Sampling with delay improves stability.
- Connection heuristics help avoid unreliable readings.
External Resources
What’s Next?
- Learn about Asynchronous Tasks and uasyncio
- Explore Sensor Integration with PicoCore