Ollie Smeenk
Published

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.

Full instructions provided820
Turning on a light when it gets dark

Things used in this project

Hardware components

SODAQ Moja
×1
0.5W Solar Panel
×1
1aH battery pack
×1
Grove Light Sensor
×1
Grove Blue LED
×1
Grove w/o Buckle Cable (any length)
×2

Story

Read more

Code

code.c

C/C++
code.c
#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);
 }
}

Credits

Ollie Smeenk

Ollie Smeenk

8 projects • 6 followers
Business-oriented Tinkerer! Ready to engage in any project for- and not for profit!

Comments