Any help or guidance in making my own code blocks for a motor hat

I’m a middle school design teacher and I’m trying to do a project with my students to design some simple remote control vehicles. I have the Keyestudio Motor Hat for microbit and I was wondering if someone could help me figure out how to make some custom blocks for it.

Here’s some test code that successfully runs the two motor outputs:

I basically want:

a block that is an on/off slider to initiate the motors
a block that alternates the pins high/low to create forward and reverse
a block that has a slider to vary each motor’s speed.

Anyone have any suggestions how? I’m looking at documentation to create custom blocks and am trying to copy some other extensions with similar looking blocks but I’m admittedly a bit of a newb.


Hi @senorlloyd ,

That sounds like a great project!

I’ve created an example of new blocks. I’m not sure if it does what you want or need, but it might help you with the next step. I don’t have a Motor hat to do any testing, so this was partly some guess work based on your example. Also, your original example used P1 for speed for both motors. I guessed you may have meant P1 and P2. If not, my example will need some minor modification.

Here’s the example:

If it’s not quite what you want, you can modify the new blocks:

  1. Click on the JavaScript slider at the top.
  2. Below the simulated micro:bit is the word “Explorer” on a button with a down arrow. Click on the arrow, which will reveal all the files in the project.
  3. The code and definitions for the new blocks are in the custom.ts file. You can update the code as needed (and the icon, toolbox color, etc.)
  4. If you want to use different styles of blocks, you may want to explore the block creation Playground at . You can select the micro:bit and editor the select several examples to browse and modify. I think most examples show their blocks in the toolbox with the puzzlepiece icon.

Once you’re happy with the blocks you can either use the Share Project link to allow your kids to each open their own version (open the link and then edit it). Or you can share the blocks via GitHub.

A few resources that you may find helpful:

Hope that helps & good luck!

P.S. If things work out, please share some examples of the projects somehow

1 Like

Whoops…I should have set the initial motor speeds to the defaults shown in the block. Here’s a very minor update:

The previous version set speeds to 0, which may seem awkward if there’s only one block to turn motors on.

1 Like

I forgot to mention: The previous projects I shared look empty, but they contain a new toolbox for the new parts. Here’s an example that uses the blocks:

1 Like

Amazing, thanks for your help!! I’ll check it out and try them with the board and report back.

Ok, everything seems to be working almost perfect. There’s an issue with the “set motor block” where it shows too many variables.
Screen Shot 2023-01-20 at 2.13.17 PM
Selecting one of them works and the other throws an error on the microbit. I think I can investigate and figure out what is going on now that you did the bulk of the work. You’re a genius!! I’ll keep updating and try and get some video of the kids’ creations when we finish them.

1 Like

Great — I’m glad it looks promising. I’ve updated it to avoid the duplicate commands:

I think there were conflicts with other enumerations that also defined right and left.


1 Like

Oh man, I’ve been pulling my hair out. I ordered the one Keyestudio board from Amazon, got everything working, and then I bought 70 more imported from China. The first one I plugged in didn’t do anything.

After a few hours of trying to troubleshoot, I figured out that the Amazon unit I bought to test must have been old stock KS0308 while the ones I ordered from Keyestudio directly were a new model with different chips KS4033. They looked identical at first glance and the documentation was identical but obviously wrong.

After a bunch of trial and error, I figured out how to run them. It’s similar but now omits Pin14 as well as P1 and P2 which were setting the PWM. Overall it seems simpler but also harder now to create custom blocks unless I’m missing something.

Here’s the new code that demos switching direction at full speed:

Here’s a picture of the two boards. I wish they’d somehow been labeled with a part number or some other obvious difference.:smile::smile:

Hi @senorlloyd ,

Ugh. Sounds a bit messy.

I can’t dig up much information about the 4033.

It looks like it uses a TI DRV8833 chip.

What did you mean by the “harder now to create custom blocks”? It looks like the main difference is in how it drives the speed.

I’ve updated my test project…like before I’m making some guesses about how this behaves. I may have right/left and other details incorrect. None the less, here’s the update:


1 Like

Thanks!!! Yeah, I just meant that with their simplification of how the motors are controlled it gets easier to control without custom blocks.

Your’s work beautifully. Here’s a demo of the code in action, I’ll add to GitHub after I make a few tweaks do that others can use. Mind if I share with their developers? Should probably be added to their product wiki.

Awesome! Thanks for sharing the video — looks great! You are very welcome to share / post / pass on / create a repo / etc.

Is the frame laser cut? If so, it’d be nice if you share the pattern for it too.

Oh, you might want to add the word “speed” to the block that sets the motor speeds.