Espruino and LDR example

A light-dependent resistor is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits.

A photoresistor is made of a high resistance semiconductor. In the dark, a photoresistor can have a resistance as high as several megohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms. If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons (and their hole partners) conduct electricity, thereby lowering resistance. The resistance range and sensitivity of a photoresistor can substantially differ among dissimilar devices. Moreover, unique photoresistors may react substantially differently to photons within certain wavelength bands.

As you can see later in the schematics its easy to connect one of these to an Espruino, you only require an LDR and a 10k resistor so that this could easily be constructed on a breadboard. I used the following module

ldr

Schematics and Layout

The layout below shows how to connect an LDR to our Espruino

 

espruino-and-ldr_bb

Code

A simple code example. So vary the light to the LDR and see what happens.

Type the following into the Espruino web ide and the Send to your Espruino

setInterval(function() {
 var light = analogRead(A1);
 console.log(light);
}, 1000);

Here is another code example that will switch on and off one of the on board LEDs depending on the reading from the LDR

setInterval(function() {
  var light = analogRead(A1);
  console.log(light);
  //the value is between 0 and 1
  if(light >= 0.5)
  {
    digitalWrite(LED1, 0); //led off
  }
  else
  {
    digitalWrite(LED1, 1); //led on
  }
}, 1000);

Now cover and uncover the LDR to see the on board LED go on and off

 

Links
20PCS x 5528 Light Dependent Resistor LDR 5MM Photoresistor