I had a Roboteq motor controller, but it wasn't fast enough. I'm hoping Arduino will be faster.

Supposing that the transfer is fast enough for my purposes, how can I read angular velocity, given that I have an encoder? Honestly, I'm not totally sure how to hook it up to an Arduino board. I can't find a name for my encoder... This is the datasheet though.

Can anyone help me with how to hook it up, and what commands to use in MATLAB?



1 Answer 1


Do all the processing on the Arduino, and supply the results to Matlab over serial.

I'm not 100% sure about your particular encoder, but generally encoders talk in "Grey Code". This is a series of bits which allow you to know the direction the encoder has turned.

There are a number of libraries and tutorials for Arduino dealing with encoders.

The one I use comes from here:

It would be best to use a library with interrupt routines on pins 2 & 3 (these support external hardware interrupts), so whenever the pin gets a signal from the encoder, the Arduino can act on it immediately. If you do not use these pins, and have time-consuming processing in the loop(), it's easy to miss 'ticks' from the encoder.

#include <Serial.h>

// Rotary Encoder (Interrupt Driven)
#define ENCODER_PIN_A       2 /* must be pin2 for interrupt */
#define ENCODER_PIN_B       3 /* must be pin3 for interrupt */
#include <Encoder.h>

void setup()
    // Gentlemen, start your encoders

void loop()
    unsigned long start_time = millis();
    Serial.println("Dear Matlab...");

    while (1)
        unsigned long now = millis();
        unsigned int encoder_value = encoder.read();

        // Do whatever angular velocity processing is needed here
        unsigned long ticks_per_ms = encoder_value / (now - start_time);

        // Send the result back to Matlab

I think for your encoder the "A" and "B" outputs are where the grey code will be coming from. These would be connected to Arduino pins 2 & 3.

Obviously the code above is not a complete solution, and needs to take care of edge-conditions like overflow of millis() after some time, etc. etc.

  • Hmm... Interesting. Do you know how many iterations per second that could run? I'm just afraid that a serial connection would be too slow. Could I do it over USB somehow?
    – TechnoSam
    Commented Jun 26, 2014 at 17:34
  • 115200 baud gives you over 10,000 bytes per second.
    – Kingsley
    Commented Jun 27, 2014 at 20:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.