This website uses cookies to ensure you get the best experience on our website.
Make an Arduino Fading Light

Build an Arduino Uno Fading Light for a Corridor or Bathroom

The code is written in Arduino IDE and it is used to control a LED light based on the input from a PIR (Passive Infrared) sensor and an LDR (Light Dependent Resistor) sensor. The PIR sensor detects motion, while the LDR sensor detects ambient light level.

The code starts by defining the input and output pins for the sensors and LED, respectively. The inputPin variable is set to 10, which is the pin connected to the PIR sensor. The LDR variable is set to 0, which is the pin connected to the LDR sensor. The ledPin variable is set to 11, which is the pin connected to the LED.

In the setup function, the serial monitor is started to display debug messages. The inputPin and ledPin are defined as inputs and output, respectively.

In the loop function, the value of the LDR sensor is read using the analogRead function and stored in the valone variable. Then, the value of the PIR sensor is read using the digitalRead function and stored in the val variable.

If the value of val is HIGH (motion detected) and the value of valone is less than 100 (darkness setting), the LED is turned on using the digitalWrite function. The LED remains on for 30 seconds, which is the delay set by the delay function. After 30 seconds, the LED starts to fade off gradually using the for loop. The analogWrite function is used to set the brightness of the LED, and the delay function is used to control the fading speed. Finally, the digitalWrite function is used to turn off the LED completely.

int inputPin = 10;    // choose the input pin (for the PIR sensor)

int LDR = 0;          // select the input pin for the LDR:A0

int ledPin = 11;      // select the pin for the LED:gate of mosfet

int valone = 0;       // variable to store the value coming from the sensor


void setup() {


  Serial.begin(9600);       //start te serial monitor


  pinMode(LDR, INPUT);       // declare the LDR as an INPUT

  pinMode(ledPin, OUTPUT);   // declare the ledPin as an OUTPUT: Connect IRF520 gate here.

  pinMode(inputPin, INPUT);  // declare pushbutton as input:PIR

}


void loop() {

  valone = analogRead(LDR);        // read the value from the sensor

  int val = digitalRead(inputPin); // read input value from PIR sensor

  //Serial.println(valone);        //prints the LDR values to serial monitor

  if (val == HIGH && valone < 100) // check if the input is HIGH from PIR and LDR and darkness setting

  {

    digitalWrite(ledPin, HIGH);    // turn LED pin 11 on if motion detected

    delay(30000);

    int delayTime = 50;

    for (int i = 255; i > 0; i--)  // slowly turn the led off

    {

      analogWrite(ledPin, i);

      delay(delayTime);

      digitalWrite(ledPin, LOW);

    }

  }

}

Leave a Reply

Your email address will not be published. Required fields are marked *


Math Captcha
6 + 1 =