Servo arm and joystick — Smart Car (Python)
This example is written twice, once for each firmware. Pick the tab that matches the firmware of your Smart Car: the tab you pick is remembered on the other examples.
Firmware 3.0 renamed the Python names a program writes, and kept none of the old ones: a Smart Car on 2.x does not know the 3.0 names, and a Smart Car on 3.0 does not know the 2.x ones. The name map lists every change. If you open the example from inside MoBot, it picks the version for the firmware of your Smart Car. If you download a file from this page, use the one in the tab for your firmware.
Two add-ons the robot keeps switched off until you ask. The servo sweeps on its own, then you steer it live with a joystick.
There is a servo block (Init Servo and Set servo angle). There is no
block at all for the joysticks, and no block for center() or off() — so
the second half of this program cannot be built out of blocks.
This is the version for a robot on firmware 3.0. For a robot that still runs firmware 2.x, MoBot opens the 2.x version of this example instead.
Project for firmware 3.0: 04-servo-and-joystick.firmware-3.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A Smart Car.
- A hobby servo (SG90 or similar) plugged into the SERVO 1 socket (GPIO 32). More servos in sockets 2, 3, 4 (GPIO 33, 25, 26) if you have them.
- A joystick module in JOYSTICK 1 (VRX → GPIO 39, VRY → GPIO 36).
- A USB cable and the serial monitor.
How to run it
Section titled “How to run it”- Open 04-servo-and-joystick.firmware-3.mobot and press Run.
- Watch the servo horn for the first twenty seconds.
- When the monitor says so, push joystick 1 left and right.
What you should see and hear
Section titled “What you should see and hear”- The servo sweeps smoothly from one end to the other and back, twice, then returns to the middle.
- Then it goes limp — you can push the horn with a finger and it stays where
you put it. That is
off(). - If you have servos in the other sockets, each one flicks to 45° and back in turn. Empty sockets do nothing at all, which is fine.
- In part 2, pushing the joystick left and right moves the servo, and the monitor prints the raw numbers: about 0 at one end, 1023 at the other, and roughly 512 when you let go.
If the servo buzzes and shakes instead of moving, it is not getting enough power — a servo wants the battery, not the USB cable.
The trap this example exists to show
Section titled “The trap this example exists to show”These pins are shared. The five line-following eyes underneath the robot sit on GPIO 36, 39, 34, 35, 32. The joysticks want 39, 36 and 35, 34. Servo 1 wants 32.
So the moment you call robot.enable_joysticks(), the line-following eyes stop
being line-following eyes. Nothing warns you and nothing crashes — the line
readings just quietly become nonsense. A line-follower with a joystick plugged
in cannot work, and this is not a wiring mistake you made; it is how the board
is laid out. When your program ends, the robot sets its parts up again and the
eyes work once more.
The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — Servo arm and joystick (Python) (robotics)## A reading copy of one file inside 04-servo-and-joystick.firmware-3.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.# Written for firmware 3.0; the 2.x version is 04-servo-and-joystick.main.py.
"""Servo arm and joystick - MoBot Smart Car.
Two add-ons that are switched OFF when the robot boots, because their pins arebeing used by something else. You have to ask for them, and asking costs youthe thing that was there before:
robot.enable_servos() wakes servo1..servo4 on pins 32, 33, 25, 26 robot.enable_joysticks() wakes joystick1 and joystick2 on pins 39/36 and 35/34
The five line-following eyes live on pins 36, 39, 34, 35 and 32 - the SAMEpins. So a robot with joysticks plugged in cannot follow a line at the sametime, and neither can a robot using servo1. Choose one.
Part 1 sweeps the servo on its own.Part 2 lets you steer the servo with a joystick, live."""
import time
CENTRE = 512 # a joystick sitting still reads about half of 0..1023DEADZONE = 80 # ignore small wobbles
def to_angle(reading): """Turn a joystick reading (0..1023) into a servo angle (0..180).""" angle = reading * 180 // 1023 if angle < 0: angle = 0 if angle > 180: angle = 180 return angle
# ---------------------------------------------------------------- part 1 ----
print("Waking the servos up...")robot.enable_servos()
arm = robot.servo1if arm is None: print("servo1 did not start. Is anything plugged into pin 32?") raise SystemExit
print("Part 1: sweeping. Watch the horn on servo 1.")arm.center() # 90 degrees, straight aheadtime.sleep(1)
for _ in range(2): for angle in range(0, 181, 10): arm.set_angle(angle) time.sleep(0.03) for angle in range(180, -1, -10): arm.set_angle(angle) time.sleep(0.03)
arm.center()time.sleep(0.5)
# off() lets the servo go limp - it stops holding its position and stops# buzzing. Useful when you are finished with it.arm.off()print("Servo let go. You can push the horn by hand now.")time.sleep(1)
# All four at once, if you have them plugged in.print("Waving all four servos (the empty sockets simply do nothing).")for servo in robot.servos: if servo is not None: servo.set_angle(45) time.sleep(0.3) servo.center()
# ---------------------------------------------------------------- part 2 ----
print("")print("Waking the joysticks up. From here on the line-following eyes are OFF.")robot.enable_joysticks()
stick = robot.joystick1if stick is None: print("joystick1 did not start.") raise SystemExit
print("Part 2: push joystick 1 left and right. The servo follows for 20 seconds.")
for _ in range(200): x = stick.x() y = stick.y() if abs(x - CENTRE) > DEADZONE: angle = to_angle(x) arm.set_angle(angle) print("x", x, " y", y, " -> servo", angle) time.sleep(0.1)
arm.center()arm.off()print("Finished. When the program ends, the line-following eyes get their pins back.")Try changing it
Section titled “Try changing it”- Use
yinstead ofxin part 2 and steer with up/down. - Make a two-axis arm:
robot.servo2.set_angle(to_angle(y)). - Print
stick.x()on its own and find your joystick’s real centre — it is rarely exactly 512.
Two add-ons the robot keeps switched off until you ask. The servo sweeps on its own, then you steer it live with a joystick.
There is a servo block (Init Servo and Set servo angle). There is no
block at all for the joysticks, and no block for center() or off() — so
the second half of this program cannot be built out of blocks.
Project for firmware 2.x: 04-servo-and-joystick.mobot. Open it in MoBot.
What you need
Section titled “What you need”- A Smart Car.
- A hobby servo (SG90 or similar) plugged into the SERVO 1 socket (GPIO 32). More servos in sockets 2, 3, 4 (GPIO 33, 25, 26) if you have them.
- A joystick module in JOYSTICK 1 (VRX → GPIO 39, VRY → GPIO 36).
- A USB cable and the serial monitor.
How to run it
Section titled “How to run it”- Open 04-servo-and-joystick.mobot and press Run.
- Watch the servo horn for the first twenty seconds.
- When the monitor says so, push joystick 1 left and right.
What you should see and hear
Section titled “What you should see and hear”- The servo sweeps smoothly from one end to the other and back, twice, then returns to the middle.
- Then it goes limp — you can push the horn with a finger and it stays where
you put it. That is
off(). - If you have servos in the other sockets, each one flicks to 45° and back in turn. Empty sockets do nothing at all, which is fine.
- In part 2, pushing the joystick left and right moves the servo, and the monitor prints the raw numbers: about 0 at one end, 1023 at the other, and roughly 512 when you let go.
If the servo buzzes and shakes instead of moving, it is not getting enough power — a servo wants the battery, not the USB cable.
The trap this example exists to show
Section titled “The trap this example exists to show”These pins are shared. The five line-following eyes underneath the robot sit on GPIO 36, 39, 34, 35, 32. The joysticks want 39, 36 and 35, 34. Servo 1 wants 32.
So the moment you call init_joysticks(), the line-following eyes stop being
line-following eyes. Nothing warns you and nothing crashes — the IR readings just
quietly become nonsense. A line-follower with a joystick plugged in cannot
work, and this is not a wiring mistake you made; it is how the board is laid
out. Reset the robot to get the eyes back.
The program
Section titled “The program”This is a reading copy of the program in the project: editing it changes nothing, so open the project and edit it there.
# main.py — Servo arm and joystick (Python) (robotics)## A reading copy of one file inside 04-servo-and-joystick.mobot.# Editing this copy changes nothing: open the project and edit it there.# This is the entry point — the board runs it as main.py.
"""Servo arm and joystick - MoBot Smart Car.
Two add-ons that are switched OFF when the robot boots, because their pins arebeing used by something else. You have to ask for them, and asking costs youthe thing that was there before:
init_servo() wakes servo1..servo4 on pins 32, 33, 25, 26 init_joysticks() wakes joystick1 and joystick2 on pins 39/36 and 35/34
The five line-following eyes live on pins 36, 39, 34, 35 and 32 - the SAMEpins. So a robot with joysticks plugged in cannot follow a line at the sametime, and neither can a robot using servo1. Choose one.
Part 1 sweeps the servo on its own.Part 2 lets you steer the servo with a joystick, live."""
import time
CENTRE = 512 # a joystick sitting still reads about half of 0..1023DEADZONE = 80 # ignore small wobbles
def to_angle(reading): """Turn a joystick reading (0..1023) into a servo angle (0..180).""" angle = reading * 180 // 1023 if angle < 0: angle = 0 if angle > 180: angle = 180 return angle
# ---------------------------------------------------------------- part 1 ----
print("Waking the servos up...")mobotROBOTICS.init_servo()
arm = mobotROBOTICS.servo1 # mobotROBOTICS.servo is another name for this oneif arm is None: print("servo1 did not start. Is anything plugged into pin 32?") raise SystemExit
print("Part 1: sweeping. Watch the horn on servo 1.")arm.center() # 90 degrees, straight aheadtime.sleep(1)
for _ in range(2): for angle in range(0, 181, 10): arm.move(angle) time.sleep(0.03) for angle in range(180, -1, -10): arm.move(angle) time.sleep(0.03)
arm.center()time.sleep(0.5)
# off() lets the servo go limp - it stops holding its position and stops# buzzing. Useful when you are finished with it.arm.off()print("Servo let go. You can push the horn by hand now.")time.sleep(1)
# All four at once, if you have them plugged in.print("Waving all four servos (the empty sockets simply do nothing).")for servo in mobotROBOTICS.servos: if servo is not None: servo.move(45) time.sleep(0.3) servo.center()
# ---------------------------------------------------------------- part 2 ----
print("")print("Waking the joysticks up. From here on the line-following eyes are OFF.")mobotROBOTICS.init_joysticks()
stick = mobotROBOTICS.joystick1if stick is None: print("joystick1 did not start.") raise SystemExit
print("Part 2: push joystick 1 left and right. The servo follows for 20 seconds.")
for _ in range(200): x = stick.readX() y = stick.readY() if abs(x - CENTRE) > DEADZONE: angle = to_angle(x) arm.move(angle) print("x", x, " y", y, " -> servo", angle) time.sleep(0.1)
arm.center()arm.off()print("Finished. Reset the robot to get the line-following eyes back.")Try changing it
Section titled “Try changing it”- Use
yinstead ofxin part 2 and steer with up/down. - Make a two-axis arm:
mobotROBOTICS.servo2.move(to_angle(y)). - Print
stick.readX()on its own and find your joystick’s real centre — it is rarely exactly 512.