logo-mobile

ROHM

ROHM
Menu
  • Arduino –
  • Raspberry Pi –
  • Trending –
  • Others –
  • About –
  • Contact –

Arduino

How to Control a Light with an Ambient Light Sensor

DevicePlus Editorial Team
Published by DevicePlus Editorial Team at October 22, 2020
Categories
  • Arduino
Tags

When the sun goes down, and it starts to get dark in your house, you might need to turn on a few lights to see. But wouldn’t it be more convenient if they could do so automatically? With photoresistor sensors and an Arduino, you can control lamps, blinds, or just about any other project based on the ambient lighting around you.

How Photoresistors Work

For this project, we’ll be focusing on photoresistors and how to incorporate them into your projects. A photoresistor is a type of resistor that changes its resistance level in response to the amount of light that hits it. While typical resistors impede the flow of electrons through a circuit at a constant level, photoresistors allow higher voltages to pass through a circuit when there’s more light hitting a sensor. In contrast, when there’s less light in an area, the resistance increases.

This resistance can then be read by the Arduino’s analog pins to return a value between 0 and 1023. With this information, you can make your project respond differently to bright daylight, waning dusk, or pure night. You could use this to open your blinds when it’s bright outside or turn on a lamp when it gets dark.

Photoresistors are small, cheap, and they often come in beginner’s kits and packs of other sensors. If you’ve bought an electronics kit, you’ve probably got one or two sitting around. To demonstrate how they work, we’ll control two LEDs, turning one on when there’s light out and another when it’s dark.

What You’ll Need

For this project, you won’t need much that doesn’t come in a standard electronics kit. However, it’s worth keeping in mind any larger projects that you might want to control using your photoresistor. For example, if you want to control blinds, you’ll need some servos, whereas if you’re controlling a lamp, you’ll need a relay. For now, we’ll focus solely on how to use the sensor itself.

Arduino Uno
Arduino IDE
Two LEDs

Ideally, different colours!

 

Photoresistor

 

One 220 ohm resistor

To help further reduce resistance running through the photoresistor, bringing down its value down to a more usable level

 

 

Two 100 ohm resistors

To help avoid burning out the LEDS

 

 

You’ll also need to source a USB Cable; if you haven’t got one lying around that is!

It’ll also be helpful to have a breadboard handy for prototyping this project. Once again, all of these materials can usually be found in any starter electronics kit, so take stock of what you already have.

The Code

For this project, we’ll use one pin on the Arduino to read the photoresistor, and two pins to control two separate LEDs. When the photoresistor detects light above a certain threshold, we’ll turn on one LED. When it falls below that threshold, we’ll switch on the other. With that in mind, here’s the full code.

int redLED = 7;
int blueLED = 8;
int photoresistorPIN = A0;
int lightValue = 0;

void setup() {
pinMode(redLED, OUTPUT);
pinMode(blueLED,OUTPUT);
Serial.begin(9600);
}

void loop(){
lightValue = analogRead(photoresistorPIN);
Serial.println(“Light value: “);
Serial.println(lightValue);
if(lightValue < 50){
digitalWrite(redLED, HIGH);
}
else{
digitalWrite(blueLED, HIGH);
}
delay(200);
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
}

There are a few parts of this code to explain, so let’s start with the variables we’re setting up.

int redLED = 7;
int blueLED = 8;
int photoresistorPIN = A0;
int lightValue = 0;

Here, we’ll assign the red LED to pin 7 and the blue LED to pin 8. These can be assigned to any of the digital pins on the Arduino. Next, we’ll assign the photoresistor to A0. This pin must be assigned to one of the six analog pins, as it needs to read the analog value coming from the photoresistor.

Finally, we’ll create a variable called lightValue to store the value that the photoresistor returns.

void setup() {
pinMode(redLED, OUTPUT);
pinMode(blueLED,OUTPUT);
Serial.begin(9600);
}

Inside the setup() section, the first two lines assign both the red and blue LED pins to be output pins.

We’ll also initialize the serial monitor, which allows us to see what value the photoresistor is returning. This can be helpful when determining which threshold to set for switching between one LED or another.

Depending on how much ambient light there is in the room where you set up your project, you may need to adjust this value based on your needs.

void loop(){
lightValue = analogRead(photoresistorPIN);
Serial.println(“Light value: “);
Serial.println(lightValue);
if(lightValue < 50){
digitalWrite(redLED, HIGH);
}
else{
digitalWrite(blueLED, HIGH);
}
delay(200);
digitalWrite(redLED, LOW);
digitalWrite(blueLED, LOW);
}

Inside the loop() section, the first line will read the analog value from the photoresistor and assign it to the lightValue variable. We’ll then print it to the serial monitor immediately.

Next, the part of the loop will activate the red LED if the light sensor reads a value lower than 50, and turn on the blue LED if it’s higher. If the value of 50 is too low for your environment and the red light always stays on, use the serial monitor to determine a good value for your project.

The Wiring

Now that you understand the coding go ahead and upload it to your Arduino. Then, we’ll set up the wiring – something that’s fortunately, extremely simple and easy to add to your other projects. To get the wiring set up, follow these steps:

● Connect the long end of the blue LED to pin 8 on the Arduino. Connect the short end of the LED to a 100-ohm resistor, then connect the other end of this resistor to GND.
● Repeat the previous step for the red LED, connecting the LED to pin 7 on the Arduino.
● Connect one end of the photoresistor to 5V. Connect the other end of the photoresistor to pin A0. Additionally, add a 220-ohm resistor to the second end of the photoresistor, and connect it to GND.
● Use the positive and negative rails on your breadboard to connect the previous circuits if you need to.

Once all your wiring is connected, you can plug your Arduino into a power source to turn the project on. Wave your hand over the photoresistor to switch from the blue LED to the red one. If either LED stays on continuously, try adjusting the value in the if() statement in the sketch until the sensor works the way you want it to. When you’re happy, you’re done!

DevicePlus Editorial Team
DevicePlus Editorial Team

Check us out on Social Media

  • Facebook
  • Twitter

Recommended Posts

  • How to Use a Relay to Control Lamp or Other High-Voltage ElectronicsHow to Use a Relay to Control Lamp or Other High-Voltage Electronics
  • How to Build a Motion Activated LightHow to Build a Motion Activated Light
  • How to Control a Light with a DimmerHow to Control a Light with a Dimmer
  • Let’s use Arduino with a light sensor!Let’s use Arduino with a light sensor!
  • What Is Current, Voltage, Resistors, and Ohm’s Law?What Is Current, Voltage, Resistors, and Ohm’s Law?
  • Create a Web Server with Arduino!Create a Web Server with Arduino!
Receive update on new postsPrivacy Policy

Recommended Tutorials

  • How to integrate an RFID module with Raspberry Pi How to integrate an RFID module with Raspberry Pi
  • How to Use the NRF24l01+ Module with Arduino How to Use the NRF24l01+ Module with Arduino
  • How to Run Arduino Sketches on Raspberry Pi How to Run Arduino Sketches on Raspberry Pi
  • Setting Up Raspberry Pi as a Home Media Server Setting Up Raspberry Pi as a Home Media Server

Recommended Trends

  • SewBot Is Revolutionizing the Clothing Manufacturing Industry SewBot Is Revolutionizing the Clothing Manufacturing Industry
  • All About The Sumo Robot Competition And Technology All About The Sumo Robot Competition And Technology
  • 5 Interesting Tips to Calculating the Forward Kinematics of a Robot 5 Interesting Tips to Calculating the Forward Kinematics of a Robot
  • Go Inside the Drones That Are Changing Food Delivery Go Inside the Drones That Are Changing Food Delivery
Menu
  • Arduino –
    Arduino Beginner’s Guide
  • Raspberry Pi –
    Raspberry Pi Beginner's Guide
  • Trending –
    Updates on New Technologies
  • Others –
    Interviews / Events / Others

Check us out on Social Media

  • Facebook
  • Twitter
  • About
  • Company
  • Privacy Policy
  • Terms of Service
  • Contact
  • Japanese
  • 简体中文
  • 繁體中文
Don’t Forget to Follow Us!
© Copyright 2016-2023. Device Plus - Powered by ROHM
© 2023 Device Plus. All Rights Reserved. Muffin group

istanbul escort istanbul escort istanbul escort