For this week’s assignment, we were tasked with controlling two LEDs using analogue and digital means. For my project, I decided to utilize the photoresistor to control one of the LEDs.
The idea was to have one LED controlled by a button, while the second LED would change its brightness depending on how much light the photoresistor detected. This works through the photoresistor sitting next to the first LED, so when it turns on, the second LED gets dimmer, and vice versa.
The components I used were:
- Arduino Uno
- Breadboard
- Wires
- 2 LEDs
- 2 330 ohms resistors
- 1 button switch
- 2 10k ohm resistors
- 1 photoresistor
Video of the circuit: https://drive.google.com/file/d/18Y1VjX12o3Z_u9x9rxtTDF2E-SOxcpOG/view?usp=sharing
Schematic:
The code:
const int buttonPin = 2; // Button input pin const int led1Pin = 8; // LED1 (button-controlled) const int photoPin = A0; // Analog input for photoresistor const int led2Pin = 9; // LED2 (brightness controlled by light) void setup() { pinMode(buttonPin, INPUT); // Set button pin as input pinMode(led1Pin, OUTPUT); // Set LED1 pin as output pinMode(led2Pin, OUTPUT); // Set LED2 pin as output Serial.begin(9600); // Start Serial Monitor for debugging } void loop() { int buttonState = digitalRead(buttonPin); // Check if button is pressed // Control LED1 based on button if (buttonState == HIGH) { digitalWrite(led1Pin, HIGH); // Turn on LED1 if button is pressed } else { digitalWrite(led1Pin, LOW); // Otherwise, turn it off } // Read the light level from the photoresistor int lightLevel = analogRead(photoPin); // 0 (dark) to 1023 (bright) Serial.println(lightLevel); // Convert light level into LED2 brightness (invert it!) int brightness = map(lightLevel, 0, 1023, 255, 0); brightness = constrain(brightness, 0, 255); // Make sure value stays in range analogWrite(led2Pin, brightness); // Set brightness of LED2 delay(10); // Small delay }
The coding was probably the hardest part, as I was struggling with having the photoresistor convert the binary values into a values that would be suitable for the resisitor, and would show a noticeable decrease in brightness in the second LED.
Overall, this was an interesting project, and I was able to better understand some functions within the Arduino IDE program and the way a photoresistor could be utilized.