Button v1.2 / digitalRead

Hi @All,
im using the GrovePi+ StarterKit on Raspberry Pi 3.

I have a serious problem with the “Button v1.2”. The buttonState switches
sometimes to 1 but the button was not pressed! When i increase the delay
to read the state, the problem happens less times in a while loop, but the
examples use 100mil secs.
I tried python and c++ sources, but its the same problem in both languages.

Is it a hardware or a code problem?

thanks in advance

Code c++:

void checkButton(int button_state, int button_pin)
{
    button_state = digitalRead(button_pin); // read the button state
    printf("[pin %d][button state = ", button_pin); // and print it on screen
    if(button_state == 0)
        printf("not pressed]\n");
    else
        printf("pressed]\n");
    delay(100); // wait 100 ms for the next reading
}

Hi @chkr,

Can you give us a dataset containing analog values taken from the port to which the button is attached? A spacing of 10ms between values and 5 seconds worth of data should be enough. You can save the data in a file a then attach it here.
We want to see what’s the Voltage vs Time graph on the port on which the button is attached.

Generally, this happens when there are interferences, like when someone is touching the bottom of the button or when there are purely electromagnetic interferences (from different electronic devices that are around). It might also be that the Grove cable is not completely pushed in the GrovePi.

Couldn’t hurt to also post a photo of your setup.

Thank you!