Georgi Angelov
Georgi Angelov

Reputation: 4388

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

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?

Upvotes: 13

Views: 60167

Answers (4)

Shalin Patel
Shalin Patel

Reputation: 31

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

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

Upvotes: 0

Jesus Cepeda
Jesus Cepeda

Reputation: 430

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

Upvotes: 33

Hernán Díaz
Hernán Díaz

Reputation: 11

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)

Upvotes: 1

user149341
user149341

Reputation:

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).

Upvotes: 0

Related Questions