Home Code KY-018 Photo resistor module and Raspberry Pi example

KY-018 Photo resistor module and Raspberry Pi example

by shedboy71

In this example we take a look at the KY-018 Photo resistor module and connect this to a Raspberry Pi using an ADC add on board

A photoresistor (or light-dependent resistor, LDR, or photo-conductive cell) is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity; in other words, it exhibits photoconductivity. A photoresistor can be applied in light-sensitive detector 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 megohms (MΩ), while in the light, a photoresistor can have a resistance as low as a few hundred ohms. If incident light on a photoresistor exceeds a certain frequency, photons absorbed by the semiconductor give bound electrons enough energy to jump into the conduction band. The resulting free electrons (and their hole partners) conduct electricity, thereby lowering resistance. The resistance range and sensitivity of a photoresistor can substantially differ among dissimilar devices. Moreover, unique photoresistors may react substantially differently to photons within certain wavelength bands.

Parts List

Name link
Raspberry Pi3 Raspberry Pi 3 Model B With WiFi & Bluetooth
37 sensor kit 37 IN 1 SENSOR KITS FOR ARDUINO HIGH-QUALITY FREE SHIPPING (Works with Official for Arduino Boards)
connecting wire Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire
Analog zero https://rasp.io/analogzero/



Being a device that outputs an analog value we need to connect this to an ADC converter – in this case we use a MCP3008 in the form of an Analog Zero module. The LDR is easy to connect to the Analog Zero

pi and ky-018 layout

pi and ky-018 layout


Save this example as ldr.py

import RPi.GPIO as GPIO
import time
from gpiozero import MCP3008

led = 7


	while True:
		adc = MCP3008(channel=0)
		voltage = 3.3 * adc.value
		print("Measured voltage is ",voltage);

except KeyboardInterrupt:

Copy this to your raspberry pi and open up a terminal and type in

sudo python ldr.py


You may also like

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