SurtrTech
Published © GPL3+

Parking Radar Sensor

Use three HC-SR04 ultrasound sensors and two battery displays to show you how close the obstacles are and from which side.

BeginnerFull instructions provided1 hour27,790

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Ultrasonic Sensor - HC-SR04 (Generic)
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Mini Battery Displays
×1

Story

Read more

Schematics

Parking Sensor v1

Code

Parking_sensor_v1.ino

Arduino
/* This code is meant to work with HC-SR04 ultrasound modules x3 and TM1651 battery display x2
 * The project is a parking sensor that shows you how close the obstacles are and from which is
 * depending on the Maxdistance that above it the display will show as safe distance
 * below that it start changing
 * Refer to www.SurtrTech.com for more details
 */
#include <TM1651.h>

#define CLK 3      //Right display wiring
#define DIO 2

#define CLK2 5     //Left display wiring
#define DIO2 4

#define trigPin 6        //Ultrasound modules wiring R=Right L=Left the other one is the middle
#define echoPin 7
#define trigPinR 8
#define echoPinR 9
#define trigPinL 10
#define echoPinL 11
#define Maxdistance 20  //Maximum distance 20cm

long duration; 
int distance;

TM1651 DisplayR(CLK,DIO);      //Display instances
TM1651 DisplayL(CLK2,DIO2);

void setup() {
     pinMode(trigPin, OUTPUT);       //Pinmodes of the HC-SR04 and starting the displays
     pinMode(echoPin, INPUT);
     pinMode(trigPinR, OUTPUT);
     pinMode(echoPinR, INPUT); 
     pinMode(trigPinL, OUTPUT);
     pinMode(echoPinL, INPUT); 
     DisplayR.init();
     DisplayR.set(BRIGHTEST);
     DisplayL.init();
     DisplayL.set(BRIGHTEST);
}

void loop() {

    int distanceM = CDistance(trigPin,echoPin);      //Calculating all the distances by calling the function below
    int distanceR = CDistance(trigPinR,echoPinR);
    int distanceL = CDistance(trigPinL,echoPinL);

    int R=min(distanceM,distanceR);                //We have two displays R and L, first we take the minimum distance between M/R and M/L
    int L=min(distanceM,distanceL);

    if(R>0 && R < Maxdistance){                   //Range of distance
      short LevelR=map(R,0,Maxdistance,0,7);      //Scaling the disntace from 0-20 to 0-7
      DisplayR.displayLevel(LevelR);              // 0-7 are the levels of the display each distance between 0-20cm has a specified level you can change that 20
    }
    if(R> Maxdistance)                           //We're safe and the displays always shows there's enough space
    DisplayR.displayLevel(7);

    if(L>0 && L < Maxdistance){                 //Same as Right
      short LevelL=map(L,0,Maxdistance,0,7);
      DisplayL.displayLevel(LevelL);
    }
    if(L> Maxdistance)
    DisplayL.displayLevel(7);
    

     delay(50);
}

int CDistance(int a, int b){             //Calculating distance function, it takes two arguments (Trigger and Echo) and it executes the usual process of the HC-SR04
     digitalWrite(a, LOW); 
     delayMicroseconds(2);       
     digitalWrite(a, HIGH);
     delayMicroseconds(10);      
     digitalWrite(a, LOW); 
     duration = pulseIn(b, HIGH); 
     distance = duration*(0.034/2); 
     return distance;                  //Return the distance calculated
}

TM1651 Battery display library

The library makes the job so much easy

Credits

SurtrTech

SurtrTech

9 projects • 207 followers
YT Channel bit.ly/35Ai76l, run by Automation and Electrical Engineer, Electronics amateur, no IT background so you may see wreckage in codes

Comments