Arduino UNO R4 - Light Sensor Control Relay

In this tutorial, we will learn how to program Arduino Uno R4 to control the relay based on value read from either LDR light sensor or LDR light sensor module.

By connecting a relay to devices like light bulbs, LED strips, motors, or actuators, we can use a light sensor to control these devices.

Arduino UNO R4 Light Sensor controls relay

Overview of Relay and Light Sensor

If you're unfamiliar with relays and light sensors (their configurations, functionalities, and programming methods), you can learn more by checking out these tutorials:

Wiring Diagram

  • The wiring diagram between Arduino Uno R4, relay module and raw LDR light sensor (analog)
The wiring diagram between Arduino UNO R4 Light Sensor Relay

This image is created using Fritzing. Click to enlarge image

If you do not use a breadboard, wire it like this:

The wiring diagram between Arduino UNO R4 Light Sensor Relay  (No Breadboard)

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Uno R4, relay module and LDR light sensor module (digital)
The wiring diagram between Arduino UNO R4 LDR Light Sensor module relay

This image is created using Fritzing. Click to enlarge image

Pushing jumper wires into the headers is quick, but the wires can slip out. In this case, use a Proto Screw Terminal Block Shield or a Screw Shield V2. It plugs into the Arduino UNO R4 and gives each pin a screw terminal. Below is the wiring diagram with the Screw Shield V2.

The wiring diagram between Arduino UNO R4 LDR Light Sensor module relay Screw Shield

This image is created using Fritzing. Click to enlarge image

See The best way to supply power to the Arduino Uno R4 and other components.

Arduino UNO R4 Code

  • The Arduino Uno R4 code for reading the value from the LDR light sensor and controls the relay
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-light-sensor-controls-relay */ #define LIGHT_SENSOR_PIN A0 // Arduino Uno R4 pin connected to light sensor's pin #define RELAY_PIN 9 // Arduino Uno R4 pin connected to Relay's pin #define ANALOG_THRESHOLD 500 // variables will change: int analogValue; void setup() { pinMode(RELAY_PIN, OUTPUT); // set arduino pin to output mode } void loop() { analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin if(analogValue < ANALOG_THRESHOLD) digitalWrite(RELAY_PIN, HIGH); // turn on Relay else digitalWrite(RELAY_PIN, LOW); // turn off Relay }
  • The Arduino Uno R4 code for reading the value from the LDR light sensor module and controls the relay
/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-light-sensor-controls-relay */ #define LIGHT_SENSOR_PIN 12 // Arduino Uno R4 pin connected to light sensor's pin #define RELAY_PIN 9 // Arduino Uno R4 pin connected to Relay's pin // variables will change: int analogValue; void setup() { Serial.begin(9600); // initialize serial communication pinMode(LIGHT_SENSOR_PIN, INPUT); // initialize the Arduino Uno R4's pin as an input pinMode(RELAY_PIN, OUTPUT); // initialize the Arduino Uno R4's pin as an output mode } void loop() { int light_state = digitalRead(LIGHT_SENSOR_PIN); if (light_state == HIGH) { Serial.println("The light is NOT present"); digitalWrite(RELAY_PIN, HIGH); // turn on Relay } else { Serial.println("The light is present"); digitalWrite(RELAY_PIN, LOW); // turn off Relay } }

Detailed Instructions

Follow these instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Connect the Arduino UNO R4 to your computer using a USB cable
  • Launch the Arduino IDE, then choose the correct board and port
  • Paste the provided code into the Arduino IDE
  • Click the Upload button in the Arduino IDE to send the code to the Arduino UND R4
Arduino IDE Upload Code
  • Sends light to the sensor
  • Observe the change in the relay's state

Code Explanation

Check the explanations in the source code comments for each line!

Relay vs MOSFET Module

A relay is not the only way to switch a device. If your device runs on DC power, such as a 12V LED strip, a 12V solenoid lock or a 12V fan, a MOSFET module can do the same job.

Relay MOSFET Module
What it can do turn the device on or off turn the device on or off, and set its power (PWM)
Noise when switching a click every time no sound
How fast it switches slow, a few milliseconds very fast
Life the contacts wear out after many switches nothing moves, so it lasts long
Type of power AC or DC DC only
Device power supply any voltage the relay is rated for about 6V or more
Isolation from the Arduino yes, between coil and contacts yes, by the PC817 optocoupler
Good for AC devices, or a simple ON/OFF DC devices, or dimming and speed control

Both the relay and the MOSFET module turn on when the Arduino UNO R4 pin is HIGH. So for a simple ON/OFF, the code above stays the same. Only the wiring changes. For AC mains devices, always use the relay.

To learn the MOSFET module pinout, wiring, PWM code and safety tips, see the Arduino UNO R4 - MOSFET Module tutorial.

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!