Project tutorial
Distance Sensor

Distance Sensor © MIT

Mount in front of your garage to help you park at teh exact right distance.

  • 748 views
  • 0 comments
  • 3 respects

Components and supplies

29c8785 40
Pushbutton Switch, Momentary
The enclosure has an opening for a 16mm button.
×1
Mfr 25frf52 10k sml
Resistor 10k ohm
×1
51gzz5eu9pl. sx425
Ultrasonic Sensor - HC-SR04 (Generic)
×1
Adafruit NeoPixel Digital RGBW LED Strip - White PCB 30 LED/m
I used a strip containing 8 NeoPixels, but you can adapt the code to use more or fewer.
×1
Ardgen mega
Arduino Mega 2560 & Genuino Mega 2560
The wiring diagram uses an Arduino Mega, but this would just as easily run an on Uno. It would probably also run on other boards, but I haven't tested it.
×1
GE Outlet Light Socket
(Optional)
×1

Necessary tools and machines

3drag
3D Printer (generic)
Used for printing the enclosure. Optional if you can find a way to secure all the components.

About this project

I kept hitting the back of my garage so instead of learning how to drive, I made a gadget.

This senses distance from a vehicle and lights up to show how close. Once you reach the desired distance, it flashes red.

How do you set the distance you ask? There is a handy push-button. Whenever the button is pressed, the device permanently saves the distance.

To power the device, I used the light socket adapter to attach it to my garage door opener. That way, whenever the garage light is on, the sensor is working.

Here's a video of the sensor in action:

Code

Distance Senor CodeArduino
This is the code to upload to the Arduino. It sets allows users to interact with the device.
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include <EEPROM.h>

#define PIN 12
#define BUTTON_PIN 11
#define echoPin 10
#define trigPin 9
#define max_dist 300
#define set_loc 1
#define NUM_LEDS 9
#define BRIGHTNESS 50

Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRBW + NEO_KHZ800);


//Initialize glabal variables
int set_dist;
float duration, distance;

void setup() {
  // Initialize neopixels
  // This is for Trinket 5V 16MHz, you can remove these three lines if you are not using a Trinket
#if defined (__AVR_ATtiny85__)
  if (F_CPU == 16000000) clock_prescale_set(clock_div_1);
#endif
  // End of trinket special code

  strip.setBrightness(BRIGHTNESS);
  strip.begin();
  strip.show();

  // Initialize SR04
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  // Initialize set_dist
  set_dist = EEPROM.read(set_loc);
  // Set for testing
  // set_dist = 10;
  Serial.begin(9600);
  // This fixes a problem with the button reading high on startup
  while(digitalRead(BUTTON_PIN) == LOW){
    
  }
}

void set_lights(uint8_t distance) {
  int total_distance = max_dist - set_dist;
  /*
      We will fill up the colors first with blue,
      then green, then red when too far.
  */
  // Deterimne dist from set_dist
  int dif = distance - set_dist;
  int half_dist = (float) total_distance / 2;

  if (dif > half_dist) {
    // Set lights to be blue
    int to_lite = ((float) dif / half_dist - 1) * NUM_LEDS;
    for (int i = 0; i < NUM_LEDS; i++) {
      if (i > to_lite) {
        strip.setPixelColor(i, strip.Color(0, 0, 255, 0));
      } else {
        strip.setPixelColor(i, strip.Color(0, 0, 0, 0));
      }
    }
    strip.show();
    return;
  }
  if (dif > 0) {
    // Set lights to be blue and green
    int to_lite = ((float) dif / half_dist) * NUM_LEDS;
    for (int i = 0; i < NUM_LEDS; i++) {
      if (i > to_lite) {
        strip.setPixelColor(i, strip.Color(0, 255, 0, 0));
      } else {
        strip.setPixelColor(i, strip.Color(0, 0, 255, 0));
      }
    }
    strip.show();
    return;
  }
  if (dif <= 0) {
    // Set all lights to red
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, strip.Color(255, 0, 0, 0));
    }
    strip.show();
  }
  Serial.println(dif);

  // Setup button
  pinMode(BUTTON_PIN, INPUT_PULLUP);

}

int get_distance() {
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  duration = pulseIn(echoPin, HIGH);
  distance = (duration * .0343) / 2;
  return (int) distance;
}
void flash(uint32_t c){
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, c);
    }
    strip.show();
    delay(1000);
    for (int i = 0; i < NUM_LEDS; i++) {
      strip.setPixelColor(i, strip.Color(0, 0, 0, 0));
    }
    strip.show();
}
void loop() {
  // put your main code here, to run repeatedly:
  set_lights(get_distance());
  delay(100);

  if (digitalRead(BUTTON_PIN) == LOW){
    flash(strip.Color(0,0,0,255));
    delay(1000);
    flash(strip.Color(0,0,0,255));
    delay(1000);
    flash(strip.Color(255,0,0,0));
    set_dist = get_distance();
    EEPROM.update(set_loc, set_dist);

  }
}

Custom parts and enclosures

3D Printed Enclosure
This houses the distance sensor, button, and NeoPixels. There isn't a spot for the Arduino, but mine just sat on top.
enclosure_Gb4oUSvaWl.fpp

Schematics

Wiring Diagram for the unit
This is how you wire the distance sensor, button, and NeoPixels to the Arduino.
Spectacular kieran 4ce19bxvdo

Comments

Similar projects you might like

HC-SR04 Distance Sensor Service

Project tutorial by Nahuel Taibo

  • 6,216 views
  • 0 comments
  • 11 respects

Arduino Mp3 player + Distance sensor = FUN

Project tutorial by javier muñoz sáez

  • 35,533 views
  • 32 comments
  • 60 respects

Distance Sensor and OLED

Project tutorial by javier muñoz sáez

  • 35,264 views
  • 25 comments
  • 88 respects

Touch Sensor and Sound Sensor Controlling AC/DC Lights

Project showcase by Anbumachi

  • 3,608 views
  • 2 comments
  • 7 respects

LCD display distance of an ultrasonic sensor

Project tutorial by adam johns

  • 7,882 views
  • 0 comments
  • 8 respects
Add projectSign up / Login