ValueError: The channel sent is invalid on a Raspberry Pi - Controlling GPIO Pin 2 (BOARD) using Python causes Error
Asked Answered
H

4

13

So I have a tiny little fan connected to pin 6(Ground) and pin 2. I am trying to manually start and stop the fan when needed but I am getting this error when trying:

ValueError: The channel sent is invalid on a Raspberry Pi

Here is my code that I am executing as root. It seems to be working on other pins but not Pin 2

import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BOARD)

GPIO.setup(2, GPIO.OUT, pull_up_down=GPIO.PUD_UP)

I am not sure how to access this pin. Is there something I am doing wrong?

Heredes answered 15/6, 2014 at 3:32 Comment(0)
K
33

It could be something stupid, i was looking exacty the same. It seems there are two modes in the GPIO. Change GPIO.setmode(GPIO.BOARD) to

GPIO.setmode(GPIO.BCM) 

It worked for me on a clean installation of Raspbian

Ken answered 2/9, 2014 at 19:20 Comment(5)
well this worked for me too. but please remove "emphasized text"Preacher
Removed. Better later than neverKen
This was my mistake. I feel so dumb!Ineffaceable
Context for why this fixes the problem: raspberrypi.stackexchange.com/questions/12966/…Husbandry
I saw this last comment after long time and solved my doubts. ThanksKen
S
1

I think that your mistake is that you gave pull_up_down to a OUT definied pin

#this is only for input pins
GPIO.setup(n, RPIO.OUT, initial=RPIO.LOW, pull_up_down=GPIO.PUD_UP)

#CORRECT ("initial" is optional)
GPIO.setup(n, RPIO.OUT, initial=RPIO.LOW)
Strickler answered 6/12, 2015 at 15:55 Comment(0)
O
0

You can't. Pin 2 of the Raspberry Pi expansion header is connected directly to the USB power supply — it isn't controlled by the CPU.

Do not try to connect the fan directly to a GPIO pin; not only do they not output the right voltage, but they can't source/sink enough current to run the fan either. Trying to do so is very likely to destroy the pin driver, and may cause damage to other parts of the BCM2835 as well.

If you need to turn a 5V fan on and off, you will need some support hardware to control it (e.g, a FET).

Ottavia answered 15/6, 2014 at 3:46 Comment(0)
B
0

In GPIO.BOARD mode, Pin 2 is 5V which cannot be setup.

While converting it into GPIO.BCM mode it is actually GPIO2.

Bodoni answered 9/4, 2019 at 16:50 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.