PIR motion sensor LED light

Hey guys!
I have tried and tested the code that ill write below, this code is for switching On the lights when motion is detected.

Now I want to add a Fade effect to my lights so that it gradually lights up and also does the same when the lights go out. Can you guys help me with the code? like, edit the code that I've written below specifically?

int sensor_pin = 8; // Initialized the pin for motion sensor
int relay_pin = 9; // Initialized the pin for relay module
int output; // Variable to store the output state of motion sensor

void setup(){
Serial.begin(9600);
pinMode(sensor_pin, INPUT); // Declared motion sensor pin as Input
pinMode(relay_pin, OUTPUT); // Declared relay module pin as Output pin
// Make the relay module initial state as low, Relay works opposite
digitalWrite(relay_pin, HIGH);
}

void loop(){
// Read the output state of motion sensor
output = digitalRead(sensor_pin);

// If output is High
if(output == 1){
digitalWrite(relay_pin, LOW);
delay(30000);}

// If output is LOW
else if(output == 0){
digitalWrite(relay_pin, HIGH);
}

Serial.println(output);
delay(0);
}

What light? The ones connected to the relay? Give some info about the lights, Are they AC lights or DC lights?

The code indicates it is a relay. You can't fade a relay. You can fade an individual LED if it is connected to a PWM output.

int sensor_pin = 8; // Initialized the pin for motion sensor
int led = 9; // Initialized the pin for relay module
int output; // Variable to store the output state of motion sensor

void setup(){
Serial.begin(9600);
pinMode(sensor_pin, INPUT); // Declared motion sensor pin as Input
pinMode(led, OUTPUT); // Declared led pin as Output pin

}

void loop(){
// Read the output state of motion sensor
output = digitalRead(sensor_pin);

// If output is High
if(output == 1){
For(int i=0; i<255; i++){

digitalWrite(led , i);
delay(30); // controls the fading
}

// If output is LOW
else if(output == 0){
For(int i=255; i>0; i--) {

digitalWrite(led , i);
delay(30); // controls the fading

digitalWrite(led , i); // the led Should be connected to a pwm enabled output like pin 3
}

Serial.println(output);
delay(0);
}

Instead of connecting a relay(because I don't see the need for that) you could connect a led to the Arduino and (with a limiting resistor) and make the led fade as a person or object approaches the sensor

Sorry, so I connected a 5 meter LED strip to it through a relay, so the project has a motion sensor that is connected to a relay and when the motion sensor is activated, the relay is energized and the circuit is completed to switch on the lights. The lights are DC and I've connected a rectifier.

By powering the LED's though a relay the circuit to fade in and fade out is a bit more complicated than it needs to be. You could drive the LED strip with a xsistor/MOSFET and use PWM to control brightness as well as do the fade in fade out.

Use the words "arduino fade in fade out led strip" in an internet search for lots of info on ho to do the thing do.

1 Like

I take it the LED string is not a programmable LED strip?

Yeah, there's no such module with the led light, though there's one video on YouTube where the guy has managed to pull it off, but it isn't clear in its description

ALITOVE 3.2ft 60 Pixels WS2812B Individual Addressable RGB LED Strip Light Programmable WS2811 IC

Easy to fade in and out.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.