📜 ⬆️ ⬇️

Reduce the number of wires in the Arduino - I2C LCD screen and RTC clock on two wires

I recently met Arduino, so many of my examples will seem simple.

However, for newcomers like me, this information will be useful and will save a lot of time.

I2C is a standard for communicating devices over 2m wires, while the number of devices that hang in parallel on these wires can be very large. Each device has its own address at which the device is accessed. Addressing can be changed if there are jumpers on the device that can be used to set an additional offset relative to the base address hard-coded in the device.
')
This is in a nutshell.

I started all with the fact that I bought a 16x2 character LCD display on Ibeya. Connecting the standard - I realized that this is not what is needed. The mass of wires - takes a bunch of legs, chaos and disorder.

image

Googled, I realized that there are displays with interfaces that simplify the connection. Googled yet, found an adapter on I2C for my LCD. Month of waiting, hooray, hooked up.

image

Looks much more interesting!



The problem arose during the search for working libraries and examples. As it turned out later, in the main, most well-known library bitbucket.org/fmalpartida/new-liquidcrystal/downloads - examples are not working! It took some time and effort =)

Specifically, here are working examples and a link to the library. arduino-info.wikispaces.com/LCD-Blue-I2C
You can also find descriptions of various versions of LCD-I2C adapters that you can buy.

I bought this such www.ebay.com/itm/310565362720 it is assembled on a PCF8574 chip

This version of the library requires the installation of the display parameters as follows.

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address 


where the first number is the device address, the rest are assigned by the adapter's control pins for a specific display. here the problem was hidden - many examples initialize the screen with incorrect data - while the screen blinks, twitches and does not work.

The correct line, with the address and the necessary pins - depends on the specific display. It is difficult for a beginner to understand the essence and score the necessary data
All that is above is a working version. It’s probably even better than the library I’m currently using. But it seemed to me too redundant and cumbersome.

The second library earned immediately. arduino-info.wikispaces.com/file/detail/LiquidCrystal_I2C1602V1.zip/341635514
Unfortunately, I don’t remember where I took the demo for this library, but it uses a simplified LCD setting, which was what I needed.

 LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display 


In this line, the address of the device is assigned and the type of display is determined, 16 characters in 2 lines.

The first and second libraries are not compatible with each other! . Examples for one library do not work with another. The following code will refer to the latest library specified for use with the I2C LCD.

After connecting the screen, I wanted to display something on it. Just numbers are not interesting. Let there be hours =)) Probably everyone goes this way, including me. Arduin's soft watches allow themselves to show the time, but the problem is in resetting data when disconnected. And since I don’t have any buttons, I have to set the clock via the cable on the COM port through the console. In general, I'm sick of it very quickly.

The RTC1307 real-time clock is probably the most common clock chip. There are many advantages - a separate microcircuit, independent of the Arduino main power supply in the presence of a battery, independent of the main program - time counts for sure! The advantage of watches based on 1307 - I2. At the same time, no additional outputs need to be activated - everything is controlled by the same 2m wires as the LCD.

The library for working with the clock on I2c earned immediately, examples are working. github.com/adafruit/RTClib

The program is simple, everything is transparent. Figures are created by functions in which a figure is displayed in separate blocks. Blocks of 5x8 dots are specified in user characters, the maximum of them can be 8. An example is taken from the forum arduino.cc.

Drawing takes place in a separate function, the numbers are displayed one by one. Running crosses - the author's whim, i.e. my)) The offset of the numbers is controlled when calling the draw function.



Code for Arduina
 // Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include <Wire.h> #include "RTClib.h" #include <LiquidCrystal_I2C.h> RTC_DS1307 rtc; LiquidCrystal_I2C lcd(0x27,16,2); // set the LCD address to 0x27 for a 16 chars and 2 line display byte LT[8] = { B00111, B01111, B11111, B11111, B11111, B11111, B11111, B11111 }; byte UB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B00000, B00000 }; byte RT[8] = { B11100, B11110, B11111, B11111, B11111, B11111, B11111, B11111 }; byte LL[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B01111, B00111 }; byte LB[8] = { B00000, B00000, B00000, B00000, B00000, B11111, B11111, B11111 }; byte LR[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11110, B11100 }; byte MB[8] = { B11111, B11111, B11111, B00000, B00000, B00000, B11111, B11111 }; byte block[8] = { B11111, B11111, B11111, B11111, B11111, B11111, B11111, B11111 }; // loop counter int count = 0; void setup () { Serial.begin(57600); Wire.begin(); rtc.begin(); lcd.init(); // initialize the lcd lcd.backlight(); lcd.home(); lcd.createChar(0,LT); lcd.createChar(1,UB); lcd.createChar(2,RT); lcd.createChar(3,LL); lcd.createChar(4,LB); lcd.createChar(5,LR); lcd.createChar(6,MB); lcd.createChar(7,block); // sets the LCD's rows and colums: lcd.clear(); if (! rtc.isrunning()) { Serial.println("RTC is NOT running!"); // following line sets the RTC to the date & time this sketch was compiled // rtc.adjust(DateTime(__DATE__, __TIME__)); } } void custom0(int x) { // uses segments to build the number 0 lcd.setCursor(x,0); // set cursor to column 0, line 0 (first row) lcd.write(0); // call each segment to create lcd.write(1); // top half of the number lcd.write(2); lcd.setCursor(x, 1); // set cursor to colum 0, line 1 (second row) lcd.write(3); // call each segment to create lcd.write(4); // bottom half of the number lcd.write(5); } void custom1(int x) { lcd.setCursor(x,0); lcd.write(1); lcd.write(2); lcd.print(" "); lcd.setCursor(x,1); lcd.write(4); lcd.write(7); lcd.write(4); } void custom2(int x) { lcd.setCursor(x,0); lcd.write(6); lcd.write(6); lcd.write(2); lcd.setCursor(x, 1); lcd.write(3); lcd.write(4); lcd.write(4); } void custom3(int x) { lcd.setCursor(x,0); lcd.write(6); lcd.write(6); lcd.write(2); lcd.setCursor(x, 1); lcd.write(4); lcd.write(4); lcd.write(5); } void custom4(int x) { lcd.setCursor(x,0); lcd.write(3); lcd.write(4); lcd.write(7); lcd.setCursor(x, 1); lcd.print(" "); lcd.print(" "); lcd.write(7); } void custom5(int x) { lcd.setCursor(x,0); lcd.write(3); lcd.write(6); lcd.write(6); lcd.setCursor(x, 1); lcd.write(4); lcd.write(4); lcd.write(5); } void custom6(int x) { lcd.setCursor(x,0); lcd.write(0); lcd.write(6); lcd.write(6); lcd.setCursor(x, 1); lcd.write(3); lcd.write(4); lcd.write(5); } void custom7(int x) { lcd.setCursor(x,0); lcd.write(1); lcd.write(1); lcd.write(2); lcd.setCursor(x, 1); lcd.print(" "); lcd.print(" "); lcd.write(7); } void custom8(int x) { lcd.setCursor(x,0); lcd.write(0); lcd.write(6); lcd.write(2); lcd.setCursor(x, 1); lcd.write(3); lcd.write(4); lcd.write(5); } void custom9(int x) { lcd.setCursor(x,0); lcd.write(0); lcd.write(6); lcd.write(2); lcd.setCursor(x, 1); lcd.print(" "); lcd.print(" "); lcd.write(7); } //void clearnumber(int x) //{ // clears the area the custom number is displayed in // lcd.setCursor(x,0); // lcd.print(" "); // lcd.setCursor(x,1); // lcd.print(" "); //} void loop () { digitalClockDisplay(); delay(1000); } void digitalClockDisplay(){ // digital clock display of the time DateTime now = rtc.now(); printDigits(now.hour()/10,0); printDigits(now.hour()%10,4); printDigits(now.minute()/10,9); printDigits(now.minute()%10,13); // lcd.setCursor(7, 1); // lcd.print(now.second()/10); // lcd.print(now.second()%10); if (now.second()%10%2==0){ lcd.setCursor(7, 0); lcd.print("+ "); lcd.setCursor(7, 1); lcd.print(" +"); lcd.setCursor(3, 1); lcd.print("+"); lcd.setCursor(12, 0); lcd.print("+"); lcd.setCursor(3, 0); lcd.print(" "); lcd.setCursor(12, 1); lcd.print(" "); } else { lcd.setCursor(7, 0); lcd.print(" +"); lcd.setCursor(7, 1); lcd.print("+ "); lcd.setCursor(3, 0); lcd.print("+"); lcd.setCursor(12, 1); lcd.print("+"); lcd.setCursor(3, 1); lcd.print(" "); lcd.setCursor(12, 0); lcd.print(" "); } //  } void printDigits(int digits, int x){ // utility function for digital clock display: prints preceding colon and leading 0 switch (digits) { case 0: custom0(x); break; case 1: custom1(x); break; case 2: custom2(x); break; case 3: custom3(x); break; case 4: custom4(x); break; case 5: custom5(x); break; case 6: custom6(x); break; case 7: custom7(x); break; case 8: custom8(x); break; case 9: custom9(x); break; } } 




I would be grateful to experienced programmers if they tell me how to make the code more compact. It seems there are plenty of opportunities to optimize the same type of data, but I do not know how to implement it. I saw examples on the same forum arduino.cc - but did not understand the essence of what is happening - the code is poorly commented out and I do not understand where the legs grow from. Something there was related to the memory of the LCD controller / I would appreciate commenting on the code.

UPD: while the note was in the sandbox, made a few more hours. Is it interesting for someone? Made a pseudo 3d font of 3x2 characters and added a beeper to tick every second.

image

Made a thin font of 2x2 characters for displaying hours, minutes, seconds on one 1602 screen + display of the day of the week and date. A thin font - I drew it myself for some random picture - I did not find it in the form of a code. Also added a beeper + RGB LED that changes hue every second. In general, all that was - all screwed))

image

Source: https://habr.com/ru/post/219137/


All Articles