Mike J McGuire
Published © GPL3+

Using An Arduino As A Garage Car Parking Sensor

A quick tutorial on how to create a garage parking sensor with lights and an ultrasonic range finder.

BeginnerFull instructions provided2 hours13,843
Using An Arduino As A Garage Car Parking Sensor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
PING))) Ultrasonic Distance Sensor
×1
LED (generic)
LED (generic)
×6

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Circuit Diagram

Code

Code snippet #1

Plain text
_ _ _ _ _ _ = Car greater than 3m from wall.
r _ _ _ _ _ = Car 3m from wall.
r r _ _ _ _ = Car 2m from wall.
r r r _ _ _ = Car 1.5m from wall.
r r r r _ _ = Car 1m from wall.
_ _ _ _ g _ = Car 45cm from wall.
_ _ _ _ _ r = Car 35cm from wall.

Code snippet #2

Plain text
/***************************************************************/
/*                                                             */
/* Garage Controller Range v2.0                                */
/*                                                             */
/***************************************************************/

#include <SPI.h>

#define SF(x) String(F(x))
#define CF(x) String(F(x)).c_str()
#define RANGE_MIN 35
#define RANGE_MAX 39
#define RANGE_1 49
#define RANGE_2 64
#define RANGE_3 84
#define RANGE_4 200

int iPinRed1 = 3;
int iPinRed2 = 4;
int iPinRed3 = 5;
int iPinRed4 = 6;
int iPinGreen1 = 7;
int iPinRed5 = 8;
int iPinRange = 9;

/***************************************************************/
/* Function: setup                                             */
/***************************************************************/
void setup()
{
	Serial.begin(9600);
	Serial.println(SF("setup()"));

	pinMode(iPinRed1, OUTPUT);
	pinMode(iPinRed2, OUTPUT);
	pinMode(iPinRed3, OUTPUT);
	pinMode(iPinRed4, OUTPUT);
	pinMode(iPinGreen1, OUTPUT);
	pinMode(iPinRed5, OUTPUT);
	pinMode(iPinRange, OUTPUT);
}

/***************************************************************/
/* Function: loop                                              */
/***************************************************************/
void loop()
{
	long lDuration = 0, lCM = 0;  

	// 2 Microsecond Clear + 5 Microsecond Pulse
	pinMode(iPinRange, OUTPUT);
	digitalWrite(iPinRange, LOW);
	delayMicroseconds(2);
	digitalWrite(iPinRange, HIGH);
	delayMicroseconds(5);
	digitalWrite(iPinRange, LOW);

	// Get Return Pulse
	pinMode(iPinRange, INPUT);
	lDuration = pulseIn(iPinRange, HIGH);
	lCM = ConvertMSToCM(lDuration);
	Serial.println("Distance: " + String(lCM) + " cm"); 

	// Too Close
	if (lCM < RANGE_MIN)
	{
		digitalWrite(iPinRed1, LOW);
		digitalWrite(iPinRed2, LOW);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, HIGH);
	}
	// Perfect
	else if (lCM >= RANGE_MIN & lCM < RANGE_MAX)
	{
		digitalWrite(iPinRed1, LOW);
		digitalWrite(iPinRed2, LOW);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, HIGH);
		digitalWrite(iPinRed5, LOW);
	}
	// Range 1
	else if (lCM >= RANGE_MAX & lCM < RANGE_1)
	{
		digitalWrite(iPinRed1, HIGH);
		digitalWrite(iPinRed2, HIGH);
		digitalWrite(iPinRed3, HIGH);
		digitalWrite(iPinRed4, HIGH);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}
	// Range 2
	else if (lCM >= RANGE_1 & lCM < RANGE_2)
	{
		digitalWrite(iPinRed1, HIGH);
		digitalWrite(iPinRed2, HIGH);
		digitalWrite(iPinRed3, HIGH);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}
	// Range 3
	else if (lCM >= RANGE_2 & lCM < RANGE_3)
	{
		digitalWrite(iPinRed1, HIGH);
		digitalWrite(iPinRed2, HIGH);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}
	// Range 4
	else if (lCM >= RANGE_3 & lCM < RANGE_4)
	{
		digitalWrite(iPinRed1, HIGH);
		digitalWrite(iPinRed2, LOW);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}
	// Range 5
	else if (lCM >= RANGE_4)
	{
		digitalWrite(iPinRed1, LOW);
		digitalWrite(iPinRed2, LOW);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}
	else
	{
		digitalWrite(iPinRed1, LOW);
		digitalWrite(iPinRed2, LOW);
		digitalWrite(iPinRed3, LOW);
		digitalWrite(iPinRed4, LOW);
		digitalWrite(iPinGreen1, LOW);
		digitalWrite(iPinRed5, LOW);
	}

	delay(50);
}

/***************************************************************/
/* Function: ConvertMSToCM                                     */
/***************************************************************/
long ConvertMSToCM(long lMicroSeconds)
{
	// The speed of sound is 340 m/s or 29 microseconds per centimeter.
	// The ping travels out and back, so to find the distance of the
	// object we take half of the distance travelled. - Arduino Site
	return lMicroSeconds / 29 / 2;
}

Credits

Mike J McGuire

Mike J McGuire

2 projects • 6 followers

Comments