Arduino Mega - Light Sensor

This guide shows you how to use the LDR light sensor with the Arduino Mega board. We will learn in detail:

Arduino Mega light sensor

Hardware Preparation

1×Arduino Mega
1×USB 2.0 cable type A/B (for USB-A PC)
1×USB 2.0 cable type C/B (for USB-C PC)
1×Light Sensor
1×10 kΩ Resistor
1×Breadboard
1×Jumper Wires

Or you can buy the following 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 (LINK_MAIN_LIGHT_SENSOR) is cheap, but you need a resistor to connect it, which makes the wiring harder. To make the wiring easier, you can use an LDR light sensor module (LINK_MAIN_DIGITAL_LIGHT_SENSOR) instead.

Overview of Light Sensor

This tutorial uses a light sensor called a photoresistor, also known as an LDR (Light-Dependent Resistor) or photocell. It helps us know how bright the light around us is.

Pinout

A photoresistor has two pins. Since it is a type of resistor, you don’t need to tell the pins apart. Both pins are the same.

Light Sensor Pinout

How It Works

A photoresistor is a type of resistor that changes its resistance with light. When there is a lot of light, its resistance is very low. When there is little light, its resistance is very high. By measuring the resistance, we can tell how bright or dark the light around us is. This helps us detect and measure light levels in different places.

How Light Sensor Works

WARNING

The light sensor shows roughly how bright it is. It doesn’t give the exact amount of light. Use it only when you don’t need very precise measurements.

Arduino Mega - Light Sensor

Arduino Mega has pins A0 to A5 that you can use as analog inputs. These pins convert a voltage (from 0 volts up to the supply voltage, VCC) into numbers from 0 to 1023. These numbers are called ADC values (analog values).

Connect one pin of the light sensor to an analog input pin on the Arduino Mega. Then write code so the Arduino reads the value with analogRead(). This tells you how bright the light is.

Wiring Diagram

The wiring diagram between Arduino Mega Light Sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code

The code below reads a value from a light sensor and gives a rough idea of how bright the light is.

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-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 steps one by one.

  • Connect the LDR light sensor to the Arduino Mega as shown in the diagram.
  • Connect the Arduino Mega to your computer with a USB cable.
  • Open the Arduino IDE on your computer.
  • Choose the right board (Arduino Mega) and the correct port.
  • Copy the code and open it in the Arduino IDE.
  • Click the Upload button in the Arduino IDE to send the code to the Arduino Mega.
  • 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!