Arduino UNO R4 - Light Sensor Control Servo Motor
In this tutorial, we will learn how to use an Arduino UNO R4 to control the servo motor based on the value read from either a light sensor or the linght sensor moldule. Specifically, the Arduino UNO R4 will read values from a light sensor.
- When the light sensor's reading is above a certain level, the Arduino UNO R4 turns the servo motor to 90 degrees.
- When the light sensor's reading is below that level, the Arduino UNO R4 moves the servo motor back to 0 degrees.

Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
The LDR light sensor is very affordable, but it requires a resistor for wiring, which can make the setup more complex. To simplify the wiring, you can use an LDR light sensor module as an alternative.
Overview of Servo Motor and Light Sensor
If you're unfamiliar with servo motors and light sensors (their connections, functionality, and programming), please refer to the tutorials below to learn more:
Wiring Diagram
- The wiring diagram between Arduino Uno R4, servo motor and light sensor (Analog)

This image is created using Fritzing. Click to enlarge image
- The wiring diagram between Arduino Uno R4, servo motor and light sensor module (digital)

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 - Analog Threshold
- The Arduino Uno R4 code for reading the value from the LDR light sensor and controls the servo motor
- The Arduino Uno R4 code for reading the value from the LDR light sensor and controls the servo motor
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.
- Compy the above code and paste the code into Arduino IDE.
- Click the Upload button in Arduino IDE to transfer the code to the Arduino UNI R4.
- Turn the light sensor.
- Observe the movement of the servo motor.