Robojax

Arduino code and video for Light Sensor Module with Relay

دروس آردوینو به فارسی

Light Sensor module for Arduino (basic digital)

This video shows you how to turn an AC light ON during the night using Light sensor and turn it OFF during the day. This code is basic version where digital output of the module is used.

 /*
 * This is the Arduino code for Light module for Arduino (basic)
This video shows you how to turn an AC light ON during the night using Light sensor 
and turn it OFF during the day. This code is basic version where 
digital output of the module is used.
 // Written for Robojax.com video 

 * watch HC-SR505 Motion Sensor video for details https://youtu.be/qhThpxiXubI
 * Code is available at http://robojax.com/learn/arduino

 * 
 // Writeen by Ahmad S. for Robojax.com on 
// on Freb 10, 2018 at 13:43 at city of Ajax, Ontario, Canada
*/
#define LIGHT 2 // define pint 2 for sensor
#define RELAY 9 // define pin 9 as for relay

/*
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */

void setup() {
  // Light LDR Sensor Code by Robojax.com 20180210
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(LIGHT, INPUT_PULLUP);// define pin as Input  sensor
  pinMode(RELAY, OUTPUT);// define pin as OUTPUT for relay
}

void loop() {
  // Light LDR Sensor Code by Robojax.com 20180210
  int L =digitalRead(LIGHT);// read the sensor 
  
      if(L == 1){
    Serial.println(" light is ON");
    digitalWrite(RELAY,LOW);// turn the relay ON
  }else{

     Serial.println("  === light is OFF");
     digitalWrite(RELAY,HIGH);// turn the relay OFF
  }
  delay(500);
 // Light LDR Sensor Code by Robojax.com 20180210
}
   

Light Sensor module for Arduino (Advanced: analog)

This video shows you how to turn an AC light ON during the night using Light sensor and turn it OFF during the day. This code is to control light either with Digital and control the sensitivity with the potentiometer on the module. You can control light using analog value from pin A0 and set your own sensitivity. Make use you turn the potentiometer to the maximum value and set the digital=false.

 /*
Light Sensor module for Arduino (Advanced: analog)
This video shows you how to turn an AC light ON during the night using 
Light sensor and turn it OFF during the day. 
This code is to control light either with Digital and control 
the sensitivity with the potentiometer on the module. 
You can control light using analog value from pin A0
and set your own sensitivity. Make use you turn the 
potentiometer to the maximum value and set the digital=false.
 // Written for Robojax.com video 

 * watch Light Sensor video https://youtu.be/CyDAWcY8_5w
 * Code is available at http://robojax.com/learn/arduino
 * 
*/
#define LIGHT 2 // define pint 2 for sensor
#define RELAY 9 // define pin 9 as for relay

/*
 * 
// Writeen by Ahmad S. for Robojax.com on 
// on Freb 10, 2018 at 13:43 at city of Ajax, Ontario, Canada
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purpose only.
 * 
 */



// LDR, Light Dependant Resistor


boolean digital = false;// set true to use digital and control the sensitiviety with poentiometer on the module
                  // set to false to control with A0 value on Arduino
unsigned int LightValue = 350;// LightValue to determine
                      // at what value the realy should be ON                      

void setup() {
  // Light LDR Sensor Code by Robojax.com 20180210
  Serial.begin(9600);// setup Serial Monitor to display information
  pinMode(LIGHT, INPUT_PULLUP);// define pin as Input  sensor
  pinMode(RELAY, OUTPUT);// define pin as OUTPUT for relay
}

void loop() {

  delay(500);
  relay();// call the realay() method
 // Light LDR Sensor Code by Robojax.com 20180210
}

void relay()
{
   if(digital == true)
   {
  // Light LDR Sensor Code by Robojax.com 20180210
  int L =digitalRead(LIGHT);// read the sensor 
  
      if(L == 1){
        Serial.println(" light is ON");
        digitalWrite(RELAY,LOW);// turn the relay ON
      }else{
         Serial.println("  === light is OFF");
         digitalWrite(RELAY,HIGH);// turn the relay OFF
      } // if   
   }// if digital end
   else
   {
    int a0Value = analogRead(A0);// read A0 value
    
      if( a0Value >= LightValue){
        Serial.print(analogRead(A0));
        Serial.println(" Light is ON");
        digitalWrite(RELAY,LOW);// turn the relay ON
      }else{
         Serial.print(analogRead(A0));
         Serial.println("  === light OFF");
         digitalWrite(RELAY,HIGH);// turn the relay OFF
      } // if      
   }
}