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 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.
What you'll learn
- Time of flight: measuring distance with sound
- Reading a pulse length with
pulseIn - Wiring a 16x2 LCD in 4-bit mode
- Handling a missing echo
- Difficulty: Intermediate
- Time: About 25 minutes
- Board: Arduino Uno R3
- Parts: 4
How it works
A 10 microsecond pulse on TRIG, pin 2, makes the HC-SR04 send a burst of sound. ECHO, pin 3, then stays high for as long as the sound takes to come back. Sound covers a centimeter there and back in about 58 microseconds.
The sketch times that pulse, divides by 58, and writes the distance on the second line of the LCD. It repeats every half second and also prints to serial.
If no echo comes back, the reading is 0 and the LCD says out of range.
Parts list
4 parts, plus the jumper wires. They retail for about $38; on Mokxi they're free.
- 1 × Arduino Uno R3In Starter Kit, Claw Machine Kit, Vending Machine Kit.
- 1 × Full-size breadboard
- 1 × Character LCD, 16x2
- 1 × Ultrasonic rangerIn Halloween Prop Kit, Robot Car Kit, Parking Assistant Kit.
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 pin | Connects to |
|---|---|
| Character LCD, 16x2 pin D7 | Arduino Uno R3 pin 13 |
| Character LCD, 16x2 pin D6 | Arduino Uno R3 pin 12 |
| Character LCD, 16x2 pin D5 | Arduino Uno R3 pin 11 |
| Character LCD, 16x2 pin D4 | Arduino Uno R3 pin 10 |
| Character LCD, 16x2 pin E | Arduino Uno R3 pin 6 |
| Character LCD, 16x2 pin RS | Arduino Uno R3 pin 4 |
| Ultrasonic ranger pin ECHO | Arduino Uno R3 pin 3 |
| Ultrasonic ranger pin TRIG | Arduino Uno R3 pin 2 |
| Character LCD, 16x2 pin VDD | Arduino Uno R3 pin 5V |
| Character LCD, 16x2 pin A | Arduino Uno R3 pin 5V |
| Ultrasonic ranger pin VCC | Arduino Uno R3 pin 5V |
| Character LCD, 16x2 pin VSS | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin V0 | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin RW | Arduino Uno R3 pin GND |
| Character LCD, 16x2 pin K | Arduino Uno R3 pin GND |
| Ultrasonic ranger pin GND | Arduino Uno R3 pin GND |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// 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);
}
Code walkthrough
The key functions of the sketch, and what each one does.
distanceCm sketch.ino
Sends the trigger pulse: low for 2 microseconds, high for 10, then low. It then uses pulseIn to time how long ECHO stays high, giving up after 30,000 microseconds. Dividing by 58 turns that time into centimeters.
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long echo = pulseIn(ECHO, HIGH, 30000UL);
return echo / 58;
}setup sketch.ino
Makes TRIG an output and ECHO an input, starts the LCD at 16 columns and 2 rows, prints Distance: on the first line, and opens serial at 9600 baud.
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");
}loop sketch.ino
Measures, clears the second line by printing spaces, then writes the number and cm, or out of range when the reading is 0. It prints the same to serial and waits half a second.
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);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- It always says out of range This project
Check TRIG goes to pin 2 and ECHO to pin 3, and the sensor has 5V and ground. - The LCD is blank or shows blocks This project
Check RS on pin 4, E on pin 6 and D4 to D7 on pins 10 to 13. V0 and RW go to ground. - 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. - 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.