Built-in project · Arduino Uno R3

Ultrasonic ranging on an LCD, on an Arduino Uno

Hold the button on A0 and an HC-SR04 (TRIG on pin 9, ECHO on 3) measures the distance in front of it, shown on a 16x2 LCD wired to pins 2, A3, 8, 7, A2 and 4. The whole build, an Arduino Uno and 3 more parts, 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.

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

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

Ultrasonic ranging on an LCD · Arduino Uno R3live0.000 s 0.00x
Hold the button on the breadboard
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

  • Measuring distance from an ultrasonic echo
  • A button read with INPUT_PULLUP
  • Using analog pins as ordinary digital pins
  • Passing any pin numbers to the LiquidCrystal library
  • Difficulty: Intermediate
  • Time: About 25 minutes
  • Board: Arduino Uno R3
  • Parts: 5

How it works

The HC-SR04 sends a burst of ultrasound from TRIG on pin 9 and times the echo coming back on pin 3. The SR04 library turns that time into centimeters.

The button on A0 uses the Uno's internal pull-up, so it reads low while held. While you hold it, the sketch measures every quarter second and writes the distance on the second line of the LCD. When you let go, the last reading stays.

A reading of zero is shown as out of range.

Parts list

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

Kits that can build it

These 5 starter kits have every part in the circuit. Each kit's page lists what else it can build.

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 TRIGArduino Uno R3 pin 9
Character LCD, 16x2 pin D4Arduino Uno R3 pin 8
Character LCD, 16x2 pin D5Arduino Uno R3 pin 7
Character LCD, 16x2 pin D7Arduino Uno R3 pin 4
Ultrasonic ranger pin ECHOArduino Uno R3 pin 3
Character LCD, 16x2 pin RSArduino Uno R3 pin 2
Character LCD, 16x2 pin VDDArduino Uno R3 pin 5V
Character LCD, 16x2 pin AArduino Uno R3 pin 5V
Ultrasonic ranger pin VCCArduino Uno R3 pin 5V
Character LCD, 16x2 pin VSSArduino Uno R3 pin GND
Character LCD, 16x2 pin V0Arduino Uno R3 pin GND
Character LCD, 16x2 pin RWArduino Uno R3 pin GND
Character LCD, 16x2 pin KArduino Uno R3 pin GND
Ultrasonic ranger pin GNDArduino Uno R3 pin GND
Pushbutton pin 2aArduino Uno R3 pin GND
Pushbutton pin 1aArduino Uno R3 pin A0
Character LCD, 16x2 pin D6Arduino Uno R3 pin A2
Character LCD, 16x2 pin EArduino Uno R3 pin A3

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 ranging: hold the button, read the distance on the LCD.
//
//   HC-SR04 TRIG -> pin 9    ECHO -> pin 3    VCC -> 5V    GND -> GND
//   LCD RS -> pin 2    E -> A3    D4 -> pin 8    D5 -> pin 7    D6 -> A2    D7 -> pin 4
//   button -> A0 and GND (INPUT_PULLUP)
//
// These are the pins of a ready-made sensor shield, which is why the LCD's six
// lines are scattered over digital and analog pins: A2 and A3 work as plain
// digital pins, and LiquidCrystal does not mind which pins it gets.
//
// While the button is held, the sketch measures four times a second and
// writes the distance on the second line. Let go and the last reading stays.

#include <LiquidCrystal.h>
#include "SR04.h"

const int TRIG_PIN = 9;
const int ECHO_PIN = 3;
const int BUTTON = A0;

LiquidCrystal lcd(2, A3, 8, 7, A2, 4);
SR04 sonar = SR04(ECHO_PIN, TRIG_PIN);

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Hold the button");
  Serial.begin(9600);
  Serial.println("Mokxi Uno: hold the button on A0 to measure");
}

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    long cm = sonar.Distance();
    lcd.clear();
    lcd.print("Distance:");
    lcd.setCursor(0, 1);
    if (cm == 0) {
      lcd.print("out of range");
    } else {
      lcd.print(cm);
      lcd.print(" cm");
    }
    Serial.print(cm);
    Serial.println(" cm");
    delay(250);
  }
}

Code walkthrough

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

setup sketch.ino

Sets the button pin to INPUT_PULLUP, starts the LCD at 16 columns and 2 rows, and shows Hold the button. It also opens serial at 9600 baud.

void setup() {
  pinMode(BUTTON, INPUT_PULLUP);
  lcd.begin(16, 2);
  lcd.print("Hold the button");
  Serial.begin(9600);
  Serial.println("Mokxi Uno: hold the button on A0 to measure");
}

loop sketch.ino

Does nothing unless the button reads LOW. When it does, it asks sonar.Distance() for centimeters, clears the screen, and prints the number, or out of range if it is 0. Then it waits 250 ms.

void loop() {
  if (digitalRead(BUTTON) == LOW) {
    long cm = sonar.Distance();
    lcd.clear();
    lcd.print("Distance:");
    lcd.setCursor(0, 1);
    if (cm == 0) {
      lcd.print("out of range");
    } else {
      lcd.print(cm);
      lcd.print(" cm");
    }
    Serial.print(cm);
    Serial.println(" cm");
    delay(250);
  }
}

Troubleshooting

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

  • The LCD says Hold the button and never changes This project
    Check the button goes between A0 and ground.
  • The display is scrambled or blank This project
    The LCD uses scattered pins: RS on 2, E on A3, D4 on 8, D5 on 7, D6 on A2 and D7 on 4. Check each against LiquidCrystal lcd(...).
  • It always says out of range This project
    Check TRIG goes to pin 9 and ECHO to pin 3, and that nothing is too close or too far from the sensor.
  • The screen lights up but shows only a row of solid boxes, or nothing at all. Character LCD
    Contrast pin V0 needs a voltage. Connect the middle pin of a 10 k pot to V0, with its ends on 5 V and GND. Also confirm that the code calls lcd.begin(16, 2).
  • Garbled characters or a blank display even with good contrast. Character LCD
    In 4-bit mode only D4 to D7 are used, and D0 to D3 stay unconnected. Tie RW to GND. Match the pins in LiquidCrystal(rs, en, d4, d5, d6, d7) exactly to your wiring.
  • 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.
  • The input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.

Change it and keep it

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