World-class manufacturing

Exploring Arduino with a Photocell and LED

Table of Contents

Introduction

Hello Hackers! Today, we’re going to explore how to use a photocell with an Arduino Uno to control the brightness of an LED. This tutorial will guide you through the process step-by-step, and you can also follow along with the video embedded below from Jaycon. Let’s dive in and get started!

Components Needed

To complete this project, you will need the following components from our Jaycon beginner kit:

  • Arduino Uno
  • Breadboard
  • Photocell (Light-dependent resistor)
  • LED
  • 10k Ohm resistor
  • 330 Ohm resistor
  • Jumper wires

Building the Circuit

First, let’s set up our circuit:

  1. Connect the Power:
    • Connect the 5V pin on the Arduino to the power rail on the breadboard.
    • Connect the GND pin on the Arduino to the ground rail on the breadboard.
  2. Set Up the Photocell:
    • Place the photocell on the breadboard. The two prongs are non-directional, so it doesn’t matter which way they are oriented.
    • Connect one side of the photocell to the 5V power rail.
    • Connect a 10k Ohm resistor from the other side of the photocell to the ground rail.
    • Use a jumper wire to connect the junction between the photocell and the resistor to the analog input pin A0 on the Arduino.
  3. Connect the LED:
    • Place the LED on the breadboard. The longer leg is the anode (positive) and the shorter leg is the cathode (negative).
    • Connect the anode to a 330 Ohm resistor, and then connect the resistor to digital pin 3 on the Arduino.
    • Connect the cathode to the ground rail on the breadboard.

Writing the Code

Now, let’s write the code to control the LED brightness based on the light level detected by the photocell. Open the Arduino IDE and enter the following code:

cppCopy codeint photocellPin = A0;     // Photocell connected to analog pin A0
int ledPin = 3;            // LED connected to digital pin 3
int photocellReading;      // Variable to store the reading from the photocell

void setup() {
  Serial.begin(9600);      // Initialize serial communication
  pinMode(ledPin, OUTPUT); // Set the LED pin as an output
}

void loop() {
  photocellReading = analogRead(photocellPin);  // Read the value from the photocell
  int ledBrightness = map(photocellReading, 0, 1023, 0, 255); // Map the reading to a range from 0 to 255
  analogWrite(ledPin, ledBrightness);           // Set the LED brightness
  Serial.print("Photocell reading: ");
  Serial.print(photocellReading);
  Serial.print("\tLED brightness: ");
  Serial.println(ledBrightness);
  delay(100); // Small delay to stabilize the reading
}
image of Exploring Arduino with a Photocell and LED

Testing the Setup

Upload the code to your Arduino and open the Serial Monitor. Cover and uncover the photocell to see the LED’s brightness change. The Serial Monitor will display the photocell reading and corresponding LED brightness.

Conclusion

By following these steps, you can control an LED using a photocell and an Arduino. This project is a great way to learn about analog sensors and basic Arduino programming. If you encounter any issues or want to explore further, check out additional resources from Adafruit and Instructables on using photocells with Arduino​ (Instructables)​​ (YouTube)​​ (Adafruit Learning System)​​ (Use Arduino for Projects)​​ (Codebender Blog)​.

Feel free to watch the accompanying video below to get a step-by-step visual guide through the process. Happy coding!

Related Post

Reflecting on Jaycon’s Partnership with Ford and GE: Rapid Response During a Crisis

Reflecting on Jaycon's partnership with Ford and GE during the COVID-19 crisis. Learn how…

10 Starter Projects for the Raspberry Pi AI Kit

Discover 10 innovative projects for hackers using the new Raspberry Pi AI Kit. Enhance…

Key Benefits of Implementing Digital Twins in Your Business

Discover the key benefits of digital twins in business, from predictive maintenance to supply…