The Circuit Playground Express and Bluefruit have a slide switch on it, above the battery connector. Though the images are of the Circuit Playground Express, the switch is in the same location on the Bluefruit. The slide switch doesn't control the power of the board. It is a switch that returns True or False depending on whether it's left or right. So, you can use it as a toggle switch in your code! Let's take a look.

Add the following code to your code.py. Remember, if you need help with this, check here.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example prints the status of the slide switch. Try moving the switch back and forth to see
what's printed to the serial console!"""
import time
from adafruit_circuitplayground import cp

while True:
    print("Slide switch:", cp.switch)
    time.sleep(0.1)

Open the serial console to see the switch status printed out. Try moving the slide switch back and forth to see the status change!

Let's take a look at the code. First we import time and cp.

Then, inside our while loop, we print the status of the switch to the serial console. This will print True if the switch is to the left, and False is the switch is to the right. We include a time.sleep(0.1) to slow down the printed output. To see the results, click the Serial button in Mu, or connect to the serial console if you're not using Mu. If the switch is to the left, you'll see Slide switch: True printing the serial console. If the switch is to the right, you'll see Slide switch: False printing to the serial console.

circuitpython_SlideSwitchIsRight.jpg
Slide switch is to the right. Slide it to the left!

Simple enough, right? Now, let's do something with it!

Blinky Switch

We just learned how to turn the little red LED on and off. Now let's incorporate an input to control it. Since the switch returns True or False, we can use it as an input.

Add the following code to your code.py.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example uses the slide switch to control the little red LED."""
from adafruit_circuitplayground import cp

# This code is written to be readable versus being Pylint compliant.
# pylint: disable=simplifiable-if-statement

while True:
    if cp.switch:
        cp.red_led = True
    else:
        cp.red_led = False

After importing cp, our loop starts with an if statement. An if statement says, "if this event is happening, do the following." Our code says, if the switch is to the left, or True, turn on the red LED.

Note that we don't have to say if cp.switch == True:. The True is implied in the if statement.

This is followed by an else statement. And else statement says, "Otherwise, do the following." An else typically follows an if. Together they say, "If this is happening, do this first thing, otherwise, do the second thing." Our code says, when the switch is to the right, or False, turn off the red LED.

Now, try moving the switch back and forth. Your red LED will turn on and off!

True is True

You may have noticed that when the switch is to the right, it's True, and when the LED is on, it is also True. We can use this to make our code even shorter. We started with the if/else block because it's easier to understand what's happening when it's written out. However, the following code does the same thing. Add the code to your code.py.

# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
# SPDX-License-Identifier: MIT

"""This example uses the slide switch to control the little red LED. When the switch is to the
right it returns False, and when it's to the left, it returns True."""
from adafruit_circuitplayground import cp

while True:
    cp.red_led = cp.switch

Whatever the switch is returning is what it will set the red LED to. So, if the switch is returning True, the LED is True. If the switch is False, the LED will be False. True is True, False is False. Move the switch back and forth and you'll still be turning the red LED on and off with this shorter code!

This guide was first published on Jun 05, 2018. It was last updated on May 22, 2018.

This page (Slide Switch) was last updated on Apr 01, 2023.

Text editor powered by tinymce.