micro:bit Python Simulator

My digital forensics and cyber security club (mostly high ability Y9s) loved seeing their python code scroll their names across the screens and soon graduated to setting each other code challenges.

A bunch of Y13s revising data structures loved being able to define and display custom images.

But I can’t use them yet with a whole class – I’ve only got 5. Even when I’ve got enough to give away to Y7s, I doubt it’ll be long before one gets left at home, broken (or sold on eBay!)

So how can I use them in class? There’s no doubt that the excitement factor goes up when students see their code on a physical device rather than just on screen. But it’s also hard do deny that boredom and frustration would be an issue if students had to wait to try out their code because of a limited number of devices.

You can currently program a micro:bit in any of four languages: CodeKingdoms (JavaScript), Microsoft Block Editor, Microsoft TouchDevelop and Python. Three out of the four languages offer a device simulator so that you can test your code without a physical micro:bit but the one I’m interested in – python – currently doesn’t.

The other languages are great, but python is the real game-changer for use in school as it can build on all of the python projects and challenges we already teach at KS3, so it’s a real shame there isn’t a python simulator (yet) on the official site.

To fill the gap, I’ve started making a micro:bit simulator for create.withcode.uk that allows you to test your micro:python code before (or without) putting it onto a micro:bit.

It’s not finished yet, but so far you can control the display and the buttons so it’s enough to get started and I hope to add the rest of the micro:python library support as soon as I can.

Why bother with a simulator?

I know that running code on an actual physical device is much more fun and rewarding than simulating it on a desktop/notebook/tablet but aside from situations when you don’t have access to a micro:bit, being able to quickly test, debug and run line by line can be really useful and streamline the development and learning process.

Using a simulator on create.withcode.uk also means it’s easier to share and run code, making the coding process more collaborative and fun.

If you happen to know someone (or if you are) from the BBC and can advise me about the licensing / legal aspect of this – I’d love to hear from you. I don’t want to infringe on any intellectual property rights but I do want my students and others to be able to create with code.

17 thoughts on “micro:bit Python Simulator

  • February 18, 2017 at 9:59 am
    Permalink

    Thanks so much for this awesome tool! Is it open source? I would love to check out the source code and see if I can play around with some functionalities.

    Reply
  • July 8, 2017 at 2:58 pm
    Permalink

    This is very helpful! Thank you very much.

    Reply
    • November 20, 2017 at 7:45 pm
      Permalink

      You’re welcome-thanks for trying it out & hope it’s useful 🙂

      Reply
  • December 31, 2017 at 3:28 pm
    Permalink

    Thank you for creating this. You are right about there being something more engaging about seeing the code running on a physical device, but this utility has just saved me a massive amount of time debugging some code with my son. Waiting for an error message to scroll by on the LED screen and then re-uploading after each change is simply painful.

    Thanks!

    Reply
    • December 31, 2017 at 4:29 pm
      Permalink

      You’re welcome-pleased it’s been useful.

      Reply
    • December 31, 2017 at 5:47 pm
      Permalink

      Out of interest, what’s the project you’re working on with your son? Post a link to the code here if he’s willing to share for others to enjoy 🙂 Happy New year.

      Reply
  • May 4, 2018 at 8:50 pm
    Permalink

    Thank you for doing this. Debugging is sooo much faster! I’ve just tried to get the file system working e.g.

    with open(fileName) as h:
    a = h.read()
    return int(a)

    but I run into a TypeError: Cannot read property ‘constructor’ of undefined. I guess it’s a limitation of the emulator as that is the documented way to access file content?

    Thanks again

    Reply
    • May 4, 2018 at 9:54 pm
      Permalink

      Thanks Shaun. Sorry, yes, the python simulator doesn’t support with blocks yet. You can get around it by using:

      h = open(fileName)
      a = h.read()
      h.close()
      return int(a)

      It’s not the most pythonic way of doing things but it should work on the simulator and micro:bit until I can work out a proper fix.

      Reply
  • July 12, 2019 at 4:15 am
    Permalink

    The simulator is very useful for debugging. So hats off to you my friend and thank you so much but it seems that the feature for the parameters of looping and waiting for the scroll command have not been implemented. If so please explain the code and if there is a specific order. Please respond so I may finish help finish my son’s project.

    Reply
    • July 12, 2019 at 7:01 am
      Permalink

      Thanks for your message. Please could you share a link to the code that isn’t working and I’ll try to fix it as soon as I can 😀
      Edit: Sorry – I’ve seen the message in context now and I think you mean this code: https://create.withcode.uk/python/4j

      I fixed a few bugs with the looping and delay parameters but you may have to clear your browser cache to load the latest version of the microbit python library for the simulator. If that doesn’t fix it, let me know and I’ll look into what the problem may be.
      Hope your son’s project goes well – what are you trying to make?

      Reply
  • July 12, 2019 at 7:07 am
    Permalink

    Just replying late in case anyone else reads this: either way for reading from a file should now work thanks to an update. Thanks for reporting this and sorry it took so long for a fix!

    Reply
      • July 22, 2019 at 11:14 am
        Permalink

        Thanks for taking the time to explain the problem and for posting the link. I’ve added in the functionality you described – sorry to have missed that before.

        Here’s a link to some code which will keep scrolling until you press the A button. It’s slightly different to your posted example because the wait parameter needs to be set to False and there’s a loop so that the program keeps checking if the button is pressed:

        https://dev.withcode.uk/python/9QH

        Thanks for helping improve the micro:bit simulator 🙂

        You may need to clear your browser cache for the updated code to work

        Reply

Leave a Reply

Your email address will not be published. Required fields are marked *