Reader small image

You're reading from  MicroPython Cookbook

Product typeBook
Published inMay 2019
Reading LevelBeginner
PublisherPackt
ISBN-139781838649951
Edition1st Edition
Languages
Right arrow
Author (1)
Marwan Alsabbagh
Marwan Alsabbagh
author image
Marwan Alsabbagh

Marwan Alsabbagh has been coding in some form or other since before the web existed and has continued to develop software, with a particular passion for Python, his preferred programming language, for over a decade. He has been a speaker at a number of global Python conferences, where he has been known to present microcontroller projects with a healthy dose of humor and stage theatrics. The snow globe intruder alert system, which he created with his creative and curious daughters, was one of his favorite MicroPython projects. His research interests include software engineering, microcontrollers, and 3D printing.
Read more about Marwan Alsabbagh

Right arrow

Fruity Tunes

In this chapter, you will learn how to create a musical instrument with Circuit Playground Express and some bananas. We will connect four bananas to the touchpads on the board so that you can play a certain musical sound for each banana you touch. We'll add some visual feedback to the project by lighting up a pixel next to each touchpad each time you make contact with it. This project will show a creative, fun way to bring your capacitive touch projects to life.

By using unexpected objects such as bananas in your projects, you can add a unique twist to your mundane MicroPython projects.

In this chapter, we will be covering the following recipes:

  • Creating a class to react to touch events
  • Creating a function to enable speaker output
  • Creating a function to play audio files
  • Using the NeoPixel object to control pixels
  • Creating a touch handler to play sounds
  • Creating...

Technical requirements

The code files for this chapter can be found in the Chapter07 folder in the GitHub repository, at https://github.com/PacktPublishing/MicroPython-Cookbook.

Many of the recipes in this chapter require four audio files to be transferred to the Circuit Playground Express board. They can all be downloaded from the Chapter07 folder in the GitHub repository. They should be saved in the top-level folder with your main.py file.

Circuit Playground Express touchpads

Circuit Playground Express comes with seven capacitive touchpads. Each of them can be connected to any object that can conduct electricity, and touching that object will, in turn, trigger the sensor. You can use good electrical conductors, such as metal, and even weaker ones, such as a banana.

Water conducts electricity, and the surfaces of many fruits have enough moisture for the touchpads to detect a touch event. Many fruits, such as bananas, limes, oranges, and apples, will do the job. You can use alligator clips to connect the fruit to the touchpads. The following photograph shows a bundle of alligator clips:

These alligator clips come in a variety of different colors. It's a good idea to use a different color wire for each touchpad. It will make it easier to trace which fruit is connected to which touchpad. In this project, we will...

Creating a class to react to touch events

In this recipe, you will learn how to define a class that can help you use handle touch events on a particular touchpad. When you create an instance of this class, you specify the pad name and a callback function that will be called each time a touch event starts and ends. We can use this class as a building block to have a callback called for each of the four touchpads that will be connected to bananas. You can use this style of code in your own projects, whenever you want to handle a variety of events with a set of callback functions.

Getting ready

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

...

Creating a function to enable speaker output

In this recipe, you will learn how to create a function that, when called, will enable the speaker. If you don't enable the speaker before audio playback, then it will be played through pin A0, which can have a headphone connected to it.

This project will use the speaker on the board instead of headphones, so we will need this function to enable the speaker at the start of our script. Beyond showing you how to enable the speaker, this recipe will also introduce you to ways of digitally controlling input/output pins.

Getting ready

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

...

Creating a function to play audio files

In this recipe, you will learn how to create a function that, when called, will play a specific audio file on the built-in speakers. This recipe will illustrate how to get access to the audio output device, as well as how to read the contents of a .wav file, convert it to an audio stream, and feed that audio stream to the onboard audio playback device. The techniques shown in this recipe can be used in all sorts of projects that need more fine control over the way audio files are played back.

Getting ready

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

...

Using the NeoPixel object to control pixels

In this recipe, you will learn how to control the pixels on the board using the NeoPixel object. We covered a lot of the methods in this object in previous chapters, but this is the first time we will directly create a NeoPixel object. It’s useful to have the skills to directly use the NeoPixel object, instead of accessing it through another object. You will need these skills if you decide to add an additional ring or strip of pixels to your project. In those cases, you will need direct access to this object to control the pixels.

Getting ready

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

...

Creating a touch handler to play sounds

In this recipe, we will create the first version of our touch handler. This first version will play a specific audio file each time a touch event is detected. We can then use this handler in later recipes, in order to map each touchpad to a specific audio file. We will also expand the functionality of this handler in future recipes to add light, as well as sound, to the touch event. Event handlers are a common part of many software systems. This recipe will help you see how you can use this common approach with your MicroPython projects.

Getting ready

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

...

Creating a touch handler to light up pixels

In this recipe, we will create a touch handler that will react to touch events by playing sounds and lighting up pixels. When the touch sensor gets triggered, the handler will play a sound and light up a specific pixel. When the touch sensor detects that you have released your finger, the specific pixel that was lit up will turn off.

In this way, you can hear and see the board react to each of the configured touchpads uniquely. This recipe shows a useful way to create different types of output, based on different triggered input. Many projects can come to life when you add a mix of unique audio and visual output that will react to different types of human input.

Getting ready

You...

Creating an event loop to handle all touch events

This final recipe in this chapter takes all the previous recipes in this chapter and combines them to complete the banana power musical machine. Beyond the previous recipes, we will need to create an event loop that combines all this logic into one structure that can handle all the four touchpads and their related audio files and pixels. After going through this recipe, you will be able to create generic event loops and handlers that can be extended to meet the varying needs of the embedded projects you might create.

Getting ready

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

...
lock icon
The rest of the chapter is locked
You have been reading a chapter from
MicroPython Cookbook
Published in: May 2019Publisher: PacktISBN-13: 9781838649951
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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Marwan Alsabbagh

Marwan Alsabbagh has been coding in some form or other since before the web existed and has continued to develop software, with a particular passion for Python, his preferred programming language, for over a decade. He has been a speaker at a number of global Python conferences, where he has been known to present microcontroller projects with a healthy dose of humor and stage theatrics. The snow globe intruder alert system, which he created with his creative and curious daughters, was one of his favorite MicroPython projects. His research interests include software engineering, microcontrollers, and 3D printing.
Read more about Marwan Alsabbagh