Microbit and KY-018 Photoresistor
In this article, we connect a KY-018 LDR to our Microbit – we will use Makecode and show the JaavScript and Python output as well
The KY-018 is a Photoresistor module which can be used to measure light intensity.
The resistance will decrease in the presence of light and increase in the absence of it. The output is analog and determines the intensity of light.
The module consists of a photoresistor, a 10 k in-line resistor and header.
This resistance can be determined using a basic voltage divider, where a known voltage is divided across the 10k resistor and the unknown resistance from the LDR. Using this measured voltage, the resistance can then be calculated. This is the basic idea
Parts Required
Name | Link | |
Microbit |
|
|
37 in one sensor kit | ||
Connecting cables |
Schematic/Connection
MICRO:BIT | SENSOR |
---|---|
0 | Signal |
3,3 V | +V |
GND | GND |
Example
All of these are from the Microsoft Makecode site
MakeCode
Python
def on_forever(): if pins.analog_read_pin(AnalogPin.P0) >= 512: basic.show_leds(""" # # # # # # # # # # # # # # # # # # # # # # # # # """) else: basic.show_leds(""" . . . . . . . . . . . . # . . . . . . . . . . . . """) basic.forever(on_forever)
JavaScript
basic.forever(function () { if (pins.analogReadPin(AnalogPin.P0) >= 512) { basic.showLeds(` # # # # # # # # # # # # # # # # # # # # # # # # # `) } else { basic.showLeds(` . . . . . . . . . . . . # . . . . . . . . . . . . `) } })
Output
Cover and uncover the sensor
Links