{"id":1538,"date":"2016-09-02T23:15:33","date_gmt":"2016-09-02T23:15:33","guid":{"rendered":"http:\/\/blog.soton.ac.uk\/webteam\/?p=1538"},"modified":"2016-09-16T10:51:43","modified_gmt":"2016-09-16T10:51:43","slug":"an-interns-waisfest-introduction-to-the-bbc-microbit","status":"publish","type":"post","link":"https:\/\/blog.soton.ac.uk\/webteam\/2016\/09\/02\/an-interns-waisfest-introduction-to-the-bbc-microbit\/","title":{"rendered":"An Intern&#8217;s WAISfest: Introduction to the BBC Microbit"},"content":{"rendered":"<p>Over the Wednesday, Thursday and Friday of last week, the WAIS (Web and Internet Science) group held its annual WAISfest. This event is a chance for people in the group to explore side-projects and ideas they haven\u2019t had time to do. The aim of the event is to get people thinking about possible areas of research. To stimulate some extra creativity, so to speak.<\/p>\n<p>Luckily, I got to take part.<\/p>\n<p>Wednesday morning began with the ideas unconference. The aim of this was to source ideas, loosely grouping people together to work on them. Ideas ranged from virtual reality workspaces to ways of teaching programming in schools using Microbits.<\/p>\n<p>It was this latter project I hopped aboard, swayed by their stash of <a href=\"https:\/\/www.kitronik.co.uk\/5604-line-following-buggy-for-the-bbc-microbit.html\">robotic buggies<\/a> and a mountain of <a href=\"https:\/\/www.microbit.co.uk\/\">BBC Microbits<\/a>.<\/p>\n<h1><a id=\"Investigating_the_BBC_Microbit_8\"><\/a>Investigating the BBC Microbit<\/h1>\n<p><a href=\"http:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/microbit.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1546 size-medium\" src=\"http:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/microbit-300x169.jpg\" alt=\"BBC Microbit\" width=\"300\" height=\"169\" srcset=\"https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/microbit-300x169.jpg 300w, https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/microbit.jpg 640w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>At the start of the project, I had no idea what exactly the Microbit was, let alone how to use it. We spent the first day of the WAISfest digging up information on how to use it. Hopefully, by posting this here, it\u2019ll make someone\u2019s life a little easier than ours was!<br \/>\nFor those who don\u2019t know, the Microbit is a low-cost embedded board given free to all year 7 students in the UK. It has an accelerometer, magnetometer, radio, GPIO pins and USB on-the-go. It\u2019s able to be programmed in a variety of languages, including Python, Javascript, Microsoft Block Editor, Microsoft Touch Develop and C++.<\/p>\n<p>Behind the scenes, all of these languages use the same <a href=\"https:\/\/github.com\/lancaster-university\/microbit-dal\">core C++ library<\/a>, published by Lancaster University. This library provides a simplified means to interact with the hardware.<\/p>\n<p>The source code for the C++ library, MicroPython runtime and editor and Touch Develop are available at <a href=\"https:\/\/www.microbit.co.uk\/open_source\">https:\/\/www.microbit.co.uk\/open_source<\/a>.<\/p>\n<h1><a id=\"Getting_Started_with_the_Microbit_20\"><\/a>Getting Started with the Microbit<\/h1>\n<p>How to get started with the Microbit depends, in my opinion, on your level of experience:<\/p>\n<ul>\n<li>If you\u2019re completely new to programming, try playing around with one of the <a href=\"https:\/\/www.microbit.co.uk\/create-code\">online editors<\/a>. They\u2019re well documented, most coming with tutorials. Uploading your program is as simple as copying the file onto the board as if it was a USB drive.<\/li>\n<li>If you\u2019re a bit more experienced, or want to do a medium-sized project, try using <a href=\"http:\/\/codewith.mu\/\">Mu<\/a> and MicroPython. Writing longer programs (up to a few hundred lines) is pretty straightforward in MicroPython. Mu makes putting the file on the board significantly quicker.<\/li>\n<li>If you\u2019re an experienced developer looking to do something complex, get the runtime and write using C++. There\u2019s issues with using MicroPython for large projects I\u2019ll get into in the next section.<\/li>\n<\/ul>\n<p>We opted for the second approach, as we only had 3 days to produce a result. It\u2019s also essential that kids and teachers could understand the code we were writing.<\/p>\n<h1><a id=\"MicroPython_for_Microbit_33\"><\/a>MicroPython for Microbit<\/h1>\n<p>\u201cMicroPython is a lean and efficient implementation of the Python 3 programming language that includes a small subset of the Python standard library\u201d &#8211; MicroPython Website<\/p>\n<p><a href=\"https:\/\/microbit-micropython.readthedocs.io\/en\/latest\/\">MicroPython<\/a> is one of the languages available to program the Microbit. In my opinion, it exists as a middle ground between Javascript\/Touch develop and C++. It\u2019s useful for programs a few hundred lines in length, but struggles with anything larger.<\/p>\n<p>Advantages:<\/p>\n<ul>\n<li>Derived from Python, a widely used programming language. Skills easily transferred to other platforms.<\/li>\n<li>Quick to upload to the Microbit. Deploying a new script takes seconds.<\/li>\n<li>Easier to debug than many other embedded languages, as error messages scroll across the Microbit\u2019s LEDs.<\/li>\n<li>Editable, buildable and uploadable offline. There\u2019s no need to use the BBC\u2019s online editors.<\/li>\n<li>Avoids needing to understand memory management, as is the case with C and C++.<\/li>\n<li>Radio library provides a simple, minimalist interface to radio based networking.<\/li>\n<\/ul>\n<p>Disadvantages:<\/p>\n<ul>\n<li>With scripts larger than a few hundred lines, the Microbit runs out of memory. This essentially imposes a limit on how long your program can be.<\/li>\n<li>Bluetooth isn\u2019t available due to the memory usage of the Bluetooth stack.<\/li>\n<li>Unable to easily split code across files. Importing requires extra files to be flashed onto the Microbit each time a script is uploaded.<\/li>\n<li>Radio library only available in the Mu editor and not the BBC\u2019s online editor.<\/li>\n<\/ul>\n<h1><a id=\"Getting_Started_with_MicroPython_54\"><\/a>Getting Started with MicroPython<\/h1>\n<p>There\u2019s two main ways of getting started with MicroPython:<\/p>\n<h2><a id=\"Using_the_online_code_editor_58\"><\/a>Using the online code editor.<\/h2>\n<p>The <a href=\"https:\/\/www.microbit.co.uk\/create-code\">online editor<\/a> is provided by the BBC. It provides the ability to write, edit and save code online and compile it for the Microbit. Uploading the code is as simple as clicking \u201cDownload\u201d and copying the file to the Microbit. No installation of any software is needed, the editor runs in any modern browser.<\/p>\n<p>While this editor is easy to use and fast to get started with, it has some downsides. To save scripts for later, you need to make an account and sign in. The editor also requires you to have a constant internet connection.<\/p>\n<p>Another downside is, as of the date this was posted, it doesn\u2019t support the Microbit \u2018radio\u2019 library, which allows Microbits to communicate with each other.<\/p>\n<h2><a id=\"Using_Mu_67\"><\/a>Using Mu<\/h2>\n<p><a href=\"http:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1549\" src=\"http:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34-300x175.png\" alt=\"Screenshot of the Mu Editor\" width=\"500\" height=\"291\" srcset=\"https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34-300x175.png 300w, https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34-768x447.png 768w, https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34-1024x597.png 1024w, https:\/\/blog.soton.ac.uk\/webteam\/files\/2016\/09\/2016-09-16_11-50-34.png 1538w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/codewith.mu\/\">Mu<\/a> is an offline, open source editor made by <a href=\"https:\/\/github.com\/ntoll\">Nicholas Tollervey<\/a>. You can write, edit and save code with it similarly to the online editor. However, files are saved locally, giving more flexibility about when you work and how you store the scripts.<\/p>\n<p>The Mu editor also has several other features the online editor does not:<\/p>\n<ul>\n<li>Upload scripts to the Microbit by pressing a button<\/li>\n<li>Browse files stored in the Microbit\u2019s flash memory<\/li>\n<li>Error messages output to the screen via \u201cREPL\u201d<\/li>\n<li>Syntax checking (identifying any errors)<\/li>\n<li>Tabs for editing multiple scripts<\/li>\n<\/ul>\n<p>The downside is that the Mu editor needs downloading and running, something that may not be easy on many school systems. On Windows, the <a href=\"https:\/\/developer.mbed.org\/handbook\/Windows-serial-configuration\">Mbed serial driver<\/a> is also needed for file browsing and REPL functionality.<\/p>\n<p>Unless you\u2019re just having a quick play, I recommend Mu over the online editor. The extra functionality (especially REPL) is invaluable, as is the ability for more experienced developers to version control their scripts.<\/p>\n<p>As for writing your first script\u2026 there\u2019s plenty of tutorials on Python, and the syntax here is identical. Access to the Microbit\u2019s hardware is provided by the Microbit library, with documentation available <a href=\"https:\/\/microbit-micropython.readthedocs.io\/en\/latest\/\">here<\/a>.<\/p>\n<h1><a id=\"Aside_Fixing_the_MicroPython_Memory_Error_84\"><\/a>Aside: Fixing the MicroPython Memory Error<\/h1>\n<p>When a MicroPython for Microbit script grows to a certain size, MemoryErrors start appearing. I\u2019m not sure if the size is file size, or number of function\/variable definitions. The error looks something like this:<\/p>\n<blockquote><p>MemoryError: memory allocation failed, allocating 1584 bytes<\/p><\/blockquote>\n<p>If this occurs, the simplest solution is to remove unused lines of code. Shortening variable and function names is an alternative solutions. Using long strings as comments exaggerates the issue, as they aren\u2019t optimised out by the MicroPython runtime. This results in them using annoyingly large amounts of memory.<\/p>\n<p>Another option is to try the online MicroPython editor. When compiled on there, I didn\u2019t get MemoryError issues. I\u2019m not certain why this is, but it seems to work!<\/p>\n<h1><a id=\"Other_Bits_of_Useful_Microbit_Info_95\"><\/a>Other Bits of Useful Microbit Info<\/h1>\n<ul>\n<li>The online editors run a different version of MicroPython to Mu and the board firmware. In situations such as the \u201cMemoryError\u201d issue, different results can occur.<\/li>\n<li>The compass in the Microbit can\u2019t be relied on for consistent bearings. It appears to be affected by any nearby cabling or bits of metal.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Over the Wednesday, Thursday and Friday of last week, the WAIS (Web and Internet Science) group held its annual WAISfest. This event is a chance for people in the group to explore side-projects and ideas they haven\u2019t had time to do. The aim of the event is to get people thinking about possible areas of [&hellip;]<\/p>\n","protected":false},"author":98708,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[52771,30036,52208,364,93,74,453],"tags":[13883,803392,803396,892463,803397,138491,129910,12,803391,803390,803398,93,453,803360],"class_list":["post-1538","post","type-post","status-publish","format-standard","hentry","category-community","category-open-source","category-outreach","category-programming","category-python","category-tips","category-tutorial","tag-bbc","tag-bbc-microbit","tag-embedded","tag-hackathon","tag-ides","tag-intern","tag-internship","tag-introduction","tag-microbit","tag-micropython","tag-mu","tag-python","tag-tutorial","tag-waisfest"],"_links":{"self":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/1538","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/users\/98708"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/comments?post=1538"}],"version-history":[{"count":4,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/1538\/revisions"}],"predecessor-version":[{"id":1550,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/posts\/1538\/revisions\/1550"}],"wp:attachment":[{"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/media?parent=1538"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/categories?post=1538"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.soton.ac.uk\/webteam\/wp-json\/wp\/v2\/tags?post=1538"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}