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.

  • 39 respects

Components and supplies

Ph a000066 iso (1) ztbmubhmho
Arduino 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.
11026 02
Jumper wires (generic)
Generic jumper wires were $6.99 shipped from Amazon.
MiHappy HC-SR04 Ultrasonic Distance Measuring Module
HC-SR04 Ultrasonic Distance Sensor was $6.84 shipped from Amazon for two.
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.
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.
Adafruit industries ada63 image 75px
9V 1A Switching Wall Power Supply

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


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


void setup() {
  if (DEBUG) {
    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:"); 
  delay(50); // Wait 50ms between pings (about 20 pings/sec). 29ms should be the shortest delay between pings.

  currentDistance = / 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:"); 
    // 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;


Wiring Schematic


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 nDefault1112DefaultDefaultSkillybobRunninbare

and 31 others

See similar projects
you might like

Similar projects you might like

Garage Parking Stop Light

Project showcase by Rich Noordam

  • 1 comment
  • 12 respects

Motion and Light Dependent LEDs

Project in progress by Joaquim Silveira

  • 10 respects

Night Light Assistant

Project showcase by Gabriele Fugazzi

  • 23 respects

WIZnet S2E Light Control

Project tutorial by Team S2ELighting

  • 7 respects

Control your light system with your voice

Project tutorial by Maha Raafat

  • 160 respects

Animated Smart Light with Alexa and Arduino

Project tutorial by Bruno Portaluri

  • 34 respects
Add projectSign up / Login