Automatic garage light

Hi guys (I'm using google translate). I'm planning to use motion sensors (HC-SR501)
I have made an automatic garage lighting with arduino nano. But I can't make it work because I want the light to stay on when I'm inside but turn off after 5 minutes when I come out but I don't know how much the sensor detects motion. And because of the length of the cable there will be no signal loss. I would put the sensors on the ceiling and the signal legs in a node and then in the arduino so any good or better ideas are welcome

The code:
const int motionPin = 2; // PIR mozgásérzékelő csatlakozása a D2-es tüskére
const int relayPin = 3; // Relé csatlakozása a D3-as tüskére
const int lightTime = 1200000; // Lámpa bekapcsolási idő (20 perc = 1200000 milliszekundum)

void setup() {
pinMode(motionPin, INPUT);
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, LOW); // Kezdetben a relét kikapcsoljuk
}

void loop() {
int motionDetected = digitalRead(motionPin);

if (motionDetected == HIGH) {
// Ha mozgást érzékelünk, bekapcsoljuk a lámpát
digitalWrite(relayPin, HIGH);
delay(lightTime);
}

// Ha 20 perc eltelt, és nem érzékelt mozgást, kikapcsoljuk a lámpát
digitalWrite(relayPin, LOW);
}

Best to post diagrams and/or images, Google translate is helpful but not doing the whole job.

I hope you tried writing some code. Please post it, framing it in code tags for proper display in the forum.

1 Like

Which processor are you using?

1 Like

Atmega328

That value doesn't fit in an int on a 328

Hello, I am new but did you try to use delay and not 1200000 milliszekundun, remember the Arduino will read line by line and to wait 1200000 milliszekundun its not small number. :slight_smile:

I would do a different code, I would tell the arduino to add 1 every X time, let's say for example that every 60 seconds add 1 and when the sum or total is 5, for example, turn off the light in the garage and you can use a while when it reaches 5, for example... Not a break because it will return again line by line, the while would wait until you press a button or do a new command. Or a switch case. Meanwhile if there is movement do not turn off the light.

In any case it would be better a presence sensor and not a movement one. If you are standing in the garage with the movement sensor, if there is no movement the arduino will turn off the light if you do not move, for example reading a book or watching a movie in the garage... meanwhile a presence sensor when opening the door will already detect more than movement sensor, that there is a presence in the room, most of them work in an average of 11 meters 360 degrees, anyone who enters the garage or room through the window or back door will capture it the signal work different from the motion sensor, that you have to install many and point in various directions

(For exemple 1 video in Spanish, try to use CC for transation to English )

You do not have to create a new time base to time 5 minutes. 'millis()' and 'delay()' can handle delays of several weeks.

Without "breaking a sweat".

You only need to change

const int lightTime = 1200000; // Lámpa bekapcsolási idő (20 perc = 1200000 milliszekundum)

to

const unsigned long lightTime = 1200000; // Lámpa bekapcsolási idő (20 perc = 1200000 milliszekundum)

Are there any remaining problems? If so, please heed my request for images or diagrams.

1 Like

As I understand, it could be used, but such a long delay could add up to approximately 48 days to function correctly, then the Arduino would remain unstable, it will also depend on the Arduino model and etc... . For my part, I would make a more simple and basic code, the more basic and simple is better... such as add X every X time and do X thing when the sum is X, for example let the light turn off when there is no movement in the garage , when 60 seconds pass add 1 and when the total is 5 turn off the light, much simpler and less complicated than saying every 1200000 milliszekundun.

This code uses the PIR library for the motion sensor. Be sure to install the library before uploading the code to your Arduino Nano. Also, connect the motion sensor to pin 2 and the relay to pin 3.

Exemple :

// Include the library for the PIR (Presence) motion sensor
#include <PIR.h>

// Define the pins used for the motion sensor and the relay
const int motionSensorPin = 2;
const int relayPin = 3;

// Variables to control time and the sum
unsigned long previousTime = 0;
unsigned long interval = 60000; // 60 seconds
int sum = 0;
const int totalSum = 5;

void setup() {
// Set the relay pin as output
pinMode(relayPin, OUTPUT);

// Initialize the motion sensor
PIR.begin(motionSensorPin);
}

void loop() {
// Read the state of the motion sensor
int motionDetected = PIR.motionDetected(motionSensorPin);

// Check if there is motion
if (motionDetected) {
// If there is motion, turn on the light and reset the sum
digitalWrite(relayPin, HIGH);
sum = 0;
} else {
// If there is no motion, check the elapsed time
unsigned long currentTime = millis();
if (currentTime - previousTime >= interval) {
// If the interval of time has passed, increment the sum
sum++;
previousTime = currentTime;
}
// Check if the total sum is reached to turn off the light
if (sum >= totalSum) {
digitalWrite(relayPin, LOW); // Turn off the light
}
}
}

The code checks if there is motion detected by the PIR sensor. If there is movement, the garage light turns on and the sum is reset to zero. If there is no movement, the code waits 60 seconds before incrementing the sum. When the sum reaches the value of 5, the garage light turns off automatically.

What such a long delay? You specified 5 minutes.

For my part, I would make a more simple and basic code, the more basic and simple is better... such as add X every X time and do X thing when the sum is X, for example let the light turn off when there is no movement in the garage , when 60 seconds pass add 1 and when the total is 5 turn off the light, much simpler and less complicated than saying every 1200000 milliszekundun.

Okay, but where is your "X time" coming from? It sounds like you want to count seconds instead of milliseconds. How do you propose to do that?

Also, please consult the forum introductory thread which explains the need and method for using code tags when posting code in the forum.

Your code is badly in need of standard code block indentation, which is easy in the IDE by entering ctrl-T or selecting auto-format in the IDE menu.

This code uses the PIR library for the motion sensor. Be sure to install the library before uploading the code to your Arduino Nano. Also, connect the motion sensor to pin 2 and the relay to pin 3.

You obviously didn't write that, it sounds like a tutorial or chatGPT output. So are we actually looking at your code, or someone else's? If someone else, please open up and show us where you are borrowing code from. Most of us don't watch videos. So if the video you posted is your source, please extract and post any information from the video page, that is appropriate.

This is just an example, you can get X from the sensor,
if (motionDetected) {
// If there is motion, turn on the light and reset the sum
digitalWrite(relayPin, HIGH);
sum = 0;

I'm not using a computer to write the code, but I prefer a simple code like this. If there is movement, the garage light turns on using a 'sum' variable to turn it off. It's easy to use '++' to count and reset to zero or a boolean (true/false)

For instance, when there is no movement, the code waits for 60 seconds before setting 'sum' to positive . When 'sum' reaches a value of 5 (which is considered true), the garage light automatically turns off. It's straightforward to implement and understand.

Of course, there are various ways to approach this, and one idea is to incorporate a photoresistor to detect natural light in the garage. This way, the light won't turn on unnecessarily during daylight hours.

The final code's design will ultimately depend on the user's specific needs. In my opinion, keeping it basic is better, especially for simple tasks like turning a light on and off. However, if more complex conditions are required, defining the code using ChartGPT might not be feasible. I am new to Arduino I only have 3 months in this I must confirm that I am still learning, sorry if the code is not valid.

Thanks, I didn't say your code was invalid. I only urged you to format it with some simple IDE and forum tools that would make it much easier for forum members to view and download for testing and understanding.

If you want simplicity, you can't get much simpler than the following logic:

repeat
{
  if car detected
  {
    turn on light
    wait 5 minutes
    turn off light
  )
}

The final code's design will ultimately depend on the user's specific needs.

Well, that's commendable, it's always a good idea to know the needs before programming. This statement implies that you are producing interim code, so what are the interim requirements?

1 Like

Nice idea, we can always improve the code :smile:

repeat
{
if person detected
{
turn on light
wait 5 minutes
turn off light
)
}

How about some boolean expressions? :slight_smile:

repeat
{
  if car detected or person detected
  {
    turn on light
    wait 5 minutes
    turn off light
  )
}

Anyway, with the last non-indented, non-code tagged reply I can see where this is going. Good luck!

1 Like

Thanks, for our part we left many ideas. I hope that our friend has been able to solve his problem. See you :slight_smile:

1 Like

Seems a bit silly to use an Arduino just for a 5min motion sensor.
The HC-SR501 already has a built-in (3sec to 7min) timer, than can be set with the "time" pot. And it has a re-trigger option (jumper). The output of an HC-SR501 syould be able to directly drive an "active high" relay.
Leo..

Nice to know HC-SR501 already has a built-in (3sec to 7min) timer, It appears that the intention is not to turn off the garage light while someone is inside the garage, but rather to automatically turn it off after being outside the garage for 5 minutes. However, the challenge is that we not knowing for certain whether there is sufficient natural light present. As I mentioned in the earlier code, the light is currently triggered by the motion sensor, but to address this issue, we would require additional programming, such as incorporating a photoresistor. This would allow us to detect the level of natural light and prevent the light from turning on unnecessarily inside the garage. The evolution of this project will undoubtedly interesting to observe. :slightly_smiling_face:

Simple enough...standard switching while inside.
Most domestic or commercial light switches have a changeover contact (allows for 2 way switching).
Use that contact to start your timer when the light switch is turned to the normal off position.(no sensor required)
Another is to have the sensor setup so any movement within the room re-triggers the time-out.
Common way we used for hospitals way back when.

Under the dome of that sensor is a provision to solder in an LDR.
The sensor will then automagically only turn the light on when it's dark.
Leo..

Interesting...

Under the dome of that sensor is a provision to solder in an LDR.
The sensor will then automagically only turn the light on when it's dark.

Well we would need to create a timer to prevent it from turning on at X hour in the darkness if there's no movement. With the Nano RP2040, I connected to Wi-Fi for automatically detect the time upon startup. However, with the Atmega328, maybe the nano basic there is no Wi-Fi but it's possible to create a clock, but we need to use an adapter to provide power too, to avoid losing the time in case of a power outage or if the Arduino loses power.
Anyway, using an Atmega328 to control the light in a garage is not be the best solution. While it can work for a basic light control, there are better alternatives available. If your only requirement an simple light control, to use others options solution would be better.
I have left a video above with another option, I would recommend a presence sensor rather than a movement sensor.

With LDR fitted, the sensor will only output a HIGH when it's dark AND there is movement.
Leo..