Please help! - code not working

Hello all, I am a beginner into this stuff. I am using an Arduino UNO and trying to make a plant watering system. I am not sure why, but the serial monitor is not printing out anything, and nothing is working. When i upload, it says upload is successful and I also tried with other example sketches and it works just fine, so i assume something is wrong with my code or something else. any help is appreciated

This is the code:

#include <dht.h>
#include <LiquidCrystal_I2C.h>
#include <string.h>
dht DHT;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A5;
const int mThreshold = 350;
const int pumpSet = 10;
const int pumpLED = 9;
int buttonValue = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.println("Arduino On");
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  pinMode(pumpSet, INPUT_PULLUP);
  pinMode(pumpLED,OUTPUT);
  Serial.begin(9600);
  lcd.init();
  delay(1000);
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  lcd.clear();
  lcdHumidity(humidity);
  lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  lcd.clear();
  lcdSM(soilMoisture);
  lcdLight(PRreading);
  delay(1000);
  Serial.println(digitalRead(pumpSet));
  Serial.println(buttonValue);
  if(digitalRead(pumpSet) == 0){
    buttonValue += 1;
  }
  if(buttonValue % 2 != 0){
    digitalWrite(pumpLED, HIGH);
  }
  else{
    digitalWrite(pumpLED, LOW);
  }
  
}

void lcdHumidity(int humidityInput){
  lcd.setCursor(0, 1); 
  lcd.print("Humidity: ");
  lcd.print(humidityInput);
  lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  lcd.setCursor(0, 0); 
  lcd.print("Temperature: ");
  lcd.print(tempInput);
  lcd.print((char)223);
  lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  lcd.setCursor(0, 0); 
  lcd.print("Moisture: ");
  lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  lcd.setCursor(0,1);
  lcd.print("Light: ");
  if(lightInput >= 400){
    lightState = "Luminous";
  }
  if(lightInput >= 600){
    lightState = "Bright";
  }
  if(lightInput >= 700){
    lightState = "Moderate";
  }
  if(lightInput >= 800){
    lightState = "Dim";
  }
  if(lightInput >= 900){
    lightState = "Dark";
  }
   
  lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold && buttonValue % 2 != 0){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

As you can see, I also wrote a print statement on the first line on setup(). When i upload and open the serial monitor, nothing appears.

Image of serial monitor after uploading:

Any help is appreciated, Thanks!

Hi @geniuscow36 ,
Welcome to the forum..

hmm.. have to begin before printing..
move the Serial.begin to the top..

good luck.. ~q

thank you, i overlooked that.
it prints "Arduino On" on the serial monitor now, but unfortunately thats all that happened

#include <dht.h>
#include <LiquidCrystal_I2C.h>
#include <string.h>
dht DHT;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A5;
const int mThreshold = 350;
const int pumpSet = 10;
const int pumpLED = 9;
int buttonValue = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino On");
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  pinMode(pumpSet, INPUT_PULLUP);
  pinMode(pumpLED,OUTPUT);
  lcd.init();
  delay(1000);
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  lcd.clear();
  lcdHumidity(humidity);
  lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  lcd.clear();
  lcdSM(soilMoisture);
  lcdLight(PRreading);
  delay(1000);
  Serial.println(digitalRead(pumpSet));
  Serial.println(buttonValue);
  if(digitalRead(pumpSet) == 0){
    buttonValue += 1;
  }
  if(buttonValue % 2 != 0){
    digitalWrite(pumpLED, HIGH);
  }
  else{
    digitalWrite(pumpLED, LOW);
  }
  
}

void lcdHumidity(int humidityInput){
  lcd.setCursor(0, 1); 
  lcd.print("Humidity: ");
  lcd.print(humidityInput);
  lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  lcd.setCursor(0, 0); 
  lcd.print("Temperature: ");
  lcd.print(tempInput);
  lcd.print((char)223);
  lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  lcd.setCursor(0, 0); 
  lcd.print("Moisture: ");
  lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  lcd.setCursor(0,1);
  lcd.print("Light: ");
  if(lightInput >= 400){
    lightState = "Luminous";
  }
  if(lightInput >= 600){
    lightState = "Bright";
  }
  if(lightInput >= 700){
    lightState = "Moderate";
  }
  if(lightInput >= 800){
    lightState = "Dim";
  }
  if(lightInput >= 900){
    lightState = "Dark";
  }
   
  lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold && buttonValue % 2 != 0){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

The rest of the code still does not work, i am still unsure why

Serial Monitor:

edit:
I used an earlier version of the code I had that used to work before and uploaded it, but now it does not work. Maybe this additional information may help in finding a solution

the code i tested with:

#include <dht.h>
#include <LiquidCrystal_I2C.h>
#include <string.h>
dht DHT;
int tempType = 0;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A1;
const int mThreshold = 350;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  Serial.begin(9600);
  lcd.init();
  delay(1000);
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  lcd.clear();
  lcdHumidity(humidity);
  lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  lcd.clear();
  lcdSM(soilMoisture);
  lcdLight(PRreading);
  delay(1000);
  
}

void lcdHumidity(int humidityInput){
  lcd.setCursor(0, 1); 
  lcd.print("Humidity: ");
  lcd.print(humidityInput);
  lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  lcd.setCursor(0, 0); 
  lcd.print("Temperature: ");
  lcd.print(tempInput);
  lcd.print((char)223);
  lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  lcd.setCursor(0, 0); 
  lcd.print("Moisture: ");
  lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  lcd.setCursor(0,1);
  lcd.print("Light: ");
  if(lightInput >= 300){
    lightState = "Luminous";
  }
  if(lightInput >= 500){
    lightState = "Bright";
  }
  if(lightInput >= 600){
    lightState = "Moderate";
  }
  if(lightInput >= 700){
    lightState = "Dim";
  }
  if(lightInput >= 950){
    lightState = "Dark";
  }
   
  lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

strange..
just simmed it, seems to do something..

Your project in a simulator..

~q

Can you print anything at the end of setup?

What happens if you comment out all the lcd stuff?

I tried printing at the end of the setup, it does not print the statement. but, i removed all the lcd stuff and it seemed to work just fine, any ideas for the cause of this issue?

Code:

#include <dht.h>
//#include <LiquidCrystal_I2C.h>
#include <string.h>
dht DHT;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A5;
const int mThreshold = 350;
const int pumpSet = 10;
const int pumpLED = 9;
int buttonValue = 0;

//LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino On");
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  pinMode(pumpSet, INPUT_PULLUP);
  pinMode(pumpLED,OUTPUT);
  //lcd.init();
  delay(1000);
  //lcd.setCursor(0,0);
  //lcd.clear();
  //lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  //lcd.clear();
  //lcdHumidity(humidity);
  //lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  //lcd.clear();
  //lcdSM(soilMoisture);
  //lcdLight(PRreading);
  delay(1000);
  Serial.println(digitalRead(pumpSet));
  Serial.println(buttonValue);
  if(digitalRead(pumpSet) == 0){
    buttonValue += 1;
  }
  if(buttonValue % 2 != 0){
    digitalWrite(pumpLED, HIGH);
  }
  else{
    digitalWrite(pumpLED, LOW);
  }
  
}

void lcdHumidity(int humidityInput){
  //lcd.setCursor(0, 1); 
  //lcd.print("Humidity: ");
  //lcd.print(humidityInput);
  //lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  //lcd.setCursor(0, 0); 
  //lcd.print("Temperature: ");
  //lcd.print(tempInput);
  //lcd.print((char)223);
  //lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  //lcd.setCursor(0, 0); 
  //lcd.print("Moisture: ");
  //lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  //lcd.setCursor(0,1);
  //lcd.print("Light: ");
  if(lightInput >= 400){
    lightState = "Luminous";
  }
  if(lightInput >= 600){
    lightState = "Bright";
  }
  if(lightInput >= 700){
    lightState = "Moderate";
  }
  if(lightInput >= 800){
    lightState = "Dim";
  }
  if(lightInput >= 900){
    lightState = "Dark";
  }
   
  //lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold && buttonValue % 2 != 0){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

Hi @geniuscow36 ,

I "ported" your sketch to Wokwi and it works there (as far as I can judge).

Wokwi provides a DHT22 simulation instead of a DHT11, so I had to change one single line

//  float structData = DHT.read11(DHT11PIN);
  float structData = DHT.read22(DHT11PIN);

You may check it here

You should probably check your wiring ...

Good luck!
ec2021

P.S.: I used the second sketch from your post #3

HI @qubits-us ,

overlap .... you were faster ... :wink:

1 Like

hey all, i did some testing and the code just stops working at the line lcd.init();

#include <LiquidCrystal_I2C.h>

#include <dht.h>
#include <string.h>
dht DHT;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A5;
const int mThreshold = 350;
const int pumpSet = 10;
const int pumpLED = 9;
int buttonValue = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino On");
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  pinMode(pumpSet, INPUT_PULLUP);
  pinMode(pumpLED,OUTPUT);
  lcd.init();
  delay(1000);
  //lcd.setCursor(0,0);
  //lcd.clear();
  //lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  //lcd.clear();
  //lcdHumidity(humidity);
  //lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  //lcd.clear();
  //lcdSM(soilMoisture);
  //lcdLight(PRreading);
  delay(1000);
  Serial.println(digitalRead(pumpSet));
  Serial.println(buttonValue);
  if(digitalRead(pumpSet) == 0){
    buttonValue += 1;
  }
  if(buttonValue % 2 != 0){
    digitalWrite(pumpLED, HIGH);
  }
  else{
    digitalWrite(pumpLED, LOW);
  }
  
}

void lcdHumidity(int humidityInput){
  //lcd.setCursor(0, 1); 
  //lcd.print("Humidity: ");
  //lcd.print(humidityInput);
  //lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  //lcd.setCursor(0, 0); 
  //lcd.print("Temperature: ");
  //lcd.print(tempInput);
  //lcd.print((char)223);
  //lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  //lcd.setCursor(0, 0); 
  //lcd.print("Moisture: ");
  //lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  //lcd.setCursor(0,1);
  //lcd.print("Light: ");
  if(lightInput >= 400){
    lightState = "Luminous";
  }
  if(lightInput >= 600){
    lightState = "Bright";
  }
  if(lightInput >= 700){
    lightState = "Moderate";
  }
  if(lightInput >= 800){
    lightState = "Dim";
  }
  if(lightInput >= 900){
    lightState = "Dark";
  }
   
  //lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold && buttonValue % 2 != 0){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

Im not sure what the reason is for this issue, thanks again for all the responses

After lcd.init() add the line:

lcd.backlight();

and see if the backlight starts.

I’d guess that you’re low on memory. What does the compiler say about how much you have used?

Sorry all, I'll be responding tomorrow, need some sleep

I reverted the changes and now the code works (printing everything, idk what happened it just suddenly worked ), but the LCD keeps turning off and on for some reason...

Code:

#include <LiquidCrystal_I2C.h>

#include <dht.h>
#include <string.h>
dht DHT;
#define DHT11PIN 2
#define AOUT_PIN A0
const int waterState = 3; 
const int pumpPin = 4;
const int prPIN = A5;
const int mThreshold = 350;
const int pumpSet = 10;
const int pumpLED = 9;
int buttonValue = 0;

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {
  Serial.begin(9600);
  Serial.println("Arduino On");
  pinMode(waterState, OUTPUT);
  pinMode(pumpPin, OUTPUT);
  pinMode(prPIN, INPUT);
  pinMode(pumpSet, INPUT_PULLUP);
  pinMode(pumpLED,OUTPUT);
  lcd.init();
  delay(1000);
  lcd.setCursor(0,0);
  lcd.clear();
  lcd.backlight();
}

void loop() {
  float structData = DHT.read11(DHT11PIN);
  float humidity = DHT.humidity;
  float temperature = DHT.temperature;
  float PRreading = analogRead(prPIN);
  int soilMoisture = analogRead(AOUT_PIN);
  lcd.clear();
  lcdHumidity(humidity);
  lcdTemp(temperature);
  delay(1000);
  checkMoisture(soilMoisture);
  lcd.clear();
  lcdSM(soilMoisture);
  lcdLight(PRreading);
  delay(1000);
  Serial.println(digitalRead(pumpSet));
  Serial.println(buttonValue);
  if(digitalRead(pumpSet) == 0){
    buttonValue += 1;
  }
  if(buttonValue % 2 != 0){
    digitalWrite(pumpLED, HIGH);
  }
  else{
    digitalWrite(pumpLED, LOW);
  }
  
}

void lcdHumidity(int humidityInput){
  lcd.setCursor(0, 1); 
  lcd.print("Humidity: ");
  lcd.print(humidityInput);
  lcd.print("%");
  Serial.print("Humidity: ");
  Serial.print(humidityInput);
  Serial.println("%");
}

void lcdTemp(int tempInput){
  lcd.setCursor(0, 0); 
  lcd.print("Temperature: ");
  lcd.print(tempInput);
  lcd.print((char)223);
  lcd.print("C");
  Serial.print("Temp: ");
  Serial.println(tempInput);
}

void lcdSM(int smInput){
  lcd.setCursor(0, 0); 
  lcd.print("Moisture: ");
  lcd.print(smInput);
  Serial.print("Moisture: ");
  Serial.println(smInput);
}

void lcdLight(int lightInput){
  String lightState = "Blinding";
  lcd.setCursor(0,1);
  lcd.print("Light: ");
  if(lightInput >= 400){
    lightState = "Luminous";
  }
  if(lightInput >= 600){
    lightState = "Bright";
  }
  if(lightInput >= 700){
    lightState = "Moderate";
  }
  if(lightInput >= 800){
    lightState = "Dim";
  }
  if(lightInput >= 900){
    lightState = "Dark";
  }
   
  lcd.print(lightState);
  Serial.print("Light: ");
  Serial.println(lightInput);
  Serial.println(lightState);
}

void checkMoisture(int smInput){
  if(smInput > mThreshold && buttonValue % 2 != 0){
    digitalWrite(waterState, HIGH);
    pumpState("ON");
  }
  else{
    digitalWrite(waterState,LOW);
    pumpState("OFF");
  }
}

void pumpState(String state){
  if(state == "ON"){
    digitalWrite(pumpPin,HIGH);
    Serial.println("PUMP ON");

  }
  else{
    digitalWrite(pumpPin,LOW);
    Serial.println("PUMP OFF");
  }
}

Oh and this is the output using the code i provided above.

edit: some stuff still does not work, still not sure ill look into it tomorrow. thanks all

that's i2c clock, not sure what is attached to prPIN, try moving it and can't use A4 either..
good luck.. ~q

1 Like

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