Programming your F1 Starter Kit

Now both your CtrlR development environment and Ctrl platform are ready, you can start playing with the onboard peripherals, e.g. the RGB LED, Buttons, Micro-SD card, etc. Before exploring the Arduino UNO compatible expansion interface, let’s start with the basic: LED and buttons.

 

Light up the Onboard RGB LED

Testing your code in REPL

If you have any experience with python, you know that the >>> means we can start typing commands! Type the following commands in the REPL terminal:

import rgbled

rgbled.heartbeat( False )   # stop the heartbeat service
rgbled.color(0x00330033)    # sets the LED color to purple

This RGB LED now turns purple! This shows how simple it is to control the RGB LED on your device with just a few lines of Python command! Notice that the REPL does not give any feedback. Only when we make a mistake, or ask it to return something will it give us a response.

Creating a project in CtrlR

In this project, we will make the on-board RGB LED flash different colors infinitely.

  • First, you will need to create a new, empty, directory on your computer. For this example, we will create one called RGB-Blink.

  • Open the project folder you have created in your VS Code.

  • Now, we will need to add some files. A standard MicroPython project will have a lib folder for additional libraries, and two python files: main.py and boot.py.
    • main.py This script runs directly after boot.py and should contain the main code you wish to run on your device.
    • boot.py This is the first script that runs on your module when it turns on. This is often used to connect a module to a network without cluttering up the main.py file. As a beginner you generally do not need to use a boot.py.

 

Controlling the on-board RGB LED

Now that you have setup and configured your project, the first thing we will need to do is import some libraries in order to interact with the on-board LED. The F1 Module firmware comes with a large number of built-in modules. You can find out more about these in the API documentation. In this tutorial, you will rename the default main.py to main_bak.py and create a new main.py file with the following code:

import rgbled
import time
		 
while True:
    #colors in hexadecimal (0xRRGGBB)
    rgbled.color(0xFF0000)  # Red
    time.sleep(1)
    rgbled.color(0x00FF00)  # Green
    time.sleep(1)
    rgbled.color(0x0000FF)  # Blue
    time.sleep(1)

This will import two libraries, rgbled which is responsible to drive the on-board LED and time which is a standard library used for timing and delays.

Now it’s time to test your code. Right Click on the main.py file, in the CtrlR panel, you will see a Run file on device button, and also an Upload to device button. For now, we will use Run file on device.

Press OK to confirm the device you want to test the code on

After running the example code above, you should see that that on-board LED now blink in red, green and blue indefinitely.

Once you run the above script, it will run forever (due to the infinite While-loop). In order to stop the script, click onto the CtrlR terminal, and press ctrl-c on your keyboard.

This stops the script and returns to the interactive REPL.

 

Uploading code to your F1 Starter Kit

In the previous steps we got code running on your F1 Starter Kit using the Run file on device feature of CtrlR. This is useful for quick testing but has a couple of drawbacks. First of all, the code does not remain on the device permanently. If you reboot the device, it will no longer be running your code. Secondly, it will only work if you are using libraries built into the firmware. If you need any extra libraries, these need to be copied to the device first. This is where the Upload to device feature comes in. If instead of Run file on device you click Upload to device, CtrlR will upload all the files in the project. The files will then persist on your device even after reboot, and thus allows you to use the libraries from the lib folder in your project.