Arduino Leonardo crashes when using I2C

Hi! I've been working with a 16x1 LCD screen using Arduino Leonardo with the LiquidCrystal_I2C.h library. It all works fine until maybe 2-10 seconds in and the program freezes. It seems to work when I'm not calling the drawFreqOnScreen() function in the loop which writes to the LCD using I2C. Currently I'm starting to think that my Arduino chip might be broke (it came new in the mail today so might have been broken from shipping). Can only the I2C be broke on the board? It seems to work fine otherwise, It starts up and draws on the screen and it even updates the LCD with new values 4-9 times until it stops. It even stops outputting in the Serial monitor when it happens so the entire program/board freezes because of it.

  • I've tried the same program on a UNO board I have and it works fine (with same LCD etc).
  • I've tried different USB cables and ports.
  • I've tried different LCD and I2C.
  • I've tried the "LiquidCrystal_PCF8574.h" library
  • I've tried unplugging LCD screen and only using serial but still calling the "drawFreqOnScreen" function and it still freezes.

Only works when I comment "drawFreqOnScreen" function in the "loop" function. But works with it for UNO board :confused: Does anyone have a clue of what could be wrong?

#include <LiquidCrystal_I2C.h> // Library for LCD

LiquidCrystal_I2C lcd(0x27, 8, 2); // I2C address 0x27, 16 column and 1 rows (but in code its 8 colums 2 rows).

//Debounce vaiables
long timeOfLastDebounce = 0;
int delayOfDebounce = 1;

// Store Previus states
int prevClk;
int prevDt;

// Frequancies
int highFreq = 128;
int lowFreq = 500;
int currHighFreq = 128;
int currLowFreq = 500;

// Rotaryencoder pins
const int clkPin = 4;
const int dtPin = 3;

// 
bool serialModeActive = true;

void setup() {
  if(serialModeActive){
    Serial.begin(9600);
    while(!Serial);
    Serial.println("serial started");
  }

  pinMode(clkPin, INPUT);
  pinMode(dtPin, INPUT);

  prevClk = digitalRead(clkPin);
  prevDt = digitalRead(dtPin);

  lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);

  drawFreqOnScreen(lcd);
}

void drawFreqOnScreen(LiquidCrystal_I2C _lcd) {
  _lcd.setCursor(0, 0);  
  _lcd.print(currHighFreq);
  _lcd.print(",");
  if (currLowFreq < 100) {
    _lcd.print("0");
    if (currLowFreq < 10) {
      _lcd.print("0");
    }
  }
  _lcd.print(currLowFreq);

  _lcd.setCursor(1, 1);
  _lcd.print(highFreq);
  _lcd.print(",");
  if (lowFreq < 100) {
    _lcd.print("0");
    if (lowFreq < 10) {
      _lcd.print("0");
    }
  }
  _lcd.print(lowFreq);
}

void loop() {
  //checkRadioFreqSet();
  if ((millis() - timeOfLastDebounce) > delayOfDebounce) {
    if (checkRotary()) {
      drawFreqOnScreen(lcd);
      if(serialModeActive){
        Serial.println(lowFreq);
      }
    }

    timeOfLastDebounce = millis();
  }
}

bool checkRotary() {
  int currentClkState = digitalRead(clkPin);
  int currentDtState = digitalRead(dtPin);

  bool stateChange = false;

  if ((prevClk == 1) && (prevDt == 0)) {
    if ((currentClkState == 0) && (currentDtState == 1)) {
      lowFreq -= 5;
      stateChange = true;
    }else if ((currentClkState == 0) && (currentDtState == 0)) {
      lowFreq += 5;
      stateChange = true;
    }
  }else if ((prevClk == 0) && (prevDt == 0)) {
    if ((currentClkState == 1) && (currentDtState == 0)) {
      lowFreq -= 5;
      stateChange = true;
    } else if ((currentClkState == 1) && (currentDtState == 1)) {
      lowFreq += 5;
      stateChange = true;
    }
  }

  if (lowFreq > 990) {
    lowFreq = 990;
  } else if (lowFreq < 0) {
    lowFreq = 0;
  }

  prevClk = currentClkState;
  prevDt = currentDtState;

  return stateChange;
}

Pin 3 on the Leonardo is (also) SCL; might that be the reason?

2 Likes

Your topic has been moved to a more suitable location on the forum; Installation and Troubleshooting is not for problemswith your project :wink:

YES IT WAS! :open_mouth: It works now :slight_smile:.

Yeah :confused:. I was just out of ideas so I though it had to be a broken board. Silly me.