Obstacle Detection System using Ultrasonic Sensor

Obstacle Detection System using Ultrasonic Sensor

1024 576 Athul Sanker

Ultrasonic sensors are used for distance measuring applications. These gadgets regularly transmit a short burst of ultrasonic sound to a target, which reflects the sound back to the sensor. The system then measures the time for the echo to return to the sensor and computes the distance to the target using the speed of sound within the medium.

In this project, you will build a Obstacle detection System  using the ‘Learn to Solder’ board.

Additional Components Used:

  1. Itty Bitty --- 1 no.
  2. USB B Type Cable --- 1 no.
  3. 5v Regulator Breakout -- 1 no.
  4. Power Adapter) --- 1 no.
  5. Red 8” Male-Male Jumper Wire --- 1 no.
  6. Black 8” Male-Male Jumper Wire --- 1 no.
  7. 4-3A Cable --- 1 no.
  8. Ultrasonic Sensor --- 1 no.

 

Procedure:

  1. Upload the code to the ATmega328p  IC, as mentioned earlier using the itty bitty.
  2. Mount the  ATmega328p  IC on Learn to Solder board.
  3. Connect the Jumper wires to one end of the  power regulator breakout, Ensure Red wire is connected to the ‘+’ & Black to  ‘-’.
  4. Similarly connect the other end to the wires to the power port on Learn to Solder board.
  5. Connect the ultrasonic sensor to the I2C port using a 4-4A interface cable such that the ground pin is connected to the black wire on both the ends and the data pin is connected to the SCL Pin and SDA Pins.
  6. Connect the DC barrel pin of the power adapter to regulator board & Power On the Adapter.
  7. The board will start functioning.

 

Code:

#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
const int trigPin = A5;
const int echoPin = A4;
Adafruit_PCD8544 display= Adafruit_PCD8544(8,9,10,12,11);
#define number_of_74hc595s 3
#define numOfRegisterPins number_of_74hc595s*8
boolean registers[numOfRegisterPins];
const byte logo_el[] PROGMEM = {16,10,
B00000000,B00000000,
B11111010,B00000000,
B11111010,B00000000,
B00000010,B00000000,
B11111010,B00000000,
B11111010,B00000000,
B00000010,B00000000,
B11111011,B11000000,
B11111011,B11000000,
B00000000,B00000000,
};
int RCLK_Pin=6;
int SRCLK_Pin = 5;
int SER_Pin=A1;
void setup() 
{
   pinMode(2,OUTPUT);
   pinMode(SER_Pin,OUTPUT);
   pinMode(RCLK_Pin,OUTPUT);
   pinMode(SRCLK_Pin,OUTPUT);
   clearRegisters();
   writeRegisters();
   display.begin();
   display.setContrast(50);
   display.clearDisplay();
   pinMode(A4,INPUT);
   pinMode(A5,INPUT);
   display_El_logo();
   display_L2S();
   display_title();
   display.drawBitmap(70,35,logo_el,10,10,BLACK);
   display.drawRect(0,0,84,48,BLACK);  
}
  int count=0;
  long duration,cm;
  void loop()
{  
  display.drawBitmap(70,35,logo_el,10,10,BLACK);
  display.drawRect(0,0,84,48,BLACK);
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
  delay(100);
   int  b=digitalRead(13);
   if(b==1){
   display.clearDisplay();      
   int b=digitalRead(13);
   while(1){
            display.drawBitmap(70,35,logo_el,10,10,BLACK);  
            display.drawRect(0,0,84,48,BLACK);
            display.setCursor(6,6);
            display.println("SET DISTANCE:");
            if(digitalRead(13)==HIGH)
                  count++;
                  delay(300);
                  display.setCursor(20,15);
                  display.println(count);
            if(digitalRead(A0)==HIGH)
                  break;
                  display.display();
                  display.clearDisplay();
                  }    
  }
  else
  {
   display.drawBitmap(70,35,logo_el,10,10,BLACK);
   display.drawRect(0,0,84,48,BLACK);
   display.setCursor(2,2);
   display.println("DISTANCE : ");
   display.setCursor(15,15);
   display.print(cm); 
   display.display();
   display.clearDisplay();
   delay(100);
    } 
     if(cm<=9)
   {
   display.setCursor(22,15);
   display.println("cm");
   }
   else if(cm<=99)
   {
   display.setCursor(27,15);
   display.println("cm");
   }
   else if(cm>99)
   {
   display.setCursor(33,15);
   display.println("cm");
   }
    alert();
  }
long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}
void alert()
{
  if(count>=cm)
  {
    delay(100);
     digitalWrite(2,HIGH);
    delay(100);
     digitalWrite(2,LOW);
  }
  else
  {

    digitalWrite(2,LOW);
  }
}
 
void clearRegisters(){
  for(int i = numOfRegisterPins - 1; i >=  0; i--){
     registers[i] = LOW;
  }
  writeRegisters();
} 
void writeRegisters(){
  digitalWrite(RCLK_Pin, LOW);
  for(int i = numOfRegisterPins - 1; i >=  0; i--)
  {
    digitalWrite(SRCLK_Pin, LOW);
    int val = registers[i];
    digitalWrite(SER_Pin, val);
    digitalWrite(SRCLK_Pin, HIGH);
  }
    digitalWrite(RCLK_Pin, HIGH);
}  
//*****To Display EL*****//
void display_El_logo()
{
   display.fillRect(15,2,21,6,BLACK);
   display.fillRect(15,12,21,6,BLACK);
   display.fillRect(15,22,21,6,BLACK);
   display.fillRect(45,2,6,26,BLACK);
   display.fillRect(50,22,18,6,BLACK);
   display.setCursor(15,35);
   display.print("ElintLabz");
   display.display();
   delay(5000);
   display.clearDisplay();
}
//*****To Display L2S*****//
void display_L2S()
{
  display.drawRect(0, 0, 84 , 48,BLACK);
  display.setCursor(26, 10);
  display.println("LEARN");
  display.setCursor(38,20);
  display.println("2");
  display.setCursor(22,30);
  display.println("SOLDER");
  display.display(); 
  delay(5000);
  display.clearDisplay();
}
//*****To Display Tittle*****//
void display_title()
{
  display.drawRect(0, 0, 84 , 48,BLACK);
  display.setCursor(16,12);
  display.println("OBSTACLE");
  display.setCursor(13,22);
  display.println("DETECTION");
  display.setCursor(20,32);
  display.println("SYSTEM");
  display.drawBitmap(70,35,logo_el,10,10,BLACK);
  display.display(); 
  delay(5000);
  display.clearDisplay();
}

 

Usage Instructions:

  • The top left push button is used for setting distance .
  • The top right push button is used for saving the corresponding distance .

Leave a Reply