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.

Arduino UNO R4 Light Sensor controls Servo Motor

Hardware Preparation

1×Arduino UNO R4 WiFi or Arduino UNO R4 Minima
1×USB Cable Type-A to Type-C (for USB-A PC)
1×USB Cable Type-C to Type-C (for USB-C PC)
1×Light Sensor
1×10 kΩ Resistor
1×Servo Motor
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Arduino UNO R4
1×Recommended: Breadboard Shield for Arduino UNO R4
1×Recommended: Enclosure for Arduino UNO R4
1×Recommended: Power Splitter for Arduino UNO R4
1×Recommended: Prototyping Base Plate & Breadboard Kit for Arduino UNO

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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)
The wiring diagram between Arduino UNO R4 Light Sensor Servo Motor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Uno R4, servo motor and light sensor module (digital)
The wiring diagram between Arduino UNO R4 Light Sensor module Servo Motor

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
/* * 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-servo-motor */ #include <Servo.h> #define LIGHT_SENSOR_PIN A0 // Arduino Uno R4 pin connected to light sensor's pin #define SERVO_PIN 9 // Arduino Uno R4 pin connected to Servo Motor's pin #define ANALOG_THRESHOLD 500 Servo servo; // create servo object to control a servo void setup() { servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { int analogValue = analogRead(LIGHT_SENSOR_PIN); // read the input on analog pin if (analogValue > ANALOG_THRESHOLD) servo.write(90); // rotate servo motor to 90 degree else servo.write(0); // rotate servo motor to 0 degree }
  • The Arduino Uno R4 code for reading the value from the LDR light sensor and controls the servo motor
/* * 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-servo-motor */ #include <Servo.h> #define LIGHT_SENSOR_PIN 11 // Arduino Uno R4 pin connected to light sensor's pin #define SERVO_PIN 9 // Arduino Uno R4 pin connected to Servo Motor's pin Servo servo; // create servo object to control a servo void setup() { Serial.begin(9600); // initialize serial communication pinMode(LIGHT_SENSOR_PIN, INPUT); // initialize the Arduino Uno R4's pin as an input servo.attach(SERVO_PIN); // attaches the servo on pin 9 to the servo object servo.write(0); } void loop() { int light_state = digitalRead(LIGHT_SENSOR_PIN); if (light_state == HIGH) { Serial.println("The light is NOT present"); servo.write(90); // rotate servo motor to 90 degree } else { Serial.println("The light is present"); servo.write(0); // rotate servo motor to 0 degree } }

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.

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!