I have written a script that controls a L298N motor controller, and I have one strange issue with it. When the script ends, one of the two motors do not stop, and instead go full speed in forward direction.

The reason for this, it appears, is the fact that I am using GPIO.Cleanup() at the end of the script.

If, however, I do not use this method, everything shuts down nicely at the end of the script.

I am using 6 pins, 4 of which indicate the direction of the motor, and two are for PWM pulses.

    # Initial set-up
    GPIO.setup(ENA, GPIO.OUT)
    GPIO.setup(IN1, GPIO.OUT)
    GPIO.setup(IN2, GPIO.OUT)
    GPIO.setup(IN3, GPIO.OUT)
    GPIO.setup(IN4, GPIO.OUT)
    GPIO.setup(ENB, GPIO.OUT)

    self.direction = 0  # Indicates the direction. 1 for forward, -1 for reverse, 0 for stationary

    # Setting the direction pins to low
    GPIO.output(IN1, GPIO.LOW)
    GPIO.output(IN2, GPIO.LOW)
    GPIO.output(IN3, GPIO.LOW)
    GPIO.output(IN4, GPIO.LOW)

and each of the enabler pins (ENA & ENB) are instances of GPIO.PWM

    self.ENA = GPIO.PWM(ENA, 200)  # Send PWM pulses to the enabler pin at a frequency of 200hz
    self.ENB = GPIO.PWM(ENB, 200)  # Send PWM pulses to the enabler pin at a frequency of 200hz

My question is, should the cleanup method be used in such a case, and is this behaviour normal?

  • The behaviour is probably normal. I say probably as we would need to know which GPIO you are using to be sure.
    – joan
    Jul 26, 2018 at 9:16
  • @Joan the one with the issue is GPIO 2. Jul 26, 2018 at 19:52
  • As I say we need to know the GPIO you are using to be sure.
    – joan
    Jul 26, 2018 at 20:09
  • @joan What do you mean by that? The GPIO pin that returns to high voltage by default is GPIO 2. I am unsure why that is happening, but it appears that even after shutdown, that GPIO still has high voltage. Jul 30, 2018 at 10:20
  • We need to know the GPIO used for ENA, ENB, IN1, IN2, IN3, IN4. The likely problem is caused by the hard pull-up on GPIO 2 but without knowing the other GPIO being used we can't be sure.
    – joan
    Jul 30, 2018 at 18:51

1 Answer 1


The GPIO used are as follows (according to the comments)

3 IN1
4 IN2
17 IN3
27 IN4
22 ENB

The RPi.GPIO cleanup() method sets each GPIO used in the script to be an INPUT and also disables any internal pull on that GPIO (so the GPIO effectively floats between low and high in the absence of an external pull).

Most GPIO without pulls seem to float low so will be seen as low by outside circuitry (ask an electronics person why, I do not know).

However GPIO 2/3 both have quite strong hardware (1k8) pulls to 3V3 (for I2C) so they will float high.

The end result will probably be

2  ENA High
3  IN1 High
4  IN2 Low
17 IN3 Low
27 IN4 Low
22 ENB Low

So motor A will be spinning full speed.

Possible solutions are don't use the cleanup() method or don't use GPIO 2/3.

  • Thank you, I did not know this, and thought GPIO 2 should behave like any other GPIO. My issue is resolved. Aug 1, 2018 at 11:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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