DIY Parking Sensor Tutorial

22 Mar

DIY Parking Sensor Tutorial

Posted by Mike Categories: Blog, Featured, Portfolio
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.

14 Comments on “ DIY Parking Sensor Tutorial ”

  1. [...] Parking Sensor using Arduino – [Link] Tags: Arduino, parking, Sensor Filed in Arduino | 1 views No Comments [...]

  2. Alex April 15, 2011 at 7:43 AM

    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

  3. Cal December 27, 2011 at 10:44 PM

    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

  4. masster January 31, 2012 at 2:43 AM

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

    Please reply.

  5. [...] but that seemed both complex (as I know zip about programming them) and expensive. I did find this project, but I wasn’t sure if the sensitivity would meet my needs, and didn’t want to risk the [...]

  6. rahmat March 9, 2012 at 5:01 PM

    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;
    }

  7. J. August 19, 2012 at 3:03 AM

    I copied this code line for line and it still didnt work.

  8. David August 29, 2012 at 3:21 AM

    getting an error on the pinmode led setup Hmm

  9. dasbudda March 4, 2013 at 5:45 PM

    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);
    }

  10. dasbudda March 4, 2013 at 5:49 PM

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

    distance =0

  11. dasbudda March 4, 2013 at 5:52 PM

    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”.

  12. dasbudda March 4, 2013 at 5:54 PM

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

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

  13. [...] DIY Parking Sensor Tutorial – example how to use ultrasonic sensor for a DIY parking application; Advertisement Share var hupso_services_t=new Array("Twitter","Facebook","Google Plus","Linkedin","StumbleUpon","Digg","Reddit","Bebo","Delicious");var hupso_background_t="#EAF4FF";var hupso_border_t="#66CCFF";var hupso_toolbar_size_t="big";var hupso_image_folder_url = "";var hupso_title_t=""; [...]

  14. Ezu October 3, 2013 at 6:08 PM

    It could be much better if you expand the tutorial in order to build more complex robotics applications. A series of tutorial can be found in this article http://www.intorobotics.com/interfacing-programming-ultrasonic-sensors-tutorials-resources/

Leave a Reply
  1. XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>