DEV Community

Cover image for Raspberry Pi Pico - Limit Switch
Arduino ESP32 Raspberry Pi Tutorials
Arduino ESP32 Raspberry Pi Tutorials

Posted on Originally published at newbiely.com

Raspberry Pi Pico - Limit Switch

A limit switch is a simple mechanical sensor that tells your circuit when a moving part has reached an endpoint, like a carriage hitting the end of a rail. This tutorial wires a limit switch to a Raspberry Pi Pico and reads its state with MicroPython, which is handy for any project where you need to detect physical position, like a 3D printer axis, a gate, or a DIY CNC rig.

The full tutorial, with every detail and the line-by-line code explanation, is on our site: Raspberry Pi Pico - Limit Switch

What you need

Wiring

Raspberry Pi Pico - Limit Switch wiring diagram

A limit switch has three pins: C (common), NO (normally open), and NC (normally closed). You only need two of the three pins to use it, giving four possible wiring combinations depending on which pair you pick and how you orient GND versus the input pin on the Pico. This tutorial uses the first of those four methods, connecting the switch to GPIO pin GP1 and GND on the Raspberry Pi Pico.

How it works

The switch's job is to detect when something reaches its travel limit, which is why it's also called a travel switch. The C pin is always active; NO closes the circuit only when the lever is pressed, and NC does the opposite. Because you can also swap which of the two pins connects to GND versus the GPIO input, there are actually eight ways to wire a limit switch to the Pico, even though this guide sticks to one.

Code

Like a pushbutton, a limit switch bounces mechanically, so the code needs debouncing to avoid false triggers. The DIYables_MicroPython_Button library handles this for you and also sets up an internal pull-up resistor, so you don't need an external one. The example creates a Button object on GPIO pin 1, sets a 50 millisecond debounce time, and calls loop() plus get_state() in a loop to print whether the switch is TOUCHED or UNTOUCHED. A second pattern, also covered, reacts only when the state changes from one to the other instead of continuously.

raspberry-pi-pico-limit-switch.py

"""
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
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")
Enter fullscreen mode Exit fullscreen mode

Full tutorial

The complete tutorial - wiring diagram, code explanation and troubleshooting - is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-limit-switch

Top comments (0)