Home > Learn > Robotics > Using Arduino to Build a Light Sensor

Using Arduino to Build a Light Sensor

Welcome to the fascinating world of robotics and electronics! If you're just starting out or looking to expand your skills, building a light sensor with an Arduino is a fantastic project to dive into. Light sensors are incredibly versatile and can be used in everything from home automation systems to robotic projects that respond to environmental changes. In this article, we'll walk you through the process of creating a simple light sensor using an Arduino, breaking down the components, code, and potential applications. Let’s illuminate this journey together!

What You’ll Need to Get Started

Before we jump into the wiring and coding, let’s gather the essentials. Don’t worry if you’re new to this—everything here is beginner-friendly, and you can find these components at most electronics stores or online.

Once you’ve got your materials, it’s time to set up the circuit and bring this project to life.

Building the Circuit

Let’s start with the hardware setup. The goal is to connect the photoresistor to the Arduino so it can read changes in light levels. Here’s a step-by-step guide:

  1. Place the Photoresistor on the Breadboard: Insert the two legs of the photoresistor into different rows on the breadboard.
  2. Create a Voltage Divider: Connect one leg of the photoresistor to the 5V pin on the Arduino. Connect the other leg to a 10kΩ resistor, and then connect the other end of the resistor to the GND (ground) pin on the Arduino.
  3. Analog Input Connection: From the point where the photoresistor and resistor meet, run a jumper wire to one of the analog input pins on the Arduino (e.g., A0). This is where the Arduino will read the voltage changes caused by varying light levels.
  4. Optional LED Setup: If you’re using an LED for output, connect its longer leg (anode) to a digital pin (e.g., pin 13) through a 220Ω resistor, and the shorter leg (cathode) to GND.

Double-check your connections to ensure everything is secure. A loose wire can lead to inaccurate readings or a non-functional circuit.

Writing the Code

Now that the hardware is ready, it’s time to program the Arduino to read data from the photoresistor and respond to it. Open the Arduino IDE (download it from the official Arduino website if you haven’t already), and let’s write a simple sketch.

const int sensorPin = A0;    // Pin connected to the photoresistor
const int ledPin = 13;       // Pin connected to the LED (optional)
int lightLevel = 0;          // Variable to store the light sensor value

void setup() {
  Serial.begin(9600);        // Start serial communication for debugging
  pinMode(ledPin, OUTPUT);   // Set LED pin as output
}

void loop() {
  lightLevel = analogRead(sensorPin);  // Read the light level (0-1023)
  Serial.print("Light Level: ");
  Serial.println(lightLevel);          // Print the value to Serial Monitor

  // Turn on LED if light level is below a threshold (e.g., in dim light)
  if (lightLevel < 500) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }

  delay(100);  // Small delay to avoid flooding the Serial Monitor
}

Upload this code to your Arduino board using the USB cable. Once uploaded, open the Serial Monitor in the Arduino IDE (set to 9600 baud) to see the light level readings in real-time. Cover the photoresistor with your hand or expose it to different light sources to observe how the values change. If you’ve connected an LED, it should turn on when the light level drops below the threshold (500 in this example—you can adjust this value based on your environment).

Understanding the Results

The photoresistor works by changing its resistance based on light intensity. In bright light, its resistance is low, and in darkness, it’s high. The Arduino reads this

Read more in Learn Robotics

Basics of Building Your First Robot

A guide to Basics of Building Your First Robot .

Build a Robot That Chases Light

A guide to Build a Robot That Chases Light in the context of learn.

Building a Robot That Avoids Cats (Sort Of)

A guide to Building a Robot That Avoids Cats (Sort Of) .