Home Raspberry Pi Light sensor modules KY-018 and LM393 (3 and 4 pin) for a...

Light sensor modules KY-018 and LM393 (3 and 4 pin) for a Raspberry PI or Arduino

Tech details:

  • Comparator output, signal clean, waveform, driving ability, more than 15 mA. ( light sensor LM393  KY-018)
  • Potentiometer to adjust the sensitivity of a light sensor (LM393)
  • Working voltage of 3.3 V – 5 V (LM393 & KY-018)
  • Output form: digital switch output (0 and 1) (LM393 – 3 pin) and analog (KY-018), both (LM393 4 pin)
  • Wide voltage LM393 comparator used  (LM393)

Connectivity:

3 pin connector, pins as follows:

  • VCC: 3.3 – 5V Supply
  • S/DO: Output (Analog, Digital)
  • GND: 0V Ground

This module requires stepping down to 3.3V if connected to a 5V power source.

What does it do?

light sensor table

Thanks to the cheap photoresistors we can respond to light conditions. These two light sensor modules are based on a photoresistor which changes the voltage of the circuit depending on the amount of light getting to it. To give you an idea how light would affect the resistor here is a short table:

KY-018 Analog light sensor module

KY-018

The KY-018 is an analog module, and you will be able to read the voltage on the output pin from 0V-3.3V or 5V depending on the power source used. This range brings us to the sensitivity issue. Each module is capable of measuring light changes within a specific range. In theory, you can tune in this range by adding an in-line resistor of specific value. In practice,  the module is already equipped with the R103 SMD resistor, and capturing the changes in the bright light levels won’t be possible, however, the light sensor will be more sensitive to the changes in the dark spectrum of the light levels. The lower the resistance of the linear resistor, the better sensitivity in bright conditions.

LM393 Digital light sensor module (3-pin)

LM393

The LM393 works like a digital switch. Its output will be set to LOW or HIGH on the DO (Digital Output) pin. The sensitivity of this module can be adjusted by turning the built-in a potentiometer to the desired value. Due to the design of the module, it would be acting as a switch, as an analog pin is not available.

LM393 Digital light sensor module (4-pin)

This light sensor module combines both the KY-018and 3-pin design of LM393 to create a universal module that can act as a digital switch if connected to DO pin or as an analog sensor if connected to AO (Analog Output). In addition to this, you can calibrate the sensor using the potentiometer, to adjust triggering sensitivity or shift the sensitivity spectrum towards dark or bright levels.

How to use it?

Untitled Sketch_schem

If connected to a 3.3V power source, you can connect the modules directly to the GPIO on your Raspberry Pi (LM393). If you have 5V power supply use a logic level converter to step it down and protect your pins. The power source is connected to the VCC pin on the modules (+ on KY-018). Ground pin should be linked with  (-) on the KY-018 or GND of the LM393. The signal is received from the sensors are linked to your free GPIO (or ADC chip for KY-018 more on that later) and this is DO pin on LM393 and middle (or S) pin on KY-018.

Raspberry and analog input

Method 1

Untitled Sketch_bssb

Since the LM393 provides us with a digital output – the case is fairly easy – pretty much plug and play. With KY-018 we need to convert this analog output into a digital information that Raspberry can read. To do this we have to employ the power of ADC and SPI interface. To hook up your light sensor using an analog output use analog pins on the  MCP3008 8 Channel A/D Converter with SPI Interface chip. A really good hookup guide is here.

Method 2

Untitled Skesstch_bb

Another way is to introduce a capacitor to the circuit and calculate the voltage on the resistor pin by timing the charge/discharge cycle of the capacitor. The complete setup guide can be read here. You will need a capacitor and some math to be done on the paper.

t = RC
where t is time,
R is resistance (ohms)
and C is capacitance (farads)

Sample code:

These two pieces of the code can be used to test the modules:

Spoiler title
Method 1Method 2
import spidev
import time
import os

# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)

# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
    adc = spi.xfer2([1,(8+channel)<<4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    return data

# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
    volts = (data * 3.3) / float(1023)
    volts = round(volts,places)
    return volts

# Define sensor channels
light_channel = 0

# Define delay between readings
delay = 5

while True:

# Read the light sensor data
    light_level = ReadChannel(light_channel)
    light_volts = ConvertVolts(light_level,2)

# Print out results
    print "--------------------------------------------"
    print("Light: {} ({}V)".format(light_level,light_volts))

# Wait before repeating loop
    time.sleep(delay)
import RPi.GPIO as GPIO, time

# Tell the GPIO library to use
# Broadcom GPIO references
GPIO.setmode(GPIO.BCM)

# Define function to measure charge time
def RCtime (PiPin):
    measurement = 0
# Discharge capacitor
    GPIO.setup(PiPin, GPIO.OUT)
    GPIO.output(PiPin, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(PiPin, GPIO.IN)
# Count loops until voltage across
# capacitor reads high on GPIO
while (GPIO.input(PiPin) == GPIO.LOW):
    measurement += 1

    return measurement

# Main program loop
while True:
    print RCtime(4) # Measure timing using GPIO4

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated in style with crypto-currency

Smart Ideas with

Automate your space in with these ecosystems and integrate it with other automation services

Learn NodeRED

NodeRED for beginners: 1. Why do you need a NodeRED server?

0
To server or not to server? That's a very silly question!

Best Automation Projects

ESP8266 WIFI lights under $5

0
Your Sonoff device can't do this!

Free wireless doorbell (Amazon Dash button hack)

0
The most terrifying situation I can imagine* is missing a parcel

New Amazon Dash Buttons

0
You can teach new buttons old tricks!

ESP8266 ceiling light – demo

0
Testing the ESP8266 as a WIFI light controller

Try Tasker and Raspberry Pi for your home automation needs!

0
Raspberry Pi can help you with your Tasker profiles

Smart Home

IFTTT Pro Alternative: Alexa ed.

0
Bypass IFTTT Pro restrictions for Alexa voice actions with this IFTTT Pro Alternative

The answer to almost all Echo needs: Alexa-remote2

0
Alexaremote2 is a Swiss army knife for Amazon Echo devices. Connect, control and listen to all Alexa enabled gadget at home.

This ESP32 is on FIRE! – M5Stack Core

0
This ESP kit is on fire! M5Stack Core merges IoT platform and learning in a very polished stackable module.

From KettleBot to ANY-Bot – SwitchBot

0
Switching anything on and of with SwitchBot -an insanely cute robot that does all the finger pressing for you

NodeRED – multiple payloads

0
Mastering multiple payloads in the function node in NodeRED