adc_ain1.c

nRF52833-DK (boards.json id: nrf52833-dk) · File : adc_ain1.c · Language : C · Use case : Lire une tension sur une entrée ADC

The nRF52 ADC done right: channel AIN1 (P0.03 on the DK, Arduino header A0), 12-bit sample against the 0.6 V internal reference. Careful: above 0.6 V you need a divider, never a direct feed.

Hardware

An nRF52833-DK board (Nordic, nRF52833 Cortex-M4F SoC, BLE 5) powered over USB: 4 LEDs on P0.13-P0.16 (active-low) and 4 buttons on P0.11, P0.12, P0.24, P0.25 (active-low, internal pull-up must be enabled). A potentiometer wired as a divider (A0 between wiper and ground) or a 0-0.6 V test source on the DK A0 pin.

Wiring

Pot wiper to P0.03 (A0), the two ends between 3.3 V (or VDD) and GND. Never above 0.6 V without a divider on the internal reference.

How it works

adc_channel_setup configures gain/reference/channel; adc_read triggers a DMA conversion and fills the buffer. The 0-4095 raw code converts to volts as (raw/4095)×0.6.

Running it

Zephyr project: west build -b nrf52833dk/nrf52833 then west flash (on-board J-Link); 115200 serial console on the USB UART connector.

Variants

Switch channels (AIN2-P0.04, etc. per the DK guide), oversample and average to cut noise, or enable the 1/4 gain for higher voltages.

adc_ain1.c

/* ADC canal 1 = AIN1. Sur le DK, AIN1 = P0.03 (broche A0
 * Arduino du connecteur) ; plage 0-Vref, Vref interne 0,6 V
 * avec gain 1 — diviser la tension si nécessaire ! */
#include <zephyr/kernel.h>
#include <zephyr/drivers/adc.h>

#define ADC_NODE DT_ALIAS(adc0)
#define CANAL    1

static const struct adc_channel_cfg ch_cfg = {
	.gain             = ADC_GAIN_1,
	.reference        = ADC_REF_INTERNAL,
	.acquisition_time = ADC_ACQ_TIME_DEFAULT,
	.channel_id       = CANAL,
};

static uint16_t echantillon;
static const struct adc_sequence seq = {
	.channels    = BIT(CANAL),
	.buffer      = &echantillon,
	.buffer_size = sizeof(echantillon),
	.resolution  = 12,
};

int main(void)
{
	const struct device *adc = DEVICE_DT_GET(ADC_NODE);
	if (!device_is_ready(adc)) {
		return 1;
	}
	adc_channel_setup(adc, &ch_cfg);

	while (1) {
		adc_read(adc, &seq);
		/* 0-4095 pour 0-0,6 V (Vref interne, gain 1) */
		printk("AIN1 = %u (raw 12 bits)\n", echantillon);
		k_msleep(1000);
	}
	return 0;
}

← Back to the board