Introduction: How to Make a Light-up Distance Sensor

About: I like making things in my free time
In this instructable, I will show you how to make a sensor that will light up more lights the closer you are to it.

Arduino UNO
Arduino ultrasonic ping distance sensor
3 green LED's
2 yellow LED's
1 Red LED
6 220 ohm Resistors
USB Cable to connect Arduino
Wires to connect everything

Step 1: Placing the Electronics

This step will show you where and how to place the electronics on the breadboard

6-220 ohm resistors
3 green LED's
2 yellow LED's
1 red LED
Ultrasonic ping distance sensor

First, place the distance sensor on one end of the breadboard so that none of the pins are connected (usually parallel to the outside rails).
Then place the LED's in the order from Left to Right : Green, Green, Green, Yellow, Yellow, Red
Each LED should have the long lead facing the same way. Mine are on the left side.
Next, connect a resistor to the short leg of each LED and to the inside rail of the breadboard.
Once that is done, move on to the next step.

Step 2: Adding the Wires

In this step, I will show you how to put the wires into the breadboard to connect to the arduino.


First, Take the pin on the sensor marked VCC. Connect that to the outside rail of the breadboard
Then take the pin marked GND and connect that to the inside rail of the breadboard.
The pins marked TRIG and ECHO can be placed in another column to combine them. Then, add a wire connected to the same column on one end but not the other. This will later be connected to that arduino, but not in this step.
Connect a wire to the long pin on each LED. These will later be connected to the arduino, but not in this step.
Finally, connect a wire to each of the rails on the breadboard. These too will later be connected to the arduino, but not in this step.

Step 3: Connecting to the Arduino

In this step I will show you how to connect the wires from the breadboard to the arduino.

Breadboard with parts

First, take the wire from the outside rail and connect it to the 5V supply o the arduino
Then take the wire from the inside rail and connect it to the GND input
Take the ECHO and TRIG combined wire and connect it to pin 12 on the Arduino
Take the wires from the LED's and connect them to pins 2-7 on the Arduino in the same order that the LED's are on the breadboard.

Step 4: The Code

This step is the code that you need to load onto your arduino

Arduino +Breadboard + Electronics
USB cable to connect arduino & computer
Arduino Environment

Here is a video of the whole thing n action
Copy and paste this code into the processing environment.

This is the  code:

const int pingPin = 12; //setup pingpin as 12

void setup() {
  Serial.begin(9600);  //this is not necessary
pinMode(2, OUTPUT); // set up these pins as outputs
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);

digitalWrite(2, HIGH);  // this is not necessary, this is just a startup
digitalWrite(3, HIGH);  // function to let you know it is ready
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);
digitalWrite(7, HIGH);
delay (500);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);
digitalWrite(7, LOW);
void loop()
  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);
  digitalWrite(pingPin, HIGH);
  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);

if (inches < 20) {
   digitalWrite(2, HIGH); }  //these numbers give the distances needed to turn lights on or off
if (inches > 20) {           //change these to chang thedistances
   digitalWrite(2, LOW); }
if (inches < 15) {
  digitalWrite(3, HIGH);}
if (inches > 15) {
  digitalWrite(3, LOW);}
if (inches < 10) {
  digitalWrite(4, HIGH);}
if (inches > 10) {
  digitalWrite(4, LOW);}
if (inches < 5) {
    digitalWrite(5, HIGH);}
if (inches >5)  {
    digitalWrite(5, LOW);}
if (inches < 3) {
    digitalWrite(6, HIGH);}
if (inches > 3) {
    digitalWrite(6, LOW);}
if (inches < 2) {
    digitalWrite(7, HIGH);}
if (inches > 2) {
    digitalWrite(7, LOW);}

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:
  return microseconds / 74 / 2;
UP! Contest

Participated in the
UP! Contest