Week 9 – Variable Threshold Lightsensor

 

Concepts

In this week’s HW, the concept is a  variable threshold light sensor where the blue led lights up depending on if the ambient light is higher than the threshold which is controlled by the switch.

I built a simple Arduino circuit that used an analog sensor and a digital sensor to control two LEDs. To begin, I connected an LDR (Light-Dependent Resistor) and a switch to the Arduino board. The LDR measured the ambient light level, and the switch was used to control the threshold at which the LDR LED turns on.

The Demo


Code

For the coding part, I programmed the Arduino to turn on the LDR LED when the ambient light level fell below the threshold set by the switch. Conversely, when the ambient light level was lower than the threshold, the LDR LED turned off. To make the circuit more interactive, I added a feature that allowed me to adjust the threshold of the LDR LED by pressing the switch. Each time I pressed the switch, the threshold decreased by 200, making the LDR LED turn on at a lower ambient light level. Once the threshold reached 0, it looped back to the maximum value of 1023, allowing me to adjust the threshold again.

digitalSensorState = digitalRead(digitalSensorPin);
if (digitalSensorState == HIGH) {
  switchPressCount++;
  ldrThreshold -= 200;
  if (ldrThreshold < 0) {
    ldrThreshold = 1023;
  }

In addition to controlling the digital LED with the switch and LDR, I also programmed the analog LED to change its brightness depending on the threshold set by the switch. The analog LED became brighter as the threshold decreased. This was done using the map function.

int brightness = map(ldrThreshold, 0, 1023, 255, 0);
analogWrite(analogLEDPin, brightness);

Challenges and Future changes

During the development of this project, one of the challenges I faced was accurately calibrating the LDR threshold with the switch. Ensuring that the threshold value was accurate and consistent required some trial and error. Additionally, mapping the threshold value to the analog LED brightness required some experimentation to ensure that the changes in brightness were noticeable and distinct. In terms of possible future changes and improvements, one idea would be to add additional sensors, such as a motion sensor, to further expand the functionality of the circuit.

Leave a Reply