Am I doing it right?

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:

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 :

  1. 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.

  2. 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.

  3. Is there a way to enhance HC-SR04 wave distance?

  4. Is the code I am using good enough for prolonged use?

pulseIn returns unsigned long, not int.

Using a 12V relay seems to be complicating stuff. A 5v relay should work just fine.

Also, the Arduino nano has a 5V regulator built in - it should be able to take you 12V in on the Vin pin.

binaryfissiongames:
Using a 12V relay seems to be complicating stuff. A 5v relay should work just fine.

Also, the Arduino nano has a 5V regulator built in - it should be able to take you 12V in on the Vin pin.

Well that's what I thought too, just wondering wether it makes the 5v relay breaks waay too soon.

Nah, that's not right. The 5V in 5 volt relay refers to the trigger voltage. a 12 volt relay isn't necessarily going to last longer. The picture you're showing here for your relay is rated for 10A - you should be fine if you're under that.

binaryfissiongames:
Nah, that's not right. The 5V in 5 volt relay refers to the trigger voltage. a 12 volt relay isn't necessarily going to last longer. The picture you're showing here for your relay is rated for 10A - you should be fine if you're under that.

Yeah should be under 10A, it's just around 2-4A for the 200cm led strip.

Thank you for the insight so far guys. Hopefully someone can tell me wether my code is okay for a long time use, since idk how to make the arduino sleep to make it less abused yet (or if it's possible to do that).