Random Characters in 20x4 LCD

I have this project about plants it consist of 3 buttons then it will display the reading of temp, soil moisture and light intensity on the lcd then there would be an actuator acting depending on the conditions

my problem is that the lcd works fine at the start and when i push a button, sometimes it would work then the next next time i pressed another button there would be a time where it would show random characters then not work all through out until i restart the arduino.

after i restart it would sometimes show the system normally then break whenever i press a button

show us the source code and a drawing of your circuit including how things are powered

1 Like

This problem appears to be caused by external electrical noise, such as the activation of inductive loads without snubbers.

Do what @J-M-L recommended and then we'll see how to help.

1 Like

unfortunately i cant show the drawing of the circuit cause i havent started the layout yet but i can show the power supply and the code

POWER SUPPLIES

  • 9V 1A for arduino MEGA
  • 5V 3A for WS1218B (LED STRIP)
  • 12V 1A for water pump
  • 12V 1A for fan
  • AC plug straight to outlet for heater plate

CODE:

byte SLANTtl[8] = { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 };
byte SLANTtr[8] = { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 };
byte SLANTbl[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 };
byte SLANTbr[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 };
byte TOP[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 };
byte BOTTOM[8] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 };
byte HIGHGAP[8] = { B11111, B00000, B00000, B00000, B00000, B11111, B11111, B11111 };
byte LOWGAP[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 };


#include <LiquidCrystal.h>  //LCD
LiquidCrystal lcd(38, 36, 34, 32, 30, 28);


#include <Wire.h>


/*for soil moisture*/
const int AirValue = 780;    //edit later
const int WaterValue = 370;  //edit later
#define On LOW
#define Off HIGH
int pumpState;
int SoilMoistureValue;
int SoilMoisturePercent;
const int SMrelay = 8;


/*for temperature*/
#include <DHT.h>
#define DHTPIN 26
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
float temp_c, temp_f;
int T1 = 26;
int T2 = 20;
int T3 = 24;


/*for light intensity*/
#include <BH1750.h>
#include <FastLED.h>
#define LedPin 10
#define NumLeds 150
CRGBArray<NumLeds> leds;
CRGBSet first(leds(0, 74));
CRGBSet second(leds(75, 149));
CRGBSet all(leds(0, 149));


BH1750 lightMeter;
float brightness;
int LI1 = 35100;  //PPFD to lux
int LI2 = 3780;   //PPFD to lux
int LI3 = 35100;  //PPFD to lux


/*plant button state*/
int lastnightshadestate;
int lastcruciferstate;
int lastlegumestate;
int currentnightshadestate;
int currentcruciferstate;
int currentlegumestate;


int x;


void setup() {
  Wire.setClock(10000);
  // initialize serial communications at 9600 bps:
  Serial.begin(9600);
  lcd.begin(20, 4);
  dht.begin();  //DHT11
  FastLED.addLeds<WS2812B, LedPin, GRB>(leds, NumLeds);
  Wire.begin();
  lightMeter.begin();


Wire.setClock(10000);
  /*```````````````````````ERRORS`````````````````````*/
  fill_solid(leds, NumLeds, CRGB(0, 0, 0));
  FastLED.show();
  digitalWrite(8, HIGH);
  digitalWrite(12, LOW);
  digitalWrite(13, LOW);


  /*`````````````````CUSTOM CHARACTERS````````````````*/
  lcd.createChar(8, SLANTtl);
  lcd.createChar(1, SLANTtr);
  lcd.createChar(2, SLANTbl);
  lcd.createChar(3, SLANTbr);
  lcd.createChar(4, TOP);
  lcd.createChar(5, BOTTOM);
  lcd.createChar(6, HIGHGAP);
  lcd.createChar(7, LOWGAP);
  /*``````````````````````````````````````````````````*/


  pinMode(51, INPUT);  //button1
  pinMode(52, INPUT);  //button2
  pinMode(53, INPUT);  //button3


  pinMode(26, INPUT);
  pinMode(12, OUTPUT);  //heater
  pinMode(13, OUTPUT);  //fan


  pinMode(A4, INPUT);  //SMsensor
  pinMode(8, OUTPUT);  //SMrelay


  pinMode(40, OUTPUT);  //green LED
  pinMode(41, OUTPUT);  //red LED


  /*``````````````````GARDENUINO DISPLAY``````````````````*/
  String GARDENUINO1 = String("                    \x08\x04\x04 \x08\x07\x01 \xFF\x07\x01 \xFF\x04\x01 \xFF\x07\x07 \x08\x01\xFE\xFF \xFF\xFE\xFF \x04\xFF\x04 \x08\x01\xFE\xFF \x08\x04\x01 ");
  String GARDENUINO2 = String("                    \x02\x05\x01 \xFF\xFE\xFF \xFF\xFE\x01 \xFF\x05\x03 \xFF\x06\x06 \xFF\xFE\x02\x03 \x02\x05\x03 \x05\xFF\x05 \xFF\xFE\x02\x03 \x02\x05\x03 ");


  for (int x = 0; x < 62; x++) {
    clearRow(2);
    lcd.print(GARDENUINO2.substring(x, x + 40));
    clearRow(1);
    lcd.print(GARDENUINO1.substring(x, x + 40));
    clearRow(3);


    delay(80);
  }
  lcd.clear();
}






void loop() {
 
  String UNSELECTED1 = String("                    \xFF\xFE\xFF \x08\x01\xFE\xFF \x08\x07\x07 \xFF\x07\x07 \xFF\xFE\xFE \xFF\x07\x07 \x08\x04\x04 \x04\xFF\x04 \xFF\x07\x07 \xFF\x04\x01 ");
  String UNSELECTED2 = String("                    \x02\x05\x03 \xFF\xFE\x02\x03 \x06\x06\x03 \xFF\x06\x06 \xFF\x05\x05 \xFF\x06\x06 \x02\x05\x05 \xFE\xFF\xFE \xFF\x06\x06 \xFF\x05\x03 ");


  digitalWrite(8, HIGH);
  digitalWrite(40, LOW);
  digitalWrite(41, HIGH);


  /*`````````` buttons ``````````*/
  lastnightshadestate = currentnightshadestate;
  lastcruciferstate = currentcruciferstate;
  lastlegumestate = currentlegumestate;
  currentnightshadestate = digitalRead(51);
  currentcruciferstate = digitalRead(52);
  currentlegumestate = digitalRead(53);


  int i = 0;


  lcd.setCursor(0, 0);
  lcd.print("Select a Plant!");
  lcd.setCursor(0, 1);
  lcd.print("> Nightshade");
  lcd.setCursor(0, 2);
  lcd.print("> Cruciferous");
  lcd.setCursor(0, 3);
  lcd.print("> Legume");


  if (lastnightshadestate == HIGH && currentnightshadestate == LOW) {
    delay(100);
    lcd.clear();
    digitalWrite(40, HIGH);
    digitalWrite(41, LOW);
    while (i < 1) {
      lastnightshadestate = currentnightshadestate;
      currentnightshadestate = digitalRead(51);
      if (lastnightshadestate == HIGH && currentnightshadestate == LOW) {
        lcd.clear();
        fill_solid(leds, NumLeds, CRGB(0, 0, 0));
        FastLED.show();
        digitalWrite(40, LOW);
        digitalWrite(41, HIGH);
        digitalWrite(8, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(13, LOW);
        for (int x = 0; x < 62; x++) {
          clearRow(2);
          lcd.print(UNSELECTED2.substring(x, x + 40));
          clearRow(1);
          lcd.print(UNSELECTED1.substring(x, x + 40));
          clearRow(3);


          delay(80);
        }
        break;
      }


      /*``````````````````````````soil moisture control``````````````````````````*/
      SoilMoistureValue = analogRead(A4);  //SM sensor
      SoilMoisturePercent = map(SoilMoistureValue, AirValue, WaterValue, 0, 100);
      if (SoilMoisturePercent <= 70)
        pumpState = On;
      else
        pumpState = Off;
      digitalWrite(SMrelay, pumpState);


/*``````````````````````````temperature control``````````````````````````*/
a:
      float temp_c = dht.readTemperature();
      float temp_f = dht.readTemperature(true);


      if (isnan(temp_c) || isnan(temp_f)) {
        lcd.setCursor(0, 2);
        Serial.println("       ERROR!       ");
        goto a;
      } else {
        if (temp_c > (T1 + 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("HOT   ");
        }
        if (temp_c < (T1 - 0.5)) {
          digitalWrite(12, HIGH);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("COLD  ");
        }
        if (temp_c< (T1 + 0.5) & temp_c >(T1 - 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          lcd.setCursor(14, 2);
          lcd.print("NORMAL");
        }


        /*``````````````````````````light control``````````````````````````*/
        float lux = lightMeter.readLightLevel();
        brightness = map(lux, 0, 54613, 255, 0);
        if (brightness > 255) { brightness = 255; }
        if (brightness < 0) { brightness = 0; }
        if (lux < 35100) {
          CRGB colorA(CRGB(brightness/2, 0, brightness/2));
          all = colorA;
          FastLED.show();
        } else if (lux > 35100) {
          fill_solid(leds, NumLeds, CRGB(0, 0, 0));
          FastLED.show();
        }


        LIGHTstatus("Nightshade");
        SMstatus("Nightshade");
        TEMPstatus("Nightshade");
      }
    }
  }


  else if (lastcruciferstate == HIGH && currentcruciferstate == LOW) {
    delay(100);
    lcd.clear();
    digitalWrite(40, HIGH);
    digitalWrite(41, LOW);
    while (i < 1) {
      lastcruciferstate = currentcruciferstate;
      currentcruciferstate = digitalRead(52);
      if (lastcruciferstate == HIGH && currentcruciferstate == LOW) {
        lcd.clear();
        fill_solid(leds, NumLeds, CRGB(0, 0, 0));
        FastLED.show();
        digitalWrite(40, LOW);
        digitalWrite(41, HIGH);
        digitalWrite(8, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(13, LOW);
        for (int x = 0; x < 62; x++) {
          clearRow(2);
          lcd.print(UNSELECTED2.substring(x, x + 40));
          clearRow(1);
          lcd.print(UNSELECTED1.substring(x, x + 40));
          clearRow(3);


          delay(80);
        }
        break;
      }


      /*``````````````````````````soil moisture control``````````````````````````*/
      SoilMoistureValue = analogRead(A4);  //SM sensor
      SoilMoisturePercent = map(SoilMoistureValue, AirValue, WaterValue, 0, 100);
      //Serial.println(SoilMoistureValue);  //remove
      if (SoilMoisturePercent <= 70)
        pumpState = On;
      else
        pumpState = Off;
      digitalWrite(SMrelay, pumpState);


/*``````````````````````````temperature control``````````````````````````*/
b:
      float temp_c = dht.readTemperature();
      float temp_f = dht.readTemperature(true);


      if (isnan(temp_c) || isnan(temp_f)) {
        lcd.setCursor(0, 2);
        Serial.println("       ERROR!       ");
        goto b;
      } else {
        if (temp_c > (T2 + 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("HOT   ");
        }
        if (temp_c < (T2 - 0.5)) {
          digitalWrite(12, HIGH);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("COLD  ");
        }
        if (temp_c< (T2 + 0.5) & temp_c >(T2 - 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          lcd.setCursor(14, 2);
          lcd.print("NORMAL");
        }


        /*``````````````````````````light control``````````````````````````*/
        float lux = lightMeter.readLightLevel();
        brightness = map(lux, 0, 54613, 255, 0);
        if (brightness < 0) { brightness = 0; }      //rounding errors
        if (brightness > 255) { brightness = 255; }  //rounding errors
        if (lux < 3780) {
          CRGB colorF(CRGB(brightness, 0, 0));
          CRGB colorS(CRGB(brightness/2, brightness/2, 0));
          first = colorF;
          second = colorS;
          FastLED.show();
        } else if (lux > 3780) {
          fill_solid(leds, NumLeds, CRGB(0, 0, 0));
          FastLED.show();
        }


        LIGHTstatus("Cruciferous");
        SMstatus("Cruciferous");
        TEMPstatus("Cruciferous");
      }
    }
  }


  else if (lastlegumestate == HIGH && currentlegumestate == LOW) {
    delay(100);
    lcd.clear();
    digitalWrite(40, HIGH);
    digitalWrite(41, LOW);
    while (i < 1) {
      lastlegumestate = currentlegumestate;
      currentlegumestate = digitalRead(53);
      if (lastlegumestate == HIGH && currentlegumestate == LOW) {
        lcd.clear();
        fill_solid(leds, NumLeds, CRGB(0, 0, 0));
        FastLED.show();
        digitalWrite(40, LOW);
        digitalWrite(41, HIGH);
        digitalWrite(8, HIGH);
        digitalWrite(12, LOW);
        digitalWrite(13, LOW);
        for (int x = 0; x < 62; x++) {
          clearRow(2);
          lcd.print(UNSELECTED2.substring(x, x + 40));
          clearRow(1);
          lcd.print(UNSELECTED1.substring(x, x + 40));
          clearRow(3);


          delay(80);
        }
        break;
      }


      /*``````````````````````````soil moisture control``````````````````````````*/
      SoilMoistureValue = analogRead(A4);  //SM sensor
      SoilMoisturePercent = map(SoilMoistureValue, AirValue, WaterValue, 0, 100);
      //Serial.println(SoilMoistureValue);  //remove
      if (SoilMoisturePercent <= 60)
        pumpState = On;
      else
        pumpState = Off;
      digitalWrite(SMrelay, pumpState);


/*``````````````````````````temperature control``````````````````````````*/
c:
      float temp_c = dht.readTemperature();
      float temp_f = dht.readTemperature(true);


      if (isnan(temp_c) || isnan(temp_f)) {
        lcd.setCursor(0, 2);
        Serial.println("       ERROR!       ");
        goto c;
      } else {
        if (temp_c > (T3 + 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("HOT   ");
        }
        if (temp_c < (T3 - 0.5)) {
          digitalWrite(12, HIGH);
          digitalWrite(13, HIGH);
          lcd.setCursor(14, 2);
          lcd.print("COLD  ");
        }
        if (temp_c< (T3 + 0.5) & temp_c >(T3 - 0.5)) {
          digitalWrite(12, LOW);
          digitalWrite(13, LOW);
          lcd.setCursor(14, 2);
          lcd.print("NORMAL");
        }


        /*``````````````````````````light control``````````````````````````*/
        float lux = lightMeter.readLightLevel();
        brightness = map(lux, 0, 54613, 255, 0);
        if (brightness < 0) { brightness = 0; }      //rounding errors
        if (brightness > 255) { brightness = 255; }  //rounding errors
        if (lux < 35100) {
          CRGB colorF(CRGB(brightness, 0, 0));
          CRGB colorS(CRGB(brightness, 0, 0));
          first = colorF;
          second = colorS;
          FastLED.show();
        } else if (lux > 35100) {
          fill_solid(leds, NumLeds, CRGB(0, 0, 0));
          FastLED.show();
        }


        LIGHTstatus("Legume");
        SMstatus("Legume");
        TEMPstatus("Legume");
      }
    }
  }
  Wire.setClock(10000);
}


void SMstatus(const char *label) {
  //```````````````````Soil Moisture Status`````````````````````
  SoilMoistureValue = analogRead(A4);  //SM sensor
  SoilMoisturePercent = map(SoilMoistureValue, AirValue, WaterValue, 0, 100);


  if (SoilMoisturePercent < 0) { SoilMoisturePercent = 0; }
  if (SoilMoisturePercent > 100) { SoilMoisturePercent = 100; }


  lcd.setCursor(0, 0);
  lcd.print(label);
  lcd.print("         ");
  lcd.setCursor(0, 1);
  lcd.print("Moisture: ");
  //lcd.print(SoilMoistureValue); //comment
  lcd.print(SoilMoisturePercent);
  lcd.print("% ");


  if (On == pumpState)
    lcd.print("On  ");
  else
    lcd.print("Off  ");
  //````````````````````````````````````````````````````````````
}


void TEMPstatus(const char *label) {
  //```````````````````Temperature Status`````````````````````
  float temp_c = dht.readTemperature();
  float temp_f = dht.readTemperature(true);
  lcd.setCursor(0, 2);
  lcd.print("Temp: ");
  lcd.print(temp_c, 1);
  lcd.write(0xdf);
  lcd.print("C   ");
  /*
  lcd.setCursor(7, 2);
  lcd.print(temp_f, 1);
  lcd.write(0xdf);
  lcd.print("F  ");
*/
  //````````````````````````````````````````````````````````````
}


void LIGHTstatus(const char *label) {
  //```````````````````Light Status`````````````````````
  float lux = lightMeter.readLightLevel();


  lcd.setCursor(0, 3);
  lcd.print("Light: ");
  lcd.print(lux);
  lcd.print(" lux   ");
  //````````````````````````````````````````````````````````````
}


void clearRow(int rowNum) {
  lcd.setCursor(0, rowNum);
  lcd.print("                                        ");
  lcd.setCursor(0, rowNum);
}





As @ruilviana said, do you protect your power supply and circuit for the inductive loads you have (pump, fan) and how do you drive all those loads ?

1 Like

can you use a more beginner term im a beginner. thank you

how do you protect inductive loads?

usually with "snubber circuit", typically a diode is wired in parallel with the inductive load (not conducting in normal operation mode). You could also look at " Freewheeling diode" or " flyback diode" (first google hit: What is a Flyback Diode? - ElectronicsHub USA)

1 Like

i have some spare of 1N4007 diode. will that work

also i forgot to state another problem when the heater plate starts heating up when the temp sensor sense low temp the display in the lcd starts breaking the same way how do i fix that one?

thank you

They could - depends on the load ➜ you need to ensure that the diode's voltage and current ratings are appropriate for your loads.

From top of memory I think that the 1N4007 is good for ~1000 volts and a maximum forward current of 1 Amp.


that being said, one way to test your system is to not power the loads and see if you have an issue with the LCD

1 Like

HI, @mirobychee

Can you please post a copy of your circuit, a picture of a hand drawn circuit in jpg, png?
Hand drawn and photographed is perfectly acceptable.
Please include ALL hardware, power supplies, component names and pin labels.

Can you post some images of your project?
So we can see your component layout.

Thanks... Tom.. :smiley: :+1: :coffee: :australia:

1 Like

i did that testing and a few hours ago and the issue seems to be with the fan and heater plate.

when im using everything except those 2 the display is perfectly normal

unfortunately i cant right now but i might be able to make one in a few hours

hello again. my fan and heater is connected to a relay im just wondering, do i put the diode in the adaptor or in the fan or it does it not matter?

also is it fine to use a diode in AC? because my heater plate use an ac supply

Does the relay have an optocoupler?

You would see snubber circuits in AC and it’s likely built in if this is a device you plug to a wall outlet

i solved the problem with the fan currently working thank you so much.

the prob lem i have now is the ac plug on the heater idk if i should put a diode because it goes both ways? can you help me? thank you

the heater is connected directly to the a wall plug ?

yes i inserted a diode on the fan and its working normally and when i put a 1N4007 diode in the AC plug it worked the first time then when u tried again its sometimes working sometimes not i think its because the diode can only support 1A but the AC plug use 6A

edit: i bought some 6A diode and it still have the same problem as the 1A sometimes it would work then sometimes it wouldn't

Where did you put the diode with the AC circuit?

Can you share a schematic?

i havent finished the whole schematic yet but this is the part for the temperature

the big iode is the 6A one