🛠 Lire un capteur analogique avec l'ADC 12 bits
Objectives
Use the SAMD21 ADC (native 12-bit, configurable 8/10/12 bits, 3.3 V reference) to read an analog sensor, calibrate the measurement, and display it on the serial console.
Hardware
- 1 Arduino MKR WAN 1300
- 1 10 kΩ potentiometer (or 3.3 V analog sensor)
- Jumper wires
- >
Complete code
// ADC 12 bits : 0..4095 pour 0..3,3 V. Broche A1.
const uint8_t CAPTEUR = A1;
void setup {
Serial.begin(115200);
while (!Serial) {}
analogReadResolution(12); // explicite : 0..4095
}
void loop {
uint32_t brut = 0;
for (uint8_t i = 0; i < 16; i++) {
brut += analogRead(CAPTEUR); // moyenne mobile
delay(2);
}
brut /= 16;
float volts = brut * 3.3f / 4095.0f;
Serial.print("brut="); Serial.print(brut);
Serial.print(" V="); Serial.println(volts, 3);
delay(500);
}
Expected verification
When turning the potentiometer (wiper towards 3.3 V, the other two pins towards GND and 3.3 V), the raw value follows 0..4095 and the voltage 0.000..3.300 V, with only a few units of residual noise thanks to averaging.
Common errors
- Sensor powered at 5 V on a 3.3 V pin : risk of damage ; power the sensor at 3.3 V.
- Forgetting analogReadResolution : the SAMD core may return 10-bit values, leading to incorrect thresholds.
- Pin not connected : values fluctuate ; add a load or an RC filter.
Other tutorials for this board
Code examples for this board
emission-p2p.inoArduinoreception-p2p.inoArduinoreglage-sp.inoArduinometeo-lora.inoArduinotrame-crc.inoArduinorelais-radio.inoArduino