Today, in this article, I am going to talk about sensing the light using Photo-Resistor Sensor and BBC Micro:Bit. Basically, you will know all the basics of how to read an analog signal using micro:bit and display the readings on the screen of the micro: bit, not only displaying, but if you want to get more creative, you can use the readings and build something useful. We can detect the light level using micro:bit built-in feature, you can read here how to do it.
So, let's get started.
What is Photo-Resistor sensor or Light Dependent Resistor (LDR)?
A Photo-Resistor sensor or light-dependent resistor (LDR) is a light controlled variable resistor. The resistance of a photo-resistor decreases with increasing incident light intensity or we can say that it exhibits photoconductivity. A photoresistor can be applied in light sensing circuits and light activated 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 megaohms; also while in the light, a photo-resistor can have a resistance as low as few hundred ohms. As it can be used in the light sensing circuit we are also using the LDR for sensing the light. And it looks like this.
So, now, let us see how we can use this sensor with micro:bit and read the light level.
Tools You Need- Micro:Bit (1)
- Alligator clip (several)
- Male jumper wire (3)
- A resistor of 10k ohm (1)
- Photoresistor or LDR sensor (1)
- Breadboard (1)
Connection
The connection is like below. First, we need to connect the photoresistor (LDR) sensor to the breadboard and then, we need to connect the resistor of 10k. We are using resistor just to control the current and providing the current that is only needed for the LDR. So, connect GND of the Micro:Bit to the one end of the resistor and the other end of the resistor will be connected to the end of the LDR sensor from where we will read the values. Connect the PIN 2 of the micro:bit to the one end of the LDR via the resistor and connect 3V pin of the Micro:bit to another end of the LDR sensor. In this way, we're giving 3V to LDR from one end and getting output from another end using PIN 2. Connect exactly as shown in the below image. You can use any of the pins of the micro:bit like PIN 0, PIN 1, and PIN 2.
This is how I have it connected.
- Go to makecode and create a new project.
- Now, go to the variable block and choose a set item to block and place it inside the forever block.
- Now, rename the item variable to input if you want otherwise, you can rename as per your choice.
- Now, go to Advance and go to the Pins block. Choose "read analog pin" and place it by replacing 0 in "set item" block like this.
Your code should look like this.
- Now, again, go to the variable and choose a set item and place it below the first set item. If you want, you can rename the item variable to something else. I have renamed it as output.
- Now, go to math block and choose Divide.
- Replace the first zero with input variable and another zero with 50.
So why are we dividing the reading with 50? Simply, because the result of analog read pin block will be a number between 0 and 1023 where 0 represents 0V and 1023 represents 3V. This value is stored in the variable input. This is then scaled to be a single digit number by dividing by 50. You can change the number 50 to make the light readings more or less sensitive.
- Now, use the show number block from basic and assign the output variable to it like this and now, this is the final code. Download it and upload to the micro:bit.
let output = 0
let input2 = 0
basic.forever(() => {
input2 = pins.analogReadPin(AnalogPin.P2)
output = input2 / 50
basic.showNumber(output)
})
MicroPython code
from microbit import *
while True:
input = pin2.read_analog()
output = int(input/ 50)
display.show(str(output))
DemoThank You :)
Comments