XIAO RP2040 “problem with reading the status of a limit switch”

Dear all,
I am experiencing a problem with the following code that is supposed to read the status values of the endpoints. However, the read values never change. Ruling out errors in the circuit, could there be problems in the code? Specifically, I have jumpered pin 9 with 3.3V, but the reading in the loop always reports the pin value as “low.”

Thanks in advance.

#include <stdio.h>
#include "pico/stdlib.h"
#include <string.h>

// Define motor control pins
#define enPinX    0  // Enable pin for axis X
#define stpPinX   1  // Step pin for axis X
#define dirPinX   2  // Direction pin for axis X

#define enPinY    3  // Enable pin for axis Y
#define stpPinY   4  // Step pin for axis Y
#define dirPinY   5  // Direction pin for axis Y

// Definisci i pin per i fine corsa
#define limitSwitchXMin 8 // Fine corsa per la posizione minima dell'asse X
#define limitSwitchXMax 9 // Fine corsa per la posizione massima dell'asse X

// Funzione per inviare una risposta via USB
void sendResponse(const char* response) {
    printf("%s", response); // Invia la stringa di risposta via USB
}

int main() {

    stdio_init_all(); // Inizializza stdio per USB
    
    // Enable motor pins as outputs
    gpio_init(enPinX);
    gpio_init(stpPinX);
    gpio_init(dirPinX);

    gpio_init(enPinY);
    gpio_init(stpPinY);
    gpio_init(dirPinY);

    gpio_set_dir(enPinX, GPIO_OUT);
    gpio_set_dir(stpPinX, GPIO_OUT);
    gpio_set_dir(dirPinX, GPIO_OUT);

    gpio_set_dir(enPinY, GPIO_OUT);
    gpio_set_dir(stpPinY, GPIO_OUT);
    gpio_set_dir(dirPinY, GPIO_OUT);

    // Initialize limit switch pins as inputs
    gpio_init(limitSwitchXMin);
    gpio_init(limitSwitchXMax);

    sleep_ms(100); 
    
    gpio_set_dir(limitSwitchXMin, GPIO_IN);
    gpio_set_dir(limitSwitchXMax, GPIO_IN);

    sleep_ms(100);
    // Disable motors initially
    gpio_put(enPinX, 0);
    gpio_put(enPinY, 0);

    gpio_put(stpPinX, 0);
    gpio_put(stpPinY, 0);

    char buffer[100];
    int index = 0;

    while (1) 
    {
        // Leggi lo stato dei fine corsa
        int minState = gpio_get(limitSwitchXMin); // Leggi il valore del fine corsa minimo
        int maxState = gpio_get(limitSwitchXMax); // Leggi il valore del fine corsa massimo

        // Invia i valori letti come stringhe
        sendResponse("\n\n");
        sendResponse("Valore fine corsa minimo: ");
        sendResponse(minState ? "1\n" : "0\n"); // Invia 1 se attivato, altrimenti 0
        sendResponse("Valore fine corsa massimo: ");
        sendResponse(maxState ? "1\n" : "0\n"); // Invia 1 se attivato, altrimenti 0
        sendResponse("\n\n");

        sleep_ms(100); // Aspetta 1 secondo prima della prossima lettura
    }
}

Hi there,
And welcome here…
So have you tried using the GPO pin numbers? Try that.

The ones in Grey are the upython.
HTH
GL :slight_smile: PJ :v:

Hi! the pin is the number D8

you may have burned the pin out… have you tried another pin? also if you are trying to do an analog read you are going to need to be on the orange pins… A0-A3

Hi there,
That would be P2 according to the picture.
GL :v: PJ

Should I use the name P2 even though I am using pico-sdk ?

Hi there,
Hmmm, I have not used that IDE or SDK,
You can try and set it (d8, p2, or pin9 ) High and check the port to see if the names are correct is about all.
is it micropython?

You can’t break anything so go for it , YOLO!
HTH
GL :slight_smile: PJ :v:

You have solved it, thank you!

Hi there,
That is Great , Good on :index_pointing_at_the_viewer: you working through it and thanks for contributing. Can you mark it as the solution so Others can Find it.
GL :slight_smile: PJ :v: