Servo sweep — Smart Car
A servo is a motor that goes to an angle and stays there, instead of just spinning. This one swings to 0°, then 90°, then 180°, and starts again.
This is one project for both firmware versions: when MoBot opens it, the blocks write the code for the firmware of your Smart Car.
The program
Section titled “The program”Read the program as text
- MoBot Start
- Print to serial
"Servo sweep. Watch the servo arm." - Initialize servo motors
- wait 1 seconds
- repeat forever
- Print to serial
"0 degrees - all the way one way" - Move servo 1 to angle 0
- wait 1.5 seconds
- Print to serial
"90 degrees - the middle" - Move servo 1 to angle 90
- wait 1.5 seconds
- Print to serial
"180 degrees - all the way the other way" - Move servo 1 to angle 180
- wait 1.5 seconds
- Print to serial
What you need
Section titled “What you need”- A Smart Car.
- A servo motor plugged into the SERVO 1 socket, with an arm or a flag screwed onto it so you can see which way it is pointing.
- A USB cable.
- A charged battery. Servos pull far more current than LEDs. On USB power alone a servo often twitches, buzzes and goes nowhere — that is a power problem, not a program problem.
How to run it
Section titled “How to run it”- Plug the servo into SERVO 1. Check the wire colours match the socket.
- Open 09-servo-sweep.mobot, plug in, press Send to Device.
- Watch the arm.
What you should see and hear
Section titled “What you should see and hear”Servo sweep. Watch the servo arm.once, then a one-second pause while the servos wake up.- The arm swings to one end, waits, moves to the middle, waits, swings to the other end, waits — then jumps back and does it again.
- Each position is held for 1.5 seconds and the serial monitor names the angle it is going to before it moves.
- A soft buzz while it moves is normal. A constant loud buzz while it is not moving means it cannot reach the angle it was asked for — something is blocking the arm, or the power is too weak.
The test passes if the arm visits three clearly different positions and the middle one really is half way between the other two.
The important block
Section titled “The important block”Initialize servo motors must run once, before any Move servo block. Delete it and the program stops with an error the moment it tries to move anything — the servo pins have not been claimed yet. It is the one setup block in the robotics set.
The blocks
Section titled “The blocks”| block | what it does |
|---|---|
mobot_servoinit |
wakes up servo sockets 1, 2, 3 and 4. Run it once, at the top. |
mobot_servowrite |
sends servo 1, 2, 3 or 4 to an angle. Use 0-180. |
repeat_forever, wait, mobot_print |
the sweep and the commentary. |
The Python code
Section titled “The Python code”The same blocks write different Python for each firmware, and MoBot picks the one that fits the robot you use. The 2.x tab is for reference. This is a reading copy: editing it changes nothing, so change the blocks instead. The name map lists the names that changed.
# Servo sweep — robotics (firmware 3.0)## This is what MoBot puts on a robot running firmware 3.0 as main.py when you open# 09-servo-sweep.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.# The firmware 2.x version is 09-servo-sweep.main.py.
from machine import Pinimport time
print("[MOBOT]", 'Servo sweep. Watch the servo arm.')robot.enable_servos()time.sleep(1)while True: print("[MOBOT]", '0 degrees - all the way one way') robot.servo1.set_angle(0) time.sleep(1.5) print("[MOBOT]", '90 degrees - the middle') robot.servo1.set_angle(90) time.sleep(1.5) print("[MOBOT]", '180 degrees - all the way the other way') robot.servo1.set_angle(180) time.sleep(1.5)# Servo sweep — robotics## This is what MoBot puts on the board as main.py when you open# 09-servo-sweep.mobot and press Send to Device. It is GENERATED from the blocks;# editing this file changes nothing. Edit the blocks instead.
from machine import Pinimport time
print("[MOBOT]", 'Servo sweep. Watch the servo arm.')mobotROBOTICS.init_servo()time.sleep(1)while True: print("[MOBOT]", '0 degrees - all the way one way') mobotROBOTICS.servo1.move(0) time.sleep(1.5) print("[MOBOT]", '90 degrees - the middle') mobotROBOTICS.servo1.move(90) time.sleep(1.5) print("[MOBOT]", '180 degrees - all the way the other way') mobotROBOTICS.servo1.move(180) time.sleep(1.5)Try changing it
Section titled “Try changing it”- Plug a second servo into SERVO 2 and add blocks that move it the opposite way — one goes to 0 while the other goes to 180.
- Add angles in between: 0, 45, 90, 135, 180. The sweep gets much smoother.
- Make every
wait0.3 and watch it get frantic. Servos do not like being rushed; you may hear it complain.