简体   繁体   中英

Arduino UNO with LCD, strange readings from rotary encoder

I have an I2C 16x2 LCD display connected to an Arduino Uno's A4 (SDA) and A5 (SCL) pins. No problem with the display, it works properly.

Then I have a rotary encoder connected to pins D3 (INT1) and D4 . The INT1 pin is used as interrupt to read the encoder, and the reading is sent via Serial.print() to the Serial monitor. There are debounce CAPs connected to the rotary encoder. The encoder pins use the Arduino's internal pullups.

The interrupt is attached to read encoderPinA when encoderPinB is falling from HIGH to LOW . When turning the rotary clockwise, encoderPinA is LOW , and when turning it counter-clockwise, encoderPinA is HIGH .

Now, when there is nothing in the main loop , I get ++++++++++ signs on the serial monitor when turning the rotary clockwise, and ---------- signs when turning it counter-clockwise, as I should.

But if I uncomment those two lines that print to the LCD, I start to get erratic readings from the rotary encoder, like this: -++-++-++-+++-++-+++-++--+ .

What's going on? Is the LCD interfering with interrupt pins?

#define encoderPinA 4
#define encoderPinB 3

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(encoderPinA, INPUT_PULLUP);
  pinMode(encoderPinB, INPUT_PULLUP);
  attachInterrupt (digitalPinToInterrupt(encoderPinB), readEncoder, FALLING);
}

void loop() {
  //lcd.setCursor(0, 0);
  //lcd.print("test");
}

void readEncoder() {
  if (digitalRead(encoderPinA) == LOW) Serial.print("+");
  else Serial.print("-");
}

Sorry folks, this problem was clearly a cable related issue.

I was using the same non-shielded flat cable to carry out rotary and lcd signals, and there was some interference, because when I switch to separate cables, the erratic behaviour is gone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM