Last active
March 7, 2019 10:43
-
-
Save bboyho/801acb08d9918cd67f73b5b690b50c11 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
SparkFun Inventor’s Kit | |
Circuit 1C-Photoresistor | |
Use a photoresistor to monitor how bright a room is, and turn an LED on when it gets dark. | |
This sketch was written by SparkFun Electronics, with lots of help from the Arduino community. | |
This code is completely free for any use. | |
View circuit diagram and instructions at: https://learn.sparkfun.com/tutorials/sparkfun-inventors-kit-experiment-guide---v40 | |
Download drawings and code at: https://github.com/sparkfun/SIK-Guide-Code | |
*/ | |
int photoresistor = 0; //this variable will hold a value based on the brightness of the ambient light | |
int threshold = 70; //if the photoresistor reading is below this value the light will turn on | |
void setup() | |
{ | |
Serial.begin(9600); //start a serial connection with the computer | |
pinMode(13, OUTPUT); //set pin 13 as an output that can be set to HIGH or LOW | |
} | |
void loop() | |
{ | |
//read the brightness of the ambient light | |
photoresistor = analogRead(A0); //set photoresistor to a number between 0 and 1023 based on how bright the ambient light is | |
Serial.println(photoresistor); //print the value of photoresistor in the serial monitor on the computer | |
//if the photoresistor value is below the threshold turn the light on, otherwise turn it off | |
if (photoresistor < threshold){ | |
digitalWrite(13, HIGH); // Turn on the LED | |
} else{ | |
digitalWrite(13, LOW); // Turn off the LED | |
} | |
delay(100); //short delay to make the printout easier to read | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment