Powered by TinkerSpace
Join free
TinkerPy Tutorial 📘 Part 2 · Coder · Chapter 16 of 24

Servos & Robotics

TinkerPy has 13 servo functions and 3 robot functions. With them a servo can open a gate with one press, sweep like a radar all by itself, or copy another servo — and a robot car can drive, spin, curve and dance.

📖Key words

Servo

A motor that turns to an exact angle from 0° to 180° and holds it. Pick Servo in Circuit Connections: gate = set_output(servo, 0) also sets its start angle.

servo_flip()

Each new press moves the servo the other way: start → end, then end → start. Holding the button does not move it again.

servo_scan_on()

Starts a servo sweeping back and forth forever, on its own — even while the rest of the code is busy or waiting.

robot_control()

Drives both wheels in one line: robot_control(left dir, left speed, right dir, right speed). Directions: "F", "B", "S" stop, "X" leave alone.

motor_control_right() / _left()

Drives just one wheel: motor_control_right("F", 70).

🎬See it work

Point the servo

Drag the slider to choose an angle and watch the servo arm and the code change.

Drive the robot

Tap the buttons to drive around the floor. See which robot_control() line makes each move.

🧠How it works

Moving a servo

  • servo_write_angle(arm, 90) — jump straight to an angle (0 one side, 90 the middle, 180 the other side).
  • servo_increment(arm, 10, 170, 100) / servo_decrement(arm, 10, 10, 100) — move once by a step, never past the limit; the move takes 100 ms.
  • servo_increment_till(arm, 90, 180, 5, 20) / servo_decrement_till(arm, 180, 0, 5, 20) — jump to the start angle, then move smoothly to the end, 5° every 20 ms.
  • servo_oscillate(arm, 90, 30, 150, 5, 15) — home 90°, sweep to 30°, then to 150°, then back home. Once.

Buttons, scans and reading back

  • servo_flip(gate, 0, 90, 5, 20) under if button == ON: — press to open, press again to close (firmware 2.14+).
  • servo_scan_on(dish, 0, 180, 5, 30) sweeps forever by itself. servo_scan_stop(dish) freezes it; servo_scan_resume(dish) carries on from the same angle and direction.
  • servo_read_angle(gate) tells you where the servo is right now, even in the middle of a move or a scan.

Two servos as a team

  • servo_sync(dish, dish2) — from now on dish2 copies every move of dish.
  • servo_sync_reverse(left_arm, right_arm) — the second one moves the opposite way: its angle is always 180 minus the first (30° ↔ 150°).
  • Perfect for two radar dishes, a pair of robot arms, or legs that step in opposite directions.

Robot cars

  • Switch on Right motor (o3/o4) and Left motor (o5/o6) in Circuit Connections: it writes import motor and four set_output lines. A wheel turns the wrong way? Swap its P and N.
  • Both "F" = straight · "B", "F" = spin left · "F", "B" = spin right · one wheel slower = a curve.
  • "S" stops a wheel; "X" leaves it exactly as it was.
  • motor_control_right() and motor_control_left() drive one wheel each — the same pins robot_control uses.

💻Code

# Add the libraries you need
import servo

# Give your pins easy-to-remember names
i1 = set_name("button")
o1 = set_name("gate")
o2 = set_name("open_led")

# ===== Automatic gate =====

# This code block executes only once
setup:
    button = set_input(push_button, active_low)
    gate = set_output(servo, 0)
    open_led = set_output(led)

# This code block executes forever
loop:
    if button == ON:  # when button is ON
        servo_flip(gate, 0, 90, 5, 20)  # 1st press opens, 2nd press closes
    if servo_read_angle(gate) > 45:  # when servo_read_angle(gate) > 45
        open_led = ON  # turn open_led ON
    else:  # otherwise
        open_led = OFF  # turn open_led OFF

An automatic gate with servo_flip: the first press of button opens the gate (0° → 90°), the next press closes it. servo_read_angle(gate) tells the program where the gate is, so open_led glows while it is more than half open. The start angle 0 is in set_output(servo, 0).

# Add the libraries you need
import servo

# Give your pins easy-to-remember names
i1 = set_name("up_button")
i2 = set_name("down_button")
o1 = set_name("arm")

# ===== Crane arm =====

# This code block executes only once
setup:
    up_button = set_input(push_button, active_low)
    down_button = set_input(push_button, active_low)
    arm = set_output(servo, 90)
    servo_increment_till(arm, 90, 180, 5, 20)  # wake-up stretch: 90° up to 180°
    servo_decrement_till(arm, 180, 0, 5, 20)  # then all the way down to 0°
    servo_write_angle(arm, 90)  # back to the middle

# This code block executes forever
loop:
    if up_button == ON:  # when up_button is ON
        servo_increment(arm, 10, 170, 100)  # up 10°, never past 170°
    elif down_button == ON:  # otherwise, when down_button is ON
        servo_decrement(arm, 10, 10, 100)  # down 10°, never below 10°
    wait(100)  # pause here for 100 milliseconds before continuing

A crane arm. At start-up it stretches up with servo_increment_till, down with servo_decrement_till and returns to the middle with servo_write_angle. Then two buttons nudge it up and down 10° at a time — never past 170° or below 10°.

# Add the libraries you need
import ultrasonic
import servo

# Give your pins easy-to-remember names
o1 = set_name("dish")
o2 = set_name("dish2")
o5 = set_name("alert")

# ===== Twin radar scanner =====

# This code block executes only once
setup:
    i3 = set_input(ultrasonic_trig)  # cannot be changed
    i4 = set_input(ultrasonic_echo)  # cannot be changed
    dish = set_output(servo, 90)
    dish2 = set_output(servo, 90)
    alert = set_output(led)
    servo_sync(dish, dish2)  # dish2 copies every move of dish
    servo_scan_on(dish, 0, 180, 5, 30)  # keep sweeping on its own

# This code block executes forever
loop:
    d = distance()  # -1 when nothing is in range
    if d > 0 and d < 20:  # when d > 0 and d < 20
        servo_scan_stop(dish)  # freeze, pointing at the object
        alert = ON  # turn alert ON
    else:  # otherwise
        servo_scan_resume(dish)  # carry on from the same place
        alert = OFF  # turn alert OFF

Twin radar dishes (TinkerPy Mega: the ultrasonic uses i3 and i4). servo_scan_on keeps dish sweeping on its own and servo_sync makes dish2 copy it. When something is closer than 20 cm the scan freezes and alert lights; when it goes away the scan resumes from the same place.

# Add the libraries you need
import motor
import servo

# Give your pins easy-to-remember names
o1 = set_name("left_arm")
o2 = set_name("right_arm")

# ===== Robot dance routine =====

# This code block executes only once
setup:
    o3 = set_output(right_motor_P)
    o4 = set_output(right_motor_N)
    o5 = set_output(left_motor_P)
    o6 = set_output(left_motor_N)
    left_arm = set_output(servo, 90)
    right_arm = set_output(servo, 90)
    servo_sync_reverse(left_arm, right_arm)  # arms always mirror each other

# This code block executes forever
loop:
    robot_control("F", 70, "F", 70)  # forward
    wait(1000)  # pause here for 1000 milliseconds before continuing
    robot_control("B", 70, "F", 70)  # spin left
    wait(600)  # pause here for 600 milliseconds before continuing
    robot_control("F", 40, "F", 90)  # curve left
    wait(1000)  # pause here for 1000 milliseconds before continuing
    robot_control("S", 0, "S", 0)  # stop and wave
    servo_oscillate(left_arm, 90, 30, 150, 5, 15)  # oscillate left_arm from angle 30 to 150, start and stop pos: angle 90
    motor_control_right("F", 70)  # only the right wheel: pivot left
    wait(700)  # pause here for 700 milliseconds before continuing
    motor_control_right("S", 0)  # stop the right wheel
    motor_control_left("B", 70)  # only the left wheel, backwards
    wait(700)  # pause here for 700 milliseconds before continuing
    motor_control_left("S", 0)  # stop the left wheel

A robot dance routine: forward, spin left, curve left, stop and wave, then pivot on one wheel and the other. servo_sync_reverse makes right_arm mirror left_arm while it oscillates. Tune the wait times on your own floor.

Open TinkerPy Code

🎉Fun examples

🚧Toll gates

A toll-plaza barrier lifts and drops between two angles — a job made for servo_flip.

📡Real radar dishes

Airport and weather radars sweep round and round while computers watch the echoes — servo_scan_on does a small version.

🚜Tank turns

Tanks and many robots turn on the spot by running one side forward and the other backward — exactly robot_control("B", 70, "F", 70).

🌍Where it’s used

🛠️Project ideas

Automatic gate

Build a cardboard school gate on a servo. Use servo_flip with a button, and add an LED that shows when the gate is open using servo_read_angle.

🧰 You need: TinkerPy board, servo, push button, LED, cardboard
Robot dance routine

Program your robot car to dance to a song: forward, spin left, spin right, curve, stop — repeat it 3 times. Add two servo arms with servo_sync_reverse that wave at the end.

🧰 You need: TinkerPy robot car kit (motors on o3–o6), 2 servos

⭐Points to remember

  1. A servo turns to an exact angle from 0° to 180°.
  2. servo_write_angle(name, angle) jumps to an angle.
  3. servo_increment / servo_decrement move once, with a limit.
  4. _till versions move smoothly from a start to an end angle.
  5. servo_oscillate sweeps to two angles and comes back home.
  6. servo_flip: each new press goes the other way.
  7. servo_scan_on sweeps forever by itself.
  8. servo_scan_stop freezes; servo_scan_resume carries on.
  9. servo_read_angle gives the angle right now.
  10. servo_sync copies; servo_sync_reverse mirrors (180 minus).
  11. Robot motors: right o3/o4, left o5/o6.
  12. robot_control(left dir, left speed, right dir, right speed).
  13. "F" forward, "B" backward, "S" stop, "X" leave alone.
  14. motor_control_right / _left drive one wheel only.
🤯
Did you know?

Mars rovers are driven by motors and turn their cameras and arms with joints controlled by code that is sent all the way from Earth.

❓Mini quiz

🔑
Log in to try the mini quiz

5 quick questions to check yourself — free with your TinkerPy account.

Log in