Need help with inserting LCD display into code.

Hey guys! :)

Need help with LCD display.

Arduino: Nano

First:

I've made this one here: https://beginnertopro.in/posts.php?pid=335

  1. I want and get information on LCD display and not LED Diode.

Like: Tank is 25% full Pump is on // or OFF

LCD i use is: https://www.ebay.com/itm/Yellow-LCD-I2C-Modul-LCD1602-16x2-Zeichen-Display-HD44780-Arduino-Raspberry-Pi/232882568447?ssPageName=STRK%3AMEBIDX%3AIT&_trksid=p2057872.m2749.l2649

Code is here:

/* This Code is Written By Sai Tat Sat Mishra
 *  Founder Of http://www.Beginnertopro.in
 *  On Date:31/12/2017
 */

// defines pins numbers
const int trigPin = 8;
const int echoPin = 9;
//height of the water tank in centimeter
const int height_tank=25;

//pin numbers of LED Pannel
const int level1=1;
const int level2=2;
const int level3=3;
const int level4=4;
const int level5=5;
//Pin Number of The Motor Relay
const int motor_relay=7;

// defines variables
int distance;
int duration;

void setup() {
    pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
    pinMode(echoPin, INPUT); // Sets the echoPin as an Input
    pinMode(level1,OUTPUT);//Set the Level1 pin as an OUTPUT
    pinMode(level2,OUTPUT);//Set the Level2 pin as an OUTPUT
    pinMode(level3,OUTPUT);//Set the Level3 pin as an OUTPUT
    pinMode(level4,OUTPUT);//Set the Level4 pin as an OUTPUT
    pinMode(level5,OUTPUT);//Set the Level5 pin as an OUTPUT
    pinMode(motor_relay,OUTPUT);//Set the Motor Relay Pin as an OUTPUT
    Serial.begin(9600); // Starts the serial communication
}
void loop() {
   distance=ping();
   int division_lenght=height_tank/5;
   int level=distance/division_lenght;
   level=5-level;
   Serial.println(level);
   Serial.println(distance);
   if(level==1){
    digitalWrite(level1,HIGH);
    digitalWrite(level2,LOW);
    digitalWrite(level3,LOW);
    digitalWrite(level4,LOW);
    digitalWrite(level5,LOW);
    digitalWrite(motor_relay,HIGH);
    Serial.println("motor On");
   }else if(level==2){
    digitalWrite(level1,HIGH);
    digitalWrite(level2,HIGH);
    digitalWrite(level3,LOW);
    digitalWrite(level4,LOW);
    digitalWrite(level5,LOW);
    digitalWrite(motor_relay,HIGH);
    Serial.println("motor On");
   }else if(level==3){
    digitalWrite(level1,HIGH);
    digitalWrite(level2,HIGH);
    digitalWrite(level3,HIGH);
    digitalWrite(level4,LOW);
    digitalWrite(level5,LOW);
    digitalWrite(motor_relay,HIGH);
    Serial.println("motor On");
   }else if(level==4){
    digitalWrite(level1,HIGH);
    digitalWrite(level2,HIGH);
    digitalWrite(level3,HIGH);
    digitalWrite(level4,HIGH);
    digitalWrite(level5,LOW);
    digitalWrite(motor_relay,HIGH);
    Serial.println("motor On");
   }else if(level==5){
    digitalWrite(level1,HIGH);
    digitalWrite(level2,HIGH);
    digitalWrite(level3,HIGH);
    digitalWrite(level4,HIGH);
    digitalWrite(level5,HIGH);
    digitalWrite(motor_relay,LOW);
    Serial.println("motor OFF");
   }
   delay(1000);
}

int ping(){
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  // Sets the trigPin on HIGH state for 10 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  duration = pulseIn(echoPin, HIGH);
  // Calculating the distance
  distance= duration*0.034/2;
  // Prints the distance on the Serial Monitor
  return distance;
}

OOPS Looks like a normal parallel LCD; have a look at the LCD examples. Note that you might have to move some pins around because they might conflict with the sensor pins. Once you understand the LCD and have it working in an individual sketch, you know what to do.

From the link posted, your display appears to have an I2C interface

Yellow LCD I2C Modul LCD1602 16x2 Zeichen Display HD44780

I would recommend that you use the currently best available library for the i2c lcd modules which is Bill Perry’s hd44780.h. It is available through the library manager.

It will auto configure the i2c address and the pin configuration. There is a comprehensive diagnostic sketch as part of the library if there are any issues. Reference material is here.

https://github.com/duinoWitchery/hd44780

Install the library, using the library manager, and this sketch should work with your device.

#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header for i2c expanders

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

void setup()
{
// initialize LCD with number of columns and rows:
lcd.begin(16,2);

// Print a message to the LCD
lcd.print("Hello, World!");
}

void loop()
{
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
delay(1000);
}

cattledog:
From the link posted, your display appears to have an I2C interface

I would recommend that you use the currently best available library for the i2c lcd modules which is Bill Perry’s hd44780.h. It is available through the library manager. It will auto configure the i2c address and the pin configuration. There is a comprehensive diagnostic sketch as part of the library if there are any issues.

GitHub - duinoWitchery/hd44780: Extensible hd44780 LCD library

Install the library, and this sketch should work with your device.

Code: [Select]
#include <Wire.h>
#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> // include i/o class header for i2c expanders

hd44780_I2Cexp lcd; // declare lcd object: auto locate & config display for hd44780 chip

void setup()
{
// initialize LCD with number of columns and rows:
lcd.begin(16,2);

// Print a message to the LCD
lcd.print(“Hello, World!”);
}

void loop()
{
lcd.setCursor(0, 1);
lcd.print(millis() / 1000);
delay(1000);
}

Thx! :slight_smile:

Going to try it when i get my LCD :slight_smile: