Ultrasonic Scanner Wiring

An HC-SR04 measures distance with sound. An SG90 turns it to face different directions. The ESP32-S3 reads the sensor and tells the PCA9685 where to move the servo.

This pin list is for an ESP32-S3 N16R8. Use the GPIO numbers printed on the board, not the positions of the pins. Unplug USB and the 5 V adapter before moving any wires.

1. Connect the PCA9685

Connect thisTo this pin or supply
5 V / 2 A adapter positivePCA9685 screw terminal V+
5 V / 2 A adapter negativePCA9685 screw terminal GND
PCA9685 side GNDESP32-S3 GND
PCA9685 side VCCESP32-S3 3V3
PCA9685 SDAESP32-S3 GPIO 8
PCA9685 SCLESP32-S3 GPIO 9

V+ powers the servo. VCC powers the driver chip and sets its signal voltage. Use the screw terminal for the adapter; the side V+ pin bypasses the board’s reverse-polarity protection. The side GND pin is fine for the ground jumper to the ESP32-S3.

2. Connect the HC-SR04

Connect thisTo this pin or supply
HC-SR04 VCCSame adapter +5 V, branched from the wire going to the screw terminal
HC-SR04 GNDShared GND
HC-SR04 TRIGESP32-S3 GPIO 4
HC-SR04 ECHOESP32-S3 GPIO 5 through the resistor divider below

Do not connect ECHO directly to GPIO 5. Use a 10 kΩ resistor from ECHO to the midpoint and a 15 kΩ resistor from the midpoint to GND. Use 1% resistors if available.

ECHO ─ 10 kΩ ─ ● ─ 15 kΩ ─ GND
              │
            GPIO 5

A 5 V Echo signal becomes about 3.0 V at the midpoint: 5 V × 15 ÷ (10 + 15). Using two 10 kΩ resistors gives only 2.5 V, too close to the minimum voltage the ESP32-S3 guarantees to read as HIGH.

Put one leg from each resistor and the jumper to GPIO 5 in the same connected breadboard row. Connect the two remaining resistor legs to ECHO and shared GND as shown. The resistors have no polarity.

3. Plug in the SG90

Use channel 0 on the PCA9685. Match the labels rather than guessing by connector direction.

  • Brown → GND
  • Red → V+
  • Orange → PWM / signal

4. Check before power-up

Connect ESP32-S3 USB power first, then switch on the adapter. The adapter powers the sensor and servo rail, not the ESP32-S3. All three boards must share GND. Switch off the adapter before unplugging the ESP32-S3.

Start the I²C bus before starting the PCA9685 library. GPIO 4 sends the trigger. GPIO 5 reads the divided Echo.

Wire.begin(8, 9);
const int TRIG_PIN = 4;
const int ECHO_PIN = 5;