Simplest Distance Indicator

This distance indicator shows you the exact distance between an object and the sensor in inches or centimeters on the Serial Monitor. It also shows what distance the object is from the sensor on a series of LEDs. This way, you can still know distance using this indicator when it isn't hooked up to a computer, and the Serial Monitor is not in use.

Things you need

It uses the HY-SRF05 Ultrasonic Sensor, which you can find everywhere: HY-SRF05

A few LEDs, I used 6

-Each LED indicates distance. Let's say that one LED represents 5 inches. If 4 LEDs light up, then the
distance between the sensor and the object is ≤20 inches.

An Arduino Uno

A bunch of jumpers (connecting wires)

This design and code is for a 0-30 inch, 6 LED indicator. It can be easily modified to change units of measurement or the amount of indicating LEDs. If you need help, just ask.

Design

Sensor pins:
-GNC: E1
-Out: [don't connect this, although Mr. Sam told you to ;)]
-Echo: E3
-Trig: E4
-Vcc: E5
Point the sensor away from rows A, B, C and D, and towards J

LEDs:
+E19 -E20
+E21 -E22
+E23 -E24
+E25 -E26
+E27 -E28
+E29 -E30

Jumpers:

GND (-)
5V (+)

D1 (-)
D3 digitalPin 6
D4 digitalPin 5
D5 (+)

A19 digitalPin 13
A20 (-)
A21 digitalPin 12
A22 (-)
A23 digitalPin 11
A24 (-)
A25 digitalPin 10
A26 (-)
A27 digitalPin 9
A28 (-)
A29 digitalPin 8
A30 (-)

The code:

//Tristan Romanov 2015

#define SONAR_TRIGGER_PIN     5
#define SONAR_ECHO_PIN        6

const int ledPin5 = 13;
const int ledPin10 = 12;
const int ledPin15 = 11;
const int ledPin20 = 10;
const int ledPin25 = 9;
const int ledPin30 = 8;

unsigned int measure_distance()
{
   // Trigger the SRF05:
   digitalWrite(SONAR_TRIGGER_PIN, HIGH);
   delayMicroseconds(10);
   digitalWrite(SONAR_TRIGGER_PIN, LOW);

   unsigned long pulse_length = pulseIn(SONAR_ECHO_PIN, HIGH);

   delay(50);

   // Convert Pulse to Distance (inches) 
   // pulse_length/58 = cm or pulse_length/148 = inches
   return( (unsigned int) (pulse_length / 148) );
}


void setup()
{
   pinMode(SONAR_TRIGGER_PIN, OUTPUT);
   pinMode(SONAR_ECHO_PIN, INPUT);
   Serial.begin(9600);
   Serial.println("distance");
   pinMode(ledPin5, OUTPUT);
   pinMode(ledPin10, OUTPUT);
   pinMode(ledPin15, OUTPUT);
   pinMode(ledPin20, OUTPUT);
   pinMode(ledPin25, OUTPUT);
   pinMode(ledPin30, OUTPUT);
}

void loop()
{
   unsigned int current_distance = measure_distance();
   Serial.println(current_distance);
   delay(125);
   if (current_distance > 4.99) {
     digitalWrite(ledPin5, HIGH);
   } else {
     digitalWrite(ledPin5, LOW);
   }
   if (current_distance > 9.99) {
     digitalWrite(ledPin10, HIGH);
   } else {
     digitalWrite(ledPin10, LOW);
   }
   if (current_distance > 14.99) {
     digitalWrite(ledPin15, HIGH);
   } else {
     digitalWrite(ledPin15, LOW);
   }
   if (current_distance > 19.99) {
     digitalWrite(ledPin20, HIGH);
   } else {
     digitalWrite(ledPin20, LOW);
   }
   if (current_distance > 24.99) {
     digitalWrite(ledPin25, HIGH);
   } else {
     digitalWrite(ledPin25, LOW);
   }
   if (current_distance > 29.99) {
     digitalWrite(ledPin30, HIGH);
   } else {
     digitalWrite(ledPin30, LOW);
   }
}

Altering the design to suit your personal needs:

Well, the Serial Monitor always shows you what you will need. However, if you want to change the amount of LEDs, or if you want to change the range that the LEDs cover, read this.

The jumpers that connect D3 to digitalPin 6 and D4 to digitalPin 5 can be moved if you want to put more LEDs on and you want to have more space on your board. Move the jumper in digitalPin 5 to digitalPin "x", and the jumper in digitalPin 6 to digitalPin "y".

Then, in the code, change

#define SONAR_TRIGGER_PIN 5

to

#define SONAR_TRIGGER_PIN "x"

and

#define SONAR_ECHO_PIN 6

to

#define SONAR_ECHO_PIN "y"

for those who don't understand:
don't actually put x and y in code, and for the reference, digitalPins x and y do not exist. For x and y, choose your own pin numbers, and put those pin numbers in the correct place in the code.

If you want to change the distance range that the LEDs indicate, simply change a few numbers.
Let's say that you want the first LED's range to be 42.42 inches, and you want the second LED's range to be 1.42 inches more than that.

Change

if (current_distance > 4.99) {

to

if (current_distance > 42.42) {

and

if (current_distance > 9.99) {

to

if (current_distance > 43.84) {

For those who don't understand:
The number 43.84 came from the first range PLUS how much you want the second range to be higher than the first range.

To change the amount of LEDs:
The design is pretty repetitive, seriously, if you can't figure this one out, either get off of this site or ask me for help.

tristanscomics:
To change the amount of LEDs:
The design is pretty repetitive, seriously, if you can't figure this one out, either get off of this site or ask me for help.

Really? You write a tutorial for beginners then suggest they get off this site if they don't understand something?

I like to use the Sharp IR distance sensors (e.g., GP2D12 or GP2D120 or different other ones).

Advantages: very quick by analog reading, just 3 wires (Vc, GND, Vout), no delays, no complicated triggering, no complicated cascading in case you need to have multiple of them - just read the raw analog value (or in case you need it: calculate the distance by a formula).

http://www.trossenrobotics.com/sharp-ir-distance-sensor-gp2d12.aspx

Etzeitet:
Really? You write a tutorial for beginners then suggest they get off this site if they don't understand something?

i said ask me for help