Arduino LCD I2C simple use and direct write from serial monitor
Hello there, welcome to SurtrTech, this is a quick tutorial on how to use the LCD i²c version, this display that I use in the majority of my projects, it’s easy, reliable and standard for most applications.
I use the i²c version because it has only 4 pins unlike the standard version that uses a lot of pins and won’t be practical for multi module projects.
Wiring
Libraries:
Here you can download the New_Liquid_Crystal library:
Codes:
In the code you’ll notice a line, and this line is very important:
1 |
#define I2C_ADDR 0x27 |
0x27 is the address of my I2c device, for these displays, there are usually two 0x27 or 0x3F, in case you’re unsure about the correct one I recommend you to download and run the I2c_scanner code from Arduino playground Check here, Don’t forget to run the code while your device is plugged in, then modify the address accordingly.
First code to test with:
This code displays a simple programmed text on the display.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
//16*2 LCD Display Code //SurtrTech channel //Code for displaying a message on the screen for 2s then erase the screen for 2s #include <Wire.h> //Libraries needed #include <LCD.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //I2C address, you should use the code to scan the address first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); } void loop() { lcd.setCursor(0,0); //Setting the position where you want to start writing the message (0,0) is top left lcd.print("SurtrTech "); //Message to display lcd.setCursor(10,1); //Start writing on the position (10,1) lcd.print("Hello"); delay(2000); //Display time lcd.clear(); //Clearing the screen delay(2000); //Blank screen time } |
Seconde code to write something on the serial monitor and see it on the screen:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
//16*2 LCD Display from serial monitor //Modified from "YourDuino.com" //SurtrTech channel //Code for displaying a message from the serial monitor #include <Wire.h> //Libraries needed #include <LCD.h> #include <LiquidCrystal_I2C.h> #define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here #define BACKLIGHT_PIN 3 // Declaring LCD Pins #define En_pin 2 #define Rw_pin 1 #define Rs_pin 0 #define D4_pin 4 #define D5_pin 5 #define D6_pin 6 #define D7_pin 7 LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); void setup() { Serial.begin(9600); //Serial baud rate lcd.begin (16,2); lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); lcd.setBacklight(HIGH); //Lighting backlight lcd.home (); lcd.print("Hello"); delay(2000); lcd.clear(); } void loop() { // when characters arrive over the serial port... if (Serial.available()) { // wait a bit for the entire message to arrive delay(100); // clear the screen lcd.clear(); // read all the available characters while (Serial.available() > 0) { // display each character to the LCD lcd.write(Serial.read()); } } } |
Categories
Yassine View All
Automation and Electrical Engineer, Electronics amateur trying to share my little projects.
can you write code please for sending lm35 data from arduino to raspberry pi via hc-05 bluetooth
Hello, unfortunately I don’t have it for the moment, maybe in the future.