Wheel encoders on a Raspberry Pi Robot

One of the problems with normal DC motors on robots is that you can’t accurately control them- you can vary the power to them using PWM but you’ve no real idea how far your’ bot will travel.

If you use stepper motors instead, then you can get very accurate results but the cheap ones don’t turn very fast and so are not suitable for a large floor area but ideal for table top use.

Recently, the Initio Robot Chassis from 4tronix has come along  and has wheel encoders built in and I’ve attempted to incorporate them into my ScratchGPIO project.

Basically, I use the RPi.GPIO library to do all my GPIO in and out and it has a facility to generate a pseudo-interupt (a callback routine)  every time it gets a pulse.  The wheel encoders produce 36 pulses for every 1 revolution of the wheels so its just a case of using the callback mechanism to count the number of pulses.

So I tell the wheels to turn 1 revolution and the count goes up by 36 – well not quite 🙂

I have no idea what power for what length of time (but approx 0.5) will give 1 revolution so I just switch the motor on, wait until count exceeds 36 then stop the motor.

The problem with this is that the motor takes some time to stop and so the final count ends up at about 48 or so.

The trick at this point is to remember the over-shoot and use the difference to compensate for the next move.


encoderpicBut, as usual with ScratchGPIO, all this is hidden beneath the bonnet and all the Scratch has to do is the scripts on the left to move forward and back one wheel circumference.

At the moment only the left hand encoder is read and I only use one of the sensing wires.


To connect it up
Looking at your PirRoCon board with GPIO pins on the left and regulator nearest you, remove the blue link nearest you in the bank of 8. (This disables the level-shifter for pin 7 (GPIO 4) which is the one I’ve designated for using for this.

Connect the brown wire from the left hand encoder (that’s left hand as if you were driving the robot yourself) and connect it to the exposed pin nearest the GPIO pins on the link you have just removed

Connect the black wire to a 0V pin and connect the red wire to 3.3 (NOT 5V!!!!!!) (The 1.2 version has dedicated 3.3V pins but if you have an original PiRoCon then you need to connect to the 3.3V on the i2C sockets using a short male-male lead)

ScratchGPIO Documentation

This post is out-of-dat.-  Please update to latest version

This post is to hopefully document all the available broadcasts and variable usage that ScratchGPIO understands and responds to


Scratch Variable “bug”

Although using variables is necessary once you start writing complex scripts, there is a “bug” or a “feature” (depending on yourpoint of view) in the way that Scratch sends out variable changes to any external programs.

If you say broadcast pin11On then Scratch will always send Pin11On out to any listening programs

But if you use a variable called Pin11 and say set Pin11 to On then it only sends this information out if the previous value of Pin11 variable was not On i.e. only changes are sent out

pindocThis code will not switch pin11 on the second time as the variable has not changed from its previous value.

One workaround is to use the green join block and broadcast any variables


This method will ensure than any variable changes are always sent out