🛠 Broches, ADC 13 bits, DAC 12 bits et PWM : dompter les E/S de la Teensy 3.2
Objectives
Master the Teensy 3.2 I/O like a pro: 34 I/O pins, with 24 easily accessible on a compact breadboard, 21 analog inputs distributed across 2 converters, a true 12-bit DAC on A14, 12 touch pins (touchRead), and 12 PWM pins divided into 3 timer groups with independent frequencies. You will wire a potentiometer, a button, and a PWM LED, and read everything in a single sketch with DAC output.
Hardware
- 1 Teensy 3.2 on a breadboard (compact format: 24 accessible pins without soldering)
- 1 10 kΩ potentiometer on A2 (wiper in the middle, the other two legs between 3.3 V and GND)
- 1 push button between pin 5 and GND (internal pull-up)
- 1 LED + 330 Ω resistor on a PWM pin (e.g., 6)
- Optional: oscilloscope on A14 to view the DAC output
Architecture points to know
- 3.3 V logic: HIGH output = 3.3 V, 10 mA recommended maximum per pin; digital inputs tolerate 5 V (never exceed 5 V). Critical exception: the purely analog pins A10–A14 are NOT 5 V tolerant — powering them above 3.3 V destroys them.
- ADC: 10 bits by default; the hardware supports 16 bits but "in practice only ~13 bits are normally usable due to noise" (PJRC shop page). AREF = 3.3 V by default;
analogReference(INTERNAL)provides a 1.2 V reference. Programmable Gain Amplifier (PGA) differential stages on pairs A10/A11 and A12/A13. 3 integrated analog comparators. - DAC: a true 12-bit converter on A14 — rare in the Arduino-compatible world.
- PWM: 12 pins, 3 independent timer groups —
analogWriteFrequencychanges the frequency per group; group FTM0 controls pins 5, 6, 9, 10, 20, 21, 22, 23. - Pin 13 LED pitfall: when pin 13 is used as an input, do NOT use INPUT_PULLUP — the integrated orange LED skews the logic level.
- All pins accept pull-up/pull-down and pin-change interrupts; optional slew-rate limitation.
Complete code
// Potentiometre (A2) -> LED PWM (broche 6) + vraie sortie DAC (A14)
// + bouton (broche 5) + touche capacitive (broche 0).
const int POT = A2, BTN = 5, LEDPWM = 6, DAC = A14, TOUCH = 0;
void setup {
Serial.begin(9600);
pinMode(BTN, INPUT_PULLUP); // jamais INPUT_PULLUP sur la broche 13 !
pinMode(LEDPWM, OUTPUT);
analogWriteFrequency(LEDPWM, 20000); // 20 kHz sur le groupe FTM0
analogWriteResolution(12); // resolution 12 bits pour le DAC A14
}
void loop {
int v = analogRead(POT); // 0..1023 par defaut (10 bits)
analogWrite(LEDPWM, map(v, 0, 1023, 0, 255));
analogWrite(DAC, map(v, 0, 1023, 0, 4095)); // veritable DAC 12 bits
Serial.printf("pot=%d touche=%d bouton=%d\n",
v, touchRead(TOUCH), digitalRead(BTN));
delay(50);
}
Expected verification
The Serial Monitor displays the potentiometer value (0–1023) and a touch value that jumps when you place your finger on pin 0 (touchRead). The oscilloscope on A14 shows a continuous voltage that follows the potentiometer — a true analog output, not a filtered PWM. On pin 6, the PWM runs at 20 kHz (inaudible on a buzzer, clean for an LED driver). The LED follows the potentiometer.
Common errors
- Powering a sensor with 5 V and reading on A10–A14: pin destruction (not 5 V tolerant). The 5 V tolerance only applies to pins outside A10–A14.
- Expecting clean 16 bits: noise limits it to ~13 real bits — average multiple readings rather than aiming for the impossible.
- Forgetting that analogWriteFrequency affects an entire group: pins on the same timer change frequency together.
- INPUT_PULLUP on pin 13 as an input: the LED draws current and skews the reading — use simple INPUT with an external resistor.
- Confusing touchRead and digitalRead: capacitive detection only exists on 12 dedicated pins and its threshold depends on wiring (wire length, electrode surface area).
- Exceeding 10 mA per pin: the board heats up and the HIGH voltage collapses; use a transistor or a buffer for loads.
Other tutorials for this board
- Premiers pas sur la Teensy 3.2 : Teensyduino, HalfKay et premier Blink
- Un instrument MIDI USB en 10 minutes : Bounce, boutons et règle d'or du drain
- Audio Shield SGTL5000 : boucle stéréo 44,1 kHz et outil de conception graphique
- OctoWS2811 : piloter 8 rubans WS2812 en parallèle par DMA (presque zéro CPU)
- Timers matériels, interruptions imbriquées et FASTRUN : la vitesse Cortex-M4
- Intégration produit : alimentation VIN/VUSB, régulateur 250 mA, RTC à quartz et EEPROM
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino