pico-micropython-examples/pio/pio_servo
Thomas A f27cda6cf9 Update README.adoc
Document concept.
2021-04-08 15:08:45 +02:00
..
README.adoc Update README.adoc 2021-04-08 15:08:45 +02:00
pio_servo.fzz Add documentation, clean up comments 2021-04-08 14:55:52 +02:00
pio_servo.png Add documentation, clean up comments 2021-04-08 14:55:52 +02:00
pio_servo.py Add Trigger/Program relationship to Readme and example 2021-04-08 15:00:34 +02:00

README.adoc

= Using PIO to drive a servo motor
:xrefstyle: short

Drive a servo motor by using two PIO statemachines for pulse output.

== Concept

Servo motors are controlled by a pulse with modulated signal.
Please see Wikipedia for details: https://en.wikipedia.org/wiki/Servo_control

The angle is encoded by the pulse length. The positive pulse length from 1ms to 2ms describes the movement from minimum to maximum.
This pulse has to be repeated every 20ms.

This pulseform is generated by two different PIO statemachines.

Statemachine 1, running the servo_trigger program generates the 20ms carrier signal.
It counts down for 20ms, then clears the IRQ, allowing the 2nd statemachine to run.

Statemachine 2, running servo_prog waits for the IRQ to be cleared.
The cleared IRQ marks the beginning of a 20ms cycle, which means, that the pulse needs to be output.
After this, the statemachine outputs a pulse of 1 to 2 milliseconds.
The duration of this pulse can be input from the FIFO.
If a value of 0 is pushed to the FIFO, the output pulse will have 1ms, which is the lowest movevement position of the servo.
Pushing a value of 1000 to the FIFO outputs a 2ms pulse, which moves the servo to the maximum position.

After the pulse has been output, Statemachine 2 waits for Statemachine 1 to clear the IRQ again, so the next pulse can be output.

== PIO Use

Each output needs a statemachine running servo_trigger and one running servo_prog.
Due to the IRQ assignments, the statemachines need to be in ordered to have the servo_trigger program first, e.g.:

```
trig = Servo_Trigger(0)                 # Statemachine 0
s = Servo(1, 16) # phys IO on pin 16    # Statemachine 1, since the trigger is on SM0

trig2 = Servo_Trigger(2)                # Statemachine 2
s2 = Servo(3, 25) # Builtin LED         # Statemachine 3, since the trigger is on SM2
```

== Wiring information

See <<servo-wiring-diagram>> for wiring instructions.

[[servo-wiring-diagram]]
[pdfwidth=75%]
.Wiring the servo to Pico
image::pio_servo.png[]

== List of Files

A list of files with descriptions of their function;

pio_servo.py:: The example code, driving one servo

== Bill of Materials

.A list of materials required for the example
[[ring-bom-table]]
[cols=3]
|===
| *Item* | *Quantity* | Details
| Breadboard | 1 | generic part
| Raspberry Pi Pico | 1 | http://raspberrypi.org/
| Servo | 1 | generic part
|===