×

Buttons and Switches - Digital Inputs in Python

 

The fourth installment of our micro:bit peripherals in Python series is an introductory look into buttons and switches and even gives some ideas for creating your own DIY buttons!

Check out the video demo here: https://youtu.be/jnnBvcvTxic

 

HOW DOES A BUTTON OR SWITCH WORK?

Buttons and switches are used in lots of different electronic products.  They can be used to start your car, turn on your bedroom light, or set your digital clock.  Most buttons and switches work the exact same way.  They simply close a circuit.  In other words, they just connect two wires together.  When a light switch is turned on, it connects the light to a power source which causes the light to turn on.  When it is turned off, it disconnects the power causing the light to turn off.  Like a switch, a button just connects the two input wires while the button is pressed down.

TALKING TO A MICRO:BIT:

The first three articles of our micro:bit peripherals series focused on sending signals out to a peripheral.  This time we will be reading an input instead! The micro:bit can be used to read inputs from its external pins.  You can read either analog signals or digital signals.  We will focus on the digital signal for buttons and switches.  Some of our later articles will cover analog signals.  

A micro:bit reads a digital signal from a pin using this function:

pin0.read_digital()

The micro:bit read_digital function will return a 0 if the voltage on the pin is near GND (0 Volts) or a 1 if the voltage on the input pin is closer to 3 Volts.  That sounds pretty vague for something as precise as digital electronics! Actually the microcontroller datasheet gives some guarantees for digital input levels relative to the power-supply voltage. 30% and 70% are the maximum “low” and minimum “high” levels respectively. With a 3V battery pack powering the micro:bit it looks like this:

By default the micro:bit pins are pulled low, which means they’ll read 0 when nothing is connected. If you connect a pin to 3V, then it will read 1. The “pull” is created by an internal resistance of about 13kΩ controlled by the microcontroller itself.

Hooking up an open (or off) switch acts the same way as if there is nothing connected to the input pin at all, so you’ll read 0 when the switch is off.  When the switch closes (or the button is pressed) the pin is connected to 3V, overcoming the pull-down. The read_digital() function will read 1 (or high) when the switch is closed.  Take a look at the following diagrams for a visual:

PULL DIRECTION:

The micro:bit will read 0 by default when the read_digital() function is called and nothing is connected to the read pin.  What if you want to read 1 by default instead?  Easy!  The micro:bit allows you to make the default value 1 with a simple function call.  You can call the function:

pin0.set_pull(pin0.PULL_UP)

This sets the “PULL DIRECTION” of the pin to UP instead of the default DOWN.  Now instead of connecting the closed side of the switch to the 3V pin, you can connect it to the GND pin.  When the switch is open (or off) the input pin will read 1 (or high).  When the switch is closed (or on) the input will read 0 (or low).  Make sure you connect the switch to the micro:bit’s GND to guarantee that it will read properly.

USING A UNIQUE BUTTON OR SWITCH:

Now you know how to read an input from a basic button or 2-position switch.  If you happen to purchase a button with four connection points, don’t worry.  Most surface mount buttons with four pads like the one shown below are simple 2-position switches behind the scenes.  The extra legs are just there to keep the button from moving around when it is placed on a circuit board.  Two of the connection points are usually tied together on one side and two are tied together on the other side.

 

3-position Switches and More!

The buttons and switches we’ve talked about so far just control a single connection. They’re called SPST switches – “single pole, single throw”. On an electronic schematic they might be shown like this:

There are lots of other switch configurations. Check them out here:

Common toggle switches like the one shown below have 3-terminals (SPDT configuration). Some of them have a center off position also.

You might think of a 3-position switch as two 2-position switches in one. One of the two 2-position switches will be on if the switch is moved in one direction.  The other switch will be on if the 3-position switch is moved in the opposite direction.  You can never have both switches on at the same time. If all you need is a simple SPST switch, you can just use two of the terminals. But if your switch has a center position, you can connect to two input pins on the micro:bit at the same time as shown, to sense all 3 positions:

MAKE A UNIQUE DIY SWITCH:

Building on the idea that a button is nothing more than a way to connect two wires you can easily build your own Do-it-Yourself switch at home.  Connect the 3V on your micro:bit to a piece of aluminum foil.  Now connect the pin0 input to a second piece of aluminum foil.  Then, touching the two pieces of aluminum foil together will be enough to act like a button. 

You could also try using copper tape in unique ways.  Copper tape is just metal that has a sticky side so you could put it on paper or the side of a piece of wood. 

What if you wanted to turn the metal zipper on your old jacket into a switch?  Just sew some conductive thread onto both sides of the zipper and connect the other ends to your micro:bit. 

I'm sure you have lots of other ideas... There are an infinite number of unique DIY switches that you can create!

 

HOW TO RUN THE EXAMPLES:

When running any of the code samples below you should:

  1. Connect the Button as outlined in the TALKING TO A MICRO:BIT section above
  2. Type one of the code examples below into https://make.firialabs.com
  3. Run the example - pressing the button should light up some LEDs on the micro:bit
If you haven't tried CodeSpace with the micro:bit, you're missing out on a FANTASTIC coding experience! Give it a try!
 

CODE EXAMPLE 1: BARE MINIMUM CODE

 

CODE EXAMPLE 2: AN EXAMPLE WITH FUNCTIONS

 

CODE EXAMPLE 3: AN OBJECT-ORIENTED “CLASS” EXAMPLE