0

I have connected a Raspberry Pi 3B+ and a L298N Dual H-Bridge using a combination of Digital IO and PWM pins on the PI, and for the life of me cannot figure out what I'm doing wrong.

The motor spins, momentarily, then the PI ceases operation. There are no logs that I can find, SSH connections cease, and the HDMI output continues, but is frozen on the PI. My thought is that some transient is spiking the PI, but even with separated power supplies, this keeps occurring.

The motor controller is receiving ~8.4 volts from a pair of 26650 cells, and I have a buck converter powering the PI, though disconnecting the buck converter and using a separate supply doesn't help.

GPIO 23/26 are being used for motor enable on the L298N (PCM controllable pins) and 21/22/4/5 are being used for directional enables.

The motors themselves are the Arduino Smart Car kit motors, and have a .1uF capacitor across both motor leads.

To the best of my knowledge, the above is all pretty standard, and well engineered to run without causing problems to the PI.

I'm also using a 16 channel I2C servo driver, and there are no problems controlling SG90 servos on it, even though its source power is the same 5V supply the PI runs off of.

Am I missing something here? My scope doesn't show any visible transients, though I doubt it takes much to spike the PI, and besides which, even when it's theoretically isolated, the PI still crashes.

Here's the circuit diagram for how things are connected for the h-bridge and motors. Logical Circuit Schematic

Here's the software component for reference of what's happening when it crashes. As soon as moveForward is called, the PI stops responding.

private static final Pin LEFT_SPEED_PIN = RaspiPin.GPIO_23;
private static final Pin RIGHT_SPEED_PIN = RaspiPin.GPIO_26;
private static final Pin LEFT_FORWARD_PIN = RaspiPin.GPIO_21;
private static final Pin LEFT_BACKWARD_PIN = RaspiPin.GPIO_22;
private static final Pin RIGHT_FORWARD_PIN = RaspiPin.GPIO_04;
private static final Pin RIGHT_BACKWARD_PIN = RaspiPin.GPIO_05;
private static final int PWM_MAX = 1023;

public DualWheelRobotMotorController() {
    leftSpeed = GpioFactory.getInstance().provisionPwmOutputPin(LEFT_SPEED_PIN,0);
    leftSpeed.setPwmRange(PWM_MAX);
    leftSpeed.setShutdownOptions(true,PinState.LOW);
    rightSpeed = GpioFactory.getInstance().provisionPwmOutputPin(RIGHT_SPEED_PIN, 0);
    rightSpeed.setPwmRange(PWM_MAX);
    rightSpeed.setShutdownOptions(true,PinState.LOW);
    leftForward = GpioFactory.getInstance().provisionDigitalOutputPin(LEFT_FORWARD_PIN, PinState.LOW);
    leftForward.setShutdownOptions(true,PinState.LOW);
    leftBackward = GpioFactory.getInstance().provisionDigitalOutputPin(LEFT_BACKWARD_PIN, PinState.LOW);
    leftBackward.setShutdownOptions(true,PinState.LOW);
    rightForward = GpioFactory.getInstance().provisionDigitalOutputPin(RIGHT_FORWARD_PIN, PinState.LOW);
    rightForward.setShutdownOptions(true,PinState.LOW);
    rightBackward = GpioFactory.getInstance().provisionDigitalOutputPin(RIGHT_BACKWARD_PIN, PinState.LOW);
    rightBackward.setShutdownOptions(true,PinState.LOW);

    Runtime.getRuntime().addShutdownHook(new Thread(() -> GpioFactory.getInstance().shutdown()));
}

private int calculatePwm(float speed) {
    if (speed < 0 || speed > 1) {
        throw new IllegalArgumentException("Speed must be a value between 0 and 1");
    }
    return Math.round(speed * PWM_MAX);
}
@Override
public void moveForward(float speed) {
    int pwm_speed = calculatePwm(speed);
    leftForward.high();
    leftBackward.low();
    rightForward.high();
    rightBackward.low();
    leftSpeed.setPwm(pwm_speed);
    rightSpeed.setPwm(pwm_speed);
}
5
  • 1
    Can you post some pics that show all of your connections? What happens if you power the Pi with the standalone charger and temporarily remove the servo controller? Have you connected all of the grounds together? Commented Jun 22, 2017 at 15:40
  • Don't have a full diagram yet, but I'll try and get one added shortly. A standalone charger doesn't help much: Once the motor spins, the pi stops responding, and operates the same up to that point. The grounds are all commoned together at the H-Bridge board terminals. That's a requirement actually since the h-bridge takes a motor drive power source, and a +5v logic source, with a single common ground terminal for both. Commented Jun 22, 2017 at 23:52
  • New findings: The H-Bridge was back feeding to the PI via the VLS pin at 5.35 V or so. While this won't kill the PI itself, that's high for the logic pins on the PI, so if it fed any back to the GPIO (it shouldn't) then that's potentially critical. In any event, I killed the back feeding, and for testing disconnected the unit and ran the software again... lockup. Also, my main PSU was running at exactly 5.0V, and the PI was showing power warnings during boot. It's a 2.5-3A buck converter, so I upped the voltage to 5.35. Still locks on execution, but looks like software now. Commented Jun 23, 2017 at 4:15
  • After boosting power, and running the gpio command under Raspian, I was able to spin the wheels without locking the unit. It appears my basic circuit layout isn't a problem, but instead that I'm either using the wrong pin identifiers, or otherwise abusing the Pi4j/WiringPi libraries in a way to make the whole device crash. Conclusion: There is a software bug causing the issue, not the underlying hardware. Commented Jun 23, 2017 at 5:08
  • The Pi's power requirement is 4.75 to 5.25v. (5v +/- 5%). I'd lower the power to 5.2v.
    – NomadMaker
    Commented Jun 2, 2018 at 23:48

1 Answer 1

0

Correct answer: You MUST run Pi4j applications using the sudo command! Pi4j REQUIRES root privileges to run properly, and will lock the device if not run as root.

My problem turned out to be software, not hardware. The Pi itself, and all of the motor connections, as shown above, are fine. (Although I do recommend putting small ceramic caps on the motor terminals)

Note about the LM298N: If you are using a pre-assembled kit, it may already have a voltage regulator onboard, enabled by a jumper. This will backfeed power to your Raspberry Pi if not turned off. On mine, this is a 78M05, which is a 5v linear regulator with a 700mA output, so I would tend to advise against going this route to actually power the Raspberry Pi as it will both waste power, and provide insufficient current.

2
  • Please accept your answer if you feel it is the best. That should close the question, and your answer feels right. I fully agree about the caps on the motors, usually I use three: two between each terminal and the casing of the motor and one between the terminals. Motors can be very noisy.
    – NomadMaker
    Commented Jul 3, 2018 at 17:03
  • As an addendum, recent releases of the Pi4j library will run without root privileges, provided you enable non-root access to GPIO. (Read the Pi4j wiki on the topic) Commented Feb 22, 2019 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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