KY-018 LDR Photo-Resistor Module as a momentary switch with Arduino

LDR Photo-Resistor Module

Photosensitive resistor module most sensitive to environmental light intensity is generally used to detect the ambient brightness and light intensity.

Parts Needed

You need to have the following parts

  • Arduino (Any version)
  • 1x KY-018
  • 1x One-Channel Relay module 5V
  • Jumper Wires

For Arduino

Wiring LDR Photo-Resistor Module

PinWiring KY-018 to Arduino
OUTDigital Pin – 2
GNDGND
VCC5V

Wiring Relay

PinWiring relay to the Arduino
SDIgital Pin – 13
GND
+5V

Schematics For Arduino

Assemble all the parts by following the schematics below:

Connect the light lamp to the relay.

Coding Arduino

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the Sensor pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);
  }
}
  • Upload the code to the Arduino.