I use Arduino coding for a temperature sensor and Arduino MEGA 2560, and I2C 20x4 LCD Display. I am trying to make a temperature sensor, and though it is reading the data in the Serial Monitor, it is not showing any data in the display. Can anyone please help?
Thank you
#include <hd44780.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
#define RELAY1 4 // Relay heating
#define RELAY2 6 // Relay cooling
int red = 8; // red LED heating
int blue = 2; // blue LED cooling
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
Serial.begin(9600);// initialize serial monitor at a baud rate of 9600
sensors.begin(); //Initialize the sensor
lcd.backlight(); //Turns backlight on
//lcd.begin();
lcd.clear();
pinMode(red, OUTPUT);//Setting the pin 8 as an output
pinMode(blue, OUTPUT);// Setting the pin 2 as an output
pinMode(RELAY1, OUTPUT);// setting the pin 4 as an output
pinMode(RELAY2, OUTPUT);// Setting the pin 6 as an output
}
void loop()
{
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print(" Temperature Control: ");
lcd.setCursor(1, 2);
lcd.display();
Serial.println(sensors.getTempCByIndex(0));
lcd.print(temperature);
if (temperature < 25)
{
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(RELAY1, 0);
digitalWrite(RELAY2, 1);
lcd.setCursor(2, 2);
lcd.print("temperature");
Serial.print("Well we will need heating \n");
delay(1000);
}
else if (temperature > 25)
{
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 0);
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
lcd.setCursor(3, 3);
lcd.print(temperature);
Serial.print("Well! We will need cooling");
delay(1000);
}
}
For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.
To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.
The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.
In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.
The I2C pins on a Mega are pins 20 (SDA) and 21 (SCL) not A4 and A5.
If the above does not help, post photos and a schematic of your wiring.
Does the backlight light?
Are there blocks showing in the display when the contrast is adjusted?
the display problem is solved, this means that the LCD displays now :
It is showing :
LCD: 0 (0X27)
00: 07: 43
The temperature sensor is also working, but LCD is not displaying the temperature values,
I have verified my code and currently, I am unsure what might be the issue, Can anyone suggest anything.
Thank you in Advance
Code is given below :
#include <hd44780.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 7
#define RELAY1 4 // Relay heating
#define RELAY2 6 // Relay cooling
int red = 8; // red LED heating
int blue = 2; // blue LED cooling
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display
void setup()
{
Serial.begin(9600);// initialize serial monitor at a baud rate of 9600
sensors.begin(); //Initialize the sensor
lcd.backlight();
lcd.clear();
lcd.setCursor(0, 0);
//lcd.clear();
pinMode(red, OUTPUT);//Setting the pin 8 as an output
pinMode(blue, OUTPUT);// Setting the pin 2 as an output
pinMode(RELAY1, OUTPUT);// setting the pin 4 as an output
pinMode(RELAY2, OUTPUT);// Setting the pin 6 as an output
}
void loop()
{
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print(" Temperature Control: ");
lcd.setCursor(1, 2);
lcd.display();
Serial.println(sensors.getTempCByIndex(0));
lcd.print(temperature);
if (temperature < 25)
{
digitalWrite(red, HIGH);
digitalWrite(blue, LOW);
digitalWrite(RELAY1, 0);
digitalWrite(RELAY2, 1);
lcd.setCursor(2, 2);
lcd.print("temperature");
lcd.print("Well we will need heating \n");
delay(1000);
}
else if (temperature > 25)
{
digitalWrite(RELAY1, 1);
digitalWrite(RELAY2, 0);
digitalWrite(red, LOW);
digitalWrite(blue, HIGH);
lcd.setCursor(3, 3);
lcd.print(temperature);
lcd.print("Well! We will need cooling");
delay(1000);
}
}