🛠 Lire un capteur analogique avec l'ADC du Boron
Objectives
The nRF52840 in the Boron integrates an analog-to-digital converter, and the board exposes 6 analog inputs A0 to A5 (3.3 V max). You will assemble a simple analog sensor — a voltage divider with a photoresistor (LDR) — read the voltage with analogRead, convert it to a percentage, and display it on the serial console. This is the basis for any analog sensor: potentiometer, flex sensor, TMP36, etc.
Hardware
- 1× Particle Boron + micro-USB cable
- 1× photoresistor (LDR) + 1× fixed 10 kΩ resistor
- Breadboard, jumper wires
- Optional: a small lamp to vary the light
Complete code
// Boron — lecture ADC sur A0 (capteur de lumière)
const uint16_t CAPTEUR_PIN = A0; // A0-A5 : entrées analogiques
void setup {
Serial.begin(9600);
pinMode(CAPTEUR_PIN, INPUT);
delay(1000);
Serial.println("Lecture capteur analogique (0-4095)");
}
void loop {
uint16_t brut = analogRead(CAPTEUR_PIN); // 0-4095 (12 bits)
float tension = brut * 3.3f / 4095.0f; // 0-3,3 V
uint16_t pourcent = map(brut, 0, 4095, 0, 100);
Serial.printf("brut=%u U=%.2f V lumiere=%u %%\n", brut, tension, pourcent);
delay(250);
}
Wiring: 3V3 → LDR → pin A0 → 10 kΩ resistor → GND. The midpoint goes to A0.
Expected verification
When covering the LDR with your finger, the raw value drops or rises significantly (depending on the divider orientation); when illuminating it, it returns toward the other extreme. The calculated voltage remains between 0 and 3.3 V. By connecting A0 to 3V3, you should read approximately 4095; to GND, approximately 0 — this is your calibration test.
Common errors
- Exceeding 3.3 V on the ADC input: the Boron is 3.3 V max on all pins, otherwise damage may occur.
- Divider wired in reverse: variations are inverted, which is not a failure but disrupts business logic.
- Reading A0 as a digital pin (
digitalRead): you will get 0 or 1 without any nuance. - Fixed resistor too far from the LDR impedance in your lighting conditions: the useful range is reduced to a few dozen points.
- Noise sources (motors on the same power supply): average multiple readings if the value jumps significantly.
Other tutorials for this board
Code examples for this board
clignotante.inoArduinobouton-evenement.inoArduinofonction-distance.inoArduinocapteur-dht.inoArduinoruban-neopixel.inoArduinopwm-respiration.inoArduino