Categories

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

Microbit and LDR example

A photoresistor (or light-dependent resistor, LDR, or photocell) 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.

A photoelectric device can be either intrinsic or extrinsic. An intrinsic semiconductor has its own charge carriers and is not an efficient semiconductor, for example, silicon. In intrinsic devices the only available electrons are in the valence band, and hence the photon must have enough energy to excite the electron across the entire bandgap. Extrinsic devices have impurities, also called dopants, added whose ground state energy is closer to the conduction band; since the electrons do not have as far to jump, lower energy photons (that is, longer wavelengths and lower frequencies) are sufficient to trigger the device. If a sample of silicon has some of its atoms replaced by phosphorus atoms (impurities), there will be extra electrons available for conduction. This is an example of an extrinsic semiconductor

Here is a typical module that you can buy

 

ldr-module

 

 

Layout

 

ldr_bb

 

Code

In this example we will display the value of the LDR. In Mu , click on the Repl button and then cover the LDR

from microbit import *
 
while True:
    x = pin0.read_analog()
    print("value " + str(x))
    sleep(1000)

 

Here is another example, when the analogue value is below a certain value all the LEDs will switch on

from microbit import *
lights_off= Image('99999:99999:99999:99999:99999:')
lights_on= Image('00000:00000:00000:00000:00000:')
 
while True:
    display.show(lights_off)
    x = pin0.read_analog()
    print("value " + str(x))
    if x>300:
        display.show(lights_on)
    sleep(1000)

chipKit Uno32 and LDR example

In this example we connect an LDR to an analog pin of our Chipkit Uno32, depending on the reading we will then vary how bright an LED will be lit

This is adapted from an Arduino example, the code was written in the MPIDE

 

Schematic

chipkit uno32 pwm led

chipkit uno32 pwm led

 

Code

const int analogInPin = A0;  // Analog input pin that the potentiometer is attached to
const int PWMOutPin = 9; // Analog output pin that the LED is attached to
 
int sensorValue = 0;        // value read from the pot
int outputValue = 0;        // value output to the PWM (analog out)
 
void setup()
{
}
 
void loop()
{
// read the analog in value:
sensorValue = analogRead(analogInPin);
// map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 0, 255);
// change the analog out value:
analogWrite(PWMOutPin, outputValue);
// wait 2 milliseconds for the a/d converter to settle
delay(2);
}

 

Links

chipKit Uno32 at Amazon

PIC18f2550 dark activated light

The concept of a dark activated light is quite simple, we use a light dependent resistor (LDR) to an ADC pin on our PIC micro, we then read in a value based on the amount of light on the LDR. Full light gives the maximum value and no light gives the minimum value, so we take a low value and if our ADC goes below this we switch on an LED, real world you would use a high brightness LED.

 

Schematics

pic18f2550 and ldr led

pic18f2550 and ldr led

Code

The code is written in mikroC pro for PIC


unsigned int adcvalue,light_res;

void main()
{

TRISB = 0x00;

do {
light_res = ADC_Read(0); // Get 10-bit results of AD conversion
adcvalue = light_res;
if(adcvalue < 200)
{
PORTB = 0x01;
}
else
{
PORTB = 0x00;
}
Delay_ms(300);

} while(1);

} // end main

Links

5pcs PIC18F2550 MICROCHIP DIP-28 IC

50PCS GL5516 5516 Light Dependent Resistor LDR 5MM

PIC16F877 and LDR warning example

In this example we will connect an LDR again but this time when the ADC value goes below a certain number we will switch an LED on, in real life this could be an alarm or a night light.

The LCD is more for debug purposes and could be removed

Schematic

 

PIC16F877 LDR and LED

PIC16F877 LDR and LED

Code

The code was witten in mikroC pro for PIC, its fairly simple when the value falls below 100 we switch on the LED connected to PORT C 0



sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
unsigned int adcvalue,value,temp_res;
unsigned char car,x,y;
char *voltage = "00.00";
long temp;

// Routine to show the value of the ADC_read
void ShowADC(int x, int y, unsigned int adcvalue)
{
car = adcvalue / 1000;
LCD_Chr(x,y,48+car);
adcvalue=adcvalue-1000*car;
car = (adcvalue / 100);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-100*car;
car = (adcvalue / 10);
LCD_Chr_CP(48+car);
adcvalue=adcvalue-10*car;
car = adcvalue;
LCD_Chr_CP(48+car);
delay_ms(30);
}

void main()
{
TRISA = 0xFF; // PORTA is input
TRISB = 0;
TRISC = 0;
PORTB = 0;
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1, 1, " ADC :");

do {
temp_res = ADC_Read(2); // Get 10-bit results of AD conversion
adcvalue = temp_res;
ShowADC (1,7,adcvalue);

if(adcvalue < 100)
{
PORTC = 0x01;
}
else
{
PORTC = 0x00;
}
Delay_ms(200);
} while(1);

} // end main

 

Links

50 pcs Light Dependent Resistors

250pcs 3MM LED Assortment Kit

QL200 PIC16F877A-I/P PIC16F877 PIC 8-bit Development Board