MBC Servo6 6-Port Servo Expander (for LEGO® SPIKE™) × Pybricks Blocks

Control 6 servos with Pybricks blocks in three steps

Blocks follow the same order as MicroPython: first import the library under "set up", then create an object and name the expander, and last, under "program", use that name to call functions. Work through the steps on the left, and the blocks on the right grow with you. These block pictures were built and exported straight from Pybricks Code, so they look just like your screen.

Check first: Pybricks block coding needs a licence code or a Patreon membership. Without one, you can write MicroPython instead. It works exactly the same (switch the right side to "Matching MicroPython" to see it). This expander only works with SPIKE Prime, and the hub needs Pybricks firmware v4.0 or later.

1

Import the library

The first thing in a block program is a red "from … import …" block under the yellow "set up" block. It tells Pybricks to use the Servo6 library.

  1. Download the library (save the file and keep its name): MBC_servo6_Lib.py. The file name is MBC_servo6_Lib.py.
  2. In Pybricks Code, click "Import a file" in the file area on the left. Add it to the same project, and do not rename the file.
  3. Click "+" to add a new file, and choose blocks.
  4. Drag the red "from … import …" block from the block list and attach it under "set up". Pick MBC_servo6_Lib and MBC_SERVO6.
2

Create an object: name the expander

Drag a red "… is a …" block and attach it under the import block. On the far left is the name you give the expander. After it come the hub port and whether to multitask. From then on, every time you turn a servo or read the current, you pick this name.

Which hub port is the expander plugged into?

Give it a name (in Pybricks you can use any English name; here are three examples):

Turn on multitasking? (the green true/false on the block)

The port box takes a number. On SPIKE, 1 to 6 = A to F, so 3 means Port C.

The first time you run it, the computer asks Custom electronics may damage your LEGO hub ... Press Y to proceed. Press Y (only once; the hub remembers). This lets the hub power the servos through the port. If you press N, the servos get no power.

3

Use the functions

Under "program", place red function blocks. In the first box, pick the name servo6. In the middle, pick await. In the last box, pick the function. The first number box after it is the servo port 1 to 6 on the expander (CN1 to CN6, not the hub port). Click a function, and the blocks and output switch to its example.

Simulated expander (one servo on each of the 6 servo ports): watch the program turn the arms, or drag an arm with the mouse

The simulator is only a sketch: turning speed and current are simplified, and real numbers depend on the servo model. Servos 1 and 3 to 6 are standard 180-degree servos. Servo 2 is a 360-degree continuous rotation servo. Orange arm = active (pushing), grey = released, red = stalled. Drag an active arm and the servo pushes back, so the current goes up; let go and it turns back. A released arm stays wherever you drag it.

Block examples for every function

15 functions, each with a set of blocks you can copy (name servo6, multitask true). Press "Try it" to jump to step 3. The blocks and output on the right switch to that example, and the simulated expander runs it too.

Good to know

  1. Import the library file into the project first. Without MBC_servo6_Lib.py in the project, you cannot pick MBC_SERVO6 in the red blocks.
  2. await and call must match multitask: multitask true means choose await; multitask false means choose call. If you mix them, the program fails on the hub.
  3. Behind the blocks is MicroPython. The code pane on the right of Pybricks Code shows the program the blocks make. It matches "Matching MicroPython" on this page (the multitask version has one extra line, await wait(0), which Pybricks adds by itself).
  4. Unplugging does not stop the program: when the expander cannot be read, the reading functions return 0 or False, and servo commands are skipped. The console prints [SERVO6] … / Servo board disconnected, then "No servo board detected" (the library prints each message in Chinese and English). Plug it back in and it reconnects by itself. Programs that only send commands also reconnect and resend the settings. While unplugged, servo_wait moves on after about 0.4 seconds instead of getting stuck.
  5. After a servo stalls and is released automatically, sending the same angle again in the loop does not make it push again (the expander skips a command exactly like the one before). Send a different angle, or put servo_release first. When the program stops, the block version does not release the servos by itself (the expander has no watchdog, so the servos stay at their last angle). To release them, put a stop_all block at the end of the program.