Arduino Ultrasonic Sensor HC-sr04 Led + Bookshelf Light Project
by bilgesu in Circuits > Arduino
241 Views, 0 Favorites, 0 Comments
Arduino Ultrasonic Sensor HC-sr04 Led + Bookshelf Light Project
![IMG_1818.jpg](/proxy/?url=https://content.instructables.com/F35/QO1P/LIOLPWI5/F35QO1PLIOLPWI5.jpg&filename=IMG_1818.jpg)
![ışık2.jpg](/proxy/?url=https://content.instructables.com/FYR/YSSI/LIOLPW81/FYRYSSILIOLPW81.jpg&filename=ışık2.jpg)
![ışık3.jpg](/proxy/?url=https://content.instructables.com/F0L/8G3W/LIOLPW83/F0L8G3WLIOLPW83.jpg&filename=ışık3.jpg)
![IMG_1731.jpg](/proxy/?url=https://content.instructables.com/FST/ITHE/LIOLPWCL/FSTITHELIOLPWCL.jpg&filename=IMG_1731.jpg)
![rbgrtb.jpg](/proxy/?url=https://content.instructables.com/F7W/UMTR/LIOLPXBA/F7WUMTRLIOLPXBA.jpg&filename=rbgrtb.jpg)
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
![FD3XDXSKDIA9LQT.png](/proxy/?url=https://content.instructables.com/FLE/WZKE/LIOLPWJF/FLEWZKELIOLPWJF.png&filename=FD3XDXSKDIA9LQT.png)
Component requirements
Arduino Uno
Ultrasonic sensor Hc-sr04
7 x LEDs
7 x 220ohm resistor
Breadboard
Jumper wire
Watch This Video
![Instructables tutorial-bookshelf light](/proxy/?url=https://content.instructables.com/F5W/8R3L/LIOLPXC9/F5W8R3LLIOLPXC9.jpg&filename=Instructables tutorial-bookshelf light)
watch this tutorial for detailed visual explanation.
Gather the Components
![FD3XDXSKDIA9LQT.png](/proxy/?url=https://content.instructables.com/F6L/B5MN/LIOLPWLZ/F6LB5MNLIOLPWLZ.png&filename=FD3XDXSKDIA9LQT.png)
Make the Connections
![FJPX0MDKDFYHO7Z.jpg](/proxy/?url=https://content.instructables.com/F5S/UNC2/LIOLPWMZ/F5SUNC2LIOLPWMZ.jpg&filename=FJPX0MDKDFYHO7Z.jpg)
![F33XWVTKDFYHO80.png](/proxy/?url=https://content.instructables.com/FSK/BUQZ/LIOLPWN0/FSKBUQZLIOLPWN0.png&filename=F33XWVTKDFYHO80.png)
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);
}
}