SODAQ
Connect Anything Anywhere

2. Turning on a light when it gets dark
In this tutorial you will learn how to use a Grove Light Sensor and connect it to a SODAQ Moja and a Blue LED. You will also learn how to make the LED turn on automatically at a certain threshold value for light intensity. Here we show the true value of automatic sensor systems!
Required components:
SODAQ Moja
0.5W Solar Panel
1aH battery pack
Grove Light Sensor
Grove Blue LED
2x Grove w/o Buckle Cable (any length)
First, plug the Light sensor into the A0/A1 pin in the always on row.
Then, plug the Blue LED into the D4/D5 pin in the always on row.
Next, plug the 0.5W solar panel and the 1A LiPo battery into the respective sockets (see http://www.sodaq.net/#!getting-started/c21ma for a board diagram)
Turn on the SODAQ board, compile and upload the following sketch from the arduino IDE onto the SODAQ Board, and then unplug the USB cable from the computer when it is working:


#include <math.h>
const int ledPin=4;
//Connect the LED Grove module to Pin4, Digital 4
const int thresholdvalue=50;
//The threshold for which the LED should turn on.
float Rsensor; //Resistance of sensor in K
void setup() {
pinMode(ledPin,OUTPUT); //Set the LED on Digital 4 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);
}
}