Ultrasonic Sensor

by ngb71 in Circuits > Arduino

182 Views, 0 Favorites, 0 Comments

Ultrasonic Sensor

5D84FBF1-901B-4D2E-AB15-83D2BB1F6982.jpeg
ECFB20EA-5900-4CE5-A85D-8D6D3D5F4C28.jpeg

This sensor is meant to detect objects that are nearby it. The LEDs light up if something is nearby, and then they get dimmer if something is getting farther away.

I chose this project because I thought this technology was intriguing and that it could be used for several things. It can detect motion, touch, and perhaps even how translucent something is. For this project, I would like to test it in the future and see what exactly it's capable of. This project is also easy to understand, so that's why I also chose it. I think this sensor would make a fairly good replacement for a motion or touch sensor since it operates similarly.

Supplies

7 x 220 resistors
7 x LEDs
1 x Ultrasonic sensor
1 x Elegeco arduino uno
7 x medium length jumper wires
5 x long jumper wires
1 x breadboard

Assembly

96FA1470-CF28-451F-BC37-B25DDC59F840.jpeg
C5B25D77-10E2-44F7-A17C-D67F2174F4D4.jpeg
46070AB0-A4C9-49D3-8BE7-678210CDAC94.jpeg
ultrasonic sensor_bb.jpg
Add the sensor to the breadboard
Connect 4 of the long jumper wires from the sensor to the elegoo uno as follows:
VCC -> 5V
Trig -> 12
Echo -> 13
Gnd-> GND (on the positive side of the elegoo)

Moving on to the LEDs and resistors,
Connect the longer side of the LED to the negative side of the breadboard, do this for all 7 LEDs.
Proceed to connect the resistor to the other side of the LED.

Prepare your medium length jumper wires and begin to place them on the other end of the resistor and connecting them to the elegoo as follows:
LED1 -> 2
LED2 -> -3
LED3 -> 4
LED4 -> -5
LED5 -> -6
LED6 -> 7
LED7 -> 8

Lastly, grab one of your longer jumper wires and connect it from the negative side of the breadboard (where you put one side of the LEDs) and connect it to the GND on the negative side of the elegoo.

Coding

1C73B37F-6F7A-4794-AA3F-B67A8D394694.jpeg
Upload this code to your arduino application:

const int trig = 12;
const int echo = 13;

const int LED1 = 8;
const int LED2 = 7;
const int LED3 = 6;
const int LED4 = 5;
const int LED5 = 4;
const int LED6 = 3;
const int LED7 = 2;

int duration = 0;
int distance = 0;

void setup()
{
pinMode(trig , OUTPUT);
pinMode(echo , INPUT);

pinMode(LED1 , OUTPUT);
pinMode(LED2 , OUTPUT);
pinMode(LED3 , OUTPUT);
pinMode(LED4 , OUTPUT);
pinMode(LED5 , OUTPUT);
pinMode(LED6 , OUTPUT);
pinMode(LED7 , OUTPUT);

Serial.begin(9600);

}

void loop()
{
digitalWrite(trig , HIGH);
delayMicroseconds(1000);
digitalWrite(trig , LOW);


duration = pulseIn(echo , HIGH);
distance = (duration/2) / 28.5 ;
Serial.println(distance);


if ( distance <= 7 )
{
digitalWrite(LED1, HIGH);
}
else
{
digitalWrite(LED1, LOW);
}
if ( distance <= 14 )
{
digitalWrite(LED2, HIGH);
}
else
{
digitalWrite(LED2, LOW);
}
if ( distance <= 21 )
{
digitalWrite(LED3, HIGH);
}
else
{
digitalWrite(LED3, LOW);
}
if ( distance <= 28 )
{
digitalWrite(LED4, HIGH);
}
else
{
digitalWrite(LED4, LOW);
}
if ( distance <= 35 )
{
digitalWrite(LED5, HIGH);
}
else
{
digitalWrite(LED5, LOW);
}
if ( distance <= 42 )
{
digitalWrite(LED6, HIGH);
}
else
{
digitalWrite(LED6, LOW);
}
if ( distance <= 49 )
{
digitalWrite(LED7, HIGH);
}
else
{
digitalWrite(LED7, LOW);
}
}