Concept
I TRIED to make a circuit in which an LED could be powered on or off by a switch (button) and a photoresistor (light sensor). When you press the button, you can switch the LED on or off. When the LED is off, however, you can either switch it on by clicking the button, or by waving your hand or any other object on top of the photoresistor to cover it. This is because when the photoresistor is not exposed to much light, it can trigger the LED to switch on.
I followed a tutorial by Electronic Explorer on YouTube in order to figure out the photoresistor’s circuit configuration as well as the coding required to go with it.
To add the button to the circuit, I placed two green wires on either side that connected the button to the rest of the circuit.
- The problem with this, however, was that when I connected the button to the circuit, the photoresistor could no longer control the LED. The only way it could switch it on is by clicking the button.
I came up with another way to fix this, but I had to completely remove the button and keep the two wire ends that were connected to it exposed. This way, I could switch on the LED by either:
- Waving my hands over the photoresistor
OR
- Connecting the two exposed wires to each other to close the circuit
Video
Here’s a video of my brother showing you how to use it:
Code
int LDR = 0; void setup() { pinMode(13, OUTPUT); pinMode(A0, INPUT); Serial.begin(9600); } void loop() { LDR = analogRead(A0); Serial.println(LDR); if(LDR < 512) { digitalWrite(13, 1); } else { digitalWrite(13, 0); } }
Reflection and Improvements:
- I couldn’t figure out how to embed coding for the button (switch) so that the LED could either turn on from the button being pressed or the photoresistor being covered.
- I want to learn how to code proficiently on Arduino because I didn’t know how to resolve any of the problems I had when trying to adjust something in the circuit.
- I also wish I could’ve implemented more creativity overall (especially for the button which I couldn’t figure out how to connect).
You can create boolean variables in Arduino similar to in JavaScript. You could make a boolean for each thing you want to incorporate into your logic and then write some if / else code to do what you want.
For example
bool buttonIsPressed = false;
bool ldrCovered = false;
void loop() {
buttonIsPressed = digitalRead(buttonPin);
int ldrValue = analogRead(A0);
if (ldrValue < 512) { ldrCovered = true; } else { ldrCovered = false; } if (ldrCovered || buttonIsPressed) { doTheThing(); } else { doSomethingElse(); } }
For the circuits you want to put the different inputs / outputs on different pins. So for example to have a button and the the LDR you could connect the button circuit to pin A1 and have your LDR circuit connected to pin A0 with similar adjustments to the code.