Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can I detect button pressing with GPIO? #14

Open
ruiqurm opened this issue May 12, 2024 · 0 comments
Open

How can I detect button pressing with GPIO? #14

ruiqurm opened this issue May 12, 2024 · 0 comments

Comments

@ruiqurm
Copy link

ruiqurm commented May 12, 2024

Hi guys. I am trying to detect button actions with a GPIO pin and a GND pin at milk-v DUO.

I connected the GPIO14 pin and the button to the GND pin. In the program, I set the GPIO14 in an input mode and read digital value per second, which worked for Raspberry Pi 4b GPIO. However when I closed the switch, it seemed to short-circuit, and the Milkv automatically restarted. I googled it and found it seems I need a resistance between the switch and the two pins. I managed to add a 150Ω resistance but the GPIO14 can not read the switch closing this time.
I am a newbie in embedded development. If there are any mistakes in what I did above, or if you have better suggestions, I would be happy to accept them. Thank you in advance.

The code:

#include <stdio.h>
#include <unistd.h>
#include <wiringx.h>

#define GPIO_14 19

int main() {

    if(wiringXSetup("duo", NULL) == -1) {
        wiringXGC();
        return -1;
    }

    if(wiringXValidGPIO(GPIO_14) != 0) {
        printf("Invalid GPIO %d\n", GPIO_14);
        return -1;
    }

    pinMode(GPIO_14, PINMODE_INPUT);

    while(1) {
        int ret;
        ret = digitalRead(GPIO_14);
        printf("value: %d\n", ret); // should print 0 if I close the switch or press the button?
        sleep(1);
    }

    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant