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

Interacting with Buttons

This chapter will introduce you to methods of interacting with the buttons and touchpads that come with the Adafruit Circuit Playground Express. You will learn how to detect whether a button is being pressed and you will also explore more advanced topics, such as fine tuning the sensitivity of capacitive touchpads.

In this chapter, we will cover the following recipes:

  • Detecting push button presses
  • Controlling LEDs with push buttons
  • Reading a slide switch
  • Calling functions on the button state change
  • Moving active LEDs with push buttons
  • Playing a beep on the button press
  • Detecting touch on a touchpad
  • Monitoring the touchpad raw measurements
  • Adjusting the touch threshold

The Adafruit Circuit Playground Express layout

The following photograph displays the locations of the two push buttons, labeled A and B, that come with the board:

Courtesy of adafruit.com

The following photograph shows the location of the slide switch on the device:

Courtesy of adafruit.com

The following photograph shows the location of the seven capacitive touchpads on the board:

Courtesy of adafruit.com

Each touchpad contains a different material that can conduct electricity. Alligator clips can be used to connect these materials to the pads. Additionally, metals, water, and fruits can conduct electricity well enough to be used as connectors to the pads.

Now, let's take a look at how to detect the press of a button.

Detecting push button presses

In this recipe, we will learn how to create a program that will print a message when the push button is pressed. Push buttons are a great way to create user interaction on your devices. This board comes with two push buttons, A and B, so you can create all sorts of different interactions with your users by reading and responding to push button events.

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...

Let's perform the following steps:

  1. First, run...

Controlling LEDs with push buttons

In this recipe, we will learn how to control two separate NeoPixels with two independent push buttons. This is a fun and simple way of creating interactivity with your device. Here, you will get immediate feedback from the board the moment you press each push button, as the pixels will light up in response.

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...

Let's perform the following steps:

  1. First, run the following lines of code in the REPL...

Reading a slide switch

In this recipe, we will learn how to create a program that will repeatedly print whether a slide switch is on or off. Slide switches have their own strengths, and this recipe will demonstrate how you can incorporate one into your project.

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...

Let's perform the following steps:

  1. Ensure that the slide switch is flipped to the left-hand side. Run the following lines of code in the REPL:
>>> from adafruit_circuitplayground...

Calling functions on the button state change

In this recipe, we will learn how to call a function when the state of a button has changed. It is a common requirement that you only want an action to be performed when a button has a state change, as opposed to while the button is pushed down. This recipe demonstrates one technique that you can use to implement this requirement in your project.

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...

Let's perform the following steps:

  1. First...

Moving active LEDs with push buttons

In this recipe, we will learn how to move the active NeoPixel clockwise and counterclockwise depending on whether the left or right push button is pressed. This recipe goes beyond the simpler button and LED interactions shown in previous recipes. This is a more involved recipe, which will create the impression that button presses are moving the light in a circular motion around the board.

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...

Let&apos...

Playing a beep on the button press

In this recipe, we will learn how to play a beeping tone when the button is pressed. The previous recipes allowed us to interact with light using the buttons. This recipe will show you how to introduce button and sound interactions in your projects.

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...

Let's perform the following steps:

  1. Run the following lines of code in the REPL while keeping push button A pressed:
>>> from adafruit_circuitplayground...

Detecting touch on a touchpad

In this recipe, we will learn how to detect when a touchpad is touched and to print a message each time this event occurs. The Circuit Playground Express comes with a number of touchpad connectors that can be attached to all sorts of objects.

Essentially, anything that can conduct electricity can be used as a way to interact with your device. You can use wires, conductive thread, fruit, water, or copper tape to interact with your device.

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...

...

Monitoring the touchpad raw measurements

In this recipe, we will learn how to monitor the touchpad raw measurements, which is a very useful way to verify how much the touch threshold should be adjusted. It is very important to be able to directly read the raw sensor values that are coming from the touch sensors.

This level of detail is necessary when you want to correctly set touch thresholds or when you want to find out why the touchpads aren't responding the way you expect them to.

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...

...

Adjusting the touch threshold

In this recipe, we will learn how to adjust the sensitivity of the touchpad by changing the threshold value. This is used to decide whether a signal will be treated as a touch event not. This is an important setting to tweak and set to the correct value. If you don't, then a lot of your touch projects won't behave correctly.

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...

Let's perform the following steps:

  1. Run the following lines of code...
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