Project tutorial
The "Tennis Ball" Garage Stop Light

The "Tennis Ball" Garage Stop Light

Eliminate the need for a hanging tennis ball with this ultrasonic distance sensor & Arduino controlled stop light for perfect parking.

  • 10,294 views
  • 2 comments
  • 36 respects

Components and supplies

A000066 iso both
Arduino UNO & Genuino UNO
I used a Lanmu branded (Chinese) Arduino for this project since it was $9.99 shipped. I took my chances on a clone and it worked just fine. I didn't want to spend $30 on an Arduino that I would have to keep inside the stoplight.
×1
11026 02
Jumper wires (generic)
Generic jumper wires were $6.99 shipped from Amazon.
×1
MiHappy HC-SR04 Ultrasonic Distance Measuring Module
HC-SR04 Ultrasonic Distance Sensor was $6.84 shipped from Amazon for two.
×1
JBtek 4 Channel DC 5V Relay Module for Arduino
This relay module was needed in order to switch AC power on and off. AC power is 120 volts and is too high for an Arduino board.
×1
Bourns 3296w 1 103lf image 75px
Multi-Turn Precision Potentiometer- 10k ohms (25 Turn)
The 10K potentiometer is used to set the distance that the green yellow and red lights will turn on.
×1
Adafruit industries ada63 image 75px
9V 1A Switching Wall Power Supply
×1

Necessary tools and machines

09507 01
Soldering iron (generic)
The soldering iron (30 watt) is briefly used to solder some jumper wires onto the potentiometer so that we can skip the use of a breadboard.
Hy gluegun
Hot glue gun (generic)
The hot glue gun is used to attach the (cheap) Arduino and relay module to the inside of the stop light.

Apps and online services

Ide web
Arduino IDE
Arduino IDE software is most commonly used. However microchip manufacturer's make software that is a bit more advanced. For this simple project, we will stick with Arduino IDE.

About this project

My garage tends to get a bit messy due to my Wife and I working in there on projects multiple times a week. I like to leave space in front of the work benches so that we have room to work so when I pull into the garage, I try to pull in as little as I can with the garage door being able to close. Before having the "Tennis Ball Stop Light" I often got in and out of my car at least once before I was in a parking spot that allowed the garage to close.

In this tutorial I will show you step by step how to make one of these on your own. Let's talk about how it actually works. The "Ultrasonic Distance Sensor" actually uses sonar ping to calculate how far away the object in front of it is. This communicates with the Arduino and tells it when to trigger the lights. First I will tell you how to wire the physical circuit. It shouldn't take long as it is not many components. Also, If you don't have a stop light laying around, feel free to run LEDs instead. They may require resistors depending on the LED. The circuit is wired as shown in the following figure.

I will try and explain the figure so you better understand the connections.

VCC (Ping Sensor) to 5V (Arduino)

Trig (Ping Sensor) to Digital Pin 12 (Arduino)

Echo (Ping Sensor) to Digital Pin 13 (Arduino)

Ground (Ping Sensor) to Ground (Arduino)

Outside Pin (Potentiometer) to 5V (Arduino)

Other Outside Pin (Potentiometer) to Ground (Arduino)

Middle Pin (Potentiometer) to Analog Pin 0 (Arduino)

VCC (Relay Board) to 5V (Arduino)

Ground (Relay Board) to Ground (Arduino)

Int 1 (Relay Board) to Digital Pin 5 (Arduino)

Int 2 (Relay Board) to Digital Pin 4 (Arduino)

Int 3 (Relay Board) to Digital Pin 3 (Arduino)

Cut 120V positive wire from each bulb and insert into each relay (Normally Closed). Here you are creating a break in the circuit so that the relay can control whether it is on or off.

Now that your circuit is wired up. We can move on to coding. Copy and paste the provided code to your Arduino software and upload it to your board. You will need a library named "NewPing" for this project and here is why. When the ping sensor can't read a distance, it delays for an entire second and your car could move quite a bit into the garage in that second. So this library fixes this and reads multiple times per second. The link to download is: 

http://playground.arduino.cc/Code/NewPing. You will need to place this into your libraries folder once extracted.

Once your code is uploaded, you should be ready for assembly. I took my boards and hot glued them inside of the light as shown in the next figure.

Next, I drilled some holes so that I have access to the Arduino USB port and power port.

All that's left is to plug in and enjoy your worry free parking! Here it is in action.


Code

Arduino "Stop Light Coding"Arduino
#include <NewPing.h>

#define DEBUG         true // Set to true to enable Serial debug
#define TRIGGER_PIN   12  // Board pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN      11  // Board pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE  300 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

// leds pins
int ledR = 3;
int ledY = 4;
int ledG = 5;

int potPin = A0;

int optDistance; 
int optDistanceGap = 5;
int optMin;
int optMax;
int yellowGap = 10;
int redGap = 20;
int maxDistance = MAX_DISTANCE;
int currentDistance;
int prevDistance = 0;
int deviationThreshold = 2;
int timerStartTime = 0;
int ledsTimout = 30; // seconds

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);

void setup() {
  if (DEBUG) {
    Serial.begin(115200);
    Serial.println("Garage sensor is starting");
  }
  
  pinMode(ledG, OUTPUT);
  pinMode(ledY, OUTPUT);
  pinMode(ledR, OUTPUT);
}

void loop() {
  
  // get pot reading for the optimal distance.
  optDistance = analogRead(potPin) / 5; // potentiometer readins are in between 0 and 1023. Deviding it by 5 makes our optimal distance range from 0 to about 200cm
  
  // recalculate optimal distance gaps
  optMin = optDistance - optDistanceGap;
  optMax = optDistance + optDistanceGap;

  if (DEBUG) {
    Serial.print("Optimal distance:"); 
    Serial.println(optDistance); 
  }
  
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

  currentDistance = sonar.ping() / US_ROUNDTRIP_CM; // Send ping, get ping time in microseconds (uS) and translate it into cm
  
  if (abs(currentDistance - prevDistance) >= deviationThreshold) {
    prevDistance = currentDistance;
    timerStartTime = millis();
    
    if (DEBUG) {
      Serial.print("Current distance:"); 
      Serial.println(currentDistance); 
    }
    
    // if distance is in range of optimal distances, turn on green led
    if (inRange(currentDistance, optMin, optMax)) {
      setLeds(LOW, LOW, HIGH);
      if (DEBUG) Serial.println("Got in optimal distance range");
    }
    // if distance is close to the optimal distances, but not yet there, turn on yellow led
    else if (inRange(currentDistance, optMin - yellowGap, optMin) || inRange(currentDistance, optMax, optMax + yellowGap)) {
      setLeds(LOW, HIGH, LOW);
      if (DEBUG) Serial.println("Close to the optimal distance");
    }
    // if distance is close to zero or too far, turn on red led
    else if (inRange(currentDistance, 0, optMin - yellowGap) || inRange(currentDistance, optMax + yellowGap, optMax + yellowGap + redGap)) {
      setLeds(HIGH, LOW, LOW);
      if (DEBUG) Serial.println("Too far from the optimal distance");
    }
    // otherwise turn all leds off
    else {
      setLeds(LOW, LOW, LOW);
      if (DEBUG) Serial.println("Turn all leds off");
    }
  } else {
    int currentTime = millis();
    if ((currentTime - timerStartTime) / 1000 > ledsTimout) {
      if (DEBUG) Serial.println((currentTime - timerStartTime) / 1000);
      setLeds(LOW, LOW, LOW);
    }
  }
}

/**
 * Turn leds on and off
 */
void setLeds(int r, int y, int g) {
  digitalWrite(ledR, r);
  digitalWrite(ledY, y);
  digitalWrite(ledG, g);
}

/**
 * Check is value in range
 */
bool inRange(int d, int rangeMin, int rangeMax) {
  return d >= rangeMin && d < rangeMax;
}

Schematics

Wiring Schematic
1235555

Comments

Team Stuart Mace

12109263 3882310461556 7530524435115358205 n
Stuart Mace
  • 1 project
  • 1 follower

Published on

May 7, 2016

Members who respect this project

Default10487434 10101544397202548 602652689234148955 nDefault1112DefaultDefaultSkillybobMe

and 28 others

See similar projects
you might like

Similar projects you might like

Garage Parking Stop Light

Project showcase by Rich Noordam

  • 2,440 views
  • 0 comments
  • 10 respects

Bluetooth Controlled Light Switch

Project showcase by Team The Instrumentation Crustaceans

  • 23,428 views
  • 4 comments
  • 26 respects

IoT arduino ESP Garage Door opener (UD)

Project in progress by David Smerkous

  • 10,859 views
  • 6 comments
  • 16 respects

SmartPhone Controlled RGB MOOD Light

Project tutorial by Prajjwal Nag

  • 3,924 views
  • 1 comment
  • 10 respects

Night Light Assistant

Project showcase by Gabriele Fugazzi

  • 3,834 views
  • 9 comments
  • 21 respects

Control your light with Arduino

Project tutorial by GiorgioTraverso

  • 1,810 views
  • 0 comments
  • 2 respects
Add projectSign up / Login