Hi, I just recently got into arduino (need a hobby under quarantine and all). And I chose to try automate the led strip behind my monitor.
I read around and combine 'knowledge' based on various project using HC-SR04 sensor. And I got it to turn on and off based on the distance I want it to. However there is some things that I need to clarify with you guys, as well as asking wether the code I am currently using is good enough for prolonged use, since I use my PC often, atleast 8 hours a day combined.
But first of all, the items:
- Arduino Nano
- Cheap chinese 5v relay module
https://images-na.ssl-images-amazon.com/images/I/61IwgJRgiyL._AC_SL1024_.jpg -
A mini 5v voltage regulator module
https://ecs7.tokopedia.net/img/cache/700/product-1/2018/10/31/950213/950213_4372c7d7-4e78-44c9-a998-e6816bd6601d_800_800.png - A 12v/3A power supply
- 200cm 12v SMD5050 RGB LED Strip with controller
- a small plastic box to put everything in, kinda almost tight fit.
Wiring :
The Code :
// Use the HC-SR04 to detect object, if there is an object at 100 cm or less, switch relay to make led strip turned on, if not switch relay to make led strip turned off.
// Relay is low active
#define trigPin 5 //Define the HC-SE04 triger on pin 6 on the arduino
#define echoPin 6 //Define the HC-SE04 echo on pin 5 on the arduino
#define bulb 9 //Define the relay signal on pin 9 on the arduino
void setup()
{
Serial.begin (9600); //Start the serial monitor
pinMode(trigPin, OUTPUT); //set the trigpin to output
pinMode(echoPin, INPUT); //set the echopin to input
pinMode (bulb, OUTPUT); //set the bulb on pin 9 to output
}
void loop()
{
int duration, distance; //Define two intregers duration and distance to be used to save data
digitalWrite(trigPin, HIGH); //write a digital high to the trigpin to send out the pulse
delayMicroseconds(500); //wait half a millisecond
digitalWrite(trigPin, LOW); //turn off the trigpin
duration = pulseIn(echoPin, HIGH); //measure the time using pulsein when the echo receives a signal set it to high
distance = (duration/2) / 29.1; //distance is the duration divided by 2 becasue the signal traveled from the trigpin then back to the echo pin, then devide by 29.1 to convert to centimeters
Serial.print(distance); //Dispaly the distance on the serial monitor
Serial.println(" CM"); //in centimeters
delay(500);
if (distance <= 100) //if the distance is the same or less than 100 CM
{
digitalWrite(bulb, LOW); //turn on the light
}
else
digitalWrite(bulb, HIGH); //turn off the light
delay(500); //delay half a second
}
So my questions :
-
Should I use a 12v relay instead?, I read around and found something that says I can make arduino nano digital pin to send a 12v to trigger the relay using 2n2222 transistor or something. Since from what I asked around the 12v relay need more than 5v to trigger.
-
Is there a way to reduce the 12v to 5v without using a regulator module? if so, can someone teach me how?. Since I prefer to keep the size of the box as small as possible.
-
Is there a way to enhance HC-SR04 wave distance?
-
Is the code I am using good enough for prolonged use?