🛠 GPIO et PWM sur le Teensy 4.1 : 55 broches en 3,3 V, ni 5 V tolérées ni traîne
Objectives
Leverage the Teensy 4.1 I/O: 55 pins in total, with 42 easily accessible on a solderless breadboard (compared to 40/24 on the 4.0). The additional row provides dedicated PWM pins (24, 25, 46, 47, 53 are listed in the official PWM map). We will read a button, generate brightness variation via PWM, and internalize the two golden rules: no pin tolerates more than 3.3 V (a radical change from the Teensy 3.2!) and the recommended output current is 4 mA per pin (a conservative value from the product page).
Hardware
- 1 Teensy 4.1 + micro-USB data cable
- 1 push button (pin 24 to GND, internal pull-up)
- 1 LED + resistor (~100 to 330 Ω) on pin 6
- A 10 kΩ potentiometer between 3.3 V, GND, and pin A0 (Arduino pin 14)
Electrical Rules
- HIGH output = 3.3 V; never apply more than 3.3 V to a pin, digital or analog. A 5 V signal requires a level shifter.
- All pins have pull-up, pull-down, and keeper resistors, as well as interrupt-on-change capability.
- PWM: 35 PWM pins, 22 groups that can run at independent frequencies via
analogWriteFrequency. The QuadTimer4 channels are reserved by OctoWS2811 and the Audio library's ADC timing — avoid them for general-purpose PWM. - Documented pitfall on pin 13: when used as an input, never use
INPUT_PULLUP, as the onboard LED biases the pin.
Complete Code
const uint8_t BTN = 24; // broche exclusive du 4.1
const uint8_t LED_PWM = 6; // broche FlexPWM
const uint8_t LED_STAT = 13;// LED orange : jamais INPUT_PULLUP ici
void setup {
pinMode(BTN, INPUT_PULLUP);
pinMode(LED_STAT, OUTPUT);
analogWriteFrequency(LED_PWM, 1000); // 1 kHz, indépendant des autres groupes
analogWrite(LED_PWM, 0);
}
void loop {
if (digitalRead(BTN) == LOW) {
// A0 (broche 14) : 0..1023 sur 0..3,3 V fixes
int v = map(analogRead(14), 0, 1023, 0, 255);
analogWrite(LED_PWM, v);
digitalWrite(LED_STAT, HIGH);
} else {
analogWrite(LED_PWM, 0);
digitalWrite(LED_STAT, LOW);
}
delay(10);
}
Expected Verification
Button released: LED off. Button pressed: brightness follows the potentiometer. The 600 MHz M7 with its TCM memories makes pin writes negligible in CPU cycles — for high-frequency blinking, prefer digitalToggle (a single compiled bit write).
Common Errors
- Connecting a 5 V sensor directly: burning out a pin is documented as the #1 risk when moving from 3.2 to 4.x.
- Exceeding 4 mA per pin without a transistor: the product page is explicit about this conservative limit (the techspecs table mentions 10 mA — design for 4).
- Confusing pin numbering: analog pin A0 is Arduino pin 14 on all Teensy models.
- Forgetting software debouncing:
delay(10)is a quick fix, but a real project should use a debouncing library (e.g., Bounce).
Other tutorials for this board
- Premiers pas sur le Teensy 4.1 : Teensyduino, Teensy Loader et premier Blink
- Entrées analogiques du Teensy 4.1 : 18 broches, 2 ADC, 0-3,3 V fixes et zéro DAC
- Logger sur carte SD via le port SDIO natif du Teensy 4.1
- Serveur web embarqué : l'Ethernet natif 10/100 du Teensy 4.1 (DP83825)
- Boîte à rythmes USB MIDI : classes MIDI, MIDIx16 et port hôte USBHost_t36
- Ingénierie mémoire du Teensy 4.1 : TCM, DMAMEM, EXTMEM, flash QSPI et EEPROM
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino