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

MicroPython Cookbook: Over 110 practical recipes for programming embedded systems and microcontrollers with Python

Arrow left icon
Profile Icon Marwan Alsabbagh
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (8 Ratings)
Paperback May 2019 452 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
Arrow left icon
Profile Icon Marwan Alsabbagh
Arrow right icon
€18.99 per month
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3 (8 Ratings)
Paperback May 2019 452 pages 1st Edition
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m
eBook
€17.99 €26.99
Paperback
€32.99
Subscription
Free Trial
Renews at €18.99p/m

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing
Table of content icon View table of contents Preview book icon Preview Book

MicroPython Cookbook

Controlling LEDs

In this chapter, we will introduce several methods of controlling the range of NeoPixel LEDs that come with the Adafruit Circuit Playground Express. In these recipes, we will examine various ways of setting the colors of the pixels, each with its own trade-off.

We will also demonstrate how to time operations so that fades and other light animation effects can be created. NeoPixels are a powerful way of allowing a richer visual interaction with your projects. These recipes will provide you with the necessary building blocks to incorporate these visual concepts into your own projects.

In this chapter, we will cover the following recipes:

  • Turning on the pin 13 LED
  • Setting the brightness of the NeoPixel
  • Controlling the color of a single NeoPixel
  • Displaying an LED color using the RGB and hex code
  • Setting an LED color using color names
  • Setting all NeoPixels to the...

The Adafruit Circuit Playground Express layout

The following diagram shows the locations of the LEDs that will be used in this chapter:

Courtesy of adafruit.com

The pin 13 LED is a simple single red LED that will be used in the first recipe. There are a total of 10 NeoPixels on the board. Each NeoPixel is made up of a red, green, and blue LED. By controlling the individual brightness of each of these LEDs, you will be able to set any NeoPixel to a specific color.

Turning on the pin 13 LED

In this recipe, we will learn how to turn the pin 13 LED on and off. This is the simplest LED available on the board as it has only one color, and interacting with it in Python is also very straightforward. For these reasons, the pin 13 LED is a good starting point.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following steps:

  1. Run the following lines of code in the REPL:
>>> from adafruit_circuitplayground.express import...

Setting the brightness of the NeoPixel

Controlling the brightness of the pixels will be the theme of this recipe. It is important to be able to set the brightness of the pixels depending on the need of the project. Note that you will have to change the brightness to a level that is bright enough so that the pixels are clearly visible, but not so bright that it causes discomfort.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following steps:

  1. Run the following...

Controlling the color of a single NeoPixel

This recipe will show you how to set the a specific NeoPixel to different colors. It will then show you how to change the color of any of the 10 NeoPixels that come with the board. This will be a useful recipe to follow so that you can start unleashing the power and flexibility of these onboard pixels.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following steps:

  1. Run the following lines of code in the REPL:
...

Displaying an LED color using the RGB and hex code

There is a common convention to express any color using the hex code, which works by representing the red, green, and blue components of the color. This recipe demonstrates how to set the color of a NeoPixel using this hex code convention. Using such a popular convention will be useful when you want to apply specific color settings from other applications on the web or your desktop.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

...

Setting an LED color using color names

Using human-readable color names can make it much easier to keep track of what colors are in use in your application. This recipe demonstrates a technique that allows you to set pixel colors using regular color names. This feature of referring to colors by a standard set of color names is available in popular languages, including CSS. This recipe shows you how to bring this feature to your MicroPython scripts.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

...

Setting all NeoPixels to the same color

This recipe explains how you can set all the pixels to one color through a single call, instead of looping through all the NeoPixels and setting their colors individually. You can use this technique to create a nice effect to set all 10 NeoPixels to the same color. They are arranged in a perfect circle, so it creates a ring of color when they are all set to the same color. It is also an easy way to turn all the NeoPixels off in one call.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

...

Setting a range of NeoPixels to one color

This recipe will explore how the slicing feature can be used to set a specific range of pixels to a specific color. This can be quite useful when you want to turn the ring of pixels into a gauge to show the level of a value from 1 to 10. Essentially, it provides a clearer and simpler way of setting a range of pixels to a particular color.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following steps:

  1. Run the following...

Generating random NeoPixel LED colors

This recipe demonstrates a technique that can be used to endlessly generate random colors. We will then use these random colors on a specific NeoPixel. Adding randomness to the color section can make projects more interesting, as you can never predict the exact sequence of colors that will appear as your script executes.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following steps:

  1. Run the following lines of code in...

Creating LED animations with random colors

This recipe will combine some aspects of the previous recipes in this chapter to create an animation using randomly selected colors. This recipe builds on the techniques of other recipes to create your first animation. With 10 pixels on the board, there are a lot of options for creating engaging visual animations on the board—this is just one of those.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

How to do it...

To do this, perform the following...

Creating LED animations with rainbow colors

This recipe will produce a ring of colors following the same sequence of colors found in a rainbow. These colors will each appear after a certain delay, creating a rainbow animation effect. Using a sequence of colors that naturally fits together, like the ones found in the rainbow, can be both pleasing and engaging. The strength of this animation is in learning how to control the exact sequence of colors being animated, whether they be in a rainbow sequence or another sequence of your choosing.

Getting ready

You will need access to the REPL on the Circuit Playground Express to run the code presented in this recipe.

...
Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Load and execute your first program with MicroPython
  • Program an IoT device to retrieve weather data using a RESTful API
  • Get to grips with integrating hardware, programming, and networking concepts with MicroPython

Description

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.

Who is this book for?

If you want to build and program projects that use microcontrollers, this book will offer you dozens of recipes to guide you through all the major applications of the MicroPython platform. Although no knowledge of MicroPython or microcontrollers is expected, a general understanding of Python is necessary to get started with this book.

What you will learn

  • Execute code without any need for compiling or uploading using REPL (read-evaluate-print-loop)
  • Program and control LED matrix and NeoPixel drivers to display patterns and colors
  • Build projects that make use of light, temperature, and touch sensors
  • Configure devices to create Wi-Fi access points and use network modules to scan and connect to existing networks
  • Use Pulse width modulation to control DC motors and servos
  • Build an IoT device to display live weather data from the internet at the touch of a button

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : May 21, 2019
Length: 452 pages
Edition : 1st
Language : English
ISBN-13 : 9781838649951
Category :
Languages :

What do you get with a Packt Subscription?

Free for first 7 days. $19.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

Product Details

Publication date : May 21, 2019
Length: 452 pages
Edition : 1st
Language : English
ISBN-13 : 9781838649951
Category :
Languages :

Packt Subscriptions

See our plans and pricing
Modal Close icon
€18.99 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
€189.99 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts
€264.99 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just €5 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total 90.97
MicroPython Projects
€32.99
Internet of Things Projects with ESP32
€24.99
MicroPython Cookbook
€32.99
Total 90.97 Stars icon

Table of Contents

16 Chapters
Getting started with MicroPython Chevron down icon Chevron up icon
Controlling LEDs Chevron down icon Chevron up icon
Creating Sound and Music Chevron down icon Chevron up icon
Interacting with Buttons Chevron down icon Chevron up icon
Reading Sensor Data Chevron down icon Chevron up icon
Button Bash Game Chevron down icon Chevron up icon
Fruity Tunes Chevron down icon Chevron up icon
Let's Move It, Move It Chevron down icon Chevron up icon
Coding on the micro:bit Chevron down icon Chevron up icon
Controlling the ESP8266 Chevron down icon Chevron up icon
Interacting with the Filesystem Chevron down icon Chevron up icon
Networking Chevron down icon Chevron up icon
Interacting with the Adafruit FeatherWing OLED Chevron down icon Chevron up icon
Building an Internet of Things (IoT) Weather Machine Chevron down icon Chevron up icon
Coding on the Adafruit HalloWing Microcontroller Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
(8 Ratings)
5 star 12.5%
4 star 25%
3 star 25%
2 star 25%
1 star 12.5%
Filter icon Filter
Top Reviews

Filter reviews by




Kindle Customer Mar 06, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Sehr gutes Buch. Die Beispiele funtionieren auch in der wirklichkeit.
Amazon Verified review Amazon
alf Dec 24, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
Sei muito pouco de electrónica! Vou tentar aprender mais qualquer coisa com este livro.
Amazon Verified review Amazon
Scott H. Sep 25, 2022
Full star icon Full star icon Full star icon Full star icon Empty star icon 4
A well-written and presented reference book. A wide variety of content.
Amazon Verified review Amazon
Peter R. Sep 19, 2022
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Kändes som all var styrt via Adafruit. Vilket jag inte tyckte om. Men troligen för att boken skrevs 2019.
Amazon Verified review Amazon
wayne durden Jan 05, 2020
Full star icon Full star icon Full star icon Empty star icon Empty star icon 3
Most of the projects in the book deal with how to use Adafruit devices with Adafruit libraries. It's OK if that's what you like to do. As far as a cookbook goes it is more of a 'how to fix canned vegetables' than how to actually make something from scratch. I was expecting more how to do interfacing that did not rely so much on canned hardware and libraries. If you want to use this book, you will need to go ahead and get the Adafruit Circuit Playground Express in order to do most of the projects. Also there are projects using the Adafruit CRICKIT, Feather HUZZAH ESP8266, Hollowing MO Express and The micro:bit. The book is well written, well structured and easy to follow.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.