Haziq Afandi
Published

Motion Activated Night Light

Lights from LEDs will light up as soon as the PIR motion sensor detects a motion.

BeginnerFull instructions provided2 hours27,472
Motion Activated Night Light

Things used in this project

Story

Read more

Schematics

Real life diagram

really simple but might look complicated with the wiring.

Code

Simple code that works

C/C++
/*#define Bpin 8;
#define Rpin 9;
#define Gpin 10;*/
/*int Bpin = analogRead(A0);
int Rpin = analogRead(A1);
int Gpin = analogRead(A2);*/
int Bpin = A0;
int Rpin = A1;
int Gpin = A2;
int Motion = 2; //PIR sensor
int state = LOW;
int val = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(Motion, INPUT);
  pinMode(Bpin, OUTPUT);
  pinMode(Rpin, OUTPUT);
  pinMode(Gpin, OUTPUT);

  //Serial.begin(9600);
}

void loop() 
{
  val = digitalRead(Motion);

if(val == HIGH)
{
  Serial.println("Menyala");
  if (state == LOW)
  {
    state = HIGH;
  }
  
// put your main code here, to run repeatedly:
for(int fade = 0; fade <=255; fade +=5)
{
analogWrite(Bpin,fade);
}
for(int fade = 255; fade >= 0; fade -=5)
{
analogWrite(Bpin,fade);
delay(10);
}
for(int fade = 0; fade <=255; fade +=5)
{
analogWrite(Rpin,fade);
delay(1000);
}
for(int fade = 255; fade >= 0; fade -=5)
{
analogWrite(Rpin,fade);
delay(1000);
}
for(int fade = 0; fade <=255; fade +=5)
{
analogWrite(Gpin,fade);
delay(1000);
}
for(int fade = 255; fade >= 0; fade -=5)
{
analogWrite(Gpin,fade);
delay(1000);
}

}

else
{
Serial.println("Tidak Menyala");
if (state == HIGH)
  {
    state = LOW;
  }
int dim = LOW;
analogWrite(Bpin,dim);
analogWrite(Rpin,dim);
analogWrite(Gpin,dim);
}

}
  
  

  

Credits

Haziq Afandi

Haziq Afandi

0 projects • 4 followers

Comments