🛠 Controlling an I2C sensor with ESP32-P4 Hydra
Objectifs
This tutorial guides you through using serial communication interfaces, specifically the I2C bus, on the Adafruit Metro ESP32-P4 Hydra board. Unlike classic microcontrollers, the P4 offers high flexibility in pin assignment. You will learn to activate the I2C peripheral, connect a standard sensor (like a BME280 or OLED display) via the STEMMA QT connector or GPIO pins, and interpret raw data. The goal is to understand register management and master-slave communication on this high-performance RISC-V architecture.
Matériel nécessaire
- Adafruit Metro ESP32-P4 Hydra board
- USB-C cable for power and flashing
- An I2C compatible sensor or module (e.g., BME280, MPU6050) with STEMMA QT connector or Dupont wires
- Computer with Arduino IDE or PlatformIO installed
Étapes
- Environment Setup: Ensure ESP32-P4 support is installed in your IDE. Select the correct serial port.
- Physical Wiring: Connect the sensor to the STEMMA QT connector (VCC, GND, SDA, SCL) or use default I2C GPIO pins. Check voltage (3.3 V).
- Code Initialization: Include necessary libraries (e.g., Wire.h). Define SDA and SCL pins if not using defaults.
- Bus Configuration: Call
Wire.begin()to initialize the I2C bus. The P4 handles hardware interrupts for optimized transfers. - Data Reading: Use
Wire.requestFrom()to request data from the sensor. Read received bytes in a loop. - Processing: Convert raw bytes to physical values (temperature, humidity) per the sensor datasheet.
Vérification
Open the Serial Monitor at 115200 bauds. Sensor values should display regularly. If no data appears, check wiring (swapped SDA/SCL) and the device's I2C address.
Pour aller plus loin
Explore DMA (Direct Memory Access) usage for I2C transfers without loading the CPU, freeing RISC-V cores for intensive calculations or video processing via MIPI.
Other tutorials for this board
- First connection and startup of the Metro ESP32-P4 Hydra
- Controlling GPIOs and a Simple Display on Metro ESP32-P4 Hydra
- Store Data on Adafruit Metro ESP32-P4 Hydra
- First Startup of the Metro ESP32-P4 Hydra
- Advanced Startup: Power, USB, and Debugging on ESP32-P4 Hydra
- High-Speed USB Storage Automation on ESP32-P4 Hydra
Code examples for this board
esp32-p4-hydra-gpio-blink.inoC++ (Arduino)esp32-p4-hydra-mpu6050-i2c.inoC++ (Arduino)esp32-p4-hydra-wifi-http.inoC++ (Arduino)esp32-p4-hydra-sdcard-write.inoC++ (Arduino)esp32-p4-hydra-usb-host-mouse.inoC++ (Arduino)esp32-p4-hydra-gpio-led-blink.inoC++ (Arduino)