🛠 Entrées analogiques et capteur de température interne : le ADC et ses limites
Objectives
Leverage the 14 analog inputs and the 2 ADC converters of the Teensy 4.0, while understanding their real limitations: fixed 0–3.3 V range, no AREF pin (analogReference has NO effect), hardware resolution up to 12 bits but — quoting Paul Stoffregen — “effective number of bits is about 10” due to noise. You will also add the on-chip temperature sensor, useful for monitoring heating during overclocking.
Hardware
- 1 Teensy 4.0 + USB cable
- 1 10 kΩ potentiometer: pin 1 to 3.3 V (not 5 V!), pin 3 to GND, wiper to A0
- Breadboard + wires
- InternalTemperature library (https://github.com/LAtimes2/InternalTemperature) via the IDE library manager or by copying it into the libraries folder
Complete code
// Lecture A0 + température du die.
#include <InternalTemperature.h>
void setup {
Serial.begin(9600);
analogReadResolution(10); // 0..1023 ; au-delà, du bruit sur T4
}
void loop {
int raw = analogRead(A0); // 0..1023
float volts = raw * (3.3f / 1023.0f); // référence fixe 3,3 V
float degC = InternalTemperature.readTemperatureC;
Serial.printf("A0 = %d (%.2f V) die = %.1f C\n", raw, volts, degC);
delay(500);
}
Expected verification
Turn the potentiometer: the value follows between 0 and 1023 and the volts remain below 3.30 V. Rub the chip with your finger or blow on it: the die temperature moves by a few tenths of a degree. Home calibration track: record the temperature at rest (cold board) then after a few minutes of intensive calculation — the M7 at 600 MHz heats up noticeably, which becomes critical in overclocking (≥912 MHz requires cooling according to community tests).
Common errors
- Applying more than 3.3 V to an analog input: fixed 0–3.3 V range, no 5 V protection — a voltage divider is required to measure a lithium battery, for example.
- Calling analogReference inherited from 8-bit Arduino code: no effect on Teensy 4.0 (no AREF pin) — the reference is the internal 3.3 V.
- Claiming 12 useful bits: the hardware can do it, noise cannot; stick to 10 effective bits, or average multiple readings (oversampling) to gain a fraction of a bit.
- Comparing voltages with 5.0f in code ported from Arduino: the Teensy 4.0 is a 3.3 V world, recheck every raw → volts conversion.
- Forgetting that there is no DAC, no PGA, no capacitive touch on the 4.0 (present on the 3.5/3.6): for a clean analog audio output, use I2S to an external DAC or use the MQS output.
- The 4 analog comparators exist but require direct programming of the IMXRT registers — outside the beginner scope.
Other tutorials for this board
- Premiers pas sur le Teensy 4.0 : Teensyduino, Loader et premier Blink
- GPIO et PWM sous 3,3 V : les règles d'or du Teensy 4.0
- Piloter un ruban WS2812 sans bloquer le CPU : WS2812Serial et OctoWS2811
- Contrôleur MIDI USB : un potard qui joue du piano (MIDIx4/MIDIx16)
- Comprendre la mémoire : 2 Mo de flash, 1 Mo de RAM TCM/DMA, EEPROM émulée et LittleFS
- Deux ports USB simultanés : périphérique 480 Mbit/s ET hôte USB sur les pastilles
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino