Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
MicroPython Cookbook

You're reading from   MicroPython Cookbook Over 110 practical recipes for programming embedded systems and microcontrollers with Python

Arrow left icon
Product type Paperback
Published in May 2019
Publisher Packt
ISBN-13 9781838649951
Length 452 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Marwan Alsabbagh Marwan Alsabbagh
Author Profile Icon Marwan Alsabbagh
Marwan Alsabbagh
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface 1. Getting started with MicroPython 2. Controlling LEDs FREE CHAPTER 3. Creating Sound and Music 4. Interacting with Buttons 5. Reading Sensor Data 6. Button Bash Game 7. Fruity Tunes 8. Let's Move It, Move It 9. Coding on the micro:bit 10. Controlling the ESP8266 11. Interacting with the Filesystem 12. Networking 13. Interacting with the Adafruit FeatherWing OLED 14. Building an Internet of Things (IoT) Weather Machine 15. Coding on the Adafruit HalloWing Microcontroller 16. Other Books You May Enjoy

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime