Project Start Planning
Table
| Function | ESP32 pin | Connected hardware |
|---|---|---|
| I2C SDA | GPIO21 | U2/U3 SDA, R3 pull-up |
| I2C SCL | GPIO22 | U2/U3 SCL, R4 pull-up |
| Ultrasonic TRIG | GPIO17 | J23 TRIG |
| Ultrasonic ECHO | GPIO16 | R1/R2 divided ECHO |
| PWM driver A | I2C 0x40 | J5-J20, channels 0-15 |
| PWM driver B | I2C 0x41 | J21-J22, channels 0-1 |
Wire library.Cpp
#include <Wire.h> #include <Adafruit_PWMServoDriver.h> Adafruit_PWMServoDriver pwmA(0x40); Adafruit_PWMServoDriver pwmB(0x41); constexpr int PIN_TRIG = 17; constexpr int PIN_ECHO = 16; void setup() { pinMode(PIN_TRIG, OUTPUT); digitalWrite(PIN_TRIG, LOW); pinMode(PIN_ECHO, INPUT); Wire.begin(21, 22); pwmA.begin(); pwmB.begin(); pwmA.setPWMFreq(50); pwmB.setPWMFreq(50); }
Cpp
long readEchoUs() { digitalWrite(PIN_TRIG, LOW); delayMicroseconds(2); digitalWrite(PIN_TRIG, HIGH); delayMicroseconds(10); digitalWrite(PIN_TRIG, LOW); return pulseIn(PIN_ECHO, HIGH, 30000); }
Pin Mapping
Arduino Dependencies
Minimal Initialization
Servo Channel Mapping
HC-SR04 Measurement Example
Startup Sequence