Built-in project · Arduino Uno R3

Ultrasonic sensor on 8 and 7, the LED on 13 when out of range, on an Arduino Uno

An HC-SR04 with TRIG on pin 8 and ECHO on 7 prints the distance in centimeters; when nothing is between 0 and 200 cm the LED on pin 13 lights. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $33. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Triggering a sensor with a short pulse
  • Timing a pulse with pulseIn
  • Converting microseconds to centimeters
  • Using an LED as a warning
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

This HC-SR04 does its own timing. A 10 microsecond pulse on TRIG (pin 8) sends the sound. ECHO (pin 7) stays HIGH until it returns, at about 58 microseconds per centimeter. The sketch measures that time and divides by 58.

If the distance is 200 cm or more, or there was no echo, it prints "out of range" and lights the LED on pin 13. Otherwise it prints the distance in centimeters and turns the LED off. It measures ten times a second.

Parts list

3 parts, plus the jumper wires. They retail for about $33; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
Ultrasonic ranger pin GNDArduino Uno R3 pin GND
Ultrasonic ranger pin TRIGArduino Uno R3 pin 8
Ultrasonic ranger pin ECHOArduino Uno R3 pin 7
Ultrasonic ranger pin VCCArduino Uno R3 pin 5V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Ultrasonic distance: TRIG on 8, ECHO on 7.
//
//   HC-SR04 VCC -> 5V   TRIG -> pin 8   ECHO -> pin 7   GND -> GND
//
// A 10 microsecond pulse on TRIG sends the sound; ECHO stays HIGH until it
// comes back, about 58 microseconds a centimeter. Past 200 cm the sketch says
// "out of range" and lights pin 13.
//
// Drag the sensor's distance slider.

const int TRIG = 8;
const int ECHO = 7;
const int LED_PIN = 13;

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: HC-SR04 on pins 8 and 7");
}

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long cm = pulseIn(ECHO, HIGH, 30000UL) / 58;
  if (cm <= 0 || cm >= 200) {
    Serial.println("out of range");
    digitalWrite(LED_PIN, HIGH);
  } else {
    Serial.print(cm);
    Serial.println(" cm");
    digitalWrite(LED_PIN, LOW);
  }
  delay(100);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

Sets TRIG as an output, ECHO as an input and LED_PIN as an output. Then it starts serial at 9600 and prints a title.

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  pinMode(LED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Mokxi Uno: HC-SR04 on pins 8 and 7");
}

loop sketch.ino

Pulses TRIG low for 2 us, high for 10 us, then low. pulseIn(ECHO, HIGH, 30000UL) times the echo, with a 30 ms limit, and dividing by 58 gives centimeters. A result of 0 or less, or 200 or more, prints "out of range" and lights the LED. Otherwise it prints the distance.

void loop() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long cm = pulseIn(ECHO, HIGH, 30000UL) / 58;
  if (cm <= 0 || cm >= 200) {
    Serial.println("out of range");
    digitalWrite(LED_PIN, HIGH);
  } else {
    Serial.print(cm);
    Serial.println(" cm");
    digitalWrite(LED_PIN, LOW);
  }
  delay(100);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • Always "out of range" This project
    Check TRIG on pin 8 and ECHO on pin 7, and 5V on VCC.
  • The LED is on when something is close This project
    The sketch lights the LED when nothing is in range, not when something is near. That is how the code is written.
  • Distance reads zero, or never changes. Ultrasonic ranger
    Power it from 5 V and share ground. Send a 10 microsecond HIGH pulse on TRIG, then time ECHO with pulseIn. Check TRIG and ECHO are not swapped.
  • Readings jump around or are wrong on a 3.3 V board. Ultrasonic ranger
    ECHO outputs 5 V, so divide it down for 3.3 V boards. Aim at flat, hard surfaces. The range is about 2 cm to 4 m, and soft or angled targets bounce the sound away.
  • Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
    Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected.
  • Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
    Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.

Change it and keep it

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