Coding Options

You've got lots of options when it comes to coding the Gemma M0! You can use Arduino, CircuitPython, and MakeCode. For this project we'll use the drag-and-drop GUI interface of MakeCode to quickly and easily build our program.

Create a MakeCode Project

First, head to the Maker.MakeCode site in your web browser. This version of MakeCode is in beta as of the writing of this guide, and it expands the available boards beyond the Circuit Playground Express to include the Gemma M0, Trinket M0, Metro M0 Express, and Feather M0 Express. 

Click on the Gemma M0 image to select it.

Now, click the New Project button to create a new program using the Gemma M0.

Building Blocks

This is the default MakeCode screen. On the left is the simulator which can be used to test your program before uploading it to the Gemma M0.

In the center is the palate of programing block categories.

On the right is the canvas where you'll drag blocks to create your program.

Click on the forever block to select it, and then press delete on your keyboard to get rid of it, we won't need it for this program because everything that happens will be triggered by an input action.

Then, click on the input category to expand it and drag the on touch D0 click block onto the canvas. 

Touch Input

You'll see that there are a couple of drop down lists on this input block that you can use to adjust the behavior. You can choose between touch and button modes for each of the three pins on the Gemma M0 -- D0, D1, and D2. We'll leave this in the default setting of touch D0.

You can also fine tune the input so that it reacts to one of four states -- click (which means press and release), long click, up (just the release action by itself), and down (just the press action).

Since we want to hold the D0 pad to light the UV LED, and then release it to turn it off, we'll change this dropdown from click to down.

Simulator Update

Notice, when you add this input block, the simulator updates to reflect the new circuit. While it has built a button wired through a breadboard in this case, and we are actually going to use the capacitive touch, it is a pretty interesting and helpful graphical layout nonetheless.

Turn On the Lights

Now we have an input block that can detect when the D0 capacitive pad is touched, but it doesn't actually do anything when pressed. Whatever action block we drag inside the on touch block is the action that will occur.

From the Pins category, drag a digital write pin D0 to LOW block onto the canvasdigital write command sets a pin either HIGH to enable 3.3 volts on the pin, or LOW which sends the pin to ground.

Drag the digital write block inside of the on touch input block. Since we want the LED that we'll plug into pin D2 to turn on when the D0 pad is touched, change the dropdown to pin D2, and switch the state slider from LOW to HIGH.

You'll see that the simulator has added an LED to represent the digital write command -- press the button on the breadboard to see it light up!

Release the Hounds

You'll notice that once you release the button, the simulated LED stays lit. This is because there is no command currently telling it to do otherwise! Let's add an input that tells the LED to turn off when your finger is "up" off the touch pad.

Duplicate the on touch D0 down block by right mouse button clicking on it and choosing Duplicate.

The duplicated block set will be greyed out because it is an identical input to the existing one, which is not allowed. 

Change the condition from down to up.

Lights Out

Now, you can tell the digital write to go LOW when the touch pad is released, or "up". 

Test out the simulator -- now the LED will illuminate when you push the button down, and turn off when you release the button.

Name It & Download It

Give your project a meaningful name, such as, oh, I don't know, "Gemma_UV_Reader", and then press the download button.

When prompted, save the file to a memorable location. This file is a .uf2 (USB Flashing Format) file which we'll use in a moment to flash the Gemma M0 with the new program.

Upload the Code

Now you can put the code on your Gemma M0! Plug the Gemma M0 into you computer with the USB cable. After a moment it will be recognized by your computer as a USB drive. (If it doesn't show up, make sure the Gemma M0's power switch is in the ON position.)

It should show up with the name 'CIRCUITPY'. In order to upload the new program, double click the reset button on the Gemma M0 -- the red onboard LED will start to pulse and the USB drive will show up named 'GEMMABOOT'.

Drage the .uf2 file onto the GEMMABOOT drive.

After the file transfers, the Gemma M0 will reboot, and the program will run. Unplug it from the USB cable, and get ready to try it out!

This guide was first published on Mar 16, 2018. It was last updated on Mar 08, 2024.

This page (Coding with MakeCode) was last updated on Mar 08, 2024.

Text editor powered by tinymce.