Home Code Micro:bit and light-dependent resistor example in Micropython

Micro:bit and light-dependent resistor example in Micropython

by shedboy71

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

 

Parts List

Name Link
Micro:bit Micro:bit Development Board
LDR module KEYES Photosensitive resistor sensor module for arduino
Edge Breakout I/O Expansion Edge Breakout I/O Expansion Extension Board for BBC micro:bit
Connecting cables Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

Layout

 

microbit and ldr example

microbit and ldr example

Code

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

[codesyntax lang=”python”]

from microbit import *

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

[/codesyntax]

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

[codesyntax lang=”python”]

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)

[/codesyntax]

You may also like

Leave a Comment