Week 9 – Arduino: analog and digital inputs & outputs

My Concept: I wanted to create a circuit in which the push button turns LED1  on and off and the photoresistor regulates the LED2 by turning on, making it blink and turning off based on the value of the photoresistor.

The video of the work: arduino_video

The code on Arduino:

The example with three push buttons controlling three LEDs on the website https://microdigisoft.com/controlling-leds-with-multiple-push-button-using-arduino/ was used in creating a circuit with one push button and one photoresistor with two LEDs.

//initializing the pins to push button and photoresistor 
const int BUTTON1 = 2; 
const int PHOTOSENSOR = A0;
//initializing the pins to LED1 and LED2
const int LED1 = 8;
const int LED2 = 12;

int BUTTONstate1 = 0;
int PHOTOSENSORvalue = 0; 

void setup()
{
//defining the button (digital) and photoresistor (analog) as input pins
  pinMode(BUTTON1, INPUT); 
  pinMode(PHOTOSENSOR, INPUT);
//defining LEDs as output pins
  pinMode(LED1, OUTPUT);
  pinMode(LED2, OUTPUT);
}

void loop()
{
//the conditional to turn on the LED1 if the button is pushed
  BUTTONstate1 = digitalRead(BUTTON1); 
  if (BUTTONstate1 == LOW)
  {
    digitalWrite(LED1, HIGH);
  }
  else
  {
    digitalWrite(LED1, LOW);
  }

//the conditional to turn on the LED2, make it blink and turn off based on the value on photoresistor
  PHOTOSENSORvalue = analogRead(PHOTOSENSOR);
  if (PHOTOSENSORvalue < 500)
  {
    // Low light condition which turns LED2 on
    digitalWrite(LED2, HIGH);
  }
  else if (PHOTOSENSORvalue >= 500 && PHOTOSENSORvalue < 1000)
  {
    // Medium light condition which makes the LED2 blink
    digitalWrite(LED2, HIGH); 
    delay(500); 
    digitalWrite(LED2, LOW); 
    delay(500); 
  }
  else
  {
    // High light condition which turns LED2 off
    digitalWrite(LED2, LOW);
  }
}

I am particularly proud of the code with reads the value of the photoresistor and gives the LED outputs accordingly (turn on, blink, turn off).

Reflections: This work might seem easy, but took lots of research and understanding of the concept. Initially, my idea was to control the brightness of LED2 based on the resistance value on the photoresistor to show that the LED2 is an analog output. However, strangely, LED1 was affected by the value of the photoresistor and LED2 did not react. Then, I changed the code to make the LED2 turn on when the value of the photoresistor is less than 500 ohms, blink when it is in the range of [500, 10000) and turn off when these conditions are not met. As this one worked well, I used this code. Nevertheless, I would like to solve the mystery of the first outcome.

Leave a Reply