Unexpected interruptions

You can never do enough testing, but unfortunately when it comes to a commercial product it can also be a case of ‘ship or die’. We did lots of tests on the HUB-ee wheels, resolving various manufacturing issues along the way, and when everything worked great we put them on the market. Then one of our early adopters got in touch with an issue – the quadrature encoders didn’t appear to work properly.

So it turned out to be a case of bad luck, or more specifically a bad motor that had slipped through our testing procedures and was causing voltage spikes as it momentarily shorted out the power supply. These spikes appeared on the sensor outputs and were triggering spurious interrupts on the micro-controller.

Phew – what a relief – so we sent some free replacement wheels ... but the same issue cropped up again and this time it wasn’t a bad motor. We spent days trying to replicate the problem, all the while exchanging e-mails and test data with our patient and understanding customer (thanks Bob!) and having sleepless nights worrying that there was some systemic issue with our product.

Eventually, after several false hypotheses we think the issue has finally been tracked down and resolved - and it all comes down to voltage levels and micro-controller pin interrupts.

Voltage Levels and design choices

We designed the wheels to work with voltages down below 5V – so you can power them with a single LiPo cell, or four AA cells – and this meant running the sensor circuit from a 3.3v regulator. Having scratched our heads for ages over the best way to implement the sensor circuit in the wheels we decided that having it output a 3.3v logic level was fine because most 5V micro-controllers, for example the ATMega range used in the Arduino™ boards, have a logic voltage threshold that was below 3.3V – meaning it would recognise a 3.3v input on a pin as a logic HIGH input.

It is actually quite common for industrial quadrature sensors to feature an open collector output – this means that instead of having the sensor output voltage switch between your chosen positive voltage and ground, you have it switch between floating (no output) and ground. This allows you to interface the sensor output to any logic level circuit you want simply by adding pull up resistors to the outputs. The resistors pull those outputs up to whatever positive voltage you want so your sensor output will now switch from ground, to the voltage of the circuit they are driving – in fact most micro-controllers nowadays will have configurable internal pull up resistors for exactly this purpose.

The problem for us was that making the sensor outputs open collector involved adding extra components to the PCB’s and increasing the cost, and because the 3.3v output appeared to drive the most common 5v systems like Arduino™ perfectly well we thought it wasn’t required.

It turns out we were a little bit wrong.

The problems occur when you use pin change interrupts to detect the quadrature encoder signals – pin change interrupts are small code functions that can be executed automatically when a digital input pin on the micro-controller changes state – they are very handy for reading things like quadrature sensors because you don’t need to write code that constantly checks the pin (and risks missing a pulse).

Most of the time, when you use interrupts to read the sensors on an Arduino, they work fine but very occasionally you will get extra interrupts occurring – which then gives you false data about how far and fast the wheel has moved.

Noisy data and errors in reading from quadrature encoders isn’t a problem unique to our wheels. Many micro-controllers or dedicated quadrature decode IC’s with hardware specifically designed for reading these sensors will have features designed to screen out things like high frequency noise, and even to detect errors caused by contamination in the encoder.

How to fix it:

Fortunately there is an easy fox that gives (as far as we can determine!) nice reliable behaviour with an micro-controller running at 5V. All you need are a couple of NPN transistors and you can convert the sensor outputs to open collector outputs, and use internal pull up resistors (if available) to drive the pins to their proper voltages. The HUB-ee sensor outputs are protected by 1k resistors so they are all set up for driving the base terminal of the transistor with no problem. With the addition of an 0.1uf capacitor between the transistor base and ground you can create a low pass filter to screen out any high frequency noise that might get through.

We have put together a little interfacing guide here, and are creating some tutorial pages all about quadrature encoders, pin interrupts and code for counting pulses, determining speed and creating speed control loops.

Within a few weeks we should also have some new breakout boards that include the transistors and capacitors for open collector sensor outputs, and an updated shield with the same additions (and a few other improvements as well).

Whilst we get on with this, here is an animation of a quadrature encoder we made.

Quadrature Animation

Forum Submit