Easy Distance Detector

Introduction: Easy Distance Detector

This Arduino Project is an easy distance detector included with code.

The idea comes from https://www.instructables.com/id/Simple-Arduino-a...

Description of project:

When the further an object is to the sensor, the more LED lights will light up. Every LED lights up, it means that the object is 5 units away from the sensor. The maximum distance this project can detect is 45 units away.

Supplies

To make this project, please prepare the materials:

glue (Any Type)

markers

cardboards

red LED light bulb *1

green LED light bulb *8

Arduino Uno (Leonardo) board

USB cable resistors *9

jumper wires

breadboard

Ultrasonic Sensors *1

Tape

Step 1: Writing the Code

Link to Arduino below

https://create.arduino.cc/editor/joseph_ray_chen/0...

or

#define trigPin 13

#define echoPin 12

#define led1 10//改

#define led2 2//改

#define led3 3//改

#define led4 4//改

#define led5 5//改

#define led6 6//改

#define led7 7//改

#define led8 8//改

#define led9 9//改

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led1, OUTPUT);

pinMode(led2, OUTPUT); }

void loop() {

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

if (distance > 5) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 10) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 15) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 20) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 25) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,HIGH);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 30) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,HIGH);//改

digitalWrite(led6,HIGH);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 35) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,HIGH);//改

digitalWrite(led6,HIGH);//改

digitalWrite(led7,HIGH);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 40) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,HIGH);//改

digitalWrite(led6,HIGH);//改

digitalWrite(led7,HIGH);//改

digitalWrite(led8,HIGH);//改

digitalWrite(led9,LOW);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance > 45) { //改

digitalWrite(led1,HIGH); //改

digitalWrite(led2,HIGH);//改

digitalWrite(led3,HIGH);//改

digitalWrite(led4,HIGH);//改

digitalWrite(led5,HIGH);//改

digitalWrite(led6,HIGH);//改

digitalWrite(led7,HIGH);//改

digitalWrite(led8,HIGH);//改

digitalWrite(led9,HIGH);//改

delay( 500 );//改

digitalWrite(led1,LOW); //改

digitalWrite(led2,LOW);//改

digitalWrite(led3,LOW);//改

digitalWrite(led4,LOW);//改

digitalWrite(led5,LOW);//改

digitalWrite(led6,LOW);//改

digitalWrite(led7,LOW);//改

digitalWrite(led8,LOW);//改

digitalWrite(led9,LOW);//改

}

if (distance >= 199 || distance <= 0){//改

Serial.println("Out of range");

}

else {

Serial.print(distance);

Serial.println(" cm");

}

delay(500);

}

Step 2: Constructing the Board

connect (via cables):

GND to negative (blue line) on breadboard

5V to positive (red line) on bread board

(LEDs)

Digital 2 to the long leg of LED1; short leg connect to a resistor then to negative (blue line)

Digital 3 to the long leg of LED2; short leg connect to a resistor then to negative (blue line)

Digital 4 to the long leg of LED3; short leg connect to a resistor then to negative (blue line)

Digital 5 to the long leg of LED4; short leg connect to a resistor then to negative (blue line)

Digital 6 to the long leg of LED5; short leg connect to a resistor then to negative (blue line)

Digital 7 to the long leg of LED6; short leg connect to a resistor then to negative (blue line)

Digital 8 to the long leg of LED7; short leg connect to a resistor then to negative (blue line)

Digital 9 to the long leg of LED8; short leg connect to a resistor then to negative (blue line)

Digital 10 to the long leg of LED9; short leg connect to a resistor then to negative (blue line)

(UltraSonic Sensor)

GND (on sensor) to negative (blue line)

Echo (on sensor) to Digital 12

Trig (on sensor) to Digital 13

VCC (on sensor) to positive (red line)

Step 3: Constructing the Outer Box

Prepare and cut out the cardboards:

(19*9)*2 pcs [front and back]

(19*13)*1 pcs [bottom]

(9*13)*2 pcs [sides]

(12*19)* 1 pcs [top]

Tape [top]

Optional:

Make holes or doors for wires or fixes

Labels

Step 4: Final Product

Yeah!

Be the First to Share

    Recommendations

    • Metal Contest

      Metal Contest
    • DIY Summer Camp Contest

      DIY Summer Camp Contest
    • Fandom Contest

      Fandom Contest

    Comments