In this article, we connect a KY-018 Photoresistor or LDR to an Arduino Uno
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
Code Example
The following code example will
Example 1
int sensorPin = 1; //define analog pin 1 int value = 0; void setup() { Serial.begin(9600); } void loop() { value = analogRead(sensorPin); Serial.println(value, DEC); // light intensity // high values for bright environment // low values for dark environment delay(500); }
Example 2
int sensorPin = A1; // Declare the input pin here void setup() { Serial.begin(9600); } 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) ); Serial.print("Voltage value:"); Serial.print(voltage); Serial.print(" mV"); Serial.println(""); Serial.print("resistance value:"); Serial.print(resitance); Serial.print(" Ohms"); Serial.println(""); Serial.println("---------------------------------------"); delay(500); }
Serial Monitor Output
Example 1
177
171
172
178
759
828
929
949
373
Example 2
Voltage value:3699.90 mV
resistance value:28458.65 Ohms
—————————————
Voltage value:938.42 mV
resistance value:2310.47 Ohms
—————————————
Voltage value:923.75 mV
resistance value:2266.19 Ohms
—————————————
Voltage value:982.40 mV
resistance value:2445.26 Ohms
—————————————
Voltage value:3543.50 mV
resistance value:24328.86 Ohms
Links
https://github.com/getelectronics/ArduinoCode/tree/main/37%20Sensor%20Kit/KY-018_PhotoResistor