rpi-gpio pin 总是 high/low

rpi-gpio pin always high/low

我们正在使用 rpi-gpio 和 node.js 到 read/write 覆盆子版本 1 引脚。

我们有一个连接到 gnd(引脚 3)和 gpio4(引脚 7)的 LED。

使用 write 非常有效:

gpio.setup(7, gpio.DIR_OUT, write);

function write() {
    gpio.write(7, true, function(err) {
        if (err) throw err;
        console.log('Written to pin');
    });
}

虽然使用了读取功能

gpio.setup(7, gpio.DIR_IN, readInput);

function readInput() {
    gpio.read(7, function(err, value) {
        console.log('The value is ' + value);
    });
}

导致以下行为:

1) LED灯熄灭 2) gpio pin的状态一直为true

将端口设置为 "true" 这样 LED 就亮了 没有不同。读取引脚时,LED 熄灭且引脚为真。

与引脚 11、12 相同但相反 - LED 熄灭且引脚为假。

使用侦听器来更改特定引脚上的值... 虽然我很想按需阅读pin-value!

对于写入,您将 GPIO 引脚设置为输出(这很好)。但是当你尝试 'read' 时,你将 GPIO 引脚设置为输入,这会改变引脚的模式。所以尝试删除:

gpio.setup(7, gpio.DIR_IN, readInput);

来自 Raspberry Pi 论坛:

Five of the 17 available GPIO lines are pulled high by default (the rest are pulled low). The numbers are GPIO0/2, GPIO1/3, GPIO4, GPIO7 and GPIO8.

将 gpio 引脚设置为 DIR_OUT 以进行写入。写入后,无需再次设置 gpio 即可读取引脚。这是我使用 gpio promise api:

的代码
    var gpio = require('rpi-gpio').promise
            
    const pin = 7
    gpio.setup(pin, gpio.DIR_OUT)
        .then(() => {
            gpio.write(pin, true)
                .then(() => {
                    gpio.read(pin)
                        .then(status => {
                            console.log('The status of the pin ', pin, ' is ', status)
                        })
                        .catch(err => {console.log(err.toString())})
                }).catch(err => {console.log(err.toString())})
         }).catch(err => {console.log(err.toString())})