Introduction: Arduino Distance Display and Alarm

About: I am Jack of all trades and master of some. Interested in Electronics though I am a chemical engineer by profession. Do practice Yoga and meditation. Likes to make new friends. Has so many hobbies that its dif…

This is my first tutorial in Instructables.com, therefore if you find any error or if if the code can be improved please suggest. This project is inspired by Car Vehicle Backup Monitor Parking Assistance System. This will display the distance as well as raise an alarm if it detects any obstacle within 50cm. As a neophyte I took help from some websites to write the code. I have also changed some libraries to suit this project.

Instruments Required:

  • Arduino UNO R3
  • HC SR04 sensor
  • 16 x 2 LCD Display
  • I2C Bus
  • Breadboard
  • Buzzer
  • Jumper Wires

Libraries Needed:

All the libraries needed can be found at

https://brainy-bits.com/tutorials/connect-a-charac...

Only difference is this uses 20 x4 LCD and does not use a Buzzer. but the same libraries are used in my project.

The only change in NewPing library will be to comment out these lines in NewPing.cpp file

#if defined (__AVR_ATmega32U4__) // Use Timer4 for ATmega32U4 (Teensy/Leonardo).
ISR(TIMER4_OVF_vect) { #else ISR(TIMER2_COMPA_vect) { #endif if(intFunc) intFunc(); // If wrapped function is set, call it. }

If you don't comment out these lines then you may get an Verbose_7 error during compilation. To avoid conflict in libraries its better to change these lines.

Step 1:

The sketch of this project is given here. It has to be connected the way it is shown in the image.

Connectivity of I2C Bus

  • VCC - To the 5v Power supply in Breadboard
  • TRIG - Pin 12 of Arduino
  • ECHO - Pin 11 of Arduino
  • GND - To GND in Breadboard

Connectivity of Buzzer

To Pin 6 and 7 of Arduino

Connectivity of I2C

  • GND - GND in Breadboard
  • VCC - To the 5v Power supply in Breadboard
  • SDA - Connected to Pin A4 in Arduino
  • SCL - Connected to Pin A5 in Arduino

Connectivity of LCD 16 x 2

As shown in the sketch 1:1 connected to I2C bus

Arduino is to be connected to PC/Laptop through a USB port.

Next step is to find out it’s HEX address of I2C module so we can communicate with it.

Step 2:

The code to get the Hex Address can be found from the link given here

https://brainy-bits.com/tutorials/connect-a-charac...

After that we need to use that code in the main code here

#include <Wire.h><br>#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <NewPing.h>
#define I2C_ADDR 0x27 //  Add your address here.
#define Rs_pin 0
#define Rw_pin 1
#define En_pin 2
#define BACKLIGHT_PIN 3
#define D4_pin 4
#define D5_pin 5
#define D6_pin 6
#define D7_pin 7
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define MAX_DISTANCE 500 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.<br>LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);
void setup()
{
  
pinMode(7,OUTPUT);
pinMode(6,OUTPUT);
lcd.begin (16,2); // <<-- our LCD is a 20x4, change for your LCD if needed
// LCD Backlight ON
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.home (); // go home on LCD
lcd.print("Range Finder"); 
}
void loop()
{
  
 digitalWrite(TRIGGER_PIN, HIGH);
 delayMicroseconds(1000);
 digitalWrite(TRIGGER_PIN,LOW);
  
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
unsigned int dist = sonar.convert_cm(uS); // Convert into centimeters
 if(dist>=200 || dist<=0){
    lcd.clear();
    lcd.setCursor (0,1); // go to start of 2nd line
    lcd.print("Distance:");
    lcd.setCursor (0,3); // go to start of 4th line
    lcd.print("Ping: ");
    lcd.print("Out of Range");
  }else if(dist >= 0 && dist <=50){
    lcd.clear();
    beepFast();
    lcd.setCursor (0,1); // go to start of 2nd line
    lcd.print("Distance:");
    lcd.setCursor (0,3); // go to start of 4th line
    lcd.print("Ping: ");
    lcd.print(dist);
    lcd.print(" cm ");
  }
  else{
    lcd.clear();
    Silent();
    lcd.print(dist);
    lcd.print(" cm ");
  }
delay(1000);
}
void beepFast(){
tone(8,440,200);
digitalWrite(7,HIGH);
delay(50);
noTone(8);
digitalWrite(7,LOW);
delay(300);
}
void Silent(){
  noTone(8);
  digitalWrite(6,HIGH);
  delay(300);
  digitalWrite(6,LOW);
}
Digital Life 101 Challenge

Participated in the
Digital Life 101 Challenge

Brave the Elements Contest

Participated in the
Brave the Elements Contest