DIY Parking Sensor Tutorial

The last time I was home visiting my parents I noticed bumper imprints caused by my mother suburban on the stairs leading up from the garage. Their garage it turns out is just barely long enough to fit their gigantic vehicles. So I decided it would be nice to have some visual cue for parking. Out came the arduino and a sonar range finder from Radio Shack and the result was this tutorial.

Materials…

  • Arduino (I had a duemilanove available)
  • Ultrasonic Range Finder
  • Wire
  • Small box
  • 9V power supply (You can find old power supplies for cheap at thrift stores)
  • Tri Color LED
  • Hot Glue Gun
  • Breadboard

Assembly…

  1. Hot glue the arduino to the bottom of the box and run the power supple to it
  2. Connect the 5V and Ground wires to the range finder.
  3. Connect the Pulse cable from the range finder to a “PWM” input on the arduino (this is necessary because we’ll be sending pulses through the same leed that we listen for a return on). I used digital pin 7 with PWM for the pulse connection.
  4. Test the tri color LED to find out which connectors make which colors. You will need to keep track of which wire creates which colors and connect them to three digital pins on the arduino. Keep track of the pin numbers. For instance, I connected to digital pins 11, 12 and 13 with red, green and blue respectively.
  5. Once you have everything connected we’ll start writing the program. After your finished with the programming and you’re sure it works, it’s a good idea to seal everything up in the box to make sure none of the wires get disconnected before you mount the sensor to your wall.

Programming…

Luckily, Arduino already provides an example of how to use the pulse sonar sensor. Select the File -> Examples -> Sensors -> Ping example in the Arduino programming kit. Once open, select the code and copy it to a blank sketch. Save the new project under sketches I used the name “parking Example”.

Now we have something to work with. Begin customizing the code. First, we are only going to be measuring inches, not cm so lets comment out the code snippet about halfway down that runs a function to calculate cm. Comment out:

//cm = microsecondsToCentimeters(duration);

Next we don’t need to send sonar pings out at such a high interval. We only need to ping about every second because the car will (hopefully) be moving slowly into the garage. So at the bottom of the loop function set the delay to 1000:

delay(1000);

Next we need to tell arduino which pins we’ll be using for our LED output. At the top where we have:

const int pingPin = 7;

We’ll add:

pinMode(13, OUTPUT); // blue
pinMode(12, OUTPUT); // green
pinMode(11, OUTPUT); // red

Now that ardiuno has a setup for those pins, we’ll need to send a signal whenever we want that color to show. So After receiving the signal from our ping, we’ll compute the distance and if it falls within certain ranges we’ll show a specific color. I want the driver to see green until they get within 24 inches of the wall, at that point, I want the light to turn blue, signaling that they are getting closer. Then when they are within 6 inches of the wall, the red light should turn on, indicating to the driver that they should stop.

Include the following code below the inches distance calculation:

inches = microsecondsToInches(duration);
// show LED colors
  if(inches > 0 && inches <= 6) {
    // show red, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
  } else if(inches <= 24 && inches > 6) {
    // show blue, all other leds are off
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(13, HIGH);
  } else {
    // show green led, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, HIGH);
  }

This code tells arduino to send a signal to a specific pin when an object comes within a certain distance. The entire script should look like:

/* Ping))) Sensor
  
   This sketch reads a PING))) ultrasonic rangefinder and returns the
   distance to the closest object in range. To do this, it sends a pulse
   to the sensor to initiate a reading, then listens for a pulse 
   to return.  The length of the returning pulse is proportional to 
   the distance of the object from the sensor.
     
   The circuit:
	* +V connection of the PING))) attached to +5V
	* GND connection of the PING))) attached to ground
	* SIG connection of the PING))) attached to digital pin 7

   http://www.arduino.cc/en/Tutorial/Ping
   
   created 3 Nov 2008
   by David A. Mellis
   modified 30 Jun 2009
   by Tom Igoe
 
   This example code is in the public domain.

 */

// this constant won't change.  It's the pin number
// of the sensor's output:
pinMode(13, OUTPUT); // blue
pinMode(12, OUTPUT); // green
pinMode(11, OUTPUT); // red
const int pingPin = 7;

void setup() {
  // initialize serial communication:
  Serial.begin(9600);
}

void loop()
{
  // establish variables for duration of the ping, 
  // and the distance result in inches and centimeters:
  long duration, inches, cm;

  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);

  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);

  // convert the time into a distance
  inches = microsecondsToInches(duration);
  // show LED colors
  if(inches > 0 && inches <= 6) {
    // show red, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);
    digitalWrite(11, HIGH);
  } else if(inches <= 24 && inches > 6) {
    // show blue, all other leds are off
    digitalWrite(12, LOW);
    digitalWrite(11, LOW);
    digitalWrite(13, HIGH);
  } else {
    // show green led, all other leds are off
    digitalWrite(13, LOW);
    digitalWrite(11, LOW);
    digitalWrite(12, HIGH);
  }
  //cm = microsecondsToCentimeters(duration);
  
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  delay(1000);
}

long microsecondsToInches(long microseconds)
{
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  // 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.
  return microseconds / 29 / 2;
}

All that’s needed after that is upload the code to your arduino, mount the device to the front wall of your garage and hang the led somewhere where the driver can see it! Let me know how these instructions work for you. I can always adjust this tutorial later.

20 thoughts on “DIY Parking Sensor Tutorial

  1. How exactly do you get the LED to light up? I’m having issues with the colors not lining up with your example…

    Also, great tutorial, but the pictures seem to be messed up. You have photos of the unit, but not of connecting wires which is what I’m looking for. It would be easier if you could post some pictures of how you actually connected to arduino.

    Thanks!
    Alex

  2. Hey Mike this is an awesome project! My friend and I were at Radioshack today looking for a project to do. We did something else, obviously since I hadn’t seen your post until now! This looks like a fun project – easy to handle and whatnot. I’ll be sure to look back at this if we ever get to it! Nice job

  3. How do you mount the ultrasonic emitter/receiver so they are not affected when raining or when washing the car ?

    Please reply.

  4. help me, toklong correction coding that I created, I created a tool with 2 ultrasonic sensors, detect when the goods in front of the buzzer sounded, and when detecting more than 10 seconds then make my relays are active, if <10 seconds only the buzzer alone,

    help me, of all peers (to activate the buzzer was able, as yet relainya)

    #include
    #include
    #define buzzer 2
    #define relay 5
    DistanceSRF04 Dist;
    int distance;

    DistanceSRF04 Dist2;
    int distance2;
    bool prevDetect=false;
    bool detected=false;
    unsigned long tadi, kini;
    LiquidCrystal lcd(6, 11, 7, 10, 8, 9); //definisi pin LCD

    void setup()
    {
    pinMode(relay, OUTPUT);
    pinMode(buzzer, OUTPUT);
    Serial.begin(9600);
    Dist.begin(3,4);
    Dist2.begin(17,16);
    lcd.begin(16,2);
    lcd.print(“ready…”);
    delay(1000);
    lcd.clear();

    }

    void loop()
    {
    distance = Dist.getDistanceCentimeter();
    distance2 = Dist2.getDistanceCentimeter();
    Serial.print(“nDistance in centimers: “); //menulis ke lcd
    Serial.print(distance);
    Serial.print(” “);

    delay(500); //make it readable
    lcd.setCursor(0,0);
    lcd.print(“Sensor 1 : “);
    lcd.setCursor(0,1);
    lcd.print(“Sensor 2 : “);

    if (distance >0 && distance 0 && distance2 <300)
    {

    lcd.setCursor(11,1);
    lcd.print(distance2);
    lcd.setCursor(14,1);

    }

    else
    {
    lcd.setCursor(11,1);
    lcd.print("—");
    lcd.setCursor(14,1);

    }
    if(distance <=100&& distance2 <=100 )
    {

    digitalWrite(buzzer, LOW);
    }
    else if(distance <100 && distance 10000)digitalWrite(relay, HIGH);
    }
    prevDetect=detected;
    }

  5. I had a little trouble with this project at first as well. I am assuming those who had problems with the led may have had a mix up between common anode and common cathode leds. this would cause the colors to light up in the wrong order. I ended building mine around the HC-SR04 sensor, so I had to modify the code a bit to work with it’s 4 wire configuration.

    Here is the code I came up with. This is only my second time coding to arduino so let me know if you have any improvement ideas;-)

    // Indicator Based Rangefinder. I built this to use with a fixed focus camera. It can be easily
    // repurposed for any situation where you need to insure a pre-determined distance. This code uses
    // the HC-SR04 rangefinder module as a sensor and a common anode RGBLED. The RF module’s vcc and the LED’s
    // anode are attached to 5v. The ground of the RF module is taken to the ground pin. A common cathode LED
    // could be substituted by connecting the cathode to ground and inverting the HIGH/LOW values of the led outputs.
    // All i/o numbers in the code match their corresponding pins.
    int ledr2 = 2; // red
    int ledb3 = 3; // blue
    int ledg4 = 4; // green
    int rf9 = 9; // trig
    int rf10 = 10; //echo
    int duration;
    int distance;

    void setup(){
    Serial.begin(9600);

    pinMode(ledr2, OUTPUT); //Green
    pinMode(ledb3, OUTPUT); //Blue
    pinMode(ledg4, OUTPUT); //Red
    pinMode(rf9, OUTPUT); //Trigger
    pinMode(rf10, INPUT); //Echo
    }
    void loop()
    {
    digitalWrite(rf9, LOW);
    digitalWrite(rf9, HIGH);
    delayMicroseconds(1000);
    digitalWrite(rf9, LOW);
    duration = pulseIn(rf10, HIGH);
    distance = (duration/2) / 74; // Distance set to inches. Modify to 29 for centimeters

    if (distance >= 47 && distance <= 49) { //light green in the hot zone
    digitalWrite(ledg4, LOW);
    digitalWrite(ledb3, HIGH);
    digitalWrite(ledr2, HIGH);

    } else if(distance = 0) { //light blue when too close
    digitalWrite(ledg4, HIGH);
    digitalWrite(ledb3, LOW);
    digitalWrite(ledr2, HIGH);

    } else if(distance >= 50) { //light red when too far
    digitalWrite(ledg4, HIGH);
    digitalWrite(ledb3, HIGH);
    digitalWrite(ledr2, LOW);

    }
    Serial.print(distance);
    Serial.print(“in, “);
    Serial.println();

    delay(1000);
    }

  6. oops. just noted an error in the close range value on that last post. here is the corrected code.

    // Indicator Based Rangefinder. I built this to use with a fixed focus camera. It can be easily
    // repurposed for any situation where you need to insure a pre-determined distance. This code uses
    // the HC-SR04 rangefinder module as a sensor and a common anode RGBLED. The RF module’s vcc and the LED’s
    // anode are attached to 5v. The ground of the RF module is taken to the ground pin. A common cathode LED
    // could be substituted by connecting the cathode to ground and inverting the HIGH/LOW values of the led outputs.
    // All i/o numbers in the code match their corresponding pins.
    int ledr2 = 2; // red
    int ledb3 = 3; // blue
    int ledg4 = 4; // green
    int rf9 = 9; // trig
    int rf10 = 10; //echo
    int duration;
    int distance;

    void setup(){
    Serial.begin(9600);

    pinMode(ledr2, OUTPUT); //Green
    pinMode(ledb3, OUTPUT); //Blue
    pinMode(ledg4, OUTPUT); //Red
    pinMode(rf9, OUTPUT); //Trigger
    pinMode(rf10, INPUT); //Echo
    }
    void loop()
    {
    digitalWrite(rf9, LOW);
    digitalWrite(rf9, HIGH);
    delayMicroseconds(1000);
    digitalWrite(rf9, LOW);
    duration = pulseIn(rf10, HIGH);
    distance = (duration/2) / 74; // Distance set to inches. Modify to 29 for centimeters

    if (distance >= 47 && distance <= 49) { //light green in the hot zone
    digitalWrite(ledg4, LOW);
    digitalWrite(ledb3, HIGH);
    digitalWrite(ledr2, HIGH);

    } else if(distance = 0) { //light blue when too close
    digitalWrite(ledg4, HIGH);
    digitalWrite(ledb3, LOW);
    digitalWrite(ledr2, HIGH);

    } else if(distance >= 50) { //light red when too far
    digitalWrite(ledg4, HIGH);
    digitalWrite(ledb3, HIGH);
    digitalWrite(ledr2, LOW);

    }
    Serial.print(distance);
    Serial.print(“in, “);
    Serial.println();

    delay(1000);
    }

  7. huh, well for whatever reason when I post it keeps modifying that line. the first else if should be:

    distance =0

  8. well I’ll be. it still clipped it.OK, in straight english

    distance greater than or equal to 46 && less than or equal to 0.

    this is coded in the same fashion as the first “if”.

  9. ha! Now i’m flustered. “Strike that and reverse it!”

    less than or equal to 46 && greater than or equal to 0

  10. I uploaded the “indicator Based Rangefinder” code and received these errors…..

    Cam_Range_TriLED:50: error: stray ” in program
    Cam_Range_TriLED:50: error: stray ” in program
    Cam_Range_TriLED.ino: In function ‘void loop()’:
    Cam_Range_TriLED:50: error: ‘u201cin’ was not declared in this scope
    Cam_Range_TriLED:50: error: ‘u201c’ was not declared in this scope

    ….Any advice to correct would help. I am new to Arduino and programming. Thanks

  11. It’s hard to find weⅼl-informed people aboᥙt this subject, however,
    you seem like ʏou know what you’ге talking about! Thаnks

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.