🛠 Deux ports USB simultanés : périphérique 480 Mbit/s ET hôte USB sur les pastilles
Objectives
The Teensy 4.0 features TWO fully independent 480 Mbit/s USB ports: the standard micro-USB port in device mode (Serial, MIDI/MIDIx4/MIDIx16, Audio, Keyboard, Mouse, Joystick, Touchscreen, MTP, Flight Sim, RawHID…) and a host port on two SMD pads on the back of the board, driven by the USBHost_t36 library. You will read a USB keyboard on the host side while communicating with the Serial Monitor on the device side — and learn about the two electrical pitfalls of this port.
Hardware
- 1 Teensy 4.0
- USB host adapter: solder D+ / D− from the two bottom pads (3.048 × 1.016 mm format) to a female USB-A connector (community adapter boards are available)
- 1 USB keyboard or mouse
- Recommended: current-limiting switch such as TPS2055A on the VBUS of the connected device
- Library: https://github.com/PaulStoffregen/USBHost_t36
Complete code
// Hôte USB (pastilles du bas) + périphérique série (micro-USB) en parallèle.
#include <USBHost_t36.h>
USBHost myusb;
USBHIDParser hid1(myusb);
KeyboardController clavier(myusb);
void setup {
Serial.begin(9600); // port DEVICE : le Moniteur passe par là
myusb.begin; // port HOST : les pastilles du dessous
Serial.println(F("branchez un clavier USB"));
}
void loop {
myusb.Task; // machine à états de l'hôte — À NE PAS OUBLIER
if (clavier) {
// getKeys renvoie les touches actives ; voir les exemples
// Keyboard de USBHost_t36 pour le mapping complet.
static uint8_t touches[6];
if (memcmp(touches, clavier.getKeys, 6)) {
memcpy(touches, clavier.getKeys, 6);
Serial.print(F("touches actives : "));
for (int i = 0; i < 6; i++)
Serial.print(touches[i], HEX), Serial.print(' ');
Serial.println;
}
}
}
Expected verification
The Serial Monitor (via micro-USB) displays key codes as you type on the keyboard connected to the pads: the two USB worlds coexist on the same board. Note that the 4.0's host port is data-only: it has no onboard current management circuit (unlike the 4.1). Workflow bonus: the same device port supports CircuitPython — flash the official .hex (https://circuitpython.org/board/teensy40/) using Teensy Loader, and the board mounts as a USB drive; PJRC warns that CircuitPython does not cover all of the 4.0's hardware.
Common errors
- Hot-plugging a power-hungry device: "hot-plugging a USB device can draw too much current and possibly crash (brown-out reset) your Teensy" (output wire, Paul Stoffregen) — add a current-limiting switch like the TPS2055A, or connect the device BEFORE powering up.
- Forgetting
myusb.Taskinloop: the host never enumerates anything. - Powering the device's VBUS from the Teensy's 3.3 V: keyboards expect 5 V; the 3.3 V output is limited to 250 mA.
- Forgetting to cut the VIN/VUSB pad: if you inject 5 V via VIN while keeping the micro-USB connected to the PC, you backfeed current into the computer — cut the bottom pads to separate the two rails.
- Confusing the pads: there are three groups on the back — USB host (2), SD 4-bit (8), pins 24-33 (10); check the card10a/card10b datasheet before soldering.
- Expecting USBHost_t36 to handle any device: the target is HID (keyboards, mice, gamepads), MIDI, and storage; webcams and USB WiFi are not supported.
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
- 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
Code examples for this board
clignotante.inoArduinotouche-capacitive.inoArduinoanalogique-12-bits.inoArduinomidi-piano.inoArduinopwm-respiration.inoArduinoservo-angle.inoArduino