Arduino Mega HC-SR04 Simulator: Wiring, Code and a Live Circuit
This is an HC-SR04 ultrasonic distance sensor wired to an Arduino Mega 2560, measuring live: the board pings ten times a second and prints the distance in centimeters. In the editor, drag the sensor’s distance slider and the reading follows.
Four wires: 5V, GND, TRIG to pin 2 and ECHO to pin 3, with no library at all, only pulseIn().
- 5 V logic
- No library: pulseIn()
- TRIG 2, ECHO 3
- Range 2 to 400 cm
Wiring the HC-SR04 to the Arduino Mega
Every connection in the circuit above, with the board’s own pin names.
| HC-SR04 pin | Arduino Mega pin | Why |
|---|---|---|
| VCC | 5V | The HC-SR04 is a 5 V module; below about 4.5 V many units stop answering. |
| TRIG | pin 2 | Output from the board: a 10 microsecond pulse starts a ping. |
| ECHO | pin 3 | Input to the board: high for as long as the sound takes to come back. |
| GND | GND | Common ground. |
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 on the Arduino Mega
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.
On a real bench
The Mega is long, so put the breadboard beside its double row of pins 22 to 53 and leave the left-hand headers free. The HC-SR04 uses only a few of its 54 pins; a Mega earns its size when one sketch drives a keypad, a display and several sensors at once.
The code
The sketch the circuit above runs. Open it in the editor, change a line and press Run: it compiles in your browser.
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);
}
Holding TRIG high for 10 microseconds (the module needs at least 10) makes it send eight 40 kHz pulses. It then raises ECHO and lowers it again when the echo returns, so the width of the ECHO pulse is the round trip time of the sound.
pulseIn(ECHO_PIN, HIGH, 38000UL) measures that width in microseconds. Sound covers a centimeter and back in about 58 microseconds, so dividing by 58 gives centimeters. The 38 ms timeout is what the module does with nothing in range; without a timeout pulseIn() waits a full second.
The 100 ms delay keeps one ping’s echo from being heard by the next. Serial.begin(9600) opens the serial monitor. Serial is the USB port; Serial1 to Serial3 are on pins 18 and 19, 16 and 17, and 14 and 15.
Common mistakes with the HC-SR04 on the 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.
Questions and answers
Can I simulate an HC-SR04 with an Arduino Mega 2560?
Yes. The circuit at the top of this page is an Arduino Mega 2560 with a HC-SR04, running in your browser on a simulated ATmega2560. The sensor’s slider sets the distance to the object, from nothing in range up to 4 meters, and the ECHO pulse is as wide as a real one would be. Press Run it in the editor to change the wiring or the code; it is free and needs no account.
Does the HC-SR04 work with the Arduino Mega?
Yes, directly: it is a 5 V module and the Arduino Mega is a 5 V board.
Why do I get 0 or "Out of range"?
pulseIn() timed out. Either nothing is within 4 meters, TRIG and ECHO are swapped, or the module has no power. A soft or angled surface can also scatter the sound so no echo returns.
Should I use the NewPing library?
You can; NewPing.h compiles here too. This page uses pulseIn() because it shows what the module actually does, and it needs no library at all.
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.