🛠 Lire un capteur avec l'ADC 12 bits du SAMD21
Objectives
Use the Tian's analog inputs: the board has 12 inputs, A0 to A11, all reusable as digital pins; A0–A5 are located in the same positions as on an UNO, while A6–A11 correspond to digital pins 4, 6, 8, 9, 10, and 12. Most importantly, the SAMD21 features a 12-bit analog-to-digital converter: unlike the UNO (10 bits, 0–1023), expect values ranging from 0 to 4095.
Hardware
- 1 Arduino Tian + micro-USB cable
- 1 10 kΩ potentiometer: one end to ground, the other to the 3.3 V power pin, the wiper to A0
- >
Complete Code
// Lecture 12 bits : 0..4095 attendus, à vérifier au Traceur série.
const uint8_t POT = A0;
void setup {
Serial.begin(115200);
}
void loop {
int brut = analogRead(POT); // ADC 12 bits du SAMD21 : 0..4095
float tension = brut / 4095.0 * 3.3; // à ajuster selon la référence réelle
Serial.print(F("brut=")); Serial.print(brut);
Serial.print(F(" ~")); Serial.print(tension, 2); Serial.println(F(" V"));
delay(10);
}
Expected Verification
In the Serial Plotter, the value should follow the potentiometer continuously. Mentally estimate the amplitude: if you read approximately 0 to 4095, the 12-bit ADC is active; if you cap out at ~1023, your Arduino core is still mapping in 10 bits — adjust the conversion accordingly. This is THE major pitfall when porting from UNO → Tian.
Common Errors
- Reusing UNO code by dividing by 1023: the calculated voltage will be off by a factor of 4.
- Powering the potentiometer with 5 V while the ADC references 3.3 V: the reading saturates at ~3.3 V at the wiper, meaning half the rotation yields no change.
- Confusing A6–A11 with dedicated pins: these inputs reside on D4, D6, D8, D9, D10, and D12; if your sketch uses D9 for PWM, A9 is no longer available.
- >
- Unpowered analog input: touching the wire with a finger causes values to drift slowly; this is not a sensor, it is an antenna.
Other tutorials for this board
- Découvrir l'Arduino Tian : un microcontrôleur et un vrai Linux sur la même carte
- Bouton et LED sur les GPIO de la Tian : les bases côté SAMD21
- Le Bridge : faire exécuter des commandes Linux depuis le croquis
- Configurer le WiFi et piloter Linino en SSH
- Télémétrie : pousser une mesure du capteur vers le réseau
- GPIO duos : commander les broches de l'Arduino depuis Linux
Code examples for this board
clignotant-rest.inoArduinoconsole-bridge.inoArduinovaleurs-partagees.inoArduinoprocess-shell.inoArduinoserveur-json.inoArduinolecture-fichier.inoArduino