Use Arduino for Wheel Encoder Counter

Program Arduino

Here is the program which you will upload to your Arduino. The program has 2 interrupt counters for the pins connected to the encoders. The main program will loop until the letter 'a' i received from the EZ-B over the UART. Once the 'a' is received, the Arduino will transmit 2 bytes, which is are the two encoder values, respectively. Once the data is transmitted, the count of the 2 bytes is reset.

Resetting the values on each query will make it much easier in ARC to identify what wheel is spinning quicker/slower.

Arduino Code:


// just reading the encoder, nothing else.

int encoderValue_A = 0;
int encoderValue_B = 0; 

void setup() {

  Serial.begin(9600);

  pinMode(2, INPUT);
  pinMode(3, INPUT);

  attachInterrupt(digitalPinToInterrupt(2), countA, FALLING);
  attachInterrupt(digitalPinToInterrupt(3), countB, FALLING);
}

void loop() {

  // if there is data to read, read it
  if (Serial.available() > 0) {

    // read the incoming data from the ezb
    int incomingByte = Serial.read();

    // command 'a' means transmit the current values and reset the counters
    if (incomingByte == 'a') {

      // send the values
      Serial.write(encoderValue_A);
      Serial.write(encoderValue_B);

      // reset the values
      encoderValue_A = 0;
      encoderValue_B = 0;
    }
}

void countA() {

  encoderValue_A++;  
}

void countB() {

  encoderValue_B++;  
}


ARC Pro

Upgrade to ARC Pro

Your robot can be more than a simple automated machine with the power of ARC Pro!