Python hangs on digitalRead

Hi folks, I’m a newbie on grovePi, but I’ve some experience in python and Arduino, well…
The following simple script has no output e break it execution itself

import time
import grovepi

button = 3

grovepi.pinMode(button,"INPUT")

while True:
    try:
        print(grovepi.digitalRead(button))
        time.sleep(.5)
    except IOError:
        print ("Error")

Obviously tried other pins but the problem is still here, and tried the grove_led_blink.py script from the dexter repository that works with no hassle.
So I guess that my problem isn’t hardware related.
How I’m wrong?

1 Like

A couple of thoughts:

  1. Do you have the button connected between power and ground on the grove connector through a pull-up resistor?
     
    For example, if you have a normally closed switch and a 5k fractional watt resistor, you’d hook one end of the resistor to the positive voltage point, the other end to the switch, the other end of the switch to ground, and the center-point to your digital input pin.  With the switch un-pressed, you get a digital zero, (ground), and when pressed you get a digital one, (positive voltage).  If you use a normally open switch, you’ll get the same thing, except the logical polarity will be reversed - 1 when not pressed and 0 when pressed.
     
    If you don’t have a pull-up resistor connected, it won’t work because there’s no voltage reference.
     
  2. Are you absolutely sure you’re using the correct pin?

Let us know what happens!