I2C connection LCD display hangs and my NANO freezes

I'm making a small water level indicator using a pressure transducer and an Arduino NANO (Chinese).

For the indicator I'm using a WS2812B RGB LED Strip and the FastLED by Daniel Garcia Library;
I'm also using a 16x2 LCD Display with a I2C backpack with the LiquidCrystal_I2C.h by FMalpartida library.

I made a small PCB with terminal block connectors.

The problem, sometimes the arduino hangs, it freezes, no serial connection, no updating the LCD display, nor the LED strip, sometimes it may resume sometimes it just stays frozen until a hard reset.

I believe it's the I2C connection hanging because it seems to work fine without it, but i cannot be sure because it's not always that happens, although it seems to happen more often with the LCD display and the I2C backpack connected.

Attached there's a picture of how everything is connected, both sides of the PCB, and here's my code.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <FastLED.h>
#define LED_PIN     3
#define NUM_LEDS    8
CRGB leds[NUM_LEDS];

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

const uint8_t sensorPin = A0;

void setup() {
  FastLED.addLeds<WS2812, LED_PIN, RGB>(leds, NUM_LEDS);
  FastLED.setBrightness(25);

  leds[0] = CRGB(0 , 0 , 255);
  leds[1] = CRGB(0 , 0 , 255);
  leds[2] = CRGB(0 , 0 , 255);
  leds[3] = CRGB(0 , 0 , 255);
  leds[4] = CRGB(0 , 0 , 255);
  leds[5] = CRGB(0 , 0 , 255);
  leds[6] = CRGB(0 , 0 , 255);
  leds[7] = CRGB(0 , 0 , 255);
  FastLED.show();

  lcd.begin(16,2);
  lcd.backlight();
  lcd.setCursor(2,0);
  lcd.print("TORNOS LEON");
  lcd.setCursor(0,1);
  lcd.print("PRESION:#.## PSI");

  pinMode(sensorPin, INPUT);

  /*Serial.begin(9600);*/
}

void loop() {
  float voltage = 5.0/1024.0 * (analogRead(sensorPin) + 0.5);
  float pressure = 174.05 / 4.0 * (voltage - 0.5);
  pressure = constrain( pressure, 0, 174.05);

  if ( pressure >= 4.80 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(255 , 255 , 0);
    leds[4] = CRGB(0   , 255 , 0);
    leds[5] = CRGB(0   , 255 , 0);
    leds[6] = CRGB(0   , 255 , 0);
    leds[7] = CRGB(0   , 255 , 0);
  } else if ( pressure >= 4.59 && pressure < 4.80 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(255 , 255 , 0);
    leds[4] = CRGB(0   , 255 , 0);
    leds[5] = CRGB(0   , 255 , 0);
    leds[6] = CRGB(0   , 255 , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure >= 4.38 && pressure < 4.59 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(255 , 255 , 0);
    leds[4] = CRGB(0   , 255 , 0);
    leds[5] = CRGB(0   , 255 , 0);
    leds[6] = CRGB(0   , 0   , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure >= 4.17 && pressure < 4.38 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(255 , 255 , 0);
    leds[4] = CRGB(0   , 255 , 0);
    leds[5] = CRGB(0   , 0   , 0);
    leds[6] = CRGB(0   , 0   , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure >= 3.96 && pressure < 4.17 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(255 , 255 , 0);
    leds[4] = CRGB(0   , 0   , 0);
    leds[5] = CRGB(0   , 0   , 0);
    leds[6] = CRGB(0   , 0   , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure >= 3.75 && pressure < 3.96 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(255 , 255 , 0);
    leds[3] = CRGB(0   , 0   , 0);
    leds[4] = CRGB(0   , 0   , 0);
    leds[5] = CRGB(0   , 0   , 0);
    leds[6] = CRGB(0   , 0   , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure >= 3.54 && pressure < 3.75 ) {
    leds[0] = CRGB(255 , 0   , 0);
    leds[1] = CRGB(255 , 255 , 0);
    leds[2] = CRGB(0   , 0   , 0);
    leds[3] = CRGB(0   , 0   , 0);
    leds[4] = CRGB(0   , 0   , 0);
    leds[5] = CRGB(0   , 0   , 0);
    leds[6] = CRGB(0   , 0   , 0);
    leds[7] = CRGB(0   , 0   , 0);
  } else if ( pressure < 3.54 ) {
    leds[0] = CRGB(255 , 0 , 0);
    leds[1] = CRGB(0   , 0 , 0);
    leds[2] = CRGB(0   , 0 , 0);
    leds[3] = CRGB(0   , 0 , 0);
    leds[4] = CRGB(0   , 0 , 0);
    leds[5] = CRGB(0   , 0 , 0);
    leds[6] = CRGB(0   , 0 , 0);
    leds[7] = CRGB(0   , 0 , 0);
  }

  lcd.clear();
  lcd.setCursor(2,0);
  lcd.print("TORNOS LEON");
  lcd.setCursor(0,1);
  lcd.print("PRESION:");
  lcd.print(pressure);
  lcd.print(" PSI");
  FastLED.show();
  FastLED.delay(250);
  /*printStatus(voltage, pressure);*/
}

/*void printStatus(float voltage, float pressure) {*/
/*  Serial.print("Status: ");*/
/*  Serial.print(voltage);*/
/*  Serial.print(" v | ");*/
/*  Serial.print(pressure);*/
/*  Serial.println(" psi");*/
/*}*/

I've already changed the whole LCD display and I2C Backpack, the problem is not as often but still happens.

Where are the pullup resistors for the I2C bus ?
There could be pullup resistors on the backpack (The I2C module on the back of the display), can you see what their value is ?

Can you give links to the libraries that you use ?
Do you do something very weird ? For example a software I2C or a special "fast mode" ? I prefer that you use the most common and most normal and the most often used libraries.

An LCD with an i2C backpack doesn't usually take that many arguments to initiate. This looks like the old way for a 4 bit without i2C.

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

According to the Wiki with i2C it should be something like this:

LiquidCrystal_I2C lcd(0x38); // Set the LCD I2C address

https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

I do not know what the problem is but I see some possibilities. The I2C library at one time hung up I beleive if there was no response; I do not know if that was fixed. Second you may be getting feedback via the USB power supply. IE. it drops voltage momentary when items are switched on. This could also cause the I2C to miss an acknowledge. Try using an external power supply and see if this helps. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

Koepel:
Where are the pullup resistors for the I2C bus ?
There could be pullup resistors on the backpack (The I2C module on the back of the display), can you see what their value is ?

Can you give links to the libraries that you use ?
Do you do something very weird ? For example a software I2C or a special "fast mode" ? I prefer that you use the most common and most normal and the most often used libraries.

I didn't use pullup resistors for the I2C bus because the backpack seems to have them, 10k each.

I'm not doing anything weird but I do think am using an old version of the malpartida library, so I will check that.

wabbitguy:
An LCD with an i2C backpack doesn't usually take that many arguments to initiate. This looks like the old way for a 4 bit without i2C.

LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

According to the Wiki with i2C it should be something like this:

LiquidCrystal_I2C lcd(0x38); // Set the LCD I2C address

https://bitbucket.org/fmalpartida/new-liquidcrystal/wiki/Home

Yes, like I said, I think I grabbed an old version of the malpartida library.

gilshultz:
I do not know what the problem is but I see some possibilities. The I2C library at one time hung up I beleive if there was no response; I do not know if that was fixed. Second you may be getting feedback via the USB power supply. IE. it drops voltage momentary when items are switched on. This could also cause the I2C to miss an acknowledge. Try using an external power supply and see if this helps. This response is to help you get started in solving your problem, not solve it for you.
Good Luck & Have Fun!
Gil

I did leave my device connected from saturday to today with an external USB power supply, no hiccups so far.

I should mention a few things that I tried and there have been no more issues so far:

  • Changed entire LCD and I2C backpack, seems the first one I used was broken
  • I read here that I might need to recall lcd.begin() again every now and then or it may hang, I made a function to call it every minute, but the problem was happening at random times, could be a few seconds in or take a few minutes, so I don't think this was really the problem
  • I commented out all of the serial communication I was doing, I also believe it could be this or maybe a problem with the USB cable like it was mentioned before

So far there haven't been anymore issues, but I will report if anything changes, I will update my library to the one pointed out to me in bitbucket and see how it goes.

Thank you all for responding