The online makecode.microbit.org editor is a fun way to throw together quick microbit scripts with no programming experience. But it lacks version control, and manually downloading the code onto connected microbits is a pain.
The MakeCode editor is actually just the visual part of a wider toolkit, developed by Microsoft, called "PXT" (the "Programming eXperience Toolkit").
Using the PXT command line tool, we can develop and deploy code to microbits in a way that will be more familiar to people who write software for a living.
PXT requires Node.js. I’d recommend you install it via your usual package manager, but if you don’t use a package manager, then you can also download and install Node.js directly.
npx
is the Node equivalent of Ruby’s bundle exec [blah]
or Python’s virtualenv
.
We will use npx
to find and run the pxt
executable from inside our project’s node_modules
directory (meaning we don’t have to install pxt
globally – yay!).
npx
comes by default with Node.js 5 and greater. But this line will check it’s available, and if it isn’t, install it for you:
npx -v || npm install --global npx
PXT is listed in our package.json
, so you can install it with:
npm install
The first time you do this, you’ll also need to install the microbit PXT bindings:
npx pxt target microbit
Now you’re ready to start working on your microbit projects!
Go into the individual project directory, eg:
cd ./acceleration-radio-broadcaster
Then edit your files (main.ts
is a good place to start) and when you’re ready to deploy:
npx pxt
The pxt
command, when called with no arguments, works out which project you’re currently looking at, compiles it, and deploys it to all connected microbits.
From the root level, create a new directory to hold your project:
mkdir my-new-project
Go into the new directory, and let pxt
set up all the project files for you:
cd my-new-project
npx pxt init
That’s it!
Sometimes you need to use code that isn’t part of the microbit’s standard library.
In the visual MakeCode editor, you would use the “Add Package…” option to find and install third-party modules.
The command line equivalent is to run:
npx pxt install <github-user>/<github-repo>
So, for example, to install the Neopixel library into the project called “my-new-project”:
cd my-new-project
npx pxt install microsoft/pxt-neopixel
The dependency will be downloaded, and recorded in the dependencies
section of your project’s pxt.json
file (so future users can just run pxt install
to install it, along with all the other modules your project might require).