Light Sensing Using Photo-Resistor Sensor (LDR) And BBC Micro:Bit

Introduction

 
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 a 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 a 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
  1. Micro:Bit(1 Pcs)
  2. Alligator Clip( pcs)
  3. Male Jumper wire( 3 pcs)
  4. A resistor of 10k ohm (1 pcs)
  5. Photoresistor or LDR sensor( 1pcs)
  6. Breadboard (1 pcs)
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 PIN2 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 PIN2. Connect exactly as shown in the below image. You can use any of the PINs of the micro:bit like PIN0, PIN1, and PIN2. 
 
 
This is how I have it connected.
 
 
Code
  1. Go to makecode and create a new project.
  2. Now, go to the variable block and choose a set item to block and place it inside the forever block.
     
     
  3. Now, rename the item variable to input if you want otherwise, you can rename as per your choice.
     
  4. Now, go to Advance and go to PINS block. Choose "read analog pin" and place it by replacing 0 in a "set item" block like this.
     
     
    Your code should look like this.
     
     
  5. 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.
     
  6. Now, go to the math block and choose Divide.
     
     
     
  7. 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.
     
  8. 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. 
    
JavaScript code
  1. let output = 0  
  2. let input2 = 0  
  3. basic.forever(() => {  
  4.     input2 = pins.analogReadPin(AnalogPin.P2)  
  5.     output = input2 / 50  
  6.     basic.showNumber(output)  
  7. })  
MicroPython code 
  1. from microbit import *  
  2. while True:     
  3.  input = pin2.read_analog()     
  4. output = int(input/ 50)      
  5. display.show(str(output))  
Demo
Get code and Sketch