Arduino MicroPython Switch
This guide teaches you how to use an ON/OFF switch with an Arduino and MicroPython. You will learn:
- How to connect the ON/OFF switch to the Arduino.
- How to write MicroPython code for the Arduino to detect the ON/OFF switch's status.
- How to write MicroPython code for the Arduino to recognize events triggered by the ON/OFF switch.
Hardware Preparation
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables.
Overview of ON/OFF Switch
An ON/OFF switch changes from ON to OFF, or from OFF to ON, when pressed. It stays in the new position after being released. Press it again to toggle it back.
Pinout
There are two primary kinds of ON/OFF switches: the two-pin switch and the three-pin switch.
In this guide, we use a switch with two pins. You do not need to worry about which pin is which.
How It Works
Here are two ways to use an ON/OFF switch. Below, you will find the wiring instructions for the ON/OFF switch and the status readings on the Arduino for each way:
pin 1 | pin 2 | Arduino Input Pin's State | |
---|---|---|---|
1 | GND | Arduino Input Pin (with pull-up) | HIGH ⇒ OFF, LOW ⇒ ON |
2 | VCC | Arduino Input Pin (with pull-down) | HIGH ⇒ ON, LOW ⇒ OFF |
We just have to choose one of the two methods. The rest of the guide will use the first method.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
We recommend using a Soldering Iron to securely solder the wires and the ON/OFF switch pin. After that, cover them with Heat Shrink Tube for protection.
Arduino MicroPython Code - ON/OFF Switch
An ON/OFF switch, similar to a button, requires debouncing. Debouncing can make the code more complex. Fortunately, the DIYables_MicroPython_Button library helps by providing a debouncing feature and includes an internal pull-up resistor, which makes programming easier.
※ NOTE THAT:
There are two use cases:
- First: If the switch is ON, do something. If the switch is OFF, do the opposite.
- Second: If the switch goes from ON to OFF or from OFF to ON, do something.
Let's learn how to program Arduino one by one.
Arduino - Read state of ON/OFF Switch
Detailed Instructions
Here’s instructions on how to run the above MicroPython code on Arduino with Thonny IDE:
- Make sure Thonny IDE is installed on your computer.
- Make sure MicroPython firmware is installed on your Arduino board.
- If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
- Connect the ON/OFF switch to the Arduino according to the provided diagram.
- Connect the Arduino board to your computer with a USB cable.
- Open Thonny IDE and go to Tools Options.
- Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
- Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
- On Thonny IDE, navigate to the Tools Manage packages on the Thonny IDE.
- Search “DIYables-MicroPython-Button”, then find the Button library created by DIYables.
- Click on DIYables-MicroPython-Button, then click Install button to install Button library.
- Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
- Save the MicroPython code to your Arduino by:
- Clicking the Save button or pressing Ctrl+S.
- In the save dialog, choose MicroPython device and name the file main.py.
- Click the green Run button (or press F5) to execute the code.
- Move the switch to the ON position.
- Check out the message in the Shell at the bottom of Thonny.
- Switch to the OFF position.
- Check out the message in the Shell at the bottom of Thonny.
Arduino - Detect Event from ON/OFF Switch
Detailed Instructions
- Copy the above code and paste it to the Thonny IDE's editor.
- Save the script to your Arduino
- Move switch between ON/OFF several time.
- Check out the message in the Shell at the bottom of Thonny.