Join Now

    Python Coding with micro:bit in Tinkercad Circuits

    Team Tinkercad
    Published on - July 14, 2021 by Team Tinkercad

    Teachers & Parents, Features

    pythonhero2rt copy

    Adding micro:bit simulation to Tinkercad Circuits was one of Tinkercad’s blockbuster features of 2020, allowing students to code, manipulate, and build electronic projects with a virtual version of the popular microcontroller board.

    For beginners, getting familiar with electronics can be daunting enough, without also having to learn the intricacies of coding language as well. This is why students typically prefer to begin their journey with code by using our visual system of blocks, allowing them to logically chain sequences together.

    In addition to supporting blocks-style programming (via Tinkercad or Microsoft MakeCode), the micro:bit can also be programmed using the popular scripting language of Python. As your students’ skills mature, they may be interested in exploring the micro:bit’s Python compatibility, and Tinkercad is ready for them.

    Turning Blocks Into Python in 3 Easy Steps

    One of the easiest ways to dip your toe into coding with Python is to convert an existing design from blocks-style code to Python. In Tinkercad Circuits we’ve made this possible with just a few clicks:

    1. Open the Code view of any of your Tinkercad micro:bit designs (or choose from our Starter examples) and you’ll see the Blocks view of your code by default.

      dropdown2 copy

    2. From the dropdown menu shown above you’ll find different options for viewing your code. In addition to the familiar Blocks view, you can also choose a Text view that represents your code written with Python, or a Blocks + Text view to see your blocks code and Python code side-by-side.

      blocksncodegif

    3. The Blocks + Text view is perfect for beginners, allowing them to explore coding possibilities using familiar blocks while seeing the same concepts represented in the written language of Python.

    Please note: Moving your blocks code to Python is a one-way street. When changing any blocks-based code to a text-based option you’ll be presented with a warning that the conversion cannot be undone.

    Any attempt to revert a design back to a Blocks view of the code will erase the code. We’ve done our best to make this as clear as possible within the editor to prevent students from accidentally deleting their code. As their instructor, you may want call special attention to this and have students duplicate a backup of their designs before exploring the different code environments.

    areusure copy

    Coding Python Directly

    Adventurous or more experienced coders are free to bypass the blocks code editor entirely and enter their Python code directly in the text editor.

    We currently support the MakeCode version of Python, which will be familiar to anyone who has learned to code micro:bit using Microsoft’s MakeCode tutorials.

    textonly copy

    Be aware, though, that the syntax of MakeCode Python is different from the Python code examples provided directly from the Micro:bit Educational Foundation website. Those examples are written in MicroPython. You can learn more about these differences on the micro:bit support site.

    The practical takeaway is that copying and pasting Python examples from the micro:bit website will not work in the Tinkercad Circuits Python editor (at least, not at this time), whereas examples taken from the MakeCode website will.

    Debugging in Tinkercad

    If your code isn’t quite up to snuff, our built-in debugger will let you know. The debugging interface will kick-in automatically if it detects an error in your code.

    Another advantage of debugging is that you can add breakpoints in your code. By selecting a line number in your code, the highlighted line marks a point where the code will pause during simulation.

    debugger2 copy

    When debugging with break points, two buttons will appear above the code window during simulation. You can use these buttons to either resume the execution of the code after the breakpoint, or step to the next line of code.

    Adding breakpoints is also a useful way to reveal variable values while simulating your project.

    variablegif

    The example above shows the Compass project located in our Starters menu. When the simulation is paused at a breakpoint anywhere in the code  you can hover over a variable (in this case “angle”) and see its value at that moment in time. For this example, we can play around with the board’s orientation and check to see if the math accurately indicates the direction.

    Going Further with micro:bit in Tinkercad

    We believe that Tinkercad is the best place for your students to play with and learn about electronics and micro:bit. Our latest series of self-paced, interactive micro:bit lessons are designed to quickly build your students’ confidence and understanding.

    microbitinteractive169

    When students are ready, our selection of simulated Arduino projects provides room to grow, with extended hardware capabilities, code libraries, and interactive guides. 

    If you have questions about using micro:bit in Tinkercad Circuits, be sure to read over our FAQ.

    We’re always looking for suggestions on new features and improvements to help Tinkercad meet your classroom needs. If you're an educator with feedback or ideas to share, we’d love to hear from you at tinkercadlearning@autodesk.com.

    -Team Tinkercad