v2.0.0
Functions
hardware_adc
Hardware APIs

Analog to Digital Converter (ADC) API. More...

## Functions
void | adc_init (void)
| Initialise the ADC HW.
static void | adc_gpio_init (uint gpio)
| Initialise the gpio for use as an ADC pin. More...
static void | adc_select_input (uint input)
| ADC input select. More...
static uint | adc_get_selected_input (void)
| Get the currently selected ADC input channel. More...
static void | adc_set_round_robin (uint input_mask)
| Round Robin sampling selector. More...
static void | adc_set_temp_sensor_enabled (bool enable)
| Enable the onboard temperature sensor. More...
static uint16_t | adc_read (void)
| Perform a single conversion. More...
static void | adc_run (bool run)
| Enable or disable free-running sampling mode. More...
static void | adc_set_clkdiv (float clkdiv)
| Set the ADC Clock divisor. More...
static void | adc_fifo_setup (bool en, bool dreq_en, uint16_t dreq_thresh, bool err_in_fifo, bool byte_shift)
| Setup the ADC FIFO. More...
static bool | adc_fifo_is_empty (void)
| Check FIFO empty state. More...
static uint8_t | adc_fifo_get_level (void)
| Get number of entries in the ADC FIFO. More...
static uint16_t | adc_fifo_get (void)
| Get ADC result from FIFO. More...
static uint16_t | adc_fifo_get_blocking (void)
| Wait for the ADC FIFO to have data. More...
static void | adc_fifo_drain (void)
| Drain the ADC FIFO. More...
static void | adc_irq_set_enabled (bool enabled)
| Enable/Disable ADC interrupts. More...

## Detailed Description

Analog to Digital Converter (ADC) API.

RP-series microcontrollers have an internal analogue-digital converter (ADC) with the following features:

- SAR ADC
- 500 kS/s (Using an independent 48MHz clock)
- 12 bit (RP2040 8.7 ENOB, RP2350 9.2 ENOB)
- RP2040 5 input mux:
  - 4 inputs that are available on package pins shared with GPIO[29:26]
  - 1 input is dedicated to the internal temperature sensor
  - 4 element receive sample FIFO

- RP2350 5 or 9 input mux:
  - 4 inputs available on QFN-60 package pins shared with GPIO[29:26]
  - 8 inputs available on QFN-80 package pins shared with GPIO[47:40]
  - 8 element receive sample FIFO
- One input dedicated to the internal temperature sensor (see Section 12.4.6)
- Interrupt generation
- DMA interface

Although there is only one ADC you can specify the input to it using the adc_select_input() function. In round robin mode (adc_set_round_robin()), the ADC will use that input and move to the next one after a read.

RP2040, RP2350 QFN-60: User ADC inputs are on 0-3 (GPIO 26-29), the temperature sensor is on input 4. RP2350 QFN-80 : User ADC inputs are on 0-7 (GPIO 40-47), the temperature sensor is on input 8.

Temperature sensor values can be approximated in centigrade as:

T = 27 - (ADC_Voltage - 0.706)/0.001721

## Example

#include <stdio.h>
#include "pico/stdlib.h"
#include "hardware/gpio.h"
#include "hardware/adc.h"
int main() {
stdio_init_all();
printf("ADC Example, measuring GPIO26\n");
adc_init();
// Make sure GPIO is high-impedance, no pullups etc
adc_gpio_init(26);
// Select ADC input 0 (GPIO26)
adc_select_input(0);
while (1) {
// 12-bit conversion, assume max value == ADC_VREF == 3.3 V
const float conversion_factor = 3.3f / (1 << 12);
uint16_t result = adc_read();
printf("Raw value: 0x%03x, voltage: %f V\n", result, result * conversion_factor);
sleep_ms(500);
}
}
gpio.h
adc_init
void adc_init(void)
Initialise the ADC HW.
Definition: adc.c:11
adc_select_input
static void adc_select_input(uint input)
ADC input select.
Definition: adc.h:113
adc_gpio_init
static void adc_gpio_init(uint gpio)
Initialise the gpio for use as an ADC pin.
Definition: adc.h:90
adc_read
static uint16_t adc_read(void)
Perform a single conversion.
Definition: adc.h:170
stdio_init_all
bool stdio_init_all(void)
Initialize all of the present standard stdio types that are linked into the binary.
Definition: stdio.c:200
sleep_ms
void sleep_ms(uint32_t ms)
Wait for the given number of milliseconds before returning.
Definition: time.c:431
adc.h
stdio.h
stdlib.h

## Function Documentation

## ◆ adc_fifo_drain()

static void adc_fifo_drain | ( | void |  | ) |
| inlinestatic

Drain the ADC FIFO.

Will wait for any conversion to complete then drain the FIFO, discarding any results.

## ◆ adc_fifo_get()

static uint16_t adc_fifo_get | ( | void |  | ) |
| inlinestatic

Get ADC result from FIFO.

Pops the latest result from the ADC FIFO.

## ◆ adc_fifo_get_blocking()

static uint16_t adc_fifo_get_blocking | ( | void |  | ) |
| inlinestatic

Wait for the ADC FIFO to have data.

Blocks until data is present in the FIFO

## ◆ adc_fifo_get_level()

static uint8_t adc_fifo_get_level | ( | void |  | ) |
| inlinestatic

Get number of entries in the ADC FIFO.

On RP2040 the FIFO is 4 samples long. On RP2350 the FIFO is 8 samples long.

This function will return how many samples are currently present.

## ◆ adc_fifo_is_empty()

static bool adc_fifo_is_empty | ( | void |  | ) |
| inlinestatic

Check FIFO empty state.

Returns
  Returns true if the FIFO is empty

## ◆ adc_fifo_setup()

static void adc_fifo_setup | ( | bool | en,
|  | bool | dreq_en,
|  | uint16_t | dreq_thresh,
|  | bool | err_in_fifo,
|  | bool | byte_shift
| ) |  |
| inlinestatic

Setup the ADC FIFO.

On RP2040 the FIFO is 4 samples long.

On RP2350 the FIFO is 8 samples long.

If a conversion is completed and the FIFO is full, the result is dropped.

Parameters

  en | Enables write each conversion result to the FIFO
  dreq_en | Enable DMA requests when FIFO contains data
  dreq_thresh | Threshold for DMA requests/FIFO IRQ if enabled.
  err_in_fifo | If enabled, bit 15 of the FIFO contains error flag for each sample
  byte_shift | Shift FIFO contents to be one byte in size (for byte DMA) - enables DMA to byte buffers.

## ◆ adc_get_selected_input()

static uint adc_get_selected_input | ( | void |  | ) |
| inlinestatic

Get the currently selected ADC input channel.

Returns
  The currently selected input channel.

On RP02040 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor.

On RP2350A 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor. On RP2350B 0...7 are GPIOs 40...47 respectively. Input 8 is the onboard temperature sensor.

## ◆ adc_gpio_init()

static void adc_gpio_init | ( | uint | gpio | ) |
| inlinestatic

Initialise the gpio for use as an ADC pin.

Prepare a GPIO for use with ADC by disabling all digital functions.

Parameters

  gpio | The GPIO number to use. Allowable GPIO numbers are 26 to 29 inclusive on RP2040 or RP2350A, 40-48 inclusive on RP2350B

## ◆ adc_irq_set_enabled()

static void adc_irq_set_enabled | ( | bool | enabled | ) |
| inlinestatic

Enable/Disable ADC interrupts.

Parameters

  enabled | Set to true to enable the ADC interrupts, false to disable

## ◆ adc_read()

static uint16_t adc_read | ( | void |  | ) |
| inlinestatic

Perform a single conversion.

Performs an ADC conversion, waits for the result, and then returns it.

Returns
  Result of the conversion.

## ◆ adc_run()

static void adc_run | ( | bool | run | ) |
| inlinestatic

Enable or disable free-running sampling mode.

Parameters

  run | false to disable, true to enable free running conversion mode.

## ◆ adc_select_input()

static void adc_select_input | ( | uint | input | ) |
| inlinestatic

ADC input select.

Select an ADC input On RP02040 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor. On RP2350A 0...3 are GPIOs 26...29 respectively. Input 4 is the onboard temperature sensor. On RP2350B 0...7 are GPIOs 40...47 respectively. Input 8 is the onboard temperature sensor.

Parameters

  input | Input to select.

## ◆ adc_set_clkdiv()

static void adc_set_clkdiv | ( | float | clkdiv | ) |
| inlinestatic

Set the ADC Clock divisor.

Period of samples will be (1 + div) cycles on average. Note it takes 96 cycles to perform a conversion, so any period less than that will be clamped to 96.

Parameters

  clkdiv | If non-zero, conversion will be started at intervals rather than back to back.

## ◆ adc_set_round_robin()

static void adc_set_round_robin | ( | uint | input_mask | ) |
| inlinestatic

Round Robin sampling selector.

This function sets which inputs are to be run through in round robin mode. RP2040, RP2350 QFN-60: Value between 0 and 0x1f (bit 0 to bit 4 for GPIO 26 to 29 and temperature sensor input respectively) RP2350 QFN-80: Value between 0 and 0xff (bit 0 to bit 7 for GPIO 40 to 47 and temperature sensor input respectively)

Parameters

  input_mask | A bit pattern indicating which of the 5/8 inputs are to be sampled. Write a value of 0 to disable round robin sampling.

## ◆ adc_set_temp_sensor_enabled()

static void adc_set_temp_sensor_enabled | ( | bool | enable | ) |
| inlinestatic

Enable the onboard temperature sensor.

Parameters

  enable | Set true to power on the onboard temperature sensor, false to power off.