Wiring in encoders on a Raspberry Pi Pico robot
Our robot has already got encoders on board, and we have already wired them in. We can take a closer look at the motors and how they are wired into Raspberry Pi Pico GPIO pins to understand the robot better.
Examining the motors
We use N20 geared motors with encoders. The following diagram labels the motor parts:
Figure 6.3 – The N20 motor parts
Figure 6.3 shows a drawing of the motors we have used. Marked on it are essential features that affect how we use the encoders. On the left is a magnetic disk with markers in it. This disk is attached to the motor’s driveshaft and sensed by the encoder sensor board. On the right are the gearbox and the motor output shaft.
The driveshaft goes through the gearbox, so the output shaft will not make the same number of rotations as the disk – the gear ratio will determine this relationship. So one revolution of the output wheel could count many pulses; this gives us high resolution.
Consult the datasheet for the motors. Some Chinese characters are likely, but important numbers are usually in English. You may need translation services built into web search engines here. The datasheet and product page have two important numbers, the number of encoder counts per disk revolution and the gear ratio. The datasheet may note counts per disk revolution as pole count.
In my case, the gear ratio is 298:1, and the pole count is 14. Interpreting these facts means I get 298 turns of my encoder wheel per output wheel revolution. Each encoder turn produces 14 poles on each sensor (two sensors), so we get 28 edges. Multiplying the number of sensor pulses by the gear ratio gives 8344 edges per turn.
Examining the wiring
We saw the wiring for our robot in Figure 4.20 of Chapter 4, Building a Robot around Pico. However, to better illustrate the encoder connections, here is a diagram focusing only on the wiring of encoders to Pico:
Figure 6.4 – Encoders wired to Raspberry Pi Pico
The preceding figure takes a closer look at data connections for a robot encoder connection schematic. On the left is Raspberry Pi Pico; this has four connections from the encoders. These are on GPIO 20, 21, 26, and 27. Each of these can be set as input pins to read the state of the encoder pins.
If we were just reading encoders alone, we could write code to check each pin in sequence. However, doing this may tie things up. What if we could get components of the Pico to monitor these pins and pulse chains for us so that we could just read a counter for them when we need it?