How to solve "ValueError: The channel sent is invalid on a Raspberry Pi" error?

3.6k Views Asked by At

I have a python code to get readings from a light sensor. I have upgraded the raspberry pi too. But when I tried to execute the code it gives me an error as follows

Error message

Here is my python code

#!/usr/local/bin/python

import RPi.GPIO as GPIO
import time


GPIO.setmode(GPIO.BOARD)
pin_to_circuit = 17

def rc_time (pin_to_circuit):
    count = 0
    GPIO.setup(pin_to_circuit, GPIO.OUT)
    GPIO.output(pin_to_circuit, GPIO.LOW)
    time.sleep(0.1)

    GPIO.setup(pin_to_circuit, GPIO.IN)

    while (GPIO.input(pin_to_circuit) == GPIO.LOW):
         count += 1

    return count

try:
    while True:
        print rc_time(pin_to_circuit)
except KeyboardInterrupt:
    pass
finally:
    GPIO.cleanup()

I have tried so many solutions found on the internet but none of it worked. Please help.

1

There are 1 best solutions below

0
On

Raspberry Pi has two ways to address the GPIO pins: GPIO.BCM and GPIO.BOARD. While BCM is how the broadcom chip, which is the brain of rpi, sees the pinheader, BOARD is relatively easy for we humans to read and address.

You have used GPIO.BOARD, and trying to address pin 17. The image below will clear that pin 17 is a GPIO as per GPIO.BCM, but according GPIO.BOARD, it's pin 11.

So, basically, you are trying to configure pin 17 which come out to be a supply 3V3, which is not user configurable.

All you need to do is either change GPIO.setmode(GPIO.BOARD) to GPIO.setmode(GPIO.BCM) or change pin_to_circuit to pin number 11.

rpi gpio header