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 Runs in your browser. Free, and no account needed.

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.

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);
  }
}

Parts list

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

How it is wired

11 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 9; Ultrasonic ranger pin TRIG
  • Arduino Uno R3 pin 8; Character LCD, 16x2 pin D4
  • Arduino Uno R3 pin 7; Character LCD, 16x2 pin D5
  • Arduino Uno R3 pin 4; Character LCD, 16x2 pin D7
  • Arduino Uno R3 pin 3; Ultrasonic ranger pin ECHO
  • Arduino Uno R3 pin 2; Character LCD, 16x2 pin RS
  • 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; Pushbutton pin 2a
  • Arduino Uno R3 pin A0; Pushbutton pin 1a
  • Arduino Uno R3 pin A2; Character LCD, 16x2 pin D6
  • Arduino Uno R3 pin A3; Character LCD, 16x2 pin E

Change it and keep it

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