Wiring guide

How to wire an HC-SR04 to an Arduino Mega

To wire an HC-SR04 to an Arduino Mega, connect VCC to 5V, TRIG to pin 2, ECHO to pin 3 and GND to GND. The Arduino Mega is a 5 V board, so ECHO connects straight to the pin and no divider is needed. The sketch below uses no library beyond what the board includes. It sends a ping every 100 ms and prints the distance in centimeters.

  • 5 V logic
  • Library: none
  • 4 wires
Arduino Mega · HC-SR04live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running on the simulator with the sketch below. Press what can be pressed; click anything else to open it in the editor.
Pin to pin

HC-SR04 to Arduino Mega wiring table

Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.

HC-SR04 pinArduino Mega pinWire colorWhy
VCC5VredThe HC-SR04 is a 5 V module; below about 4.5 V many units stop answering.
TRIGpin 2greenOutput from the board: a 10 microsecond pulse starts a ping.
ECHOpin 3amberInput to the board: high for as long as the sound takes to come back.
GNDGNDblueCommon ground.
Voltage

Power and logic levels for HC-SR04 and Arduino Mega

Logic level

The Arduino Mega’s pins are 5 V logic. A 5 V part can drive them directly; a part whose signal pins are limited to 3.3 V needs a level shifter.

Supply

The HC-SR04 takes 5 V from 5V. That is the Arduino Mega’s 5 V rail, so the part shares the current the board can supply.

This pair

Everything on this module is 5 V, the Arduino Mega’s own level, so TRIG and ECHO connect straight to the pins.

Pins to leave alone

Pins 14 to 19 are the three extra serial ports and 20 and 21 are I2C, so keep them free if you will need those. An Uno shield’s I2C on A4 and A5 is not connected to anything on a Mega.

The sketch

The working code for the Arduino Mega

Library: none, the sketch times the ECHO pulse with pulseIn(). This is the sketch the circuit above runs.

HC-SR04 on Arduino Mega
const int TRIG_PIN = 2;   // pin 2
const int ECHO_PIN = 3;   // pin 3

void setup() {
  Serial.begin(9600);
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
}

void loop() {
  // A pulse of at least 10 us on TRIG starts one measurement.
  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);

  // ECHO stays high for the sound's round trip; 38 ms means nothing in range.
  unsigned long us = pulseIn(ECHO_PIN, HIGH, 38000UL);
  if (us == 0) {
    Serial.println("Out of range");
  } else {
    Serial.print("Distance: ");
    Serial.print(us / 58);
    Serial.println(" cm");
  }
  delay(100);
}
Output

What you should see

Open the serial monitor at 9600 baud. Each ping prints one line. Set the sensor’s distance to 150 cm and the line reads Distance: 150 cm; the sketch divides the ECHO pulse in microseconds by 58 to get centimeters.

When nothing is in range, or ECHO is not connected, the pulse never arrives and the sketch prints Out of range after its 38 ms timeout.

The serial monitor prints this, recorded from the circuit above with the distance set to 150 cm after the first 300 ms:

Distance: 100 cm
Distance: 150 cm
Pitfalls

Common mistakes wiring an HC-SR04 to an Arduino Mega

Swapping TRIG and ECHO

TRIG is an input on the module and ECHO an output. Swapped, the board pulses the ECHO pin and listens on TRIG, and every reading is 0 or out of range. Check the labels on the module, not the order of the header.

pulseIn() without a timeout

With nothing in front of the sensor, pulseIn() with no third argument waits a whole second for a pulse that never comes, and the sketch looks frozen. Pass a timeout of about 38000 microseconds.

Pinging too fast

Fire the next ping before the last echo has died away and the module hears its own previous ping, so short random distances appear. Leave at least 60 ms between measurements.

Build your own HC-SR04 project

Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.