Arduino MicroPython Switch

This guide teaches you how to use an ON/OFF switch with an Arduino and MicroPython. You will learn:

Arduino MicroPython ON/OFF Switch

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Wires
1×ON/OFF Square Switch
1×(Alternative) ON/OFF Round Switch
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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.

ON/OFF Switch Pinout

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

The wiring diagram between Arduino MicroPython ON/OFF Switch

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

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the on/off switch connected to the Arduino Giga WiFi pin D2 switch = Button('D2') # Set debounce time to 50 milliseconds (similar to the Arduino code) switch.set_debounce_time(50) while True: switch.loop() # Update the button state state = switch.get_state() if state == 1: # GPIO HIGH print("The switch: OFF") else: print("The switch: ON")

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF The switch: OFF The switch: OFF The switch: OFF The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: ON The switch: OFF The switch: OFF The switch: OFF
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Arduino - Detect Event from ON/OFF Switch

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the on/off switch connected to the Arduino Giga WiFi pin D2 switch = Button('D2') # Set debounce time to 50 milliseconds (similar to the Arduino code) switch.set_debounce_time(50) while True: switch.loop() # Update the button state if switch.is_pressed(): print("The switch: OFF -> ON") if switch.is_released(): print("The switch: ON -> OFF")

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.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The switch: OFF -> ON The switch: ON -> OFF The switch: OFF -> ON The switch: ON -> OFF
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!