# MicroBlocks Circuit Playground Express Ornament

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/066/409/medium800thumb/sensors_all-leds-demo.jpg?1543176202)

Block based programming environments are becoming very popular. First Scratch and now MakeCode, and now there are others like EduBlocks (block based Python) and MicroBlocks.&nbsp;

MicroBlocks is an especially interesting one. It doesn't use an intermediate language like some others; it compiles directly to virtual machine bytecode.

MicroBlocks has three main components:

1. The block based editor
2. The bytecode compiler
3. The code manager that can keep the code on your board seamlessly up to date with what's in the editor.

Not only does it support whole programs, but any valid snippet can be immediately executed on the board. That includes prototype blocks on the palette as well as code under construction. This lets you interactively see how code works, piece by piece as you work on it. This is much like CircuitPython's REPL, but more powerful. For those with an interest in programming languages, this capability is much like what Smalltalk provides: being able to execute any valid code at any time.

We are going to explore a bit of MicroBlocks through making a festive ornament using a Circuit Playground Express.

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
![A Black woman's manicured hand holds a round microcontroller with lit up LEDs.](https://cdn-shop.adafruit.com/640x480/3333-05.jpg)

### Lithium Ion Polymer Battery - 3.7v 500mAh

[Lithium Ion Polymer Battery - 3.7v 500mAh](https://www.adafruit.com/product/1578)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 500mAh for a total of about 1.9 Wh. If you need a larger (or smaller!) battery, <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1578)
[Related Guides to the Product](https://learn.adafruit.com/products/1578/guides)
![Angled shot of a Lithium Ion Polymer Battery 3.7V 500mAh with JST-PH connector.](https://cdn-shop.adafruit.com/640x480/1578-03.jpg)

### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
![USB cable - USB A to Micro-B - 3 foot long](https://cdn-shop.adafruit.com/640x480/592-01.jpg)

### DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground

[DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground](https://www.adafruit.com/product/4036)
Have you put up with mainstream, uninspiring, low-tech tree ornaments for too long? This season why not deck the halls with _codes_ of holly?

This **DIY Ornament Kit** is just the right size (6 cm diameter) for a Circuit Playground Express, 500mAh LiPoly battery, and...

In Stock
[Buy Now](https://www.adafruit.com/product/4036)
[Related Guides to the Product](https://learn.adafruit.com/products/4036/guides)
![6cm Diameter DIY Ornament Kit with Circuit board inside](https://cdn-shop.adafruit.com/640x480/4036-00.jpg)

# MicroBlocks Circuit Playground Express Ornament

## Exploring MicroBlocks

## Installation

Unlike MakeCode, MicroBlocks needs to be installed on your computer. While this isn't always a great thing (like where you aren't allowed to install software, at some schools, for example), it does make possible some interesting capabilities.

On the [Get MicroBlocks](http://microblocks.fun/download.html) page you can find installers for various platforms. Download and install as is appropriate for your system and launch the app.

MicroBlocks needs to install it's VM (virtual machine) and runtime on your board. In this regard it is much like CircuitPython. Plug in your Circuit Playground Express, double-press the reset button (the small button between the larger A and B buttons. Then select `install MicroBlocks on board` from MicroBlocks' `About` menu.

![sensors_InstallMenu.png](https://cdn-learn.adafruit.com/assets/assets/000/066/384/medium640/sensors_InstallMenu.png?1543173114)

You'll be asked to select the model of board. Click on it and your board will be prepared.

![sensors_install.png](https://cdn-learn.adafruit.com/assets/assets/000/066/407/medium640/sensors_install.png?1543174213)

## Connecting
In order to use MicroBlocks with your board, you need to connect them. Use the Connect menu for this. You will be asked to select the serial port to use.&nbsp;

![sensors_ConnectMenu.png](https://cdn-learn.adafruit.com/assets/assets/000/066/385/medium640/sensors_ConnectMenu.png?1543173149)

Next to the connect menu button there is an indicator. If it's red you will need to do this connection step, if it's green you're good to go.

&nbsp;

When the connection is established, the indicator will change to green.

![sensors_Connected.png](https://cdn-learn.adafruit.com/assets/assets/000/066/386/medium640/sensors_Connected.png?1543173177)

## Real Time Execution

One major difference with MicroBlocks is that it's live. By that, I mean that it's a programming environment like MakeCode but it's also like CircuitPython's REPL.&nbsp;

To illustrate, select the `Output` blocks from the list on the far left. You will see one at the top of the list of blocks just to the right of that. At the top is one to set the user LED (aka the D13 LED).&nbsp;

![](https://cdn-learn.adafruit.com/assets/assets/000/066/387/medium800/sensors_SetUserLEDBlock.png?1543080655)

That's pretty cool, and applies generally: if you click on a block anywhere in the IDE, it will get executed immediately on the board.

Any blocks that are currently running on the board will have a faint outline in the editor pane. As you make changes, they go into effect immediately on the board.

There's no save required, no downloading to the board, it gets updated on the board immediately, in real time, without you having to do anything. That's pretty cool, and very powerful.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/408/medium800thumb/sensors_User_LED.jpg?1543176137)

Click on the block. Assuming your board is installed and connected (as described above) the D13 LED on your board will light up. Click the slide switch in the block so that it turns red and click it again. The LED on your board turns off.

## Extensions

The out of the box experience with MicroBlocks is fairly generic. You have to dig a bit to get access to additional functionality that the Circuit Playground Express has to offer.&nbsp; You do that with the `Library` menu.

Library opens a window that gives you access to the bundled libraries, your desktop, and anywhere on your filesystem. When you start out there will only be the bundled libraries (shown when you open the library window).

![sensors_library.png](https://cdn-learn.adafruit.com/assets/assets/000/066/406/medium640/sensors_library.png?1543173960)

There are libraries for various supported boards and various bits of hardware. You'll want to load the Circuit Playground and NeoPixel libraries for this project. Select each one in turn and click `Okay`. You will need to reopen the library window each time.

## How It Works

MicroBlocks is sort of like MakeCode and EduBlocks, and sort of like CircuitPython. But it's also unlike all of them as well.

The thing that gets installed on your board is the MicroBlocks virtual machine (VM). When you use CircuitPython its VM is installed on your board. That's what reads, compiles to bytecode, and executes your Python code.

MakeCode works a bit differently: your code (which is really JavaScript behind the blocks) is bundled with the runtime and downloaded to your computer as a file that you then copy to your board.

MicroBlocks compiles your program to bytecode, and ships that to your board. the bytecode then gets executed by the VM installed on there.

One big difference is that your block code compiles directly to bytecode, rathering than being converted to another language (Javascript or Python) first. This makes it faster and more flexible since it's one step and there's no third language involved.

The other big difference is that as you make changes, they get compiled, downloaded, and executed. The board is always running the latest valid version of your code. This assumes that you clicked `Start` at the top right of the MicroBlocks window.

The[MicroBlocks site has a page](http://microblocks.fun/tech.html) explaining a bit about how the whole thing works.

# MicroBlocks Circuit Playground Express Ornament

## Making an Ornament

Take a pair of the 60mm plastic half-spheres. Be sure to take a pair that fit together.

&nbsp;

Since the Circuit Playground Express is about 50.6mm in diameter, it will be too loose on it's own. A simple solution is to trace a half sphere on cardboard and cut a disk to just fit inside.&nbsp;Cut a notch at one spot on the edge for battery wires and attach the Circuit Playground Express on one side and a battery on the other.

&nbsp;

&nbsp;

![sensors_IMG_3110.jpg](https://cdn-learn.adafruit.com/assets/assets/000/066/401/medium640/sensors_IMG_3110.jpg?1543177050)

![sensors_IMG_3111.jpg](https://cdn-learn.adafruit.com/assets/assets/000/066/410/medium640/sensors_IMG_3111.jpg?1543177111)

![sensors_IMG_3115.jpg](https://cdn-learn.adafruit.com/assets/assets/000/066/411/medium640/sensors_IMG_3115.jpg?1543177188)

![sensors_IMG_3116.jpg](https://cdn-learn.adafruit.com/assets/assets/000/066/412/medium640/sensors_IMG_3116.jpg?1543177237)

Once programming is done, this assembly can be placed inside one half and the sphere snapped together.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/405/medium800/sensors_IMG_3119.jpg?1543160218)

## The code

First we have to decide what effect we want. Since the Circuit Playground Express has a ring of NeoPixels we can twinkle them in festive colors: red, green, and white.&nbsp;

Since we're going to work with NeoPixels, we need to hook up to them. We can use the `attach` block for that. The Circuit Playground Express has RGB NeoPixels, not RGBW ones, so be sure the `has white` switch is off/red. We can place it in a `when started` block to have it run when the program starts running. When you've done that (as shown below), click the `when started` block. That will compile, download, and execute it. Now we can play with NeoPixels.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/394/medium800/sensors_starting.png?1543108200)

We can randomly pick a color for each NeoPixel. We can experiment and get it working with just one pixel.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/392/medium800/sensors_setting_pixel.png?1543107867)

Remember what was said about clicking on any value block code to have it executed. Well, the above is just that. Make sure you're connected to your board and click the top of the above code (the `set` block in this case). LED 1 (to the left of the USB connector should light up with one of the colors. Click repeatedly to see one of the three colors randomly appear.

We just need to do that for each pixel (1-10). We can just wrap a `for` block around what we have:

![](https://cdn-learn.adafruit.com/assets/assets/000/066/393/medium800/sensors_setting_all_pixels.png?1543107935)

Click the `for` block and see all 10 NeoPixels get set randomly.

Now that we have all ten pixels being randomly set to one of the three colors, we can make it twinkle by doing it repeatedly with a short delay between updates:

![](https://cdn-learn.adafruit.com/assets/assets/000/066/398/medium800/sensors_twinkle.png?1543113923)

We don't want NeoPixels flashing constantly, that would burn through battery in no time. Since the Circuit Playground Express has a built-in accelerometer and there are blocks to access it (the&nbsp;`tilt`blocks), we can trigger the twinkling effect when the ornament is jostled. Since it's hanging vertically, and the Z axis extends perpendicular to the board, it's reasonable to watch it for changes.

If we keep track of readings, we can compare the current value to the previous one. If the difference is large enough we can assume that the ornament has been jostled or tapped.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/399/medium800/sensors_shake.png?1543114119)

The final step is to put it all together. The constant (50 as shown) that the Z axis reading difference is compared to can be adjusted to make it more or less sensitive.

![](https://cdn-learn.adafruit.com/assets/assets/000/066/400/medium800/sensors_all.png?1543114173)

Here's the code. Unzip this and load it into MicroBlocks.

[ornament.zip](https://cdn-learn.adafruit.com/assets/assets/000/066/413/original/ornament.zip?1543178846)

## Featured Products

### Circuit Playground Express

[Circuit Playground Express](https://www.adafruit.com/product/3333)
 **Circuit Playground Express** is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to...

In Stock
[Buy Now](https://www.adafruit.com/product/3333)
[Related Guides to the Product](https://learn.adafruit.com/products/3333/guides)
### DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground

[DIY Ornament Kit - 6cm Diameter - Perfect for Circuit Playground](https://www.adafruit.com/product/4036)
Have you put up with mainstream, uninspiring, low-tech tree ornaments for too long? This season why not deck the halls with _codes_ of holly?

This **DIY Ornament Kit** is just the right size (6 cm diameter) for a Circuit Playground Express, 500mAh LiPoly battery, and...

In Stock
[Buy Now](https://www.adafruit.com/product/4036)
[Related Guides to the Product](https://learn.adafruit.com/products/4036/guides)
### Lithium Ion Polymer Battery - 3.7v 500mAh

[Lithium Ion Polymer Battery - 3.7v 500mAh](https://www.adafruit.com/product/1578)
Lithium-ion polymer (also known as 'lipo' or 'lipoly') batteries are thin, light, and powerful. The output ranges from 4.2V when completely charged to 3.7V. This battery has a capacity of 500mAh for a total of about 1.9 Wh. If you need a larger (or smaller!) battery, <a...></a...>

Out of Stock
[Buy Now](https://www.adafruit.com/product/1578)
[Related Guides to the Product](https://learn.adafruit.com/products/1578/guides)
### USB cable - USB A to Micro-B

[USB cable - USB A to Micro-B](https://www.adafruit.com/product/592)
This here is your standard A to micro-B USB cable, for USB 1.1 or 2.0. Perfect for connecting a PC to your Metro, Feather, Raspberry Pi or other dev-board or microcontroller

Approximately 3 feet / 1 meter long

Out of Stock
[Buy Now](https://www.adafruit.com/product/592)
[Related Guides to the Product](https://learn.adafruit.com/products/592/guides)
### Circuit Playground Express - Base Kit

[Circuit Playground Express - Base Kit](https://www.adafruit.com/product/3517)
It's the **Circuit Playground Express Base Kit!** &nbsp;It provides&nbsp;the few things you'll need to get started with the new [Circuit Playground Express](https://www.adafruit.com/product/3333).&nbsp;This version of Circuit Playground is super powered, and will...

In Stock
[Buy Now](https://www.adafruit.com/product/3517)
[Related Guides to the Product](https://learn.adafruit.com/products/3517/guides)
### Circuit Playground Express Advanced Pack

[Circuit Playground Express Advanced Pack](https://www.adafruit.com/product/2769)
 **Circuit Playground Express** &nbsp;is the next step towards a perfect introduction to electronics and programming. We've taken the original Circuit Playground Classic and made it even better! Not only did we pack even more sensors in, we also made it even easier to program....

In Stock
[Buy Now](https://www.adafruit.com/product/2769)
[Related Guides to the Product](https://learn.adafruit.com/products/2769/guides)
### Code.org Circuit Playground Express Educators' Pack

[Code.org Circuit Playground Express Educators' Pack](https://www.adafruit.com/product/3399)
For many years, instructors and teachers have asked us to come up with a better way to teach programming and electronics. We have worked on Circuit Playground Express for over a year to come up with a board that is powerful, beautiful, fun, and perfect for teaching

In Stock
[Buy Now](https://www.adafruit.com/product/3399)
[Related Guides to the Product](https://learn.adafruit.com/products/3399/guides)

## Related Guides

- [Adafruit Circuit Playground Express](https://learn.adafruit.com/adafruit-circuit-playground-express.md)
- [Comparison and Experimentation with Flammable Gas Sensors](https://learn.adafruit.com/gas-sensor-comparison.md)
- [Circuit Playground Bike Light](https://learn.adafruit.com/circuit-playground-bike-light.md)
- [Chinese Dragon Puppet with Motion-Reactive Flame Effect](https://learn.adafruit.com/chinese-dragon-puppet-with-motion-reactive-flame-effect.md)
- [UART Communication Between Two CircuitPython Boards](https://learn.adafruit.com/uart-communication-between-two-circuitpython-boards.md)
- [Make It Shake, Rattle, and Roll: Accelerometer Use](https://learn.adafruit.com/make-it-shake-rattle-and-roll.md)
- [MakeCode Course for Circuit Playground Express](https://learn.adafruit.com/makecode-circuit-playground-express-course.md)
- [Fireflies with MakeCode](https://learn.adafruit.com/circuit-playground-fireflies-makecode.md)
- [How to Use Video Greenscreen in MakeCode](https://learn.adafruit.com/how-to-use-video-greenscreen-in-makecode.md)
- [Your First Power Switch Relay Project - Circuit Playground Smart Plug](https://learn.adafruit.com/beginner-first-project-power-switch-relay-circuit-playground-smart-plug.md)
- [Sipping Power With NeoPixels](https://learn.adafruit.com/sipping-power-with-neopixels.md)
- [CircuitPython Sound Box](https://learn.adafruit.com/circuitpython-sound-box.md)
- [Color Spinner Camera Ring Light ](https://learn.adafruit.com/camera-ring-light-with-cpx.md)
- [Circuit Playground Bike Glove](https://learn.adafruit.com/circuit-playground-bike-glove.md)
- [Qu'est-ce que MakeCode?](https://learn.adafruit.com/makecode-fr.md)
- [Circuit Playground Musical Glove](https://learn.adafruit.com/cpx-musical-glove.md)
