Pololu Robotics & Electronics
My account Comments or questions? About Pololu Contact Ordering information Distributors

Pololu Forum

Using Encoder with Raspberry Pi Zero W

Hi there,

My setup includes the following components:

  • 1 Raspberry Pi Zero W
  • 2 Pololu Micro Metal Gearmotor #3056
  • 1 Pololu DRV8833 Dual Motor Driver Carrier #2130
  • 2 Pololu Magnetic Encoders #4761

I’m able to successfully run both motors in both directions, but I’m struggling to track the ticks on the encoder. I’m reading in ticks using both sensors on each encoder and dividing the total tick count by 12 (CPR) x 298 (gear ratio) to compute 1 revolution. However, when I tell a motor to cycle 1 revolution (3576 ticks with my setup), it moves the motor about 1.5 revolutions.

I think my code on the Pi is missing ticks as they go by, making it take longer for the code to count the ticks required for 1 revolution. From what I can find on the internet this error seems to be due to the fact that the Pi is occupying itself with other tasks besides counting the ticks leading it to miss some while it completes other tasks. As was mentioned on this forum, the Pi doesn’t have a “real-time operating system”.

It seems that one option may be to try to get around this the software way with some interrupt handling, but I’ve struggled to implement this. Another option it seems is to have another piece of hardware dedicated to counting the encoder ticks so that no ticks are missed, such as this.

Does Pololu offer a piece of hardware like this? Ideally it would be smaller than the Raspberry Pi Zero W (i.e. length < 60 mm). Do you have any other suggestions?


We do not have any products specifically intended for counting encoder ticks like what you linked to, but there are many small microcontrollers you could probably also use for that purpose.

You might consider using one of our A-Star 32U4 robot controllers for that. They were specifically designed to operate as a auxiliary controller for a Raspberry Pi, and they have built in motor drivers so you could program one to handle the motor control and encoder reading for your Raspberry Pi.

The A-Star robot controllers follow the same size as a Raspberry Pi Model A rather that the Raspberry Pi Zero W, but if you have a tight size constraint you could probably also do something similar with a smaller A-Star (such as our 32U4 Micro or 328PB Micro). However, with that approach you would need to use a separate motor driver (like the one you already have) and make all the required connections yourself.

- Patrick

Thank you for your help Patrick! Size is definitely a major constraint, so I think the A-Star 32U4 Micro should do the job.

1 Like