Arduino Ultrasonic Sensor HC-sr04 Led + Bookshelf Light Project
by bilgesu in Circuits > Arduino
261 Views, 0 Favorites, 0 Comments
Arduino Ultrasonic Sensor HC-sr04 Led + Bookshelf Light Project
A bookshelf light that lights up when an object or your hand is close to the sensor. The design is minimal and rectangular so it doesn't get too much attention and can fit in like a book.
Supplies
Component requirements
Arduino Uno
Ultrasonic sensor Hc-sr04
7 x LEDs
7 x 220ohm resistor
Breadboard
Jumper wire
Watch This Video
watch this tutorial for detailed visual explanation.
Gather the Components
Make the Connections
Connect Ultrasonic sensor ( Hc-sr04 ) pins to arduino Uno pins.
1. Connnect ultrasonic sensor Trig Pin to arduino digital Pin 12.
2. Connnect ultrasonic sensor Echo Pin to arduino digital Pin 13.
3. Connnect ultrasonic sensor VCC Pin to arduino 5v Pin.
4. Connnect ultrasonic sensor GND Pin to arduino GND Pin.
Connect LEDs pins to arduino uno pins.
1. Connnect LED1(+) Pin to arduino Digital Pin 8.
2. Connnect LED2(+) Pin to arduino Digital Pin 7.
3. Connnect LED3(+) Pin to arduino Digital Pin 6.
4. Connnect LED4(+) Pin to arduino Digital Pin 5.
5. Connnect LED5(+) Pin to arduino Digital Pin 4.
6. Connnect LED6(+) Pin to arduino Digital Pin 3.
7. Connnect LED7(+) Pin to arduino Digital Pin 2.
8. Connnect all LED Negative Terminal ( - ) to arduino GND Pin.
Don't forget to add a 220ohm resistor to each led, refer the circuit diagram.
The Code
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);
}
}