ValueError:发送的通道在 Raspberry Pi 上无效 - 使用 Python 控制 GPIO 引脚 2 (BOARD) 导致错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24226310/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:16:15  来源:igfitidea点击:

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

pythonraspberry-piraspbiangpio

提问by Georgi Angelov

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:

所以我有一个小风扇连接到引脚 6(接地)和引脚 2。我试图在需要时手动启动和停止风扇,但在尝试时出现此错误:

ValueError: The channel sent is invalid on a Raspberry Pi

ValueError:发送的通道在 Raspberry Pi 上无效

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

这是我以 root 身份执行的代码。它似乎适用于其他引脚但不适用于 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?

我不确定如何访问此引脚。有什么我做错了吗?

回答by duskwuff -inactive-

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.

你不能。Raspberry Pi 扩展接头的引脚 2 直接连接到 USB 电源——它不受 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.

不要尝试将风扇直接连接到 GPIO 引脚;它们不仅不能输出正确的电压,而且它们也不能提供/吸收足够的电流来运行风扇。尝试这样做很可能会损坏引脚驱动器,并且还可能损坏 BCM2835 的其他部分。

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

如果您需要打开和关闭 5V 风扇,您将需要一些支持硬件来控制它(例如,FET)。

回答by Jesus Cepeda

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 中似乎有两种模式。将 GPIO.setmode(GPIO.BOARD)改为

GPIO.setmode(GPIO.BCM) 

It worked for me on a clean installation of Raspbian

它在 Raspbian 的全新安装中对我有用

回答by Hernán Díaz

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

我认为你的错误是你给了一个 OUT 定义的引脚 pull_up_down

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

回答by Shalin Patel

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

在 GPIO.BOARD 模式下,Pin 2 为 5V,无法设置。

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

在将其转换为 GPIO.BCM 模式时,它实际上是 GPIO2。