All wiring guides

Free wiring guides

Wire the HC-SR04 ultrasonic sensor to the ESP32-C3

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 partOn the ESP32-C3
VCC3V3
TRIGGPIO 0
ECHOGPIO 1
GNDGND

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-C3 Super Mini
// https://stem.manusignal.com/en/parts/hc-sr04-ultrasonic-sensor-esp32-c3

const int PIN_TRIG = 0;
const int PIN_ECHO = 1;

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 > ESP32C3 Dev Module (USB CDC On Boot: Enabled), 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.

The same part on the ESP32-S3

Build something with it

These Build Packs use this part on this board, with the same pins.

More parts on the ESP32-C3