Week 10 – Double Light Switch

For this assignment, I wanted to make something a bit funny. Thus, I decided to make a light switch powered by light. My idea was simple, point an LED at an LDR, record its readings, and light up another LED at the brightness the LDR recorded.

Unfortunately, after building by circuit, I ran into an issue with the Arduino, where it would not read anything from the IN pins and would not allow me to print anything to the terminal either.

Not to be discouraged, I decided to build the entire circuit in analog.

Had I been able to use the analog IN pins, my code would have looked like this.

// Define the LDR pin
const int ldrPin = A0;
// Define the digital LED pin
const int digitalLedPin = 13;

// Variable to store the LDR value
int ldrValue = 0;

void setup() {
  Serial.begin(9600);
  pinMode(digitalLedPin, OUTPUT);
}

void loop() {
  // Read the analog value from LDR
  ldrValue = analogRead(ldrPin);
  
  // Print the LDR value to Serial Monitor
  Serial.print("LDR Value: ");
  Serial.println(ldrValue);

  // Set the brightness of the digital LED based on the LDR value
  analogWrite(digitalLedPin, map(ldrValue, 0, 1023, 0, 255));

  // Add a delay to avoid flooding the Serial Monitor with data
  delay(500);
}

Additionally, I noticed that the who design does not work very in lit environments, so I wanted to add a function which would calculate the average light in a room, and set the output LED to 0, so that a change in lighting from the other LED would be more noticeable. This, however, is impossible to do with my understanding of analog circuits.

Leave a Reply