Home CodeArduino micro:bit and LDR example

micro:bit and LDR example

by shedboy71

In this example we connect a photoresistor to a micro:bit, the value read from the photoresistor corresponds to the amount of light present. The photoresistor is connected to P3 in this example.

photoresistor (or light-dependent resistorLDR, or photocell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibitsphotoconductivity. 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 practical example could be a dark room sensor for photography, if the reading approached a critical level an alarm could be activated or even a night light

Here is a sample module

 

Schematic

In this example I connected 3v3 to the module and it worked fine

microbit and ldr

microbit and ldr

 

Code

In this example we simply output the reading via the serial port.

[codesyntax lang=”cpp”]

int sensorValue;

void setup()
{
  Serial.begin(9600); // starts the serial port at 9600
}

void loop()
{
  sensorValue = analogRead(A0); // read analog input pin 0
  Serial.print(sensorValue, DEC); // prints the value read
  Serial.print(" \n"); // prints a space between the numbers
  delay(1000); // wait 100ms for next reading
}

[/codesyntax]

 

Testing

Open the serial monitor and move the LDR closer to a light, cover the LDR.

10
4
254
254
112
142
282
665
742
983
350
98
59

 

Links

You can just buy some photoresistors, they are cheap items

50 pcs (5 values * 10pcs) LDR Photo Light Sensitive Resistor Photoelectric Photoresistor Kit for 5506 5516 5528 5537 5539

You may also like

Leave a Comment