10

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?

4 Answers 4

30

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

5
  • well this worked for me too. but please remove "emphasized text"
    – sudocoder
    Oct 9, 2014 at 17:18
  • Removed. Better later than never May 31, 2017 at 16:41
  • This was my mistake. I feel so dumb!
    – Phil
    Dec 18, 2017 at 10:16
  • 2
    Context for why this fixes the problem: raspberrypi.stackexchange.com/questions/12966/…
    – NJTabit
    Feb 16, 2018 at 21:59
  • I saw this last comment after long time and solved my doubts. Thanks Dec 10, 2018 at 12:00
1

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

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

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.