Home Esplora Projects Esplora LDR example

Esplora LDR example

by shedboy71

In this example we will look at the RGB led and the LDR that are fitted to the Esplora.

You can see these in the image below

esplora

Code

This is a basic example that will display the reading from the LDR via the serial port

[codesyntax lang=”cpp”]

#include <Esplora.h>

void setup()
{
 Serial.begin(9600);
} 

void loop()
{
 int value = Esplora.readLightSensor();
 Serial.println(value);
 delay(1000);
}

[/codesyntax]

OPen the serial monitor in the Arduino IDE and cover and uncover the LDR, you should see something like the following. The low value is dark

932
934
461
426
451
389
930
105
878
931

 

Now we will take this one step further, if the value is lower than 600 then we will make the RGB led go red otherwise it will be green. Effectively a darkness detection

[codesyntax lang=”cpp”]

#include <Esplora.h>

void setup()
{
  Serial.begin(9600);
} 

void loop()
{
  int value = Esplora.readLightSensor();
  Serial.println(value);
  if(value <600)
  {
    Esplora.writeRGB(255, 0, 0);
  }
  else
  {
    Esplora.writeRGB(0, 255, 0);
  }
  delay(1000);
}

[/codesyntax]

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More