Sunday, March 14, 2021

Garage Parking Helper - ESP32 and Ultrasonic Distance Measuring Device

Looking through my box of miscellaneous electronics, I noticed that I had one of those Ultrasonic Measuring devices that typically come with Arduino kits.  It was probably banging around in one of my overflow boxes labeled "Keep until 2025 then garbage".   Just kidding - I would never throw a good piece of electronics away! (maybe...)

Trying to think about what I could possibly use this for, I go out to the garage to get something out of my car, which requires me to navigate around my wife's vehicle, which continues to be a challenge since it is always parked way too close to the front wall.  My attempts at placing a tennis ball on a string were futile, as my wife eventually just started treating it like a game - seeing how fast she could hit the tennis ball with the windshield as she pulled into the garage.  So much for low-tech parking assist!

So, I started to think - maybe I can fix this situation.... maybe I can use an ESP32 and strategically place the HC-SR04 so that it will count out the distance on a LCD screen!  Besides, I have no less than a dozen different LCD displays.  Nah - the screen would be way to small no matter where I put it.  Plus, trying to remember the distance, might be tough - especially being gone for a while.

How about use an ESP32 with a red/green LED and hard code the distance in the chip, and just pull forward until the green light turns red!  <-- I think that's the ticket!

This was one of my Sunday morning quick projects, so I decided to breadboard it and drop some code into the Arduino IDE (yeah, I use the IDE to program my ESP32 chips).  Only an ESP32, a HC-SR04, and I placed a resistor on the ground side of the LED.

Here is a diagram of what I ended up doing (forgot the resistor in this drawing):



You will see that if you rotate the HC-SR04 by 180 degrees, you can line up the Ground, Trigger and Echo pins... I created a jumper to the 3.3v pin.  Note: There are "5v only" HC-SR04 units out there, so if you aren't getting any indication, you might have to power the HC-SR04 with the 5v pin and use a voltage divider set of resistors from the echo pin so you don's smoke the ESP32.  A keen eye in the photo below will show that I ended up having to do this with mine - my vcc pin goes to the 5v ESP32 pin, and have some resistors on the bottom to bring the echo pin down to 3v.

I used a proto board I had from a surplus box of equipment a friend of mine gave me - Soldered it all together using the ugly solder bridging method, and it turned out great!


Here is my code - for those that are interested.  I pulled some ideas from various places and came up with the distance algorithm, which is really just arbitrary.  I just figured out what distance I wanted the car to be at while watching the output on the serial monitor, and placed it in variable parkingDist:

const int trigPin = 14;

const int echoPin = 5;

const int redPin = 21;

const int greenPin = 19;

const int parkingDist = 100;

long duration;

int distance;


void setup() {

   pinMode(trigPin, OUTPUT);

   pinMode(echoPin, INPUT);

   pinMode(redPin, OUTPUT);

   pinMode(greenPin, OUTPUT);

   Serial.begin(115200); 

}


void loop() {

   digitalWrite(trigPin, LOW);

   delayMicroseconds(2);

   digitalWrite(trigPin, HIGH);

   delayMicroseconds(10);

   digitalWrite(trigPin, LOW);

   duration = pulseIn(echoPin, HIGH);

   distance= duration*0.017;

   Serial.print("Distance: ");

   Serial.println(distance);

   if (distance<parkingDist) {

    digitalWrite(greenPin,HIGH);

    digitalWrite(redPin,LOW);

   }

   if (distance>=parkingDist) {

    digitalWrite(greenPin,LOW);

    digitalWrite(redPin,HIGH);

   }

   delay(100);

}

Now its time to design and print an enclosure!