Maverick Den STEM Robotics Pvt. Ltd.
Email: info@maverickden.com
Contact No. : 7715916683 (India)
60123250147 (Malaysia)

Start Your Career with STEM Based Robotics (In 6/12 Weeks Internship)

ULTRASONIC SENSOR PROXIMITY

In this tutorial we will show you how Ultra Sensor Works  with Arduino UNO / Nano.

COMPONENTS USED :

SR NO COMPONENT NAME QUANTITY
1 Arduino Board(Uno or Nano) 1
2 BreadBoard 1
3 Resistor(220 ohm) 9
4 Ultrasonic Sensor 1
5 Jumper Cbles(male to Female) 1 set
6 LEDs (5mm) 9
7 16X2 LCD 1

 

Schematic Diagram :

Source Code:

#define trigPin 8

#define echoPin 7

#define led 13

#define led2 12

#define led3 11

#define led4 10

#define led5 9

#define led6 6

#define led7 5

#define led8 4

#define led9 2

#define buzzer 3

int sound = 230;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(led5, OUTPUT);

pinMode(led6, OUTPUT);

pinMode(led7, OUTPUT);

pinMode(led8, OUTPUT);

pinMode(led9, OUTPUT);

pinMode(buzzer, OUTPUT);

}

void lop() {

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(5);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance <= 45) {

digitalWrite(led, HIGH);

sound = 230;

}

else {

digitalWrite(led,LOW);

}

if (distance < 40) {

digitalWrite(led2, HIGH);

sound = 240;

}

else {

digitalWrite(led2, LOW);

}

if (distance < 35) {

digitalWrite(led3, HIGH);

sound = 250;

}

else {

digitalWrite(led3, LOW);

}

if (distance < 30) {

digitalWrite(led4, HIGH);

sound = 260;

}

else {

digitalWrite(led4,LOW);

}

if (distance < 25) {

digitalWrite(led5, HIGH);

sound = 270;

}

else {

digitalWrite(led5,LOW);

}

if (distance < 20) {

digitalWrite(led6, HIGH);

sound = 280;

}

else {

digitalWrite(led6,LOW);

}

if (distance < 15) {

digitalWrite(led7, HIGH);

sound = 290;

}

else {

digitalWrite(led7,LOW);

}

if (distance < 10) {

digitalWrite(led8, HIGH);

sound = 300;

}

else {

digitalWrite(led8,LOW);

}

if (distance < 5) {

digitalWrite(led9, HIGH);

sound = 310;

}

else {

digitalWrite(led9,LOW);

}

if (distance > 45 || distance <= 0){

Serial.println(“Out of range”);

noTone(buzzer);

}

else {

Serial.print(distance);

Serial.println(” cm”);

tone(buzzer, sound);

}

delay(500);

}

Video: