Experiment 31
Experiment 31
Aim: Interface Ultrasonic sensor with Arduino and display the result on serial monitor and
LCD display.
Apparatus Required: Arduino uno board, Ultrasonic sensor, serial monitor, LCD display,
Proteus software package, Arduino software, jumper wires, resistor, Breadboard.
Serial Monitor:
Arduino uno board has one serial port at digital pins 0(RX) and 1(TX) to communicate with
other external serial devices or with computer through USB cable. The process of sending and
receiving data can be observed by flashing of TX and RX LEDs on the Arduino board.
Functions Used for Serial Communication
Serial.begin (baud rate)
This function is used to initiate the serial communication with baud rate.
e.g. Serial.begin(9600)
Serial.print (data, format type (optional))
It sends ASCII character on serial port so human can read it easily. This function converts the data to
ascii character and then send (print) it.
e.g. Serial.print (123);
Serial.print (“123”);
Serial.print (12, HEX)
Serial.write()
This function writes binary data to the serial port. It returns number of byte written.
e.g. Serial.write (65); // it writes actual byte to serial port
Serial.read()
This function is used to read data serially.
e.g. received_data = Serial.read()
Serial port Interfacing:
void setup()
Serial.begin(9600);
void loop()
{
delay(1000);
Ultrasonic Sensor:
An ultrasonic sensor is an electronic device that measures the distance of a target object by emitting
ultrasonic sound waves, and converts the reflected sound into an electrical signal.
It consists of two main parts transmitter and receiver. Ultrasonic sensors emit a chirp usually between
23 kHz and 40 kHz, much higher than the typical audible range of human hearing at 20 kHz, hence the
term ultrasonic. Using this chirp, they measure the amount of time it takes for the sound to bounce
off an object. This is based on the same basic principles of echolocation used by bats to find their prey.
As the speed of sound in air at room temperature is 343 meters per second, that time can be easily
converted to distance, remembering that the ultrasonic chirp travels both to and from the object being
sensed.
Schematic diagram
Sketch:
#include <LiquidCrystal.h>
//#include <SoftwareSerial.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7); //LCD : (rs, enable, d4, d5, d6, d7)
//SoftwareSerial mySerial(2, 3); // RX, TX
void setup()
{
Serial.begin(9600); // Starting Serial Terminal
//mySerial.begin(9600);
lcd.begin(16,2);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
lcd.setCursor(0,0);
lcd.print(" Distance ");
Serial.print(" Distance ");
lcd.setCursor(0,1);
lcd.print(" Measurement ");
Serial.print(" Measurement ");
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Made By : ");
delay(1000);
Serial.print("Made By :");
lcd.setCursor(0,0);
lcd.print("OverAllTechnical");
lcd.setCursor(0,1);
lcd.print("Gyan");
void loop()
{
long duration, inches, cm;
pinMode(trigPin, OUTPUT);
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
// Serial.print(inches);
// Serial.print("in, ");
Serial.print("Distance:");
Serial.print(cm);
Serial.print("cm");
delay(100);
Serial.println();
lcd.setCursor(0,0);
lcd.print("");
delay(10);
lcd.setCursor(0,1);
lcd.print("Distance:");
lcd.print(cm);
lcd.print("cm");
delay(100);
// mySerial.println(cm);
// mySerial.println("cm");
// mySerial.println();
//
// delay(100);
}
return microseconds / 29 / 2;