🛠 Controlling an I2C sensor with ESP32-P4 Hydra

Adafruit Metro ESP32-P4 Hydra · intermediaire · Apprenez à configurer le bus I2C et à lire les données d'un capteur via les broches GPIO de la carte 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

Étapes

  1. Environment Setup: Ensure ESP32-P4 support is installed in your IDE. Select the correct serial port.
  2. 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).
  3. Code Initialization: Include necessary libraries (e.g., Wire.h). Define SDA and SCL pins if not using defaults.
  4. Bus Configuration: Call Wire.begin() to initialize the I2C bus. The P4 handles hardware interrupts for optimized transfers.
  5. Data Reading: Use Wire.requestFrom() to request data from the sensor. Read received bytes in a loop.
  6. 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

Code examples for this board

← Back to the board 🛠 All tutorials