🛠 GPIO et PWM sous 3,3 V : les règles d'or du Teensy 4.0

Teensy 4.0 · debutant · Schéma : IMG-teensy-40-debutant-2-teensy40-gpio-pwm-3v3.svg — Teensy 4.0 sur breadboard (24 broches accessibles sans soudure) : bouton poussoir sur la broche 2 vers GND, LED + 330 Ω sur la broche 6 (broche PWM), broches 24-33 sur les pastilles inférieures ; encart « 3,3 V max — aucune tolérance 5 V ».

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

Key Numbers

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

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials