BLYNK
HOME       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Blynk WIth I2c 1602LCD Display and Blynk App

I have create a project that works with LCD display 1602 via i2c and monitor with Blynk Apps Also, so we have two display run at same time on blynk.

#define BLYNK_PRINT Serial    
#include SPI.h>
#include Ethernet.h>
#include BlynkSimpleEthernet.h>
#include SimpleTimer.h>
#include Wire.h> 
#include LiquidCrystal_I2C.h>
#include DHT.h>

char auth[] = "";

#define DHTPIN 2          
LiquidCrystal_I2C lcd(0x27,16,2);

#define DHTTYPE DHT11     
DHT dht(DHTPIN, DHTTYPE);
SimpleTimer timer;

void sendSensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature(); // or dht.readTemperature(true) for Fahrenheit

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Blynk.virtualWrite(V5, h);
  Blynk.virtualWrite(V6, t);
}

void setup()
{  
  lcd.init();
  lcd.backlight();
  dht.begin();
  lcd.setCursor(0, 0);
  lcd.print("Temp:");
  lcd.setCursor(0, 1);
  lcd.print("Humidity:");
  Serial.begin(9600);
  Blynk.begin(auth);
  
  timer.setInterval(1000L, sendSensor);
}

  void loop() {

  delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
{
  Blynk.run(); // Initiates Blynk
  timer.run(); // Initiates SimpleTimer
}
}
4 Likes

You’re a life saver! I was making something simmilar and i couldnt figure it out, thanks!

I want to know if the lcd is updating even if the system is not connected to BLYNK.
Please let me know about this.
thanks

Blynk.begin is a blocking function. The code execution wont progress beyond this point if a connection to either the Wi-Fi or Blynk isn’t possible.

Changing the code to use Blynk.config and Blynk.connect would overcome this issue.

Pete.

Why the delay(500) in the loop? Everytime the loops goes round it will stop for half a second?

1 Like

Not a good idea !!
Move all into void sendSensor() and delete delay(500);

 void loop() {

  delay(500);
 
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  if (isnan(h) || isnan(t)) {
    lcd.print("ERROR");
    return;
  }
  lcd.setCursor(5,0);
  lcd.print(t);
  lcd.setCursor(9,1);
  lcd.print(h);
{

I have used the following code and it works.

/*
 * Program: Water_Palnt_ESP_R4.ino
 * Plant watering system based on Espresso Lite V2.0 and
 * Blynk app.
 * Auto Mode: ------------------------------------
 * V1 - Slider to set the moisture threshhold
 * V5 - Moisture % derived from analog data A0
 * Soil moisture sensor detects water in the soil and
 * triggers a pump when dry.
 * 
 * Manual Mode:-----------------------------------
 * V5 - Moisture % derived from analog data A0
 * V3 - Pump ON/OFF switch
 * V2 - Auto/Manual select switch
 * Two buttons are defined in Blynk to control
 * manual pump operation.
 * 
 */
 
#define BLYNK_PRINT Serial      // Blynk debug enabled
#include <Wire.h>               // I2C library
#include <LiquidCrystal_I2C.h>  // I2C LCD library
#include <ESP8266WiFi.h>        // ESP8266 library for Espresso
#include <BlynkSimpleEsp8266.h> // Blynk library

#define relayPin 0              // Pump relay output

// Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxx";

// WiFi credentials....
// Set password to "" for open networks.
char ssid[] = "xxxxx";
char pass[] = "xxxxx";

int sensorData;            // Moisture sensor ADC value
int moistPercent;          // Moisture % reading
int setValue;              // Moisture % set point
int modSelect = 0;         // Auto/Manual select, Auto
int pumpStat;              // Pump ON/OFF status

BlynkTimer timer;                 // Blynk Timer object
LiquidCrystal_I2C lcd(0x27,20,4); // LCD object

// In the app, Widget's reading frequency should be set to PUSH. 
// Here data is sent to Blynk App every second
void myTimerEvent(){
  sensorData = analogRead(A0);       // Read moisture data
  moistPercent = map(sensorData,450,1024,100,0); // Map min/max values to 100-0%(inverted)
  if (modSelect == 0){              // Auto mode selected
    if (moistPercent < setValue) {  // If Moist % below set point
      lcd.setCursor(8,2);           // Display data on LCD
      lcd.print(moistPercent);     
      lcd.print("  ");
      lcd.setCursor(8,3);
      lcd.print("ON ");
      digitalWrite(relayPin, HIGH); // Turn ON pump
      Serial.println(sensorData);
    } else {                        // If Moist % above set point
      lcd.setCursor(8,2);           // Display data on LCD
      lcd.print(moistPercent);
      lcd.print("  ");
      lcd.setCursor(8,3);
      lcd.print("OFF");
      digitalWrite(relayPin, LOW); // Turn OFF pump
      Serial.println(sensorData); 
    }
  } else {                         // Man mode selected    
    if (pumpStat == 1){            // Pump ON selected
      digitalWrite(relayPin, HIGH);
    }else{                         // Pump OFF selected
      digitalWrite(relayPin, LOW);  
    }
  }
  
  Blynk.virtualWrite(V5, moistPercent);
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
}
// Every time we connect to the cloud...

BLYNK_CONNECTED()
{
  // Request the latest state from the server
  Blynk.syncVirtual(V1);
  //Blynk.syncVirtual(V2);
}
// Set the moisture % threshhold
BLYNK_WRITE(V1) {
  setValue = param.asInt();
  lcd.setCursor(8,1);
  lcd.print(setValue);
  lcd.print("  ");
}

// Pump ON/OFF select
BLYNK_WRITE(V3){
  pumpStat = param.asInt();
  if(pumpStat == 0){
    lcd.setCursor(8,3);
    lcd.print("OFF"); 
  }else{
    lcd.setCursor(8,3);
    lcd.print("ON ");       
  }
}

// Auto/Manual Mode select
BLYNK_WRITE(V2) {
  modSelect = param.asInt();
  if(modSelect == 0){
    lcd.setCursor(8,0);
    lcd.print("Auto"); 
  }else{
    lcd.setCursor(8,0);
    lcd.print("Man ");       
  }
}

void setup(){
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  pinMode(relayPin, OUTPUT);
  digitalWrite(relayPin, LOW);

  lcd.init();
  lcd.backlight();
  lcd.print("Plant Water Sys R4"); // Prog version
  delay(2000);                     // Wait 2 sec
  lcd.clear();                     // Clear display
  
  lcd.print("Mode  : ");           // Mode disp line 1
  lcd.setCursor(8,0);              // Default Auto mode
  lcd.print("Auto"); 
  lcd.setCursor(0,1);
  lcd.print("Set % : ");           // Set Point disp line 2
  lcd.setCursor(0,2);    
  lcd.print("Moist%: ");           // Moist % disp line 3
  lcd.setCursor(0,3);
  lcd.print("Pump  : ");           // Pump disp line 4



  // Setup timer function to be called every second
  timer.setInterval(1000L, myTimerEvent);
}

void loop(){
  Blynk.run(); // Update Blynk
  timer.run(); // Update BlynkTimer
}
1 Like