Site Guide

Find your way around the Engineering Club.

Know what you need? Search for a component, concept or project.

Around the site

Start

Labs

TestingExperimental

Build

Club

LearnExperimental

Go a little deeper

Entry points to larger collections. Use Search for individual records.

Learning paths & reference

Inside PiCrawlerExperimental

Inside Inventory

← Mega Components

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

  1. Disconnect USB and use the module’s printed VCC/TRIG/ECHO/GND labels.
  2. Reconnect USB and aim the sensor at a broad flat object within its roughly 2–400 cm range.
Illustration © ELEGOO. All rights reserved.
Illustration © ELEGOO. All rights reserved. Open full-size ↗
Reviewed pin-to-pin connections
FromToPurpose / notes
Mega: 5VHC-SR04: VCC
Mega: GNDHC-SR04: GND
Mega: D12 / TRIGHC-SR04: TRIG
Mega: D11 / ECHOHC-SR04: ECHO
Open vector connection diagram ↗

Code

PlatformIO configuration
platformio.ini
[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.

main.cpp
#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.