- The walls, and
- the car.
- I don’t want to have wires running all over the garage.
- I don’t want to use batteries
- I want to use minimal sensors
- I want to be simply informed when I’m in the right spot within a certain tunable range
- It should be cheap to build (well mostly)
- The sensor is a single distance sensor at the front of the parking area. LED lights can be close by.
- My outlets are at the front of the parking area, so I can use a power supply transformer.
- I found this: Sharp range sensor, so a single sensor could do it.
- 3 LEDs would do… too close (go back), just right (stop), too far (keep going).
- 3 LEDs, 1 Sharp range sensor, 1 220 ohm resistor, 1 5v transformer, 1 arduino (uno) => <$50
- yellow, too close, pin 2
- red, stop, pin 6
- green, keep going, pin 10

/*
Parking Assistant Using Range Sensor
Created by Mark Slemko - October 5, 2013
Parts taken from: http://arduino.cc/en/Tutorial/AnalogInput
*/
#include <math.h>
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 13; // select the pin for the LED
int ledStopPin = 6; // select the pin for the stop LED
int ledClosePin = 10;
int ledFarPin = 2;
int settleTime = 20000; // when same value is reached for this time, stop checking as often
int recheckTime = 0;
int timer = 0;
double settleDistance = 0.0;
double variance = 2.0;
#define STOP_DISTANCE 50.0
#define STOP_BRACKET 5.0
#define MAX_RECHECK_TIME 3000
#define SENSOR_ACTIVE 160.0
#define SENSOR_TOO_CLOSE 25.0
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
pinMode(ledStopPin, OUTPUT);
pinMode(ledClosePin, OUTPUT);
pinMode(ledFarPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
// read the value from the sensor:
double distance = 92.0 * pow((averageVoltage() + 0.30221), -1.5281);
Serial.print(distance);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(10);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
Serial.print(" wait ");
delay((int)distance);
timer += (int)distance + 50;
// check if settle time reached
if (checkSettleTimeReached(distance)) {
digitalWrite(ledStopPin, LOW); // turn off stop LED
Serial.print(" delay recheck ");
Serial.print(recheckTime);
delay(recheckTime); // wait for next check
if (recheckTime < settleTime) {
recheckTime = recheckTime * 2;
if (recheckTime > MAX_RECHECK_TIME) {
recheckTime = MAX_RECHECK_TIME;
}
}
timer = settleTime; // no overflow ...
// ensure low pins
digitalWrite(ledStopPin, LOW);
digitalWrite(ledClosePin, LOW);
digitalWrite(ledFarPin, LOW);
} else {
recheckTime = settleTime / 5;
digitalWrite(ledStopPin, LOW);
digitalWrite(ledClosePin, LOW);
digitalWrite(ledFarPin, LOW);
if (shouldShowStopLED(distance)) {
if (distance < (STOP_DISTANCE - STOP_BRACKET)) {
// too close
digitalWrite(ledClosePin, HIGH);
} else if (distance > (STOP_DISTANCE + STOP_BRACKET)) {
// too far
digitalWrite(ledFarPin, HIGH);
} else {
// perfect
digitalWrite(ledStopPin, HIGH);
}
}
}
Serial.println("");
}
boolean shouldShowStopLED(double distance) {
//abs(distance - STOP_DISTANCE) < (variance * (1.0 + distance / 50.0)) + (STOP_BRACKET * 2)) {
if ( abs(distance) < SENSOR_ACTIVE && abs(distance) > SENSOR_TOO_CLOSE) {
return true;
}
return false;
}
boolean checkSettleTimeReached(double distance) {
boolean delta = abs(settleDistance - distance) > (variance * (1.0 + distance / 100.0));
if (delta) {
timer = 0;
}
if (timer < settleTime) {
settleDistance = distance;
return false;
} else {
return true;
}
}
// return a voltage between 0.0 and 5.0 v
double averageVoltage() {
int sensorValue = 0;
int averaging = 10;
for (int i=0; i < averaging; i++) {
sensorValue += analogRead(sensorPin);
}
return sensorValue / 1024.0 / (averaging/5.0); // 5 reads at 5 volts
}
Notes:
- The power supplied via the USB cable connected to a computer changes the result of the output coming from the sensor.
- The formula for determining the distance is fairly accurate using the USB power, however, the values are 'stretched' using the power supply through the 5v transformer.
- The LEDs suck quite a bit of power, and without the current limiting resistor, the LEDs draw more than the power supply can provide and keep the arduino powered
- The LEDs when they turn on changes the power supply's voltage/current levels affecting the measurement of distance
- A 5v transformer is not enough power for the arduino, and need to use a bigger one. A 12v one is used and restores the distance measurements to be far more accurate and stablizes the circuit.
- The sensor reading is quite variable as the distance grows, and I'm not sure how to reduce the variability. Is it related to the power supply ripple? Or it's own circuit power draw?
I got an Arduino Pro Mini, which is sufficient for this purpose (and quite a bit cheaper), to test the idea that the issues with the distance variability is related to loose wiring or power supply ripple. I've discovered a couple of things during my minor update:
- Soldering the wiring did improve the stability a little.
- The measurements using the IR Distance Sensor variability was minimal in my work area.
- Aiming the IR Distance Sensor at the headlights produced horrible distance measurements
- By chance I aimed the IR Distance Sensor at the license plate with similarly horrible distance measurements. So I re-aimed the Sensor at the hood as before, which improved things immensely
- I washed my car and to my surprise, the IR Distance Sensor produced quite variable distance measurements.
- When the sun is in direct line of the IR Distance Sensor, it cannot get a proper reading at all. Much like the sun shining in your eyes... hopelessly unable to 'see'.