Beginner’s Guide to Using LDR Sensor and Relay Module with Micro:bit | How to Use an LDR Sensor

Hello and welcome back! In this tutorial, we will learn how to use an LDR sensor with a Micro:bit board. Additionally, I have used a relay module for this project, allowing us to control the relay module (turn it on and off) based on the sensor readings. The LDR sensor is called it is a light-sensitive resistor whose resistance decreases as the intensity of light increases. We can commonly use it in light-detection circuits. I also connected an LED strip to the relay module, for example. However, you can connect other AC or DC components as needed. Just ensure that the components you use are compatible with the relay’s ampere rating. I think this system can be a great solution for light-controlling applications. I also used the MakeCode platform to create the project code. This platform allows you to write code using code blocks, making it ideal for beginners.
- What is the LDR sensor and how to use it step by step – Click on me
Ok, let’s do this project step by step. The required components are given below.
- Micro: bit V2 or V1 board x 1 — Our store / Amazon
- Micro: bit sensor shield x 1 — Our store / Amazon
- 5v Relay module — Our store / Amazon
- LDR sensor x 1 — Our Store / Amazon
- Jumper wires — Our store / Amazon
- Keyestudio BBC Micro: bit starter kit — Click on me
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.





Step 2
Second, connect the Micro:bit board to the sensor shield. This will allow us to connect the components easily.

Step 3
Thirdly, connect the LDR and relay modules to the sensor shield using the circuit diagram provided below.







Step 4
Next, connect the Micro:bit board to the computer and follow the steps below to upload the program.

- Now, go to the MakeCode website and create a new project of your choice. You will then see the MakeCode interface.



- Afterward, copy and paste the following Python script into the Python tab. You will now see auto-generated code blocks.
- Python Script and Circuit Diagram — Download
value = 0
basic.show_string("Hello!")
basic.pause(1000)
basic.clear_screen()
def on_forever():
global value
value = pins.analog_read_pin(AnalogReadWritePin.P10)
if value < 500:
pins.digital_write_pin(DigitalPin.P15, 1)
basic.show_leds("""
. . # . .
. # # . .
. . # . .
. . # . .
. # # # .
""")
else:
pins.digital_write_pin(DigitalPin.P15, 0)
basic.show_leds("""
. # # . .
# . . # .
# . . # .
# . . # .
. # # . .
""")
basic.forever(on_forever)


- Next, click the Connect Device button and select your device. Finally, click the Download button.






Step 5
Now, disconnect the USB cable and connect the LED strip to the relay. However, you can also test this project without the LED strip.




Step 6
Next, provide a 7V-12V DC power supply to the Micro: bit board. I used two Li-ion batteries for this. Now, connect the 12VDC power supply to the LED strip. Ok, now you can test this project. The full video guide is below. So we hope to see you in the next project. Have a great day.


Beginner’s Guide to Using LDR Sensor and Relay Module with Micro:bit | How to Use an LDR Sensor