Light Sensor

Light sensor

In this example we will demonstrate how to use a Grove Light Sensor to control an LED. The LED will be automatically switched on or off depending on the level of light hitting the sensor. This is a good example of how to use analog input to control digital output. In this case an analog signal is processed and converted to a digital signal based on a threshold value.

Required Components400px-Grove_-_touch_sensor_Photo

  • SODAQ Mbili Board
  • 0.5W Solar Panel
  • 1aH Battery Pack
  • Grove Light Sensor
  • Grove LED (any colour)
  • 2x Grove Cable (any length)

Required Libraries

  • none

Hardware Setup

You should refer to both the board diagram and Grove sockets page for additional information.

  1. First, plug the Light Sensor into the socket for the analog pins A4 A5.
  2. Then, plug the LED into the Grove socket for the digital pins D4 D5.
  3. Next, plug the 0.5W solar panel and the 1A LiPo battery into their respective sockets.

Turn on the SODAQ Mbili board, compile and upload the following sketch from the Arduino IDE onto the SODAQ Mbili board, and then unplug the USB cable from the computer when it has completed the upload.

Sketch Code

#define LED_PIN 4 //Use digital pin 4 for the LED
#define SENSOR_PIN A4 //Use analog pin A4 for the sensor
#define THRESHOLD_VALUE 50 //Activation threshold

void setup() 
{
  //Set the LED digital pin to OUTPUT mode
    pinMode(LED_PIN, OUTPUT); 
}

void loop() 
{
    //Read the analog value from the sensor
    int sensorValue = analogRead(SENSOR_PIN); 
    //Calculate the resistance from the sensor
    float rSensor=(float)(1023-sensorValue)*10 / sensorValue; 

    //Compare the calculated resistance against the threshold
    if (rSensor > THRESHOLD_VALUE)
    {
    //If the result is above the threshold, turn the LED on     
        digitalWrite(LED_PIN, HIGH);
    }
    else
    {
    //If not, turn the LED off
        digitalWrite(LED_PIN, LOW);     
  }
}

If you cover the Light Sensor with your hand, you should see the LED light up.

Sketch Code Breakdown

Globals

Here we define which pins will be used for both the LED and the Light Sensor. Additionally, we specify an activation threshold for the digital signal. If the processed analog signal is above this threshold then the digital signal (and the LED) is activated.

#define LED_PINВ 4 //Use digital pin 4 for the LED
#define SENSOR_PINВ A4 //Use analog pin A4 for the sensor
#define THRESHOLD_VALUEВ 50 //Activation threshold

setup()

In the setup method we set the specified LED digital pin to OUTPUT mode (using thepinMode() method). There is no need to specify the usage mode for the analog pin connected to the Light Sensor.

void setup() 
{
  //Set the LED digital pin to OUTPUT mode
    pinMode(LED_PIN, OUTPUT); 
}

loop()

The first step is to read the raw analog value from the sensor pin. This uses a call toanalogRead() which returns a 10bit unsigned integer value which has a range of 0…1023. The value is then converted to a logarithmic floating point value representing the resistance across the sensor. This resistance value is compared to the threshold value, if it is above the threshold the digital pin connected to the LED is set to HIGH otherwise it is set to LOW (using the digitalWrite() method).

void loop() 
{
    //Read the analog value from the sensor
    int sensorValue = analogRead(SENSOR_PIN);

    //Calculate the resistance from the sensor
    float rSensor=(float)(1023-sensorValue)*10 / sensorValue; 

    //Compare the calculated resistance against the threshold
    if (rSensor > THRESHOLD_VALUE)
    {
    //If the result is above the threshold, turn the LED on     
        digitalWrite(LED_PIN, HIGH);
    }
    else
    {
    //If not, turn the LED off
        digitalWrite(LED_PIN, LOW);     
  }
}