Hello all,
I’m trying to access GPIO port to read HC-SR04 ultrasonic sensor values; when I try this code without the Grovepi plugged in the script works fine:
import time
import RPi.GPIO as GPIO
# Which GPIO's are used [0]=BCM Port Number [1]=BCM Name [2]=Use [3]=Pin
# ----------------------------------------------------------------------
arrgpio = [(23,"GPIO24","Echo",16),(24,"GPI23","Trig",18)]
# Set GPIO Channels
# -----------------
GPIO.setmode(GPIO.BCM)
GPIO.setup(arrgpio[0][0], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.setup(arrgpio[1][0], GPIO.OUT)
GPIO.output(arrgpio[1][0], False)
# A couple of variables
# ---------------------
EXIT = 0 # Infinite loop
decpulsetrigger = 0.0001 # Trigger duration
inttimeout = 2100 # Number of loop iterations before timeout called
# Wait for 2 seconds to allow the ultrasonics to settle (probably not needed)
# ---------------------------------------------------------------------------
print "Waiting for 2 seconds....."
time.sleep(2)
# Go
# --
print "Running...."
# Never ending loop
# -----------------
while EXIT == 0:
try:
# Trigger high for 0.0001s then low
GPIO.output(arrgpio[1][0], True)
time.sleep(decpulsetrigger)
GPIO.output(arrgpio[1][0], False)
# Wait for echo to go high (or timeout)
intcountdown = inttimeout
while (GPIO.input(arrgpio[0][0]) == 0 and intcountdown > 0):
intcountdown = intcountdown - 1
# If echo is high
if intcountdown > 0:
# Start timer and init timeout countdown
echostart = time.time()
intcountdown = inttimeout
# Wait for echo to go low (or timeout)
while (GPIO.input(arrgpio[0][0]) == 1 and intcountdown > 0):
intcountdown = intcountdown - 1
# Stop timer
echoend = time.time()
# Echo duration
echoduration = echoend - echostart
# Display distance
if intcountdown > 0:
intdistance = (echoduration*1000000)/58
print "Distance = " + str(intdistance) + "cm"
time.sleep(100)
else:
print "Distance - timeout"
# Wait at least .01s before re trig (or in this case .1s)
time.sleep(100)
except KeyboardInterrupt:
GPIO.cleanup()
<code></code>
When I try with the Grovepi plugged in I have this error:
RuntimeWarning: This channel is already in use, continuing anyway. Use GPIO.setwarnings(False) to disable warnings.
GPIO.setup(arrgpio[0][0], GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
Are the GPIO’s port available when Grovepi is plugged in?
Thanks, bye bye