# Raspberry Pi & L298N & Johnny Five, motor turns when instantiated

I'm a newcomer to this kind of tinkering so please bear with me here. I've searched high and low for an answer but have been unable to find one. I have a Raspberry Pi Zero W hooked up to a L298N motor controller and want to turn a motor forward or backward using Johnny Five. Oddly enough, when I do a simple trial of simply instantiating the motor via script, it begins turning without instruction and won't stop until I reboot the Pi; even killing the script doesn't stop the motor once it's started. Here's the code I'm using which starts the motor:


const raspi = require('raspi-io').RaspiIO
const five = require('johnny-five')
const board = new five.Board({
io: new raspi()
})

const motor = new five.Motor(['GPIO23', 'GPIO24'])
})



That's it. the new five.Motor call is the one that starts the motor going.

Script/lib info:

NodeJS: v10.15.0
"johnny-five": "^1.4.0"
"raspi-io": "^11.0.0"


And, an image describing my wiring:

I have the L298N using 5v power from the Pi board, ground connected, and the in1/in2 is hooked to GPIO23 and GPIO24.

If I run the script with GPIO23 connected but not GPIO24, the motor does not run. If the opposite is true (GPIO24 connected but not GPIO23), then the motor runs on startup.

It seems like what I'm trying to do is very simple, but I also understand that I'm at the mercy of two libraries (raspi-io and johnny-five) that I may not be using properly. I've tried my best to follow all examples I could find (especially This one and pared my code back to the bare minimum to still create this effect. Any help would be greatly appreciated.

In reading through the Johnny Five examples, it seems like the basic Motor command is supposed to be used if you're driving a motor directly. You're not, though - you're using an H-Bridge configuration. There is an example for that as well that gives the following for the motor command:

const motor = new Motor({
pins: {
pwm: 3,
dir: 12
},
invertPWM: true
});


It's not immediately clear to me if you need the invertPWM: true line or not, but try it and if it doesn't work then remove it. It just flips between a default of "low" or "high," so having it one way will be stopped at 0 and running at 255 and having it wrong will make it run at 0 and stop at 255.

Anyways, looks like once you've got the motor configured, run it with motor.forward() or motor.reverse() and put your desired speed (0-255) in the parenthesis.

You need to make sure your GPIO pins are able to do PWM, though.

• invertPWM: true caused the motor to turn the other direction, but it still starts turning on script start and still won't stop even if I kill the script. – Matt May 18 '20 at 17:35
• For what it's worth, I also swapped out cables, changed GPIO headers to 3 different sets, and used the "B" side of the L298 board in case that was the problem. Same behavior despite all those changes so this tells me it's a problem in the setup or a bug in the libraries. – Matt May 18 '20 at 17:36
• @Matt - Did you try writing any speed commands? motor.forward(0), or motor.forward(255), etc? – Chuck May 18 '20 at 17:56
• @Matt - It doesn't look like those pins are setup for PWM. You need to use PWM pins. Here's a graphic for your board I found - try pins 32 and 35. – Chuck May 18 '20 at 19:11
• @Matt - Happy to help! I think you only need to have ONE of the pins with PWM; which one needs it would be defined in your motor creation line. You could, I believe, use pins 32 and 31 (where 31 is NOT a PWM pin) with const motor = new Motor({pins: {pwm: 32, dir: 31}, invertPWM: true}); and I believe that would work. This would free up the other PWM pin (35) for use with other motors or anything else you want with an analog output. – Chuck May 18 '20 at 19:18