Your concept
I created an arduino that responds to light using the photolight sensor. When the room is dark, the LEDs automatically turn on. Alternatively, the user can also press on the button to turn on the LEDs, however, they only remain lit up as long as the user presses on the button.
Schematic
Video of the circuit
IMG_7822
How this was made
I referred to the class notes and schematics to create the photolight sensor part of the circuit. Similarly, for the switch, I also referred to class notes. In order to get the response of the LEDs when the room gets dark, I put a specific threshold in the code, and when the the sensor reading is below that threshold, the LEDs turn on. The switches responsiveness was similar to exercises we did in class.
Code
int photoresistor = 0; // this variable will hold a value based on the brightness of the ambient light
int threshold = 500; // if the photoresistor reading is below this value the the light will turn on
void setup()
{
Serial.begin(9600); // starts a serial connection with the computer
pinMode(A2, INPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT); // set pin 8 & 9 as an output that can be set to HIGH or LOW
pinMode(13, OUTPUT); // used pin 13 to troubleshoot since sometimes my LEDs wouldn't be connected correctly
}
void loop()
{
//read the brightness of the ambient light
photoresistor = analogRead(A0); // sets photoresistor to a number between 0 and 1023 based on how bright the ambient light is
int switchPosition = digitalRead(A2);
Serial.println(photoresistor); // print the value of photoresistor in the serial monitor on the computer
if (switchPosition == HIGH || photoresistor < threshold) {
digitalWrite(8, HIGH); // turn the LED on (high voltage)
digitalWrite(9, HIGH);
digitalWrite(13, HIGH);
} else {
digitalWrite(8, LOW); // turn the LED off by making the voltage LOW
digitalWrite(9, LOW);
digitalWrite(13, LOW);
}
}
Reflection and ideas for future work or improvements
I’m proud that I was able to get the circuit to actually work and be responsive! As simple as it seems, I was really struggling to do a simple circuit with a switch and one LED, my connections were all not working and it was not being responsive at all. Hence, I’m really happy I was able to incorporate multiple inputs and outputs in the end.
There are a few improvements I can think of for this circuit. For starters, I think I should’ve used different components and edited the code so that when the user presses on the switch, the LEDs stay on until user presses again. Another possible improvement is allowing the user to turn off the LEDs using the switch when they’re on due to the photoresistor. I think this would require me to play around with the code more and possibly user others components from the kit. I also hope to get more creative with my circuits and have more unusual interactions in the future.
