Problem with I2C LED and endcoder on Arduino Mega

Hi, i am encountering a problem with my project based on a lcd monitor, a rotary encoder and some other stuff that aren't the issue.
The LCD i'm using is a 20x4 and its address is 0x27(i'm using a I2C shield on it), the endcoder has 5 pins(CLK, VCC, GND, SW, DT), and the board is an arduino mega.
The single components when i tested them didn't give me any problems, but when i try to plug together both the LCD monitor and the endcoder, the encoder stops to work. I tried to see what was happening using serial print and i've noticed that the reading from the encoder were kinda noisy, incorrect and the problem disappeared when i restarted my arduino without the LCD plugged in.
So i suspect that maybe the IC2 protocol is doing something, but here my knowledge stops and i've no idea how to resolve it by myself. Every example that i've found searching on internet is based on an Arduino Uno or a LCD monitor with address 0x3F(a 16x2 one), so nothing really helped me. I don't want to use another controller to be capable to read the encoder, it will be a waste of money and hardware, so i'm asking you if you have encountered the same problem and, hopefully, how i can fix it.

Here a sample of my code, just to let you see what i'm doing:

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int pinClk = 7;
const int pinDt = 18;
const int pinSw = 19;
const int pinVcc = 10;
const int pinGnd = 11;

int prevClk;
int prevDt;
int pigio;
int interazione;

int contatore;
int contatore_dif = 0;
int prev_contatore = 0;

void rotazione();
void pigiata();

void setup() {

  pinMode(pinClk, INPUT);
  pinMode(pinDt, INPUT);
  pinMode(pinSw, INPUT);
  pinMode(pinVcc, OUTPUT);
  pinMode(pinGnd, OUTPUT);

  digitalWrite(pinVcc, HIGH);
  digitalWrite(pinGnd, LOW);

  prevClk = digitalRead(pinClk);
  prevDt = digitalRead(pinDt);

  contatore = 0;
  lcd.begin(20, 4);

void loop()
  // put your main code here, to run repeatedly:

void pigiata()
  pigio = digitalRead(pinSw);
  if (pigio != 1)

    while(pigio != 1)
      pigio = digitalRead(pinSw);
    contatore = 0;
    contatore_dif = 0;
    prev_contatore = 0;
    interazione = 1;

void rotazione()
  int currClk, currDt;
  currClk = digitalRead(pinClk);
  currDt = digitalRead(pinDt);

  if (currClk != prevClk) 

    if (currDt == currClk) {
      contatore_dif --;
    } else {
      contatore_dif ++;
    if (prev_contatore != contatore_dif/2)
      contatore = contatore_dif/2;
      prev_contatore = contatore_dif/2;
    prevClk = currClk;
    prevDt = currDt;

having delays in your code will play against you to efficiently capture state changes.

I'd recommend to move to an encoder library that is known for working well

I will try and i will let you know, thx

Perhaps you should correct your subject heading. :face_with_raised_eyebrow:

There are 2 main I2C addresses for the PCF8574 I2C backpacks. 0x27 and 0x3f. There are 2 models of the chip. PCF8574 (0x27) and PCF8574A (0x3f). The code is exactly the same for either with the exception of the address. So the code for 0x3f will work fine if you just change the address to 0x27.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.