Book Image

MicroPython Cookbook

By : Marwan Alsabbagh
Book Image

MicroPython Cookbook

By: Marwan Alsabbagh

Overview of this book

MicroPython is an open source implementation of Python 3 that runs in embedded environments. With MicroPython, you can write clean and simple Python code to control hardware instead of using complex low-level languages such as C and C++. This book guides you through all the major applications of the MicroPython platform to build and program projects that use microcontrollers. This MicroPython book covers recipes that will help you experiment with the programming environment and hardware programmed in MicroPython. You'll find tips and techniques for building a variety of objects and prototypes that can sense and respond to touch, sound, position, heat, and light. This book will take you through the uses of MicroPython with a variety of popular input devices and sensors. You'll learn techniques to handle time delays and sensor readings, and apply advanced coding techniques to create complex projects. As you advance, you'll deal with Internet of Things (IoT) devices and integration with other online web services. In addition to this, you'll use MicroPython to make music with bananas and create portable multiplayer video games that incorporate sound and light animations into the gameplay. By the end of this book, you'll have mastered the tips and tricks to troubleshoot your development problems and take your MicroPython project to the next level.
Table of Contents (17 chapters)

Executing your first program

In this recipe, we will show you how to load your first program on the Circuit Playground Express and how to modify the program and reload it. The program will then light one of the ten NeoPixels that come available on the board.

Getting ready

Once the Circuit Playground Express has had the CircuitPython firmware flashed, you may load Python scripts onto the board and run them.

How to do it...

Let's have a look at how to do this:

  1. Make sure that the board is connected to your computer with a USB cable and that the CIRCUITPY drive appears.
  2. Save a text file on the drive with the following contents and name it main.py:
from adafruit_circuitplayground.express import cpx
import time

cpx.pixels[0] = (255, 0, 0) # set first NeoPixel to the color red
time.sleep(60)
  1. Once the file has been saved, eject the drive, and remove and reconnect the USB cable from the computer.
  2. The first NeoPixel on the drive should light up with a red color.
  3. Open the main.py file in your text editor of choice and change the cpx.pixels[0] line to cpx.pixels[1]. Save the file. This change will make the second NeoPixel light up instead of the first.
  4. Eject the drive, remove, and then reconnect the USB cable to see the change take effect.

How it works...

When the device is turned on it looks for certain files, such as code.py or main.py, that, if found, will be executed as part of the startup process. In this way, you can specify the code you want run when the device is powered on. The script first imports the adafruit_circuitplayground.express library so that it can control the NeoPixels. The first NeoPixel is set to the color red by giving it a set of appropriate RGB values.

Finally, the script will sleep for 60 seconds so that the LED remains lit for one minute before the script ends execution.

There's more...

Now that the board has been loaded with a Python script, it can be disconnected from the computer and have the battery pack attached to it. Once the battery pack is powered on by the script, it should run and light up the selected NeoPixel.

This is a simple way to create portable and inexpensive projects that can have a code running directly from the board with no need for a connected PC and can be powered simply by three AAA batteries.

See also