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.
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);
}