🛠 GPIO et PWM sous 3,3 V : les règles d'or du Teensy 4.0
Objectives
Master the digital side: the Teensy 4.0 offers 40 pins (24 of which are accessible on a breadboard without soldering), all interrupt-capable, 31 PWM pins distributed across 19 independently clocked groups (analogWriteFrequency), and fine per-pin adjustments: pull-up/pull-down/keeper resistors, slew rate limiting, 7 output drive strength levels (≈150 Ω to ≈21 Ω), and 4 output bandwidth steps (50/100/150/200 MHz). Above all, you will integrate survival rule #1: nothing is 5 V tolerant (except VIN and VUSB).
Hardware
- 1 Teensy 4.0 + USB cable
- 1 push button between pin 2 and GND
- 1 LED + 330 Ω resistor on pin 6
- Breadboard + wires — powered by USB only (5 V)
Key Numbers
- Output high level: 3.3 V; input above 3.3 V = risk of damage (Paul Stoffregen's words in the output thread: "Except the VIN and VUSB pins, nothing is 5V tolerant on these new parts").
- Output current: the product page recommends 4 mA max per pin (the technical comparison table shows 10 mA — a documented official inconsistency; stick to the conservative value, 4 mA, and check the schematic).
- Any LED in series with an appropriate resistor (330 Ω for 3.3 V ≈ 3 mA) works without a driver.
- 49 timers in total (3 32-bit FlexPWM modules, 32 × 16-bit, 4 quadrature decoders, 4 IntervalTimers, 3 watchdogs, 2 GPTs).
Complete Code
// Bouton sur 2 (pull-down interne), LED PWM sur 6.
const uint8_t BTN = 2;
const uint8_t LED = 6;
void setup {
Serial.begin(9600);
pinMode(BTN, INPUT_PULLDOWN); // pull-down interne : pas de résistance externe
pinMode(LED, OUTPUT);
analogWriteFrequency(LED, 20000); // 20 kHz pour le groupe de la broche 6
}
void loop {
static uint16_t pwm = 0;
static int8_t pas = 8;
pwm += pas; // respiration 0..255
if (pwm >= 255 || pwm == 0) pas = -pas;
analogWrite(LED, pwm);
if (digitalRead(BTN) == HIGH) { // front simple
Serial.println(F("bouton!"));
delay(100);
}
delay(10);
}
Expected Verification
The LED breathes at 20 kHz (a frequency well above perceptible flicker — an oscilloscope or a simple smartphone recording in slow motion confirms the PWM). Pressing the button prints to the Monitor. Change analogWriteFrequency and observe that each pin group is independently clocked: this is the basis for driving multiple motors or LEDs without frequency coupling.
Common Errors
- Connecting a 5 V sensor (classic Arduino, HC-SR04, etc.) directly to a pin: the Teensy 4.0 is NOT 5 V tolerant, unlike the Teensy 3.2 — use a voltage divider or level shifters.
- Confusing the official PWM counters: the product page announces 31 PWM pins, the technical table says "27 total" for the 4.0 — documented inconsistency between the two PJRC pages; verify functionality on the schematic for your pin.
- Forgetting the pin 13 rule: when used as input, never use INPUT_PULLUP (the orange LED biases the pin).
- Overloading a pin: beyond the recommended 4 mA, the output voltage collapses; for a relay or LED strip, use a transistor.
- Pins 24 to 33 are on the LOWER pads of the board (along with those for the USB host port and SD) — invisible from the breadboard side, they must be soldered or accessed with probes.
- Powering via 3.3 V: not supported without special hardware modification; the 3.3 V pin is an output from the onboard regulator.
Other tutorials for this board
- Premiers pas sur le Teensy 4.0 : Teensyduino, Loader et premier Blink
- Entrées analogiques et capteur de température interne : le ADC et ses limites
- 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