Contains an LDR resistor whose resistance value decreases with brighter environment. This resistance can be determined with the help of a voltage divider, where a known voltage is divided over a known (10KΩ) and an unknown (variable) resistance. Using this measured voltage, the resistance can then be calculated - the exact calculation is included in the code examples below.

Pin assignment

Code example Arduino

Pin assignment Arduino

Arduino Sensor
pin A5 signal
5V +V
ground GND

The program measures the current voltage value at the sensor, calculates the current resistance value of the sensor from this and the known series resistance and outputs the results on the serial output.

int sensorPin = A5; // Declare the input pin here
 
// Serial output in 9600 baud
void setup()
{
    Serial.begin(9600);
}
 
// The program measures the current voltage value at the sensor,
// calculates from this and the known series resistance the current
// resistance value of the sensor and outputs the results to the serial output
 
void loop()
{      
        // Current voltage value is measured...
    int rawValue = analogRead(sensorPin);
        float voltage = rawValue * (5.0/1023) * 1000;
         
        float resitance = 10000 * ( voltage / ( 5000.0 - voltage) );
         
    // ... and here output to the serial interface
    Serial.print("Voltage value:"); Serial.print(voltage); Serial.print("mV");
    Serial.print(", resistance value:"); Serial.print(resitance); Serial.println("Ohm");
    Serial.println("---------------------------------------");
 
    delay(500);
}

Example program download

KY018-Arduino.zip

Code example Raspberry Pi

Pin assignment Raspberry Pi

Raspberry Pi Sensor
KY-053 A0 Signal
3.3V [Pin 1] +V
Ground [Pin 6] GND
Sensor KY-053
Signal A0
+V 3.3V [Pin 1]
GND Ground [Pin 6]
Raspberry Pi KY-053
GPIO 3 [Pin 5] SCL
Gpio 2 [Pin 3] SDA

Analog sensor, therefore the following must be considered.

In contrast to the Arduino, the Raspberry Pi has no analog inputs or there is no ADC (analog digital converter) integrated in the chip of the Raspberry Pi. This limits the Raspberry Pi, if you want to use sensors, where not digital values are output [voltage value exceeded -> digital ON | voltage value undercut -> digital OFF | example: button pressed [ON] button released [OFF]], but it should be a continuous variable value (example: potentiometer -> other position = other voltage value).

To avoid this problem, our sensor kit X40 has the KY-053, a module with 16 bit accurate ADC, which you can use on the Raspberry to expand it with 4 analog inputs. This module is connected to the Raspberry Pi via I2C, takes over the analog measurement and passes the value digitally to the Raspberry Pi.

Thus, we recommend to connect the KY-053 module with the said ADC in between for analog sensors of this set. More information can be found on the information page for the KY-053 Analog Digital Converter.

The program uses the corresponding ADS1x15 and I2C Python libraries from Adafruit to control the ADS1115 ADC. These have been published at the following link https://github.com/adafruit/Adafruit_CircuitPython_ADS1x15 under the MIT license. The required libraries are not included in the download package below.

The program measures the current voltage value at the sensor, calculates the current resistance value of the sensor from this and the known series resistance, and outputs the results to serial output.

Please note that you need to enable I2C on your Raspberry Pi before using this example.

#!/usr/bin/python
# coding=utf-8

import time
import board
import busio
import adafruit_ads1x15.ads1115 as ADS
from adafruit_ads1x15.analog_in import AnalogIn
import math
# Create the I2C bus
i2c = busio.I2C(board.SCL, board.SDA)

# Create the ADC object using the I2C bus
ads = ADS.ADS1115(i2c)
voltageMax = 3.3
# Create single-ended input on channels
chan0 = AnalogIn(ads, ADS.P0)
chan1 = AnalogIn(ads, ADS.P1)
chan2 = AnalogIn(ads, ADS.P2)
chan3 = AnalogIn(ads, ADS.P3)



while True:
    resistance = chan0.voltage / (voltageMax - chan0.voltage) * 10000

    print("Voltage value: ",'%.2f' % chan0.voltage, "V, resistance: ",'%.2f' % resistance, "Ω")
    print("---------------------------------------------------")
    time.sleep(1)

Example program download

KY018-RPi.zip

To start with the command:

sudo python3 KY018-RPi.py

Code example Micro:Bit

Pinout Micro:Bit:

Micro:Bit Sensor
Pin 1 Signal
3V +V
Ground GND

This is a MakeCode example for Micro:Bit which essentially does the same as the examples of the other two variants. However, this example is closer to the Raspberry Pi example than the Arduino example.

Sample program download

microbit-KY-018.zip