I was reading the encoder value in arduino uno. But the output is not comming properly. It is coming like this: enter image description here I am using this arduino code to read encoder:

/* Rotary encoder read example */
#define ENC_A 14
#define ENC_B 15

void setup()
  /* Setup encoder pins as inputs */
  pinMode(ENC_A, INPUT);
  digitalWrite(ENC_A, HIGH);
  pinMode(ENC_B, INPUT);
  digitalWrite(ENC_B, HIGH);
  Serial.begin (115200);

void loop()
 static uint8_t counter = 0;      //this variable will be changed by encoder input
 int8_t tmpdata;
  tmpdata = read_encoder();
  if( tmpdata ) {
    Serial.print("Counter value: ");
    Serial.print(counter, DEC);
    counter += tmpdata;

/* returns change in encoder state (-1,0,1) */
int8_t read_encoder()
  static int8_t enc_states[] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
  static uint8_t old_AB = 0;
  old_AB <<= 2;                   //remember previous state
  old_AB |= ( ENC_PORT & 0x03 );  //add current state
  return ( enc_states[( old_AB & 0x0f )]);

Looks line your terminal has the wrong baud rate, in your code you set it to 115200 baud with the line Serial.begin (115200); You can change th baud rate on your serial terminal by using the highlighted drop down menu:



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy