Project Problems

I'm trying to create a circuit that monitors the moisture, light and temperature of a plant. However, I've been experiencing problem after problem. Essentially, when one of these conditions is off, a piezo needs to go off, and a message needs to be printed to the LCD. Except, for some reason, the LCD doesn't work at all here, the values being pulled out are all out of whack, and the piezo just goes off indefinitely.

Here is the Tinkercad mockup, that way you can go in and play around with the circuit: Login - Tinkercad

Here's a picture:

Here's the code:

#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int moistureInputPin = A0;
int moistureOutputPin = A1;
int temperaturePin = A2;
int lightPin = A3;
int piezoPin = 8;

int temperature;
int light;
int moisture;

float coldestTemp = 50;
float hottestTemp = 95;
int minLight = 10;
int minMoisture = 100;

bool prevLightFine = true;
bool prevTempFine = true;
bool prevMoistureFine = true;

bool conditionsRight;
bool prevConditionsState;

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
}

void loop() {
  temperature = (((analogRead(temperaturePin) / 1024.0) * 5.0) - 0.5) * 100;
  light = analogRead(lightPin);
  digitalWrite(moistureOutputPin, HIGH);
  delay(10);
  moisture = analogRead(moistureInputPin);
  digitalWrite(moistureOutputPin, LOW);
  conditionsRight = (temperature < hottestTemp && temperature > coldestTemp) && (light >= minLight) && (moisture >= minMoisture);

  Serial.print("Moisture: ");
  Serial.print(moisture);
  Serial.print(" Light: ");
  Serial.print(light);
  Serial.print(" Temperature: ");
  Serial.println(temperature);

  if (moisture < minMoisture) {
    if (prevMoistureFine) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Moisture low!");
    }
    tone(piezoPin, 349);
  }
  if (light < minLight) {
    if (prevLightFine) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Light low!");
    }
    tone(piezoPin, 349);
  }
  if (temperature > hottestTemp) {
    if (prevTempFine) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Too hot!");
    }
    tone(piezoPin, 349);
  }
  if (temperature < coldestTemp) {
    if (prevTempFine) {
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("Too cold!");
    }
    tone(piezoPin, 349);
  }
  if (conditionsRight == true && prevConditionsState == false) {
    lcd.clear();
    lcd.setCursor(0, 0);
    lcd.print("All good.");
    noTone(piezoPin);
  }

  prevLightFine = light > minLight;
  prevTempFine = temperature > coldestTemp && temperature < hottestTemp;
  prevMoistureFine = moisture > minMoisture;
  prevConditionsState = conditionsRight;
}

What's wrong with it? I think it's probably some kind of programming error.

unable to login to view drawing. please post the code

it's important to write code verifying the usage of each device before combining them into a larger project.

1 Like

I did not spot a programming error.
Did you select the correct lcd?
Is your wiring correct?

I recommend you to add Serial.print(someCondition) for all conditions in your loop. That will tell you which sensor causes the alarm.
Are you sure your tmp is connected correctly?

I got some help from somebody a little bit ago; he said it may have been some faulty wiring with the LCD somewhere.

So your problem is solved?
If so, please mark as solved. That will save time for the helpers of the forum to help others or grab a coffee.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.