ELEGOO MEGA · LESSON 10
Ultrasonic Sensor Module
Print the HC-SR04’s measured distance once per second.
Convert an echo pulse duration into distance.
Parts List
- ELEGOO Mega 2560 R3 is a 5 V ATmega2560 AVR board with many I/O pins and four hardware UARTs for control systems with several peripherals. Long ELEGOO-branded Mega PCB with USB-B and barrel connector, ATmega2560 marking, extended side headers and a large double-row digital header at the end. Read printed Mega labels; its SPI hardware placement is not the same numeric pins as an Uno.
- A USB-A to USB-B cable carries USB power and, when its data conductors are present, a host/device data link. USB-A plug and square USB-B plug. Inspect the plug shape; micro-USB is not USB-C, and USB-B is the squarer device connector.
- An HC-SR04-compatible module estimates distance by timing the round trip of an ultrasonic burst reflected from a target. Two round metal acoustic cans side by side on a narrow board, normally with VCC, Trig, Echo and GND labels. The can pair distinguishes it from reflective infrared sensors.
- Male-to-female jumper leads make removable point-to-point connections for low-current prototype signals. Pin at one end and socket at the other. Check intact insulation and snug contacts before use.
Assembly
- Disconnect USB and use the module’s printed VCC/TRIG/ECHO/GND labels.
- Reconnect USB and aim the sensor at a broad flat object within its roughly 2–400 cm range.

Reviewed pin-to-pin connections
| From | To | Purpose / notes |
|---|---|---|
| Mega: 5V | HC-SR04: VCC | |
| Mega: GND | HC-SR04: GND | |
| Mega: D12 / TRIG | HC-SR04: TRIG | |
| Mega: D11 / ECHO | HC-SR04: ECHO |
Code
PlatformIO configuration
[env:mega]
platform = atmelavr@5.3.0
board = megaatmega2560
framework = arduino
monitor_speed = 9600
Open PlatformIO Monitor at 9600 baud after Upload. Close Monitor before another Upload if the port is busy.
#include <Arduino.h>
// TRIG sends an ultrasonic pulse; ECHO receives a pulse whose width tells us the distance.
const uint8_t TRIG_PIN = 12;
const uint8_t ECHO_PIN = 11;
// Configure TRIG as an output, ECHO as an input, and Serial Monitor at 9600 baud.
void setup() {
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
// Start with TRIG LOW so the next HIGH pulse has a clean edge.
digitalWrite(TRIG_PIN, LOW);
// Serial lets us print measurements to the computer.
Serial.begin(9600);
delay(1000);
}
// Start a short trigger pulse, measure the echo, and report distance.
void loop() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
// Hold TRIG HIGH for 10 microseconds to start a measurement.
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// pulseIn() measures the ECHO HIGH time in microseconds; 30000UL is the timeout.
const unsigned long echoUs = pulseIn(ECHO_PIN, HIGH, 30000UL);
// If the timeout expires without a pulse, report no usable echo.
if (echoUs == 0) {
Serial.println("No echo within timeout");
} else {
// Divide the echo time by about 58 to estimate centimeters (out and back travel).
Serial.print(echoUs / 58UL);
Serial.println("cm");
}
delay(1000);
}
Expected behavior
The 9600-baud monitor prints distance in whole centimeters about once per second, or an explicit timeout when no echo is received.
Experiments & Challenges
- Move the target between two measured distances and compare readings.
Troubleshooting
- Timeout or unstable readings: check TRIG/ECHO are not swapped and use a broad target away from the minimum range.