Digital and Analog Sensors

Concept

In this assignment, I built a circuit on the breadboard controlled by two sensors, a digital one and an analog one. The digital sensor is the button switch that controls the state of the red LED. The analog sensor is a light sensor that I designed to control the brightness of the green LED. I designed the circuit creatively so that the brightness of the red light will affect the light sensor and result in turning on the green light. Therefore, by just pressing the button, the user can see both the red and blue lights turned on. You can see the schematic for my project attached below.

leftmost resistor is also 10kOhms

In the schematic above you can see how I designed my circuit having the light sensor as an analog input that is being read by pin A2. and the switch as digital input that is being read by pin 8. Based on the values read by these pins I control the values produced by pin 12 and pin 7 which control the state of the two LED light outputs on the right. Here is the code that shows how I did this in Arduino.

void setup(){
  pinMode(8,INPUT);
  pinMode(7,OUTPUT);
  pinMode(A2,INPUT);
  pinMode(12,OUTPUT);
  Serial.begin(9600);
}

void  loop(){
  int switchState=digitalRead(8);
  int lightSensorInput=analogRead(A2);\
  Serial.println(lightSensorInput);
  delay(1);

  if(switchState==HIGH){
    digitalWrite(7,HIGH);

  }
  else{
    digitalWrite(7,LOW);

  }

  if(lightSensorInput>700){
    digitalWrite(12,HIGH);
  }
  else{
    digitalWrite(12,LOW);
  }

}

As you can see in the above code, the switch controls the state of the red LED. Then if the light sensor value is more that 700 then the green LED will turn on. Initially the light sensor values are not more than 620. However, I attached the red LED to be very close to the light sensor, so that when it is turned on, the value taken by the light sensor becomes greater than 700, hence the green LED light turns on. The result for this can be seen in the video below.

I really enjoyed creating this creative project, but I think it needs some improvements, especially in the way I did the wiring, I believe it needs to be more organized. To improve on this project, I might also add another light sensor to the other light and create an oscillating behaviour between the two lights.

Leave a Reply