Raspberry Pi Pico - Limit Switch

This guide shows you how to use a limit switch with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico with Limit Switch

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Limit Switch (KW12-3)
1×Limit Switch (V-156-1C25)
1×Wires
1×(Optional) Heat Shrink Tubing
1×(Optional) Soldering Iron
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 Limit Switch

It is called Limit Switch because its main function is to detect when a moving object reaches its endpoint. It is also known as a travel switch. It is also called the travel switch.

Pinout

Different types of limit switches are commonly used, such as the KW12-3 and V-156-1C25. Each model includes 3 pins.

  • C pin: This is the common pin. It is used in both the open and closed settings.
  • NO pin: This means the Open pin. It is used when the setting is open.
  • NC pin: This means the Closed pin. It is used when the setting is closed.
Limit Switch Pinout
image source: diyables.io

How It Works

The limit switch has three pins, but usually, only two pins are used: the C pin and one of the other two pins. There are four ways to connect the limit switch. Here is a table showing the wiring of the limit switch and its readings on the Raspberry Pi Pico for all four methods:

C pin NO pin NC pin Raspberry Pi Pico Input Pin's State
1 GND Raspberry Pi Pico Input Pin (with pull-up) NOT connected HIGH when untouched, LOW when touched
2 GND NOT connected Raspberry Pi Pico Input Pin (with pull-up) LOW when untouched, HIGH when touched
3 VCC Raspberry Pi Pico Input Pin (with pull-down) NOT connected LOW when untouched, HIGH when touched
4 VCC NOT connected Raspberry Pi Pico Input Pin (with pull-down) HIGH when untouched, LOW when touched

We can change the positions of the GND pin and the input pin on the Raspberry Pi Pico for each method. This provides us with 8 different methods to connect the Raspberry Pi Pico to a limit switch.

We only need to choose one of the four methods mentioned earlier. For this tutorial, we will use the first method.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico limit switch

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Limit Switch

A limit switch, similar to a button, requires debouncing to function properly. Debouncing can make the code more complex. Fortunately, the DIYables_MicroPython_Button library provides a debouncing feature and includes an internal pull-up resistor, which simplifies the programming process.

Here are two usual situations:

  • The first: If the switch is TOUCHED, do something. If it is UNTOUCHED, do the opposite.
  • The second: If the switch goes from UNTOUCHED to TOUCHED or from TOUCHED to UNTOUCHED, do something.

Let's see how to write the MicroPython script in both cases.

Raspberry Pi Pico Code - Read Limit Switch's State

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-limit-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the limit switch connected to GPIO pin GP1 of the Raspberry Pi Pico limit_switch = Button(1) # Set debounce time to 50 milliseconds (similar to the Arduino code) limit_switch.set_debounce_time(50) while True: limit_switch.loop() # Update the button state state = limit_switch.get_state() if state == 1: # GPIO HIGH print("The limit switch: UNTOUCHED") else: print("The limit switch: TOUCHED")

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the limit switch according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 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.
Raspberry Pi Pico Button library
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press, hold, then release the limit switch while checking out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: TOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED The limit switch: UNTOUCHED
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Raspberry Pi Pico Code - Check Limit Switch's touched/untouched event

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-limit-switch """ from DIYables_MicroPython_Button import Button import time # Initialize the limit switch connected to GPIO pin GP1 of the Raspberry Pi Pico limit_switch = Button(1) # Set debounce time to 50 milliseconds (similar to the Arduino code) limit_switch.set_debounce_time(50) while True: limit_switch.loop() # Update the button state if limit_switch.is_pressed(): print("The limit switch: UNTOUCHED -> TOUCHED") if limit_switch.is_released(): print("The limit switch: TOUCHED -> UNTOUCHED")
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Press, hold, then release the limit switch while checking out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The limit switch: UNTOUCHED -> TOUCHED The limit switch: TOUCHED -> UNTOUCHED
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!