Programming with a mechanical encoder

Jump to: navigation, search
ArduinoArduino Tutorials and Guided Projects → Tutorials/Arduino Projects/RC car Hacking Project/Programming with a mechanical encoder

An Unexpected Development

Often, when disassembling RC cars, you can be thrown a curveball - a design feature that you were not expecting in the slightest. This happened to us while working on the Gymkhana - we found the most curious method of turn regulation.

The Gymkhana is unique in the fact that it has a 'turbo drift' button. This button functions to make the steering turn at a greater angle when it is pressed. What this ultimately means, however, is that the Gymkhana had to feature some sort of turn regulation - it couldn't use a 'dumb' system like, say, the Interceptor, where a DC motor simply goes full speed left for a left turn, and full speed right for a right turn; the Gymkhana needed something that let the control board know how far the wheels had turned.

The mechanical encoder

It turns out that the Gymkhana's encoder decided to hook up a 4-bit mechanical encoder to the steering system.

Basically, there are four outputs (and a ground connection). The encoder wheel is designed so that when it turns, each contact is either touching metal or some non-conductive surface. The pattern seen in the image is designed so that for every possible angle of turn, a different combination of outputs is achieved. We can then use this combination of outputs to our advantage by plugging them into the Arduino's digital ports. Since there are 4 binary wires (that is, they carry either an 'on' or 'off' signal), this is called a 4-bit system.

Using the encoder values

The Gymkhana's designers did not expect their average user to need to know the combination of signals for each turn point; as such, we are going to have to figure it out for ourselves.


The first thing we need to do is connect the encoder wires to the Arduino. Unplug the cable from the control board:

Now, you will need to find which wire is which. Usually this is listed on the circuit board the wires are emerging from, but if not, this is what we determined:

Wire color Function
Aqua Signal 1
Yellow Signal 2
Orange Signal 3
Red Signal 4
Brown Ground

Connect these to the Arduino. Ground should go to ground. It doesn't matter which ports the other wires go to, but we used digital port 2 for signal 1, port 3 for signal 2, port 4 for signal 3, and port 5 for signal 4. It would have been nice to use 1 for 1 and so forth, but port 1 is reserved for UART communication.

We couldn't use the breadboard here as the pin spacing in the plug did not conform with the 1/10 inch pin spacing we know and love. As such, standard single-core wires inserted into the pin holes work nicely. Jumpers do not fit as they are 1/10 inch wide.

Deciphering the encoder values

Now that we are all plugged in, we are going to use the RobotC debugger to find the values the encoder is spitting out for each turn angle.

Write a simple empty program:

#pragma config(CircuitBoardType, typeCktBoardUNO)
#pragma config(UART_Usage, UART0, uartSystemCommPort, baudRate200000, IOPins, dgtl1, dgtl0)
#pragma config(Sensor, dgtl2,  wire1,          sensorDigitalIn)
#pragma config(Sensor, dgtl3,  wire2,          sensorDigitalIn)
#pragma config(Sensor, dgtl4,  wire3,          sensorDigitalIn)
#pragma config(Sensor, dgtl5,  wire4,          sensorDigitalIn)
task main()

Run it, and take a look at the 'Sensors' section of the debugger. You should have four sensors, wires 1-4, each displaying a value of either 1 or 0.

Now, get out a piece of paper, or open a spreadsheet. Set out a table like so:

Relative turn amount wire 1 wire 2 wire 3 wire 4

Keep in mind that you don't know how high or low it will go.

With the steering mechanism assembled, turn the wheel to its center point - i.e. where it would be if the car was going straight. Now, take a look at the combination of values in the debugger.