Limit Switch Issues and Solutions - CodeClubLuxembourg/plottybot-toolkit-web GitHub Wiki

Situation

In our current setup, we are using limit switches connected to the GPIO pins of a Raspberry Pi. The GPIO pins are configured with internal pull-up resistors to ensure a stable default state. However, we are experiencing false positives, where the limit switches appear to be activated when they are not, especially when the motor is running.

Potential Problem

The false positives are likely caused by electrical noise or interference from the motor affecting the GPIO pins. This noise can cause the GPIO pins to register spurious signals, leading to incorrect readings of the limit switch states.

Suggested Solution

To diagnose and mitigate the issue, we recommend the following steps:

  1. Use an Oscilloscope: An oscilloscope can help you visualize the electrical signals on the GPIO pins and identify any noise or interference. By examining the signal waveforms, you can determine the nature and source of the noise.

  2. Electrical Connections:

    • External Pull-up Resistors: Use external pull-up resistors (e.g., 10kΩ) instead of relying on the internal pull-up resistors. This can provide better noise immunity.
    • Decoupling Capacitors: Place decoupling capacitors (e.g., 0.1µF) across the power supply lines of the motor and near the GPIO pins to filter out high-frequency noise.
    • Shielded Cables: Use shielded cables for the connections to the limit switches to reduce electromagnetic interference.
    • Twisted Pair Wires: Use twisted pair wires for the connections to the limit switches to further reduce the impact of electromagnetic interference.
    • Ferrite Beads: Place ferrite beads on the wires connecting the limit switches to the GPIO pins to suppress high-frequency noise.

Correct Code to Setup External Pull-up Resistors

Here is the updated code to configure the GPIO pins for the limit switches without using internal pull-up resistors. This assumes that you have added external pull-up resistors in your hardware setup.

import RPi.GPIO as GPIO

# Define GPIO pins for limit switches
limit_switch_top = 17
limit_switch_bottom = 4
limit_switch_left = 3
limit_switch_right = 2

# Setup GPIO pins with external pull-up resistors
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(limit_switch_top, GPIO.IN)  # External pull-up resistor used
GPIO.setup(limit_switch_bottom, GPIO.IN)  # External pull-up resistor used
GPIO.setup(limit_switch_left, GPIO.IN)  # External pull-up resistor used
GPIO.setup(limit_switch_right, GPIO.IN)  # External pull-up resistor used

# Example function to read limit switch states
def read_limit_switches():
    top_state = GPIO.input(limit_switch_top)
    bottom_state = GPIO.input(limit_switch_bottom)
    left_state = GPIO.input(limit_switch_left)
    right_state = GPIO.input(limit_switch_right)
    return top_state, bottom_state, left_state, right_state

# Main loop
try:
    while True:
        top, bottom, left, right = read_limit_switches()
        print(f"Top: {top}, Bottom: {bottom}, Left: {left}, Right: {right}")
        time.sleep(0.1)

except KeyboardInterrupt:
    GPIO.cleanup()

Summary

By using an oscilloscope to diagnose the issue and implementing the suggested electrical connections and code changes, you can reduce the likelihood of false positives caused by electrical noise. This will result in more reliable readings from the limit switches and improve the overall performance of your system.

This document should be added to your GitHub repository documentation under a title like "Limit Switch Issues and Solutions" to help others understand and address similar issues.

Reference: GPIO.setup Parameters

The GPIO.setup function is used to configure the behavior of a GPIO pin. Here are the parameters used in the function:

  • channel: The GPIO pin number to be configured. This can be specified using the BCM or BOARD numbering system.
  • direction: The direction of the pin, either GPIO.IN for input or GPIO.OUT for output.
  • pull_up_down: (Optional) Configures the internal pull-up or pull-down resistors. Possible values are:
    • GPIO.PUD_UP: Enables the internal pull-up resistor, which pulls the pin to a high state (3.3V) by default.
    • GPIO.PUD_DOWN: Enables the internal pull-down resistor, which pulls the pin to a low state (0V) by default.
    • GPIO.PUD_OFF: Disables the internal pull-up/down resistors (default if not specified).

Example usage:

GPIO.setup(channel, GPIO.IN, pull_up_down=GPIO.PUD_UP)

This configuration ensures that the GPIO pin reads a high signal (logical 1) when it is not connected to anything, preventing it from floating and picking up random noise.