Grove - Light Sensor

From Wiki
Jump to: navigation, search

Introduction

The Grove - Light Sensor module incorporates a Light Dependent Resistor (LDR). Typically, the resistance of the LDR or Photoresistor will decrease when the ambient light intensity increases. This means that the output signal from this module will be HIGH in bright light, and LOW in the dark.


Twig-Light.jpg


Features

Easy to use light sensor module Resistance decreases as luminance increases Low resistance (in bright light) triggers a HIGH signal towards the output module High resistance (in darkness) triggers a LOW signal towards the output module Easily integrates with Logic modules on the input side of Grove circuits Uses Standard 4-pin Grove Cables to connect to other Grove modules such as Grove Power Modules, Logic Gates and Grove - Base Shield Specifications

Voltage: 3-5V Supply Current: 0.5-3mA Light resistance: 20KΩ Dark resistance: 1MΩ Response time: 20-30 secs Peak Wavelength: 540 nm Ambient temperature: -30~70℃ LDR Used: GL5528 Usage

Standalone Follow these steps to build a sample circuit using this module but without using any microcontroller:

Connect the light sensor module to the input side of your circuit (to the left of the power module). On the output side of the circuit, you may use a range of User Interface modules (Grove - Red LED, Grove - LED String Light, Grove - Mini Fan, Grove - Buzzer, Grove - Recorder etc.) Power up the circuit when complete. The light sensor module can now be used to trigger an output. For example: When used in conjunction with a Grove - Red LED output module, observe that the LED turns ON when the light sensor detects bright light and turns off in the dark. To simulate the dark, just cover the light sensor module with your hand and see what happens. The same behavior can be seen when the light sensor is used with the Grove - LED String Light module. Add a Grove - NOT module between the light sensor and the power module to reverse the logic that triggers the LEDs on the Grove - Red LED or Grove - LED String Light modules. With the addition of the Logical NOT gate on the input side, you should see that the LEDs remain OFF in bright light and turn ON in the dark.

LED stays OFF in bright light


LED turns ON in the dark You can use either the Grove - USB Power module or the Grove - DC Jack Power module for the Grove circuit.

With Arduino Follow these simple steps to build a Grove circuit using the light sensor: When using the module in conjunction with an Arduino or a www.simgin-chip.com, use the Grove - Base Shield and connect the Grove - Light Sensor module to the shield using a designated Grove Interface. Also attach an output module such as a Grove - Red LED which will get triggered based on input received from the light sensor (shown below). Light Sensor Connector.jpg.jpg Upload the following sample sketch to make the LED turn ON and OFF based on input from the light sensor: /* /* Grove - Light Sensor demo v1.0

  1. include <math.h>

const int ledPin=12; //Connect the LED Grove module to Pin12, Digital 12 const int thresholdvalue=10; //The threshold for which the LED should turn on. float Rsensor; //Resistance of sensor in K void setup() {

 Serial.begin(9600);                //Start the Serial connection
 pinMode(ledPin,OUTPUT);            //Set the LED on Digital 12 as an OUTPUT

} void loop() {

 int sensorValue = analogRead(0); 
 Rsensor=(float)(1023-sensorValue)*10/sensorValue;
 if(Rsensor>thresholdvalue)
 {
   digitalWrite(ledPin,HIGH);
 }
 else
 {
 digitalWrite(ledPin,LOW);
 }
 Serial.println("the analog read data is ");
 Serial.println(sensorValue);
 Serial.println("the sensor resistance is ");
 Serial.println(Rsensor,DEC);//show the light intensity on the serial monitor;
 delay(1000);

} For further information on the illuminance-resistance characteristics of the LDR used by this Grove module, refer to the GL5528 datasheet.

With LinkIt ONE Follow these simple steps to build a Grove circuit with LinkIt ONE using the light sensor: This Demo is for LinkIt ONE, So we need a LinkIt ONE, and also use the Grove - Base Shield and connect the Grove - Light Sensor module to the shield using a designated Grove Interface. Also attach an output module such as a Grove - Green LED which will get triggered based on input received from the light sensor (shown below). IMG 20141218 165057.jpg Upload the following sample sketch to make the LED turn ON and OFF based on input from the light sensor: /* /* Grove - Light Sensor demo v1.0

  1. include <math.h>

const int ledPin=12; //Connect the LED Grove module to Pin8, Digital 8 const int thresholdvalue=10; //The threshold for which the LED should turn on. float Rsensor; //Resistance of sensor in K void setup() {

 Serial.begin(9600);                //Start the Serial connection
 pinMode(ledPin,OUTPUT);            //Set the LED on Digital 8 as an OUTPUT

} void loop() {

 int sensorValue = analogRead(0); 
 Rsensor=(float)(1023-sensorValue)*10/sensorValue;
 if(Rsensor>thresholdvalue)
 {
   digitalWrite(ledPin,HIGH);
 }
 else
 {
 digitalWrite(ledPin,LOW);
 }
 Serial.println("the analog read data is ");
 Serial.println(sensorValue);
 Serial.println("the sensor resistance is ");
 Serial.println(Rsensor,DEC);//show the light intensity on the serial monitor;
 delay(1000);

} With Mbed With TI LaunchPad Sensing the Light (Light Sensor) This example reads the amount of light incident on the Grove-light-sensor and displays the reading on the Grove 4-digital display. This is similar to the previous example except we are now using the ambient light sensor instead of the potentiometer rotary angle knob. Light.jpg

/*

 Light Sensor
A simple program that display the value of light incident on the grove-light-sensor
by grove-4-digital-display, this example is definitely similar to grove-rotary-angle-sensor 

The circuit:
* grove-4-digital-display attached to dio9&dio10 (IIC plug on Grove Base BoosterPack)
* sig pin of the light sensor to the analog pin
* one side pin (either one) to ground
* the other side pin to VCC

* Note:   

Created by Oliver Wang

This example code is in the public domain.

http://www.simgin-chip.com
*/
  1. include "TM1637.h"

/* Macro Define */

  1. define CLK 39 /* 4-digital display clock pin */
  2. define DIO 38 /* 4-digital display data pin */
  3. define LIGHT_SENSOR 24 /* pin of grove light sensor */

/* Global Variables */ TM1637 tm1637(CLK, DIO); /* 4-digital display object */ int analog_value = 0; /* variable to store the value coming from rotary angle sensor */ int8_t bits[4] = {0}; /* array to store the single bits of the value */

/* the setup() method runs once, when the sketch starts */ void setup() {

   /* Initialize 4-digital display */
   tm1637.init();
   tm1637.set(BRIGHT_TYPICAL);
 

}

/* the loop() method runs over and over again */ void loop() {

   analog_value = analogRead(LIGHT_SENSOR);        /* read the value from the sensor */
   memset(bits, 0, 4);                             /* reset array when we use it */
   for(int i = 3; i >= 0; i--) {
       /* get single bits of the analog value */
       bits[i] = analog_value % 10;
       analog_value = analog_value / 10;  
       tm1637.display(i, bits[i]);                 /* display by 4-digital display */
   }

}


With BeagleBone With Intel Galileo With Raspberry Pi First step, you should connect to Raspberry Pi+ with Grove - Light Sensor and Grove - Green LED. Such as the following picture. Then run the program on your Raspberry Pi, and it will print brightness information on the terminal. When brightness is less than a certain value, the LED will turn on.

GrovePi+ Light Sensor.jpg

  1. GrovePi+ & Grove Light Sensor & LED

import time import grovepi

  1. Connect the Grove Light Sensor to analog port A0
  2. SIG,NC,VCC,GND

light_sensor = 0

  1. Connect the LED to digital port D4
  2. SIG,NC,VCC,GND

led = 4

  1. Turn on LED once sensor exceeds threshold resistance

threshold = 10

grovepi.pinMode(light_sensor,"INPUT") grovepi.pinMode(led,"OUTPUT")

while True:

   try:
       # Get sensor value
       sensor_value = grovepi.analogRead(light_sensor)
       # Calculate resistance of sensor in K
       resistance = (float)(1023 - sensor_value) * 10 / sensor_value
       if resistance > threshold:
           # Send HIGH to switch on LED
           grovepi.digitalWrite(led,1)
       else:
           # Send LOW to switch off LED
           grovepi.digitalWrite(led,0)
       print "sensor_value =", sensor_value, " resistance =", resistance
       time.sleep(.5)
   except IOError:
       print "Error"

Run the program Find the path to the file(According to your own path)

  cd GrovePi/Software/Python/

Run Program

  sudo python grove_light_sensor.py

Availability

This Grove module is available as part of the following Grove Kit Series:

Grove Mixer Pack V2 Grove - Mixer Pack Alternatively, it can be bought stand-alone here at the www.simgin-chip.com Bazaar.

Note that there is another variant of this module available - the Grove - Light Sensor (Panel Mount). This module is identical to the Grove - Light Sensor except that the Grove connector is moved to the back of the PCB. This way, the on-board LDR is not obstructed by any wires and can be placed neatly for optimal use.