park-jiyungledel
Published

Reading A Photoresistor

HI! Long time no see!

IntermediateShowcase (no instructions)30 minutes7,926
Reading A Photoresistor

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Photo resistor
Photo resistor
×1
Breadboard (generic)
Breadboard (generic)
×1
LED (generic)
LED (generic)
×1
Resistor 100 ohm
Resistor 100 ohm
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Reading a Photoresistor

Code

Reading a Photoresistor

Arduino
/*
SparkFun Inventor's Kit
Example sketch 07
PHOTORESISTOR
Read a photoresistor (light sensor) to detect "darkness" and turn on an LED when it
is "dark" and turn back off again when it is "bright.
This sketch was written by SparkFun Electronics,
with lots of help from the Arduino community.
This code is completely free for any use.
Visit http://learn.sparkfun.com/products/2 for SIK information.
Visit http://www.arduino.cc to learn about the Arduino.
*/
Experiment 7: Reading a Photoresistor
82
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
const int sensorPin = 0;
const int ledPin = 9;
// We'll also set up some global variables for the light level a calibration value and
//and a raw light value
int lightCal;
int lightVal;
void setup()
{
// We'll set up the LED pin to be an output.
pinMode(ledPin, OUTPUT);
lightCal = analogRead(sensorPin);
//we will take a single reading from the light sensor and store it in the lightCal
//variable. This will give us a prelinary value to compare against in the loop
}
void loop()
{
//Take a reading using analogRead() on sensor pin and store it in lightVal
lightVal = analogRead(sensorPin);
//if lightVal is less than our initial reading (lightCal) minus 50 it is dark and
//turn pin 9 HIGH. The (-50) part of the statement sets the sensitivity. The smaller
//the number the more sensitive the circuit will be to variances in light.
if (lightVal < lightCal - 50)
{
digitalWrite(9, HIGH);
}
//else, it is bright, turn pin 9 LOW
else
{
digitalWrite(9, LOW);
}
}

Credits

park-jiyun
4 projects • 2 followers
I am a high school student studying hard.
Contact
gledel
100 projects • 116 followers
Looking back on my childhood, I was happy when I was making something and I was proud of myself. "Making is instinct!"
Contact

Comments

Please log in or sign up to comment.