Invalid Encoder Readings in Arduino Mega

Questions about using encoders with the Roboclaw product line
Post Reply
Posts: 2
Joined: Tue Dec 11, 2018 10:23 pm

Invalid Encoder Readings in Arduino Mega

Post by iHaa » Mon Dec 17, 2018 10:07 pm

Hi! I've recently posted in another thread, but I think it's better to start a thread of my own.

I am trying to get readings from my encoder from my Arduino Mega, with the sample code, PacketSerialEncoderPosition, but I am having issues with the Arduino not outputting any values and is invalid.

To start off, I am using a Roboclaw 2x30A, with an Arduino Mega, with the updated Arduino library downloaded directly from Basic Micro's download section, and the firmware of 4.1.27.

When connected to Basic Micro's Motion Studio, auto-tuning works with the encoders are counting in the correct direction (when it is forward, it is counting up, with the speed values that are similar to the max values).
WhatsApp Image 2018-12-18 at 1.44.08 PM.jpeg
WhatsApp Image 2018-12-18 at 1.44.08 PM.jpeg (112.6 KiB) Viewed 1258 times
Device Status Page
WhatsApp Image 2018-12-18 at 1.44.09 PM.jpeg
WhatsApp Image 2018-12-18 at 1.44.09 PM.jpeg (174.55 KiB) Viewed 1258 times
Device Settings Page

Now here's the problem that I am facing:

When I am using the PacketSerialEncoderPositon sample code in the Arduino Mega, it does not receive the encoder details, it is not outputting any numbers in the serial monitor.

However, when I have the serial monitor open and I then connect the USB from the Roboclaw to my PC (with the Arduino code still running), the values show up.

Is there something that I am doing wrong, and if so, how do I fix it?

Code: Select all

//See BareMinimum example for a list of library functions

//Includes required to use Roboclaw library
#include <SoftwareSerial.h>
#include "RoboClaw.h"

//See limitations of Arduino SoftwareSerial
SoftwareSerial serial(10,11);	
RoboClaw roboclaw(&serial,10000);

#define address 0x80

//Display Encoder and Speed for Motor 1
void displayspeed(void)
  uint8_t status1,status2;
  bool valid1,valid2;
  int32_t enc1 = roboclaw.ReadEncM1(address, &status1, &valid1);
  int32_t speed1 = roboclaw.ReadSpeedM1(address, &status2, &valid2);
    Serial.print(" ");
    Serial.print(" ");
  } else {
    Serial.print(" ");

//This is the first function arduino runs on reset/power up
void setup() {
  //Open Serial and roboclaw at 38400bps

void loop() {
  roboclaw.ForwardM1(address, 50);
  long last = millis();

Readings with only Arduino Mega Plugged in to PC
w usb.png
w usb.png (186.64 KiB) Viewed 1258 times
Readings with USB plugged in from Roboclaw to PC

Here's the electrical block diagram of how my encoder is connected.

User avatar
Basicmicro Support
Posts: 1336
Joined: Thu Feb 26, 2015 9:45 pm

Re: Invalid Encoder Readings in Arduino Mega

Post by Basicmicro Support » Tue Dec 18, 2018 10:10 am

When USB is plugged in to the Roboclaw the roboclaw clock gets adjusted to match the USB clock. Its a minor adjustment but would indicate the baudrate doesnt matech well enough between the software serial on the mega and the roboclaws hardware uart.

1. Make sure you are using a recent release of the Arduino ide(1.8.X should be good enough).

2. If that doesnt fix the problem switch to using hardware serial on the arduino. The mega has 4 hardware serial ports. Software serial is bitbanging the serial data which isnt nearly as accurate as the hardware serial ports.

Post Reply