A Raspberry Pi Pico and KY-018 Photoresistor

In this article, we connect an KY-018 Photoresistor or LDR to a Raspberry Pi Pico, any Rp2040 type board will be suitable. We actually tested this with a Cytron Maker Pi Pico

We will use Micropython for these examples but of course you can use the Arduino IDE as well if you have Raspberry Pi Pico support enabled

The sensor looks like this

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

You can connect to the module using dupont style jumper wire.

Name   Link
Raspberry Pi Pico
37 in one sensor kit
Connecting cables

 

Schematic/Connection

Pico SENSOR
GPIO28 S
3.3v middle pin
GND

 

Code Examples

Basic example in  thonny

from machine import Pin
import time
 

class LDR:
    def __init__(self, pin):
        self.ldr_pin = machine.ADC(Pin(pin))
        
    def get_raw_value(self):
        return self.ldr_pin.read_u16()
    
    def get_light_percentage(self):
        return round(self.get_raw_value()/65535*100,2)
 
ldr = LDR(28)

while True:
     print(ldr.get_light_percentage())
     time.sleep(1)

 

REPL Output

Run -c $EDITOR_CONTENT
31.31
30.99
31.11
57.09
67.28
23.32
14.8
14.55
14.77
13.92
13.68
13.92

Links

https://github.com/getelectronics/rp2040learning/tree/main/Sensor%20Kit/KY-018_PhotoResistor

 

 

 

Related posts

A Raspberry Pi Pico and KY-006 Passive Buzzer

A Raspberry Pi Pico and KY-012 Active Buzzer

A Raspberry Pi Pico and KY-032 Obstacle Detector

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More