Free wiring guides
Wire the HC-SR04 ultrasonic sensor to the ESP32-S3
Ultrasonic distance sensor, HC-SR04, 3.3 V version. Sends a ping and times the echo to measure distance.
When buying: Ask for the 3.3 V version (often sold as HC-SR04P or RCWL-1601). The classic 5 V one puts 5 V on ECHO, which an ESP32 pin must not receive.
Which pin goes where
| On the part | On the ESP32-S3 |
|---|---|
| VCC | 3V3 |
| TRIG | GPIO 5 |
| ECHO | GPIO 6 |
| GND | GND |
Wire it with the USB cable unplugged. Follow the names printed on your own part: makers sometimes reorder the pins.
In 3D
Example sketch
// Ultrasonic distance sensor, HC-SR04, 3.3 V version
// on the ESP32-S3-DevKitC-1 (N8R2 or N16R8)
// https://stem.manusignal.com/en/parts/hc-sr04-ultrasonic-sensor-esp32-s3
const int PIN_TRIG = 5;
const int PIN_ECHO = 6;
void setup() {
Serial.begin(115200);
pinMode(PIN_TRIG, OUTPUT);
pinMode(PIN_ECHO, INPUT);
}
void loop() {
digitalWrite(PIN_TRIG, LOW);
delayMicroseconds(2);
digitalWrite(PIN_TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(PIN_TRIG, LOW);
unsigned long us = pulseIn(PIN_ECHO, HIGH, 30000);
Serial.print(us * 0.0343 / 2.0, 1);
Serial.println(" cm");
delay(300);
}
- Tools > Board > ESP32S3 Dev Module, then Tools > Port and choose the board's port.
This sketch is compiled for the board before the page is published. It has not been run on real hardware by us.
What you should see
Open the Serial Monitor at 115200. It prints the distance to whatever is in front of the sensor, three times a second.
If it does not work
- The distance is always 0.
- TRIG and ECHO are swapped, or the sensor has no power.
Build something with it
These Build Packs use this part on this board, with the same pins.
More parts on the ESP32-S3
- ST7789 1.54 in TFT screenESP32-S3 · 7 wires
- ST7789 2.0 in TFT screenESP32-S3 · 7 wires
- GC9A01 round screenESP32-S3 · 7 wires
- SSD1306 OLED 0.96 in screenESP32-S3 · 4 wires
- SSD1306 OLED 0.91 in screenESP32-S3 · 4 wires
- 4.2 in e-paper screenESP32-S3 · 8 wires
- TTP223 touch sensorESP32-S3 · 3 wires
- Push buttonESP32-S3 · 2 wires
- Foot switchESP32-S3 · 2 wires
- Slide switchESP32-S3 · 2 wires
- SW-520D tilt switchESP32-S3 · 2 wires
- MC-38 door switchESP32-S3 · 2 wires
- AM312 PIR motion sensorESP32-S3 · 3 wires
- Capacitive soil moisture sensorESP32-S3 · 3 wires
- BME280 sensorESP32-S3 · 4 wires
- MLX90614 infrared thermometerESP32-S3 · 4 wires
- SGP30 air quality sensorESP32-S3 · 4 wires
- HX711 load cellESP32-S3 · 4 wires
- KY-040 rotary encoderESP32-S3 · 5 wires
- 3×4 keypadESP32-S3 · 7 wires
- Passive buzzerESP32-S3 · 2 wires
- WS2812 LED ringESP32-S3 · 3 wires
- WS2812 LED stickESP32-S3 · 3 wires
- WS2812B LED stripESP32-S3 · 3 wires
- KY-016 RGB LEDESP32-S3 · 4 wires
- 2-channel relay moduleESP32-S3 · 4 wires
- 28BYJ-48 stepper motor (ULN2003)ESP32-S3 · 6 wires
- MAX98357A I2S amplifierESP32-S3 · 5 wires
- SX1262 LoRa module (Ra-01SH)ESP32-S3 · 9 wires