Imx6 apalis & ixora carrier board & gpio simple switch button if it is pressed or not


I am trying to get the value of a push button which is connected to gpio and placed on a breadboard

I followed this link Basic GPIO usage - Ixora Carrier Board - Apalis iMX6
but it only show “0” whether or not if I press the button, it returns every time value of “0”

I need to understand if button is pressed or not.

also this is the simple script

int main(int argc, char *argv[]){
int fd;
char value;

// export GPIO
fd = open("/sys/class/gpio/export", O_WRONLY);
write(fd, "39", 2);

// configure as input
fd = open("/sys/class/gpio/gpio39/direction", O_WRONLY);
write(fd, "in", 2);

// read GPIO once
fd = open("/sys/class/gpio/gpio39/value", O_RDONLY);
read(fd, &value, 1); // read GPIO value
printf("GPIO value: %c\n", value); // print GPIO value
close(fd); //close value file

return 0; 

I will support a picture also. alt text

Thanks for help.

It’s not clear from your picture - how exactly that button connected to Ixora board?


I am sending new picture

when I run the code like this “./code” if I don’t press the button it could show 0, but if I press the button while running the code it should show 1 or something else that shows it is pressed but it doesn’t.

green cable is connected to pin 16 which is “39” and the yellow one is ground

I tried it with different pins and settings, but it is still showing “0”

Thanks for help

alt text

I believe you did get the pin mapping from Ixora extension header to Linux kernel GPIO number right. However, I suspect your circuit to be flawed as you only seem to provide one single level being ground. What level should it be when your button is not pressed? You probably want a pull-up resistor there in that case or you would need to configure such SoC internal pull-up explicitly via pin muxing.


I am adding the solution here

With same codes above may be little bit changed but not too much, you should use switch button with 3.3V

also here is an image

May be it helps to someone

Good luck.

alt text

You are very welcome.