├── LiquidCrystal_I2C.cpp ├── LiquidCrystal_I2C.h ├── README.md ├── examples └── esp32lcd │ └── esp32lcd.ino └── keywords.txt /LiquidCrystal_I2C.cpp: -------------------------------------------------------------------------------- 1 | #include "LiquidCrystal_I2C.h" 2 | #include 3 | #include 4 | #include 5 | 6 | // When the display powers up, it is configured as follows: 7 | // 8 | // 1. Display clear 9 | // 2. Function set: 10 | // DL = 1; 8-bit interface data 11 | // N = 0; 1-line display 12 | // F = 0; 5x8 dot character font 13 | // 3. Display on/off control: 14 | // D = 0; Display off 15 | // C = 0; Cursor off 16 | // B = 0; Blinking off 17 | // 4. Entry mode set: 18 | // I/D = 1; Increment by 1 19 | // S = 0; No shift 20 | // 21 | // Note, however, that resetting the Arduino doesn't reset the LCD, so we 22 | // can't assume that its in that state when a sketch starts (and the 23 | // LiquidCrystal constructor is called). 24 | 25 | LiquidCrystal_I2C::LiquidCrystal_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t charsize) 26 | { 27 | _addr = lcd_addr; 28 | _cols = lcd_cols; 29 | _rows = lcd_rows; 30 | _charsize = charsize; 31 | _backlightval = LCD_NOBACKLIGHT; 32 | } 33 | 34 | void LiquidCrystal_I2C::begin(int8_t sda, int8_t scl) { 35 | if(sda == -1 || scl == -1){ 36 | Wire.begin(); 37 | } else { 38 | #ifdef ESP32 39 | Wire.begin(sda, scl); 40 | #endif 41 | } 42 | for(int8_t i=0;i<2; i++){ 43 | _displayfunction = LCD_4BITMODE | LCD_1LINE | LCD_5x8DOTS; 44 | 45 | if (_rows > 1) { 46 | _displayfunction |= LCD_2LINE; 47 | } 48 | 49 | // for some 1 line displays you can select a 10 pixel high font 50 | if ((_charsize != 0) && (_rows == 1)) { 51 | _displayfunction |= LCD_5x10DOTS; 52 | } 53 | 54 | // SEE PAGE 45/46 FOR INITIALIZATION SPECIFICATION! 55 | // according to datasheet, we need at least 40ms after power rises above 2.7V 56 | // before sending commands. Arduino can turn on way befer 4.5V so we'll wait 50 57 | delay(50); 58 | 59 | // Now we pull both RS and R/W low to begin commands 60 | expanderWrite(_backlightval); // reset expanderand turn backlight off (Bit 8 =1) 61 | delay(1000); 62 | 63 | //put the LCD into 4 bit mode 64 | // this is according to the hitachi HD44780 datasheet 65 | // figure 24, pg 46 66 | 67 | // we start in 8bit mode, try to set 4 bit mode 68 | write4bits(0x03 << 4); 69 | delayMicroseconds(4500); // wait min 4.1ms 70 | 71 | // second try 72 | write4bits(0x03 << 4); 73 | delayMicroseconds(4500); // wait min 4.1ms 74 | 75 | // third go! 76 | write4bits(0x03 << 4); 77 | delayMicroseconds(150); 78 | 79 | // finally, set to 4-bit interface 80 | write4bits(0x02 << 4); 81 | 82 | // set # lines, font size, etc. 83 | command(LCD_FUNCTIONSET | _displayfunction); 84 | 85 | // turn the display on with no cursor or blinking default 86 | _displaycontrol = LCD_DISPLAYON | LCD_CURSOROFF | LCD_BLINKOFF; 87 | display(); 88 | 89 | // clear it off 90 | clear(); 91 | 92 | // Initialize to default text direction (for roman languages) 93 | _displaymode = LCD_ENTRYLEFT | LCD_ENTRYSHIFTDECREMENT; 94 | 95 | // set the entry mode 96 | command(LCD_ENTRYMODESET | _displaymode); 97 | 98 | home(); 99 | } 100 | } 101 | 102 | /********** high level commands, for the user! */ 103 | void LiquidCrystal_I2C::clear(){ 104 | command(LCD_CLEARDISPLAY);// clear display, set cursor position to zero 105 | delayMicroseconds(2000); // this command takes a long time! 106 | } 107 | 108 | void LiquidCrystal_I2C::home(){ 109 | command(LCD_RETURNHOME); // set cursor position to zero 110 | delayMicroseconds(2000); // this command takes a long time! 111 | } 112 | 113 | void LiquidCrystal_I2C::setCursor(uint8_t col, uint8_t row){ 114 | int row_offsets[] = { 0x00, 0x40, 0x14, 0x54 }; 115 | if (row > _rows) { 116 | row = _rows-1; // we count rows starting w/0 117 | } 118 | command(LCD_SETDDRAMADDR | (col + row_offsets[row])); 119 | } 120 | 121 | // Turn the display on/off (quickly) 122 | void LiquidCrystal_I2C::noDisplay() { 123 | _displaycontrol &= ~LCD_DISPLAYON; 124 | command(LCD_DISPLAYCONTROL | _displaycontrol); 125 | } 126 | void LiquidCrystal_I2C::display() { 127 | _displaycontrol |= LCD_DISPLAYON; 128 | command(LCD_DISPLAYCONTROL | _displaycontrol); 129 | } 130 | 131 | // Turns the underline cursor on/off 132 | void LiquidCrystal_I2C::noCursor() { 133 | _displaycontrol &= ~LCD_CURSORON; 134 | command(LCD_DISPLAYCONTROL | _displaycontrol); 135 | } 136 | void LiquidCrystal_I2C::cursor() { 137 | _displaycontrol |= LCD_CURSORON; 138 | command(LCD_DISPLAYCONTROL | _displaycontrol); 139 | } 140 | 141 | // Turn on and off the blinking cursor 142 | void LiquidCrystal_I2C::noBlink() { 143 | _displaycontrol &= ~LCD_BLINKON; 144 | command(LCD_DISPLAYCONTROL | _displaycontrol); 145 | } 146 | void LiquidCrystal_I2C::blink() { 147 | _displaycontrol |= LCD_BLINKON; 148 | command(LCD_DISPLAYCONTROL | _displaycontrol); 149 | } 150 | 151 | // These commands scroll the display without changing the RAM 152 | void LiquidCrystal_I2C::scrollDisplayLeft(void) { 153 | command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVELEFT); 154 | } 155 | void LiquidCrystal_I2C::scrollDisplayRight(void) { 156 | command(LCD_CURSORSHIFT | LCD_DISPLAYMOVE | LCD_MOVERIGHT); 157 | } 158 | 159 | // This is for text that flows Left to Right 160 | void LiquidCrystal_I2C::leftToRight(void) { 161 | _displaymode |= LCD_ENTRYLEFT; 162 | command(LCD_ENTRYMODESET | _displaymode); 163 | } 164 | 165 | // This is for text that flows Right to Left 166 | void LiquidCrystal_I2C::rightToLeft(void) { 167 | _displaymode &= ~LCD_ENTRYLEFT; 168 | command(LCD_ENTRYMODESET | _displaymode); 169 | } 170 | 171 | // This will 'right justify' text from the cursor 172 | void LiquidCrystal_I2C::autoscroll(void) { 173 | _displaymode |= LCD_ENTRYSHIFTINCREMENT; 174 | command(LCD_ENTRYMODESET | _displaymode); 175 | } 176 | 177 | // This will 'left justify' text from the cursor 178 | void LiquidCrystal_I2C::noAutoscroll(void) { 179 | _displaymode &= ~LCD_ENTRYSHIFTINCREMENT; 180 | command(LCD_ENTRYMODESET | _displaymode); 181 | } 182 | 183 | // Allows us to fill the first 8 CGRAM locations 184 | // with custom characters 185 | void LiquidCrystal_I2C::createChar(uint8_t location, uint8_t charmap[]) { 186 | location &= 0x7; // we only have 8 locations 0-7 187 | command(LCD_SETCGRAMADDR | (location << 3)); 188 | for (int i=0; i<8; i++) { 189 | write(charmap[i]); 190 | } 191 | } 192 | 193 | // Turn the (optional) backlight off/on 194 | void LiquidCrystal_I2C::noBacklight(void) { 195 | _backlightval=LCD_NOBACKLIGHT; 196 | expanderWrite(0); 197 | } 198 | 199 | void LiquidCrystal_I2C::backlight(void) { 200 | _backlightval=LCD_BACKLIGHT; 201 | expanderWrite(0); 202 | } 203 | bool LiquidCrystal_I2C::getBacklight() { 204 | return _backlightval == LCD_BACKLIGHT; 205 | } 206 | 207 | 208 | /*********** mid level commands, for sending data/cmds */ 209 | 210 | inline void LiquidCrystal_I2C::command(uint8_t value) { 211 | send(value, 0); 212 | } 213 | 214 | inline size_t LiquidCrystal_I2C::write(uint8_t value) { 215 | send(value, Rs); 216 | return 1; 217 | } 218 | 219 | 220 | /************ low level data pushing commands **********/ 221 | 222 | // write either command or data 223 | void LiquidCrystal_I2C::send(uint8_t value, uint8_t mode) { 224 | uint8_t highnib=value&0xf0; 225 | uint8_t lownib=(value<<4)&0xf0; 226 | write4bits((highnib)|mode); 227 | write4bits((lownib)|mode); 228 | } 229 | 230 | void LiquidCrystal_I2C::write4bits(uint8_t value) { 231 | expanderWrite(value); 232 | pulseEnable(value); 233 | } 234 | 235 | void LiquidCrystal_I2C::expanderWrite(uint8_t _data){ 236 | Wire.beginTransmission(_addr); 237 | Wire.write((int)(_data) | _backlightval); 238 | Wire.endTransmission(); 239 | } 240 | 241 | void LiquidCrystal_I2C::pulseEnable(uint8_t _data){ 242 | expanderWrite(_data | En); // En high 243 | delayMicroseconds(1); // enable pulse must be >450ns 244 | 245 | expanderWrite(_data & ~En); // En low 246 | delayMicroseconds(50); // commands need > 37us to settle 247 | } 248 | 249 | void LiquidCrystal_I2C::load_custom_character(uint8_t char_num, uint8_t *rows){ 250 | createChar(char_num, rows); 251 | } 252 | 253 | void LiquidCrystal_I2C::setBacklight(uint8_t new_val){ 254 | if (new_val) { 255 | backlight(); // turn backlight on 256 | } else { 257 | noBacklight(); // turn backlight off 258 | } 259 | } 260 | 261 | void LiquidCrystal_I2C::printstr(const char c[]){ 262 | //This function is not identical to the function used for "real" I2C displays 263 | //it's here so the user sketch doesn't have to be changed 264 | print(c); 265 | } 266 | -------------------------------------------------------------------------------- /LiquidCrystal_I2C.h: -------------------------------------------------------------------------------- 1 | #ifndef FDB_LIQUID_CRYSTAL_I2C_H 2 | #define FDB_LIQUID_CRYSTAL_I2C_H 3 | 4 | #include 5 | #include 6 | 7 | // commands 8 | #define LCD_CLEARDISPLAY 0x01 9 | #define LCD_RETURNHOME 0x02 10 | #define LCD_ENTRYMODESET 0x04 11 | #define LCD_DISPLAYCONTROL 0x08 12 | #define LCD_CURSORSHIFT 0x10 13 | #define LCD_FUNCTIONSET 0x20 14 | #define LCD_SETCGRAMADDR 0x40 15 | #define LCD_SETDDRAMADDR 0x80 16 | 17 | // flags for display entry mode 18 | #define LCD_ENTRYRIGHT 0x00 19 | #define LCD_ENTRYLEFT 0x02 20 | #define LCD_ENTRYSHIFTINCREMENT 0x01 21 | #define LCD_ENTRYSHIFTDECREMENT 0x00 22 | 23 | // flags for display on/off control 24 | #define LCD_DISPLAYON 0x04 25 | #define LCD_DISPLAYOFF 0x00 26 | #define LCD_CURSORON 0x02 27 | #define LCD_CURSOROFF 0x00 28 | #define LCD_BLINKON 0x01 29 | #define LCD_BLINKOFF 0x00 30 | 31 | // flags for display/cursor shift 32 | #define LCD_DISPLAYMOVE 0x08 33 | #define LCD_CURSORMOVE 0x00 34 | #define LCD_MOVERIGHT 0x04 35 | #define LCD_MOVELEFT 0x00 36 | 37 | // flags for function set 38 | #define LCD_8BITMODE 0x10 39 | #define LCD_4BITMODE 0x00 40 | #define LCD_2LINE 0x08 41 | #define LCD_1LINE 0x00 42 | #define LCD_5x10DOTS 0x04 43 | #define LCD_5x8DOTS 0x00 44 | 45 | // flags for backlight control 46 | #define LCD_BACKLIGHT 0x08 47 | #define LCD_NOBACKLIGHT 0x00 48 | 49 | #define En B00000100 // Enable bit 50 | #define Rw B00000010 // Read/Write bit 51 | #define Rs B00000001 // Register select bit 52 | 53 | /** 54 | * This is the driver for the Liquid Crystal LCD displays that use the I2C bus. 55 | * 56 | * After creating an instance of this class, first call begin() before anything else. 57 | * The backlight is on by default, since that is the most likely operating mode in 58 | * most cases. 59 | */ 60 | class LiquidCrystal_I2C : public Print { 61 | public: 62 | /** 63 | * Constructor 64 | * 65 | * @param lcd_addr I2C slave address of the LCD display. Most likely printed on the 66 | * LCD circuit board, or look in the supplied LCD documentation. 67 | * @param lcd_cols Number of columns your LCD display has. 68 | * @param lcd_rows Number of rows your LCD display has. 69 | * @param charsize The size in dots that the display has, use LCD_5x10DOTS or LCD_5x8DOTS. 70 | */ 71 | LiquidCrystal_I2C(uint8_t lcd_addr, uint8_t lcd_cols, uint8_t lcd_rows, uint8_t charsize = LCD_5x8DOTS); 72 | 73 | /** 74 | * Set the LCD display in the correct begin state, must be called before anything else is done. 75 | */ 76 | void begin(int8_t sda=-1, int8_t scl=-1); 77 | 78 | /** 79 | * Remove all the characters currently shown. Next print/write operation will start 80 | * from the first position on LCD display. 81 | */ 82 | void clear(); 83 | 84 | /** 85 | * Next print/write operation will will start from the first position on the LCD display. 86 | */ 87 | void home(); 88 | 89 | /** 90 | * Do not show any characters on the LCD display. Backlight state will remain unchanged. 91 | * Also all characters written on the display will return, when the display in enabled again. 92 | */ 93 | void noDisplay(); 94 | 95 | /** 96 | * Show the characters on the LCD display, this is the normal behaviour. This method should 97 | * only be used after noDisplay() has been used. 98 | */ 99 | void display(); 100 | 101 | /** 102 | * Do not blink the cursor indicator. 103 | */ 104 | void noBlink(); 105 | 106 | /** 107 | * Start blinking the cursor indicator. 108 | */ 109 | void blink(); 110 | 111 | /** 112 | * Do not show a cursor indicator. 113 | */ 114 | void noCursor(); 115 | 116 | /** 117 | * Show a cursor indicator, cursor can blink on not blink. Use the 118 | * methods blink() and noBlink() for changing cursor blink. 119 | */ 120 | void cursor(); 121 | 122 | void scrollDisplayLeft(); 123 | void scrollDisplayRight(); 124 | void printLeft(); 125 | void printRight(); 126 | void leftToRight(); 127 | void rightToLeft(); 128 | void shiftIncrement(); 129 | void shiftDecrement(); 130 | void noBacklight(); 131 | void backlight(); 132 | bool getBacklight(); 133 | void autoscroll(); 134 | void noAutoscroll(); 135 | void createChar(uint8_t, uint8_t[]); 136 | void setCursor(uint8_t, uint8_t); 137 | virtual size_t write(uint8_t); 138 | void command(uint8_t); 139 | 140 | inline void blink_on() { blink(); } 141 | inline void blink_off() { noBlink(); } 142 | inline void cursor_on() { cursor(); } 143 | inline void cursor_off() { noCursor(); } 144 | 145 | // Compatibility API function aliases 146 | void setBacklight(uint8_t new_val); // alias for backlight() and nobacklight() 147 | void load_custom_character(uint8_t char_num, uint8_t *rows); // alias for createChar() 148 | void printstr(const char[]); 149 | 150 | private: 151 | void send(uint8_t, uint8_t); 152 | void write4bits(uint8_t); 153 | void expanderWrite(uint8_t); 154 | void pulseEnable(uint8_t); 155 | uint8_t _addr; 156 | uint8_t _displayfunction; 157 | uint8_t _displaycontrol; 158 | uint8_t _displaymode; 159 | uint8_t _cols; 160 | uint8_t _rows; 161 | uint8_t _charsize; 162 | uint8_t _backlightval; 163 | }; 164 | 165 | #endif // FDB_LIQUID_CRYSTAL_I2C_H 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | visit IOTSHARING.COM for more 2 | the original version is for Arduino, I modified a little to adapt with esp32. 3 | 4 | Demo 4: How to use Arduino ESP32 to display information on I2C LCD 5 | http://www.iotsharing.com/2017/05/how-to-use-arduino-esp32-to-display-i2c-lcd.html 6 | 7 | # Installation # 8 | Create a new folder called "LiquidCrystal_I2C" under the folder named "libraries" in your Arduino sketchbook folder. 9 | Create the folder "libraries" in case it does not exist yet. Place all the files in the "LiquidCrystal_I2C" folder. 10 | 11 | # Usage # 12 | To use the library in your own sketch, select it from *Sketch > Import Library*. 13 | 14 | ------------------------------------------------------------------------------------------------------------------- 15 | This library is based on work done by DFROBOT (www.dfrobot.com). 16 | -------------------------------------------------------------------------------- /examples/esp32lcd/esp32lcd.ino: -------------------------------------------------------------------------------- 1 | //YWROBOT 2 | //Compatible with the Arduino IDE 1.0 3 | //Library version:1.1 4 | #include 5 | #include 6 | 7 | LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display 8 | 9 | void setup() 10 | { 11 | lcd.begin(12, 14); // initialize the lcd 12 | // Print a message to the LCD. 13 | lcd.backlight(); 14 | lcd.setCursor(0,0); 15 | lcd.print("Hello, world!"); 16 | lcd.setCursor(0,1); 17 | lcd.print("by EasyIoT"); 18 | } 19 | 20 | 21 | void loop() 22 | { 23 | } 24 | -------------------------------------------------------------------------------- /keywords.txt: -------------------------------------------------------------------------------- 1 | ########################################### 2 | # Syntax Coloring Map For LiquidCrystal_I2C 3 | ########################################### 4 | 5 | ########################################### 6 | # Datatypes (KEYWORD1) 7 | ########################################### 8 | 9 | LiquidCrystal_I2C KEYWORD1 10 | 11 | ########################################### 12 | # Methods and Functions (KEYWORD2) 13 | ########################################### 14 | init KEYWORD2 15 | begin KEYWORD2 16 | clear KEYWORD2 17 | home KEYWORD2 18 | noDisplay KEYWORD2 19 | display KEYWORD2 20 | noBlink KEYWORD2 21 | blink KEYWORD2 22 | noCursor KEYWORD2 23 | cursor KEYWORD2 24 | scrollDisplayLeft KEYWORD2 25 | scrollDisplayRight KEYWORD2 26 | leftToRight KEYWORD2 27 | rightToLeft KEYWORD2 28 | shiftIncrement KEYWORD2 29 | shiftDecrement KEYWORD2 30 | noBacklight KEYWORD2 31 | backlight KEYWORD2 32 | autoscroll KEYWORD2 33 | noAutoscroll KEYWORD2 34 | createChar KEYWORD2 35 | setCursor KEYWORD2 36 | print KEYWORD2 37 | blink_on KEYWORD2 38 | blink_off KEYWORD2 39 | cursor_on KEYWORD2 40 | cursor_off KEYWORD2 41 | setBacklight KEYWORD2 42 | load_custom_character KEYWORD2 43 | printstr KEYWORD2 44 | ########################################### 45 | # Constants (LITERAL1) 46 | ########################################### 47 | --------------------------------------------------------------------------------