News    The Project    Technology    RoboSpatium    Contribute    Subject index    Download    Responses    Games    Shopping tips    Contact   




<<< Digital ruler         Demonstration servo motor >>>

Raspberry PICO servo

Video about the PICO servo


About the Pico Servo

Raspbery Pico Servo
Figure 1:
Servos are very useful components in many electromechanical projects which is why there are already some chapters on that on my website:
With the servo based on the Raspberry Pico discussed in this chapter, I would like to deal with the electronics and software for controlling homemade servos in a little more detail. The mechanical design of the PICO servo is not perfect, instead the construction is based on an enlarged version of conventional RC servos. Nevertheless, a quite strong and really usable servo has been created, which can be controlled with the pulse width signal known from RC servos. Of course, the USB interface built into the Raspberry Pico can also be used for data transfer. Also thinkable are I2C or SPI or whatever you need and the Raspberry Pico can deliver.

Parts list

When buying components via the affiliate partner links I have listed in the table (or through the banners on my pages) you help to keep my my projects going without extra costs for you - many thanks!
Clicking on the links does not mean you have to buy - you can simply browse through the pages ;-)

Of course, supporting my freely accessible educational platform without shopping but by making a donation or becoming a Patreon also works. Many thanks to everyone who already sent me an obol!



Component Quantity Online shops Remarks
Raspberry Pico 1 Berrybase.de
ebay.co.uk
None of my affiliate partners, but a good source.
DC motor 1 RS-Components Drive motor
H-bridge BTS7960 1 eBay.com
ebay.co.uk
Motor driver
0.96 Inch OLED 1 ebay.com
ebay.co.uk
Display for a bit more comfort
Hall sensor type AH3503 3 ebay.com
ebay.co.uk
Sensors
Magnets 5x20x2mm 4 ebay.com
ebay.co.uk
Sensors
Dupont cables, female, 20cm many ebay.com
ebay.co.uk
Wiring
5V Step-Down Converter 4 ebay.com
ebay.co.uk
Supply voltage Pico, with USB interface for simple use.
Zener diode 3V 4 ebay.com
ebay.co.uk
Pin protection Pico
Resistors 3kΩ 4 ebay.com
ebay.co.uk
Pin protection Pico
Diode 6A10G 1 ebay.com
ebay.co.uk
Reverse polarity protection, 6A or more
PLA 1 Geekbuying.com
Gearbest.com
ebay.com
ebay.co.uk
Mechanics, PET-G or what ever material you prefer.
Aluminum tube 6mm 1 Hardware store Gear shafts, 6mm steel or M6 threaded rods will work, too.
1x 42mm length, 1x 64mm length.
Ball bearings 609 2 ebay.com ebay.co.uk Output shaft, dimensions 8x22x7mm
Threaded rod M8, 100mm 1 Hardware store Output shaft
Threaded rod M5, 175mm 8 Hardware store Case mount
Threaded rod M3, 1m 1 Hardware store Pico mount
Screws M5*16 4 Hardware store Motor mount
Screws M3*20 1 Hardware store Motor pinion
Nuts M3 many Hardware store  
Nuts M5 many Hardware store  
Nuts M8 5 Hardware store Output shaft
Waschers 8mm hole 8 Hardware store Output shaft
Tapping screws 2x20mm 8 Hardware store Pico and OLED mount

Mechanics

Raspbery Pico Servo, gedruckte Teile
Figure 2:
Almost all mechanical components are made of PLA with a 3D printer.

Raspbery Pico Servo, sonstige Kleinteile
Figure 3:
Screws, washers, threaded rods, two ball bearings and other small parts are also needed.
The axes for the gearbox have a diameter of 6mm. I used aluminum tubes because I had them in my stock.

Raspbery Pico Servo, Getriebe
Figure 4:
From the gears of the gearbox I created two variants:
The first version had a 355:1 reduction with very high torque, but was clearly too slow, so I changed two of the gears, giving a total reduction of 177:1. With that, the lever rotates about three times the speed of the high reduction variant.

Raspbery Pico Servo, Unterseite
Figure 5:
The electronic components are located in the lower area.

Electronics

Raspbery Pico Servo, elektronische Komponenten
Figure 6:
I got the motor as a gift from RS-Components years ago, but haven't used it yet - sometimes a project just needs a little longer to form. The electrical input power is supplied via an H-bridge type BTS7960. The already mentioned Raspberry Pico runs the control software. The 12V motor delivers up to 36W. For a little more comfort, I connected a 0.96 inch OLED display.

Raspbery Pico Servo, Hallsensoren
Figure 7:
To give the Pico an idea of what is going on in its environment, three Hall sensors type AH3503 are used. These sensors provide an output signal that is equivalent to the strength and direction of the magnetic flux density passing through them.

Raspbery Pico Servo, Sensorscheibe
Figure 8:
A sensor disc with 3 magnets is attached directly to the motor shaft. This disc is scanned by 2 Hall sensors - why 2 sensors are needed to detect the rotation, is described in detail in a previous chapter.

Raspbery Pico Servo, Spannungsteiler Hallsensor
Figure 9:
The Raspberry Pico works with logic levels of no more than 3.3V.
A voltage divider composed of a 3 kilohm resistor and a 3V Zener diode prevents the logic voltage at the pin of the pico from being exceeded. Without a magnet, the corresponding pin returns a logical "1", while in the presence of a magnet with the south pole towards the Hall sensor, a logical "0" is returned.

Raspbery Pico Servo, circuit layout
Figure 10:
Circuit layout

Wiper motor as servo

Raspbery Pico Servo aus Scheibenwischermotor
Figure 11:
And if you don't own a 3D printer, you can also modify a wiper motor to turn it into a servo. Together with the OLED screen, the same pins are used on the Pico, only a few parameters in the software have to be adjusted.

Raspbery Pico Servo, Sensorscheibe an Scheibenwischermotor
Figure 12:
On the Mercedes motor (type SWF 4195B) I am using, the motor shaft sticks out at the rear end so that I could cut off the cap on the housing and glue a sheet metal sensor disk to it. This wheel is scanned by two infrared light sensors.

Raspbery Pico Servo, Endschalter
Figure 13:
The reduction of the worm gear is 72:1. The limit switch for the servo lever is another photo sensor.

Software

A special feature of the Raspberry Pico is the possibility of running small programs in an extra part of the chip, completely independently of the main program. This means that there are no interruptions from the main routine and the processing of the signals at the pins is extremely fast. I use this property to quickly process the input signals, both on the sensor disk and to read the servo control PWM signal at another pin. However, the command set of these mini-programs is very limited and the number of commands per program may not exceed 32 (jump labels do not count as command lines).
The sensor disc on the engine is read out with the help of the first, so-called state machine:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# Definition of first "State Machine"  
# that checks the pins of the rotary sensor disc  
 
@asm_pio(autopush=True, push_thresh=32)  
def encoder():  
    label("start")  
    set(x, 0)  
 
    label("loop_high")  
    wait(1, pin, 0)         # Wait until in_base pin goes HIGH  
    jmp(pin, "CW_1")        # if jmp_pin is low CW rotation  
    jmp(x_dec, "CCW_1")     # if not, CCW rotation, Decrement X  
    label("CCW_1")  
    jmp("loop_low")         # jump to "wait for loop going low"  
    label("CW_1")           # CW rotation  
    mov(y, invert(x))       # Increment X  
    jmp(y_dec, "CW_1a")  
    label("CW_1a")  
    mov(x, invert(y))  
 
 
    label("loop_low")  
    wait(0, pin, 0)         # Wait until in_base pin goes LOW  
    jmp(pin, "CCW_2")       # if jmp_pin is low CCW rotation  
    mov(y, invert(x))       # if not, CW rotation Increment X  
    jmp(y_dec, "CW_2")  
    label("CW_2")  
    mov(x, invert(y))  
    jmp("loop_high")  
    label("CCW_2")          # CCW rotation detected  
    jmp(x_dec, "loop_high") # Decrement X  
    jmp("loop_high")  
 
    wrap()  
 
sm1 = StateMachine(1, encoder, freq=125_000_000, in_base=Pin(HALL_1_PIN), jmp_pin=Pin(HALL_2_PIN))  
sm1.active(1) # Start the mini program  
    

With this program, only 2 of the 4 possible states are registered per tooth (magnet) on the sensor disc, which is due to the limited possibilities of the state machines - I did not manage to accommodate all 4 states in 32 command lines. The number of pulses determined here is multiplied by 2 in the main program. Furthermore, the current state of the two sensors is read in so that the intermediate states can also be calculated (through micropython code, not with the high speed of a state machine).

The pulse width signal for controlling the servo is read in with the second state machine:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# Definition of second "State Machine"  
# that checks for incoming servo control PWM signal  
# Note that in_base pin must be identical to jmp_pin !!!  
 
@asm_pio(autopush=True, push_thresh=32)  
def servo_PWM_read():  
    label("start")  
    set(x, 0)  
 
    label("loop")  
    set(y, 0)  
    mov(y, invert(y))  
    wait(1, pin, 0)         # Wait until in_base pin goes HIGH  
    label("still_high")  
    jmp(y_dec, "plus_1")    # Pin went from LOW to HIGH, start countdown  
    label("plus_1")  
    jmp(pin, "still_high")  # if jmp_pin still high, continue counting  
    mov(y, invert(y))       # Pin went to LOW, no more counting  
    mov(x, y)               # Store number of counted loops in X  
    jmp("loop")  
 
    wrap()  
 
sm2 = StateMachine(2, servo_PWM_read, freq=125_000_000, in_base=Pin(PWM_READ), jmp_pin=Pin(PWM_READ))  
sm2.active(1) # Start the mini program  
    

The complete source code in Micropython is available in the download package (see below).


Download

The Download package (7.2MB) includes the software, the circuit diagram, as well as the 3D files of the mechanics.


<<< Digital ruler         Demonstration servo motor >>>


News    The Project    Technology    RoboSpatium    Contribute    Subject index    Archives    Download    Responses    Games    Links    Shopping tips    Contact    Imprint   





Twitter YouTube Hackaday Patreon TPO