[Project 12] – Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino

In this project we will make  Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor an Arduino. I will use arduino uno in the circuit design program. I use usually arduino mega 2560.  You can use the other arduino cards. Good Luck.

[Project 12] - Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino 1

Materials:


Arduino UNO / Other Arduino Cards    x 1
Breadboard     x 1
Ultrasonic Sensor (HC-SR04)    x 1 
LED    x 10
220 ohm  Resistor    x 10
Jumper Cable

 

Program:

	
#define trigPin 3 
#define echoPin 2 
int led1 = 13;
int led2 = 12;
int led3 = 11;
int led4 = 10;
int led5 = 9;
int led6 = 8;
int led7 = 7;
int led8 = 6;
int led9 = 5;
int led10 = 4;
int maxDistance = 250; 
int minDistance = 2;  
long microSecond, cmDistance; 
int ledTime = 400;
void setup() {
 Serial.begin (9600); 
 pinMode(trigPin, OUTPUT); 
 pinMode(echoPin, INPUT); 
 pinMode(led1, 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(led10, OUTPUT);
 }
void loop() {
 digitalWrite(trigPin, LOW); 
 delayMicroseconds(2); 
 digitalWrite(trigPin, HIGH); 
 delayMicroseconds(10); 
 digitalWrite(trigPin, LOW); 
 microSecond = pulseIn(echoPin, HIGH); 
 cmDistance = microSecond/58.3; 
 if (cmDistance >= maxDistance) 
 {
 Serial.println("....>250cm");
 }
 else if (cmDistance == 5) 
 {
  Serial.println("5 cm");
  digitalWrite(led1, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
 }
 else if (cmDistance == 10) 
 {
  Serial.println("10 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
 }
 else if (cmDistance == 15) 
 {
  Serial.println("15 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
 }
 else if (cmDistance == 20)  
 {
  Serial.println("20 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
 
 }
 else if (cmDistance == 25) 
 {
  Serial.println("25 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
 }
  else if (cmDistance == 30) 
 {
  Serial.println("30 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
 }
 else if (cmDistance == 35) 
 {
  Serial.println("35 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
 }
 else if (cmDistance == 40)
 {
  Serial.println("40 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
 }
 else if (cmDistance == 45) 
 {
  Serial.println("45 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led9, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
 }
 else if (cmDistance == 50)
 {
  Serial.println("50 cm");
  digitalWrite(led1, HIGH);
  digitalWrite(led2, HIGH);
  digitalWrite(led3, HIGH);
  digitalWrite(led4, HIGH);
  digitalWrite(led5, HIGH);
  digitalWrite(led6, HIGH);
  digitalWrite(led7, HIGH);
  digitalWrite(led8, HIGH);
  digitalWrite(led9, HIGH);
  digitalWrite(led10, HIGH);
  delay(ledTime);
  digitalWrite(led1, LOW);
  digitalWrite(led2, LOW);
  digitalWrite(led3, LOW);
  digitalWrite(led4, LOW);
  digitalWrite(led5, LOW);
  digitalWrite(led6, LOW);
  digitalWrite(led7, LOW);
  digitalWrite(led8, LOW);
  digitalWrite(led9, LOW);
  digitalWrite(led10, LOW);
 }
 else if (cmDistance <= minDistance)
 {
  Serial.println("....< 2cm");   
 }
 else {
 Serial.print(cmDistance); 
 Serial.println(" cm"); 
 }
   delay(100);   
 }
 

Serial Monitor Image:


[Project 12] - Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino 2

Project Related Images:


[Project 12] - Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino 17[Project 12] - Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino 111[Project 12] - Ultrasonic Distance Sensor(HC-SR04) Using 10 LED and Serial Monitor + Arduino 154



 

You may also like...