Reversing radar: ultrasonic, buzzer and I2C LCD, on an Arduino Uno
An HC-SR04 (TRIG 8, ECHO 7) measures the gap behind the car; an active buzzer on pin 9 beeps faster as it closes, and an I2C LCD on A4 and A5 shows the distance. 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.
The parts retail for about $37. On Mokxi they're free.
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// A reversing radar: the closer the wall, the faster the beeps.
//
// HC-SR04 VCC -> 5V TRIG -> pin 8 ECHO -> pin 7 GND -> GND
// active buzzer + -> pin 9, - -> GND
// I2C LCD GND -> GND VCC -> 5V SDA -> A4 SCL -> A5
//
// The beep is timed with millis(), not delay(), so the sensor keeps being
// read between beeps: under 5 cm it beeps every 0.1 s, under 10 cm every
// 0.5 s, under 20 cm every second, and further away every two seconds.
//
// Drag the distance slider on the sensor.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
const int TRIG = 8;
const int ECHO = 7;
const int BUZZER = 9;
LiquidCrystal_I2C lcd(0x27, 16, 2);
unsigned long lastBeep = 0;
long distanceCm() {
digitalWrite(TRIG, LOW);
delayMicroseconds(2);
digitalWrite(TRIG, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG, LOW);
long echo = pulseIn(ECHO, HIGH, 30000UL);
return echo / 58;
}
unsigned long intervalFor(long cm) {
if (cm > 0 && cm < 5) return 100;
if (cm > 0 && cm < 10) return 500;
if (cm > 0 && cm < 20) return 1000;
return 2000;
}
void setup() {
pinMode(TRIG, OUTPUT);
pinMode(ECHO, INPUT);
pinMode(BUZZER, OUTPUT);
lcd.init();
lcd.backlight();
lcd.print("Reverse radar");
Serial.begin(9600);
Serial.println("Mokxi Uno: reversing radar, sensor on 8 and 7, buzzer on 9");
}
void loop() {
long cm = distanceCm();
if (millis() - lastBeep >= intervalFor(cm)) {
lastBeep = millis();
digitalWrite(BUZZER, HIGH);
delay(50);
digitalWrite(BUZZER, LOW);
}
lcd.setCursor(0, 1);
lcd.print("Dist: ");
lcd.print(cm);
lcd.print(" cm ");
delay(50);
}
Parts list
5 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.
How it is wired
7 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; Buzzer pin 1
- Arduino Uno R3 pin 8; Ultrasonic ranger pin TRIG
- Arduino Uno R3 pin 7; Ultrasonic ranger pin ECHO
- Arduino Uno R3 pin 5V; Ultrasonic ranger pin VCC; Character LCD with I2C backpack pin VCC
- Ground: Arduino Uno R3 pin GND; Ultrasonic ranger pin GND; Buzzer pin 2; Character LCD with I2C backpack pin GND
- Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
- Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.