简体   繁体   中英

Raspberry Pi Node.js GPIO control

I'm trying to command two motors connected with a l298n module from a RPI4. This is not working on node.js but the same script (sort of porting) in python works because motors run.

Python script:

import RPi.GPIO as GPIO
from time import sleep

in1 = 24
in2 = 23
en = 25

GPIO.setmode(GPIO.BCM)
GPIO.setup(in1,GPIO.OUT)
GPIO.setup(in2,GPIO.OUT)
GPIO.setup(en,GPIO.OUT)
GPIO.output(in1,GPIO.LOW)
GPIO.output(in2,GPIO.LOW)
p=GPIO.PWM(en,1000)
p.start(25)

while(1):
    GPIO.output(in1,GPIO.HIGH)
    GPIO.output(in2,GPIO.LOW)

JS script (maybe not interesting, launched with sudo):

var Gpio = require('pigpio').Gpio;

const in1 = new Gpio(24, {mode: Gpio.OUTPUT});
const in2 = new Gpio(23, {mode: Gpio.OUTPUT});
const en = new Gpio(25, {mode: Gpio.OUTPUT});

in1.digitalWrite(0);
in2.digitalWrite(0);
en.pwmWrite(255);
setTimeout(function(){ console.log("finish"); }, 10000);

pigpiod -v gives me version 71

Where is the problem? (PS: i started with https://www.npmjs.com/package/motor-l298n but when found that motors were not working i tried to go directly with pigpio). The project is in JS so python script is just to check that things works. Thanks

this code works:

const Gpio = require('pigpio').Gpio;

const in1 = new Gpio(24, {mode: Gpio.OUTPUT});
const in2 = new Gpio(23, {mode: Gpio.OUTPUT});
const en = new Gpio(25, {mode: Gpio.OUTPUT});

let dutyCycle = 0;
in1.digitalWrite(1);
    in2.digitalWrite(0);
setInterval(() => {
    en.pwmWrite(dutyCycle);

dutyCycle += 5;
if (dutyCycle > 255) {
    dutyCycle = 0;
}
}, 20);

so i'm gonna work on it

I believe that the error here is that both in1.digitalWrite(0) and in2.digitalWrite(0) have value of zero. The correct approach is that one should have the value of one: in1.digitalWrite(1); and in2.digitalWrite(0); OR in1.digitalWrite(0); and in2.digitalWrite(1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM