Problem using encoder with lcd shield

Hello, I am trying to make a project with the Arduino UNO REV 3, LCD Keypad Shield and a rotary encoder of type "YT06-0P-400B-2M". The purpose of the project is to create a simple counter which increases or decreases when the encoder turns clockwise or counter-clockwise respectively, and display the current value on the LCD. For reference, this is one of the many implementations of the code that I have tried:

#include <LiquidCrystal.h>

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);  // Initialize the library with the pins you're using for LCD connections

volatile unsigned int temp, counter = 0;

void setup() {
  Serial.begin(9600);

  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);

  attachInterrupt(0, ai0, RISING);
  attachInterrupt(1, ai1, RISING);

  lcd.begin(16, 2);  // Initialize the LCD with 16 columns and 2 rows
  lcd.print("Counter:");  // Initial display text
}

void loop() {
  if (counter != temp) {
    Serial.println(counter);
    lcd.setCursor(8, 0);  // Move cursor to the position to update the counter value
    lcd.print(counter);
    temp = counter;
  }
}

void ai0() {
  if (digitalRead(3) == LOW) {
    counter++;
  } else {
    counter--;
  }
}

void ai1() {
  if (digitalRead(2) == LOW) {
    counter--;
  } else {
    counter++;
  }
}

The problem I am facing is that when I connect the encoder by itself, the serial monitor displays the values correctly and increments/decrements them properly as I turn the encoder. When I add the LCD and connect the encoder on top of it, the value on the screen jumps immediately to some high number around 65000, while at the same time, the serial monitor displays weird symbols and squares and sometimes the number on the screen. The baud rate is properly selected to be the same in both the code and the serial monitor and, as you can see from the code above, I also have the pullup resistors activated in the pins that I connect the phases of the encoder (the directions for the encoder itself instruct me to add 1kohm resistors between the phases and VCC). I have tried using my own resistors instead of those on the Arduino, but I have the same problem.

I am attaching photos of my hardware, as well as my wiring and serial monitor

output:

My encoder operates between 5-24V with Red, Black, Green, White = VCC, GND, A phase, B phase.

Wiring with all components, if I remove the shield and connect the encoder either directly to the Arduino, or through the breadboard it works fine.

The serial monitor with the LCD on the Arduino and the encoder connected to the top pins.

The serial monitor without the LCD and only the encoder.

I have written many different implementations of code for the same operation, I even asked ChatGPT for help, going even as far as describing my project and asking it to write a code for me from the beginning, which ended up being really close to what I had, but a little more clean and tidy.

What could cause these issues. The hardware all works properly on it's own. ChatGPT suggested a wiring issue but as you can see in the pictures, the wiring is pretty simple. Everything is powered by the computers USB cable and I don't think an Arduino, an LCD and an encoder are that demanding in terms of power consumption. The only other thing I can think of is interference, but what could cause so much interference and I have tried to separate the encoder from the rest of the circuit by moving it a lot further. What else could it be?

It's better to know. Do you have a DMM you could use to measure the current draw of the encoder alone?

If the count variable is 0 and goes negative because of a decrement it rolls over to 65535. Make your count variable an unsigned data type ( I would use long) and that won't happen. Or test for less than 0 and prevent printing.

Thank you for the replies, I figured what was the problem. The phase wires were touching inside the adapter. I realized it by accidentally moving them around in frustration. Switching the adapter out for a new one fixed the problem and it now works just fine. Lesson of the day, inspect the connectors as well, not just the wires. Thank you for your contributions!!!!

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