Skip to content

Commit 23ec884

Browse files
committed
Add more servo examples, close gpiozero#758
1 parent 70c5392 commit 23ec884

File tree

6 files changed

+76
-0
lines changed

6 files changed

+76
-0
lines changed

docs/examples/angular_servo.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from gpiozero import AngularServo
2+
from time import sleep
3+
4+
servo = AngularServo(17, min_angle=-90, max_angle=90)
5+
6+
while True:
7+
servo.angle = -90
8+
sleep(2)
9+
servo.angle = -45
10+
sleep(2)
11+
servo.angle = 0
12+
sleep(2)
13+
servo.angle = 45
14+
sleep(2)
15+
servo.angle = 90
16+
sleep(2)

docs/examples/servo_1.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from gpiozero import Servo
2+
from time import sleep
3+
4+
servo = Servo(17)
5+
6+
while True:
7+
servo.min()
8+
sleep(2)
9+
servo.mid()
10+
sleep(2)
11+
servo.max()
12+
sleep(2)

docs/examples/servo_2.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from gpiozero import Servo, Button
2+
3+
servo = Servo(17)
4+
btn = Button(14)
5+
6+
while True:
7+
servo.min()
8+
btn.wait_for_press()
9+
servo.max()
10+
btn.wait_for_press()

docs/examples/servo_sweep.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from gpiozero import Servo
2+
from gpiozero.tools import sin_values
3+
4+
servo = Servo(17)
5+
6+
servo.source = sin_values()
7+
servo.source_delay = 0.1

docs/recipes.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,26 @@ Run a function when something gets near the sensor:
377377

378378
.. literalinclude:: examples/distance_sensor_2.py
379379

380+
Servo
381+
=====
382+
383+
Control a servo between its minimum, mid-point and maximum positions in
384+
sequence:
385+
386+
.. literalinclude:: examples/servo_1.py
387+
388+
Use a button to control the servo between its minimum and maximum positions:
389+
390+
.. literalinclude:: examples/servo_2.py
391+
392+
Automate the servo to continuously slowly sweep:
393+
394+
.. literalinclude:: examples/servo_sweep.py
395+
396+
Use :class:`AngularServo` so you can specify an angle:
397+
398+
.. literalinclude:: examples/angular_servo.py
399+
380400
Motors
381401
======
382402

gpiozero/output_devices.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,7 @@ class Servo(SourceMixin, CompositeDevice):
14611461
from time import sleep
14621462
14631463
servo = Servo(17)
1464+
14641465
while True:
14651466
servo.min()
14661467
sleep(1)
@@ -1469,6 +1470,16 @@ class Servo(SourceMixin, CompositeDevice):
14691470
servo.max()
14701471
sleep(1)
14711472
1473+
You can also use the :attr:`value` property to move the servo to a
1474+
particular position, on a scale from -1 (mid) to 1 (max) where 0 is the
1475+
mid-point::
1476+
1477+
from gpiozero import Servo
1478+
1479+
servo = Servo(17)
1480+
1481+
servo.value = 0.5
1482+
14721483
:type pin: int or str
14731484
:param pin:
14741485
The GPIO pin that the servo is connected to. See :ref:`pin-numbering`

0 commit comments

Comments
 (0)