Week 9: Analog & digital sensors

Concept

For this assignment we were required to get information from at least one analog sensor and at least one digital sensor (switch), and use this information to control at least two LEDs, one in a digital fashion and the other in an analog fashion, in some creative way. At first I thought of doing a distance-related project using the ultrasonic sensor, but I decided to stick to my previous project on moisture detection to try and make it better. So for this assignment I incorporated a digital sensor (switch) and more LEDs for different scenarios.

Code

The coding aspect of the implementation was a bit challenging as it involved a lot of trial, observation and error.  A snippet of the code below shows how the LEDs change color based on amount of moisture detected.

void loop() {
  int low = 350, high = 100, water, pepsi;
  bool status = false;
  int sensorValue = analogRead(A0);
  if(digitalRead(7)==1)
    { digitalWrite(6, HIGH);
    if(sensorValue < 550)
     digitalWrite(5, HIGH);
    else
     digitalWrite(5, LOW);
   if(sensorValue > 550)
     digitalWrite(4, HIGH);
   else
     digitalWrite(4, LOW);}

Result / Future improvements

The original idea was to create a demo of an instrument that can measure moisture of say the area where a tree should be planted. If the there isn’t enough moisture the yellow LED turns on indicating that the area is dry. If there is optimal moisture levels for the tree to be planted then the green LED turns on. The blue LED is controlled by the digital sensor to allow electricity to pass and flow throughout the circuit. In the video below, the green towel has been moisturized with water and the orange one is the dry one. The original idea was to implement the digital switch as a toggle switch, which when clicked stays on until clicked again. However, this proved to be very complicated and couldn’t implement it in the end.

Leave a Reply