Project showcase
Display Ultrasonic Sensor and Servo

Display Ultrasonic Sensor and Servo © GPL3+

An ultrasonic sensor rotating on a servo and displaying values.

  • 3,736 views
  • 0 comments
  • 5 respects

Components and supplies

Apps and online services

About this project

A proof of concept to learn Arduino. The LCD displays the position of the servo and the calculated distance that the ultrasonic sensor sees.

Code

Untitled fileArduino
#include<LiquidCrystal.h>
#include<Servo.h>

int trigPin=2;
int echoPin=3;
int distance;

 const int rs = 12, en = 11, d4 = 7, d5 = 6, d6 = 5, d7 = 4;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

long duration;
Servo servo;

void setup()
{
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  Serial.begin(9600);
  servo.attach(9);
  lcd.begin(16, 2);
}

void loop()
{
  for(int i=15;i<=165;i++)
  {
    servo.write(i);
    delay(200);
    printOut();
  }
  for(int i=165;i>15;i--)
  {
    servo.write(i);
    delay(200);  
   printOut(); 
  }
}

int calculateDistance()
{
  digitalWrite(trigPin,LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin,HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin,LOW);
  
  duration=pulseIn(echoPin,HIGH);
  distance=duration*0.034/2;
  return distance;
}  

void printOut()
 {
  lcd.clear();
    lcd.setCursor(0, 0); 
    lcd.print("Servo Pos: ");
    lcd.print(servo.read());
    lcd.setCursor(0, 1); 
    lcd.print("Distance: ");
    lcd.print(calculateDistance());
 }

Schematics

arduino_ultrasonic_lcd_bb_sU88VPdaWo.jpg
Arduino ultrasonic lcd bb su88vpdawo

Comments

Similar projects you might like

Simple Ultrasonic Distance Measurer With LCD Display

Project showcase by onatto22

  • 11,961 views
  • 4 comments
  • 33 respects

Ultrasonic Sensor with LCD Display Using 433MHz

Project tutorial by mcharrison500

  • 10,406 views
  • 1 comment
  • 24 respects

LCD display distance of an ultrasonic sensor

Project tutorial by adam johns

  • 7,571 views
  • 0 comments
  • 8 respects

Room temperature sensor with display on Mobile

Project showcase by Kaustubh Agarwal

  • 29,469 views
  • 9 comments
  • 63 respects

DHT11 Humidity + Temperature Sensor with 16x2 LCD display

Project showcase by onatto22

  • 25,279 views
  • 6 comments
  • 42 respects
Add projectSign up / Login