🛠 Lire un capteur analogique avec le SAADC 12 bits
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
- 1× Adafruit Feather nRF52840 Express
- 1× 10 kΩ potentiometer (or 3.3 V analog sensor)
- Jumper wires + micro-USB data cable
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
- Powering the sensor with 5 V: The Feather logic is 3.3 V; never exceed 3.3 V on an ADC pin.
- Forgetting
analogReadResolution(12): By default, Arduino emulates 10 bits, and you lose the native precision of the SAADC. - No common ground between the sensor and the board: The reading drifts or stays at 0.
- Confusing ADC pins: Only 6 pins are analog on the Feather; check the pinout before wiring.
Other tutorials for this board
Code examples for this board
neopixel_pulsation.pyPythonled_bouton.pyPythonentrees_num.pyPythonanalogique_lumiere.pyPythonservo_pwm.pyPythonscanner_i2c.pyPython