Hi guys!
I was trying to use a HC-05 Bluetooth module to control a 16x2 LCD with I2C, but when I write and send the text the LCD prints an extrange code like 255255255255.
The code is like this:
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11);
LiquidCrystal_I2C lcd (0x27, 2, 1, 0, 4, 5, 6, 7);
void setup() {
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
lcd.begin(16, 2);
mySerial.begin(9600);
lcd.clear();
}
void loop() {
if (mySerial.available()) //Si el puerto serie (Bluetooth) está disponible
{
lcd.print(mySerial.read());
}
}
Can you guys help me?
Thanks!
The problem is SoftwareSerial read() returns an int rather than a char
because of this when you call print() with the return value of read() like
lcd.print(myserial.read());
The print() code will interpret the value as an integer rather than a character so it prints the numerical value of the character rather than the character itself.
You either call write() or cast the return value.
I would simply cast it.
lcd.print((char)(myserial.read());
--- bill
hey bro! thanks for the answer.
I still have problems, now when I write "hello" the LCD prints 5 big blocks, each one use an space of it.
The only change I made is the char in the lcd.print
Have you tried to adjust the contrast? Too much contrast will result in blocks instead to characters.
Yes, it's perfect
What does that mean? Did adjusting the contrast fix the display? Or the contrast was perfect and no adjustment helped so the blocks are still there?
The contrast is perfect and the blocks still there
Can you post a photo of the display?
LCD prints an extrange code like 255255255255.
lcd.write(255);
will result in a block character. What is incoming from the HC05?
I sended "hello" from the mobile
Have you tried to serial print what comes in instead of sending to the LCD?
Put
Serial.begin(9600);
in setup() and
void loop()
{
if (mySerial.available()) //Si el puerto serie (Bluetooth) está disponible
{
Serial.print(mySerial.read());
}
}
Do you get "hello" in serial monitor?
No, it doesn't print anything
I have connected a HD05 and I2C LCD to my Uno. I use an Android tablet with a Bluetooth terminal to send a message to the Uno. The entire message is read before printing to the LCD. The message must be no longer than 16 characters. A new message will overwrite the old one. Each message must end with a line feed ('\n'). The code was tested and works well. The messages should show on the LCD and in serial monitor. Note serial monitor baud rate is 115200.
I don't have the LCD library that you are using. I use the hd44780 library. It is superior to the other I2C LCD libraries available. Adapt the provided code to use your library or install the hd44780 library and use my program with out any changes. I'll put installation instructions at the bottom of the post.
And the program uses the example code from the serial input basics tutorial.
// HC05 to LCD by c. goulding
// uses Robin2's serial input basics example 2 for serial input
// uses Bill Perry's hd44780 I2C LCD library
//input one line (up to 16 characters via HC05 and print to LCD
#include <Wire.h>
#include <hd44780.h> // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <SoftwareSerial.h>
SoftwareSerial hc05(10, 11);
hd44780_I2Cexp lcd;
const byte numChars = 17; // limit to 16 chars + NULL
char receivedChars[numChars];
boolean newData = false;
// LCD geometry
const int LCD_COLS = 16;
const int LCD_ROWS = 2;
void setup()
{
Serial.begin(115200);
lcd.begin(LCD_COLS, LCD_ROWS);
hc05.begin(9600);
lcd.print("Message");
}
void loop()
{
recvWithEndMarker();
showLCDData();
}
void recvWithEndMarker()
{
static byte ndx = 0;
char endMarker = '\n';
char rc;
while (hc05.available() > 0 && newData == false)
{
rc = hc05.read();
if (rc == '\r')
{
return;
}
if (rc != endMarker)
{
receivedChars[ndx] = rc;
ndx++;
if (ndx >= numChars)
{
ndx = numChars - 1;
}
}
else
{
receivedChars[ndx] = '\0'; // terminate the string
ndx = 0;
newData = true;
}
}
}
void showLCDData()
{
if (newData == true)
{
Serial.println(receivedChars);
lcd.setCursor(0, 1);
lcd.print(" ");
lcd.setCursor(0, 1);
lcd.print(receivedChars);
newData = false;
}
}
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, 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.
From what you are saying, everything now seems to working just fine with the LCD.
i.e. SoftwareSerial read() is returning the value 255 the lcd is printing the character represented by value of 255 which is 0xff
which is a block.
If that is not what you are sending, then the issue is in the serial stuff not the LCD.
Are you sure the baud rates are matched?
--- bill