Built-in project · Arduino Uno R3

Ultrasonic distance on an LCD (TRIG 2, ECHO 3), on an Arduino Uno

An HC-SR04 with TRIG on pin 2 and ECHO on pin 3 measures the distance in front of it, shown on a 16x2 LCD wired to pins 4, 6 and 10 to 13. The whole build, an Arduino Uno and 2 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 Runs in your browser. Free, and no account needed.

Ultrasonic distance on an LCD (TRIG 2, ECHO 3) · 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.

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
// Distance on an LCD: an HC-SR04 on pins 2 and 3.
//
//   HC-SR04 TRIG -> pin 2   ECHO -> pin 3   VCC -> 5V   GND -> GND
//   LCD RS -> pin 4    LCD E -> pin 6
//   LCD D4 -> pin 10   D5 -> pin 11   D6 -> pin 12   D7 -> pin 13
//   LCD RW, VSS, K -> GND     VDD, A -> 5V
//   LCD V0 (contrast) -> GND here; on the bench, the middle leg of a pot
//
// A 10 microsecond pulse on TRIG sends a burst of sound; ECHO then stays HIGH
// for as long as the sound takes to come back. pulseIn() measures that time,
// and sound covers a centimeter there and back in about 58 microseconds.
//
// Drag the distance slider on the sensor.

#include <LiquidCrystal.h>

const int TRIG = 2;
const int ECHO = 3;

LiquidCrystal lcd(4, 6, 10, 11, 12, 13);

long distanceCm() {
  digitalWrite(TRIG, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG, LOW);
  long echo = pulseIn(ECHO, HIGH, 30000UL);
  return echo / 58;
}

void setup() {
  pinMode(TRIG, OUTPUT);
  pinMode(ECHO, INPUT);
  lcd.begin(16, 2);
  lcd.print("Distance:");
  Serial.begin(9600);
  Serial.println("Mokxi Uno: HC-SR04 on pins 2 and 3, LCD on 4, 6 and 10 to 13");
}

void loop() {
  long cm = distanceCm();
  lcd.setCursor(0, 1);
  lcd.print("                ");
  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(500);
}

Parts list

4 parts, plus the jumper wires. Every one is in the editor's parts bin.

How it is wired

10 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Arduino Uno R3 pin 13; Character LCD, 16x2 pin D7
  • Arduino Uno R3 pin 12; Character LCD, 16x2 pin D6
  • Arduino Uno R3 pin 11; Character LCD, 16x2 pin D5
  • Arduino Uno R3 pin 10; Character LCD, 16x2 pin D4
  • Arduino Uno R3 pin 6; Character LCD, 16x2 pin E
  • Arduino Uno R3 pin 4; Character LCD, 16x2 pin RS
  • Arduino Uno R3 pin 3; Ultrasonic ranger pin ECHO
  • Arduino Uno R3 pin 2; Ultrasonic ranger pin TRIG
  • Arduino Uno R3 pin 5V; Character LCD, 16x2 pin VDD; Character LCD, 16x2 pin A; Ultrasonic ranger pin VCC
  • Ground: Arduino Uno R3 pin GND; Character LCD, 16x2 pin VSS; Character LCD, 16x2 pin V0; Character LCD, 16x2 pin RW; Character LCD, 16x2 pin K; Ultrasonic ranger pin 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.