Labs / Bipes + MicroPython / 7 Sensors: Enable Programs to "Perceive the World"

7.4 Does Your Robot Car Fear Hitting the Wall? Here’s How to Detect If an Object Reaches the End! —— Limit Switch Sensor

Author: Li Zisheng · Source: FreakStudio open documentation · License: CC BY 4.0

Contents

1. Module Introduction

In actual machines and equipment, how do you know that "it has reached the end of its service life"? For example:

The answer is —— limit switch.

A limit switch module is a position detection input module that immediately changes its output level when an object actuates the switch contacts, signaling to the development board:"I've been triggered / I've reached the limit position!"

It has the following distinct features:

In graphical programming, limit switches are mainly used with these blocks:

In the project, it is often used to:

2. Wiring Instructions

To make the development board "sense" your fingers, the first step is still —— connect the wires correctly!

7.4 Does Your Robot Car Fear Hitting the Wall? Here’s How to Detect If an Object Reaches the End! —— Limit Switch Sensor

We connect the TTP223 touch key module to the DIO0 interface of the development board, and the corresponding relationship is as follows:

Development Board Pins

Module Pins

Description

Pin 6

DIN

MCU upper limit switch signal input interface

Pin 7

NC

-

📌 Working Status Description (pull-up mode is used by default):

As long as the development board reads this level change, it can determine the current state of the limit switch.

3. Example: Check whether the limit switch is triggered

Experiment Objective: Implement the most basic limit detection function: when the limit switch is pressed, a prompt message will be printed on the terminal, and the printing will stop after the switch is released.

Everyday Thought: When you close a door at home and it clicks into place, that sound actually stems from a principle similar to limit detection.

The graphical program is as follows:

7.4 Does Your Robot Car Fear Hitting the Wall? Here’s How to Detect If an Object Reaches the End! —— Limit Switch Sensor

Building Block Assembly Steps:

  1. Infinite Loop Detection
    • Use the repeat while true block to keep the program running continuously;
  2. Read limit switch status
    • Use the read digital input block;
    • Pin Selection Pin 12 / GPIO9;
    • Set Pull-up to true;
  3. Determine whether to trigger
    • Use if not read digital input for judgment;
    • When a low level is read, it indicates that the limit switch has been pressed;
  4. Trigger Feedback
    • When the condition is true, add the print "Limit trigger detected" block.

Experimental Effect:

Graphical blocks will be automatically converted into MicroPython code for execution on the development board:

7.4 Does Your Robot Car Fear Hitting the Wall? Here’s How to Detect If an Object Reaches the End! —— Limit Switch Sensor

XML file (can be directly imported into building blocks):

Limit.xml 2 KB

Tips

  1. If the limit switch does not respond:
    • Check whether the pin is selected as GPIO9 / Pin12;
    • Whether to enable Pull-up;
  2. A limit switch is a mechanical structure:
    • Requires when triggered: press firmly and properly;
  3. If repeated triggering is detected:
    • A delay of 200~500 ms can be added;
    • Or use the logic of "trigger only once".

✨ Interactive Mini Quiz

  1. Think about it:
    If you remove the not, what will happen? And why?
  2. Give it a try:
    Reverse the logic of the LED (trigger on/off) and experience the difference.

Summary

  1. A limit switch is a position detection input module used to determine whether a "limit position" has been reached.
  2. It outputs digital signals (high/low), which are ideal for beginners to learn conditional judgment;
  3. Commonly used in scenarios such as safety protection and motion end point detection;
  4. Proper use of variables and delays can make limit switch detection more stable and reliable.