Built-in project · Arduino Uno R3

Ultrasonic distance on pins 12 and 11, on an Arduino Uno

An HC-SR04 with TRIG on pin 12 and ECHO on pin 11: the SR04 library times the echo and prints the distance in centimeters once a second. 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.

Ultrasonic distance on pins 12 and 11 · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • How ultrasonic ranging works
  • Converting echo time to distance
  • Using a library for a sensor
  • Handling a reading of zero
  • Difficulty: Beginner
  • Time: About 10 minutes
  • Board: Arduino Uno R3
  • Parts: 3

How it works

The HC-SR04 sends eight pulses of 40 kHz sound when TRIG (pin 12) goes high for 10 microseconds. Then it holds ECHO (pin 11) high until the sound comes back. Sound travels about 343 meters a second, so every 58 microseconds of echo is one centimeter there and back.

The SR04 library does the timing and the sum. Once a second the sketch asks for the distance and prints it in centimeters. A result of 0 means no echo came back, so it prints "nothing in range". Drag the slider on the sensor to move the obstacle.

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 12
Ultrasonic ranger pin ECHOArduino Uno R3 pin 11
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: an HC-SR04 with TRIG on pin 12 and ECHO on pin 11.
//
//   HC-SR04 VCC  -> 5V       HC-SR04 TRIG -> pin 12
//   HC-SR04 ECHO -> pin 11   HC-SR04 GND  -> GND
//
// The sensor sends eight pulses of 40 kHz sound when TRIG goes high for 10
// microseconds, then holds ECHO high until the sound comes back. Sound travels
// about 343 meters a second, so every 58 microseconds of echo is one
// centimeter there and back. The SR04 library does the timing and the sum.
//
// Note the order in the constructor: echo first, then trigger.
//
// Drag the slider on the sensor to move the obstacle nearer or farther.

#include "SR04.h"

const int TRIG_PIN = 12;
const int ECHO_PIN = 11;

SR04 sonar = SR04(ECHO_PIN, TRIG_PIN);

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: HC-SR04 on pins 12 (TRIG) and 11 (ECHO)");
}

void loop() {
  long cm = sonar.Distance();
  if (cm == 0) {
    Serial.println("nothing in range");
  } else {
    Serial.print(cm);
    Serial.println(" cm");
  }
  delay(1000);
}

Code walkthrough

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

setup sketch.ino

Starts serial at 9600 and prints which pins the sensor uses. The sensor object was made at the top: SR04(ECHO_PIN, TRIG_PIN). Notice the order: echo comes first, then trigger.

void setup() {
  Serial.begin(9600);
  Serial.println("Mokxi Uno: HC-SR04 on pins 12 (TRIG) and 11 (ECHO)");
}

loop sketch.ino

Calls sonar.Distance() to get the distance in centimeters as a long. If it is 0, it prints "nothing in range". Otherwise it prints the number and " cm". Then it waits a second.

void loop() {
  long cm = sonar.Distance();
  if (cm == 0) {
    Serial.println("nothing in range");
  } else {
    Serial.print(cm);
    Serial.println(" cm");
  }
  delay(1000);
}

Troubleshooting

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

  • It always says "nothing in range" This project
    TRIG must go to pin 12 and ECHO to pin 11. The wires swapped gives no echo.
  • The pins look right but the library does not work This project
    In SR04(ECHO_PIN, TRIG_PIN) the echo pin comes first. Keep that order.
  • 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.