How to use a Photo-Resister(LDR)


Photocell


Introduction 



Picture








A photoresistor or photocell is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. It's also called light-dependent resistor (LDR).
In this tutorial you will learn how to use a photoresistor with and without arduino uno.



How to use a photoresistor

Picture
Let's see how a photoresistor react in light. Build the circuit above and notice how led brightness change.
The resistance value becomes smaller when there is much light in the room. So in the dark the led remains off because the resistance has become very big.
The Arduino will help us to reverse this situation, let's see how in next step.

What you will need - Hardware

For this tutorial you will need:
  • Arduino uno
  • Breadboard
  • Photoresistor
  • LED
  • 220 Ohm & 10KOhm resistors

The Circuit

Picture
The connections are pretty easy, see the image above with breadboard circuit schematic.

The Code ::
How it works:
  • Read analog value from photoresistor/photocell
    -> value=analogRead(pResistor)
  • Send command to turn on/off the led
***********************************************************
/* Use a photoresistor (or photocell) to turn on an LED in the dark */
  
//Constants
const int pResistor = A0; // Photoresistor at Arduino analog pin A0
const int ledPin=9;       // Led pin at Arduino pin 9

//Variables
int value;  // Store value from photoresistor (0-1023)

void setup(){
 pinMode(ledPin, OUTPUT);  // Set lepPin - 9 pin as an output
 pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional)
}

void loop(){
  value = analogRead(pResistor);
  
  //You can change value "25"
  if (value > 25){
    digitalWrite(ledPin, LOW);  //Turn led off
  }
  else{
    digitalWrite(ledPin, HIGH); //Turn led on
  }

  delay(500); //Small delay
}
***********************************************************

Well done!

You have successfully completed one more Arduino "How to" tutorial and you learned how to use a photoresistor!
I hope you liked this, let me know in the comments.






Comments

Popular posts from this blog

Build Your Own Arduino Weather Station

Arduino ​Obstacle Avoiding Robot Ardunio Computer

Complete Guide for Nokia 5110 LCD with Arduino