Hello, am trying display the data from a moisture sensor in an LCD as well as viewing it from a blynk platform. when I upload the code the lcd is not displaying. Whereas when I upload a simple sketch it displays on the LCD meaning that the lcd is working.I did the serial debug tests and noticed my timer function is not being called. How can i fix it?
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266_Lib.h>
// Set the LCD address and dimensions (20x4)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Correct I2C address for your module
const int moisturePin = A0;
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL2zgKrEgch"
#define BLYNK_TEMPLATE_NAME "SENSOR PROTOTYPE"
#define BLYNK_AUTH_TOKEN "xwAofwSbst98Ovq5HCojGNiDuAENgBpO"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
char ssid[] = "Laarias";
char pass[] = "laaria9251";
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 38400
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// Read the moisture sensor value and send it to Blynk
void updateMoistureSensor() {
int moistureValue = analogRead(moisturePin);
float moisturePercentage = map(moistureValue, 0, 1023, 100, 0);
// Display moisture level on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(moisturePercentage);
lcd.print("%");
// Display moisture level in the serial monitor
Serial.print("Moisture Level: ");
Serial.print(moisturePercentage);
Serial.println("%");
// Send soil moisture level value to Blynk
Blynk.virtualWrite(V0, moisturePercentage);
}
void setup() {
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
// Debug console
Serial.begin(115200);
pinMode(moisturePin, INPUT);
// Initialize ESP8266 for Blynk
EspSerial.begin(ESP8266_BAUD);
delay(1000);
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
// Setup a timer to call the updateMoistureSensor function every 2 seconds (2000 milliseconds)
timer.setInterval(2000L, updateMoistureSensor);
}
void loop() {
Blynk.run(); // Run the Blynk library
timer.run(); // Run the Blynk timer
}
@LAARIA Please edit your post, using the pencil icon at the bottom, and add triple backticks at the beginning and end of your code so that it displays correctly.
Triple backticks look like this:
```
Copy and paste these if you can’t find the correct symbol on your keyboard.
I would also suggest shortening the title of your topic, and placing the explanation of the issue inside your initial post, along with details of the hardware you are using and how it is connected and powered.
@LAARIA you’ve edited your post, but didn’t use triple backticks at the beginning and end, instead you used some other characters.
This is despite me providing you with triple backticks to copy/paste and saying…
Please edit your post and use the correct characters.
Okay, my guess is that your device is failing to connect to the Blynk server, but we’d need to see your serial output to confirm that.
Copy the text from your serial monitor, and paste it here (once again with triple backticks at the beginning and end).
It would be helpful if you provided…
You obviously aren’t using the latest version of the Blynk library, because that requires these lines of code to be at the very top of your sketch…
So, you should update to the latest version of the Blynk library and move these lines of code.
Assuming that you are using an Arduino Uno, SoftwareSerial on the Uno can’t reliably support emulating a serial port with this baud rate…
I updated the code for you to see the serial debug that i did.Am using an arduino uno with esp-01[Prefered an UNO than esp8266 MCU because i needed more analog pins since i would require to input more sensors later]. I tested the esp-01 and its working fine with the UNO and set the baud rate to 38400.Powering it with 3.3v external supply and the RX, TX are inverted.I also had connected it to a blynk server and i could read values from my sensor. My problem now came when I added the LCD, it is not displaying. Remember the LCD is working since when i test with a simple “Hello World” it works.
UPDATED CODE.
#include <Arduino.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266_Lib.h>
// Set the LCD address and dimensions (20x4)
LiquidCrystal_I2C lcd(0x27, 20, 4); // Correct I2C address for your module
const int moisturePin = A0;
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "TMPL2zgKrEgch"
#define BLYNK_TEMPLATE_NAME "SENSOR PROTOTYPE"
#define BLYNK_AUTH_TOKEN "xwAofwSbst98Ovq5HCojGNiDuAENgBpO"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
char ssid[] = "Laarias";
char pass[] = "laaria9251";
#include <BlynkSimpleShieldEsp8266.h>
#include <SoftwareSerial.h>
SoftwareSerial EspSerial(2, 3); // RX, TX
#define ESP8266_BAUD 38400
ESP8266 wifi(&EspSerial);
BlynkTimer timer;
// Read the moisture sensor value and send it to Blynk
void updateMoistureSensor() {
int moistureValue = analogRead(moisturePin);
float moisturePercentage = map(moistureValue, 0, 1023, 100, 0);
// Debugging message for moisture level calculation
Serial.print("Moisture Value: ");
Serial.print(moistureValue);
Serial.print(", Moisture Percentage: ");
Serial.print(moisturePercentage);
Serial.println("%");
// Display moisture level on the LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Moisture: ");
lcd.print(moisturePercentage);
lcd.print("%");
// Display moisture level in the serial monitor
Serial.print("Moisture Level: ");
Serial.print(moisturePercentage);
Serial.println("%");
// Send soil moisture level value to Blynk
Blynk.virtualWrite(V0, moisturePercentage);
}
void setup() {
// Debug console (initialize Serial first)
Serial.begin(115200);
Serial.println("Serial monitor initialized.");
// Initialize the LCD
lcd.init();
lcd.backlight();
lcd.setCursor(0, 0);
Serial.println("LCD initialized.");
pinMode(moisturePin, INPUT);
Serial.println("MoistrePin set as input.");
// Initialize ESP8266 for Blynk
EspSerial.begin(ESP8266_BAUD);
delay(1000);
Serial.println("Initialize ESP8266 for Blynk.");
Blynk.begin(BLYNK_AUTH_TOKEN, wifi, ssid, pass, "blynk.cloud", 80);
// Setup a timer to call the updateMoistureSensor function every 2 seconds (2000 milliseconds)
timer.setInterval(2000L, updateMoistureSensor);
Serial.println("timer set");
}
void loop() {
Blynk.run(); // Run the Blynk library
timer.run(); // Run the Blynk timer
}
serial monitor display
LCD initialized.
MoistrePin set as input.
Initialize ESP8266 for Blynk.
[2075]
___ __ __
/ _ )/ /_ _____ / /__
/ _ / / // / _ \/ '_/
/____/_/\_, /_//_/_/\_\
/___/ v1.3.2 on Arduino Uno
#StandWithUkraine https://bit.ly/swua
[2589] Connecting to Laarias
[5635] 0018000902
[8716] 192.168.1.2
[8716] Connected to WiFi
[9333] Ready (ping: 12ms).
It’s still not very transparent. Does the above mean that you are powering the LCD from the UNO’s 5v pin?
Does this…
mean that you have a separate 3.3v power supply to power the ESP-01? Of so, what current rating does the PSU have?
Does this PSU share a common ground with the Arduino’s GND?
In that case, I’d strongly suggest you consign your current hardware to the bin and spend $5 on an ESP32 dev board.
Am using an external supply to power up my devices. both 5v and 3.3v. I don’t think the issue is on the power.Also its a school project and an arduino uno was a requirement.
Okay, we’ll I’m not going to keep guessing or asking for details of how you’re powering your devices, I’ll sit back and let you decide if you want to share in an an understandable format or not.
I have changed my ESP-01 baud rate to 9600, also i have decided to power up the sensor,LCD and esp-01 using the arduino uno 5v and 3.3v so i can share the same ground.Will this work?