🛠 Lire un capteur analogique avec le SAADC 12 bits

Adafruit Feather nRF52840 Express · debutant · Schéma : IMG-nrf52840-feather-debutant-3-nrf52840-capteur-analogique-saadc.svg — Potentiomètre alimenté en 3,3 V, curseur relié à A0 de la Feather nRF52840, masse commune, tension lue affichée en console.

Objectives

Configure the nRF52840 analog-to-digital converter (SAADC, 6 12-bit pins on the Feather) to read a potentiometer, convert the value to millivolts, and understand the 3.3 V logic. By the end, you will also be able to read the battery voltage divider to monitor a LiPo battery.

Hardware

Complete Code

// nrf52840-capteur-analogique-saadc.ino
const uint8_t SENSOR_PIN = A0;
// Diviseur batterie : sur les Feather nRF52840, le diviseur VIN est cablé sur
// une broche analogique ; la revue D du schema a une soie-ecrosee D5 la ou
// devrait figurer A6.
// > 
// le schema officiel Adafruit (github.com/adafruit/Adafruit-nRF52-Bluefruit-Feather-PCB).

void setup {
  Serial.begin(115200);
  analogReadResolution(12);      // 0..4095 sur SAADC 12 bits
  analogReference(AR_INTERNAL_3_6); // reference interne 3,6 V
}

void loop {
  uint16_t raw = analogRead(SENSOR_PIN);
  float mv = raw * 3600.0f / 4095.0f;
  Serial.print("raw="); Serial.print(raw);
  Serial.print("  ~"); Serial.print(mv, 0); Serial.println(" mV");
  delay(100);
}

Expected Verification

When turning the potentiometer, the raw value follows 0→4095 and the millivolts 0→3300 approximately (range limited by the 3.3 V supply). Values are stable at rest (±2 LSB); with a finger on the wiper, slight normal noise appears.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials