Arduino UNO R4 - Light Sensor

This tutorial instructs you how to use the LDR light sensor with Arduino UNO R4. In detail, we will learn:

Arduino UNO R4 light sensor

If you need a light sensor in module form, refer to Arduino UNO R4 - LDR Light Sensor Module tutorial.

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Light Sensor
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure 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. We appreciate your support.

Overview of Light Sensor

This tutorial uses a light sensor known as a photoresistor, also referred to as a Light-Dependent Resistor (LDR) or photocell. It is used to find and measure how bright the surrounding light is.

Pinout

A photoresistor has two pins. Because it is a type of resistor, we do not need to identify these pins separately. They are the same.

Light Sensor Pinout

How It Works

A photoresistor is a special type of resistor that changes its resistance based on the amount of light it detects. When there is a lot of light, the resistance of the photoresistor becomes very low. When there is little or no light, the resistance becomes very high. By measuring the resistance of the photoresistor, we can determine how bright or dark the surrounding light is. This way, we can use it to detect and measure light levels in different environments.

How Light Sensor Works

WARNING

The light sensor value shows a rough idea of how bright the light is, but it does not give the exact amount of light. So, you should use it only in situations where you do not need very accurate measurements.

Arduino UNO R4 - Light Sensor

The Arduino UNO R4 has pins A0 to A5 that can be used for analog input. These pins convert voltage, ranging from 0 volts to VCC, into numbers between 0 and 1023. These numbers are called ADC or analog values.

By connecting a pin of the photoresistor to an analog input pin of Arduino UNO R4, we can program Arduino UNO R4 to read the analog value from the pin using the analogRead() function. This helps us determine the relative light levels.

Wiring Diagram

The wiring diagram between Arduino UNO R4 Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code

The following code reads the value from a photocell and qualitatively determines the light level.

/* * 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 */ void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } void loop() { // reads the input on analog pin A0 (value between 0 and 1023) int value = analogRead(A0); Serial.print("Analog reading: "); Serial.print(value); // the raw analog reading // We'll have a few threshholds, qualitatively determined if (value < 10) { Serial.println(" - Dark"); } else if (value < 200) { Serial.println(" - Dim"); } else if (value < 500) { Serial.println(" - Light"); } else if (value < 800) { Serial.println(" - Bright"); } else { Serial.println(" - Very bright"); } delay(500); }

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.
  • Connect the LDR light sensor to the Arduino UNO R4 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.
  • Copy the code and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to transfer the code to Arduino UNO R4.
  • Open the Serial Monitor.
  • Shine light on the sensor.
  • Check the Serial Monitor to see the results.
COM6
Send
Analog reading: 163 - Dim Analog reading: 152 - Dim Analog reading: 187 - Dim Analog reading: 188 - Dim Analog reading: 957 - Very bright Analog reading: 972 - Very bright Analog reading: 981 - Very bright
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

※ 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!