0

I appreciate any help in this puzzling situation - I'm at a loss here.

As part of a Robotics project, I'm trying to control a motor with a Raspberry Pi. The exact components I'm using are:

This Speed Controller works with signals similar to a servo, with PWM signals in the range 1050-1950 microseconds.

I followed the instructions on this manufacturer's video, and adapted the instructions that come for ARDUINO to a Raspberry. This resulted in the following wiring:

Wiring

I also tried to code a very simple code that would do the same as the manufacturers example code, to generate the proper PWM signals to

start at rest, accelerate the motor in one direction until 50% max speed, decelerate, halt, accelerate in the other direction until 50% max speed, decelerate, finally halt again.

I calculated the duty cycles to do this in arbitrary steps. I hope the calculation are right: For a 50Hz PWM signal, I believe that a duty cycle of 7.5% means rest (1500us), while 5.25% means full reverse (1050us) while 9.75% means full forward speed (1950us).

import RPi.GPIO as GPIO
from time import sleep

print('Starting Program')

# Set the GPIO to be used with BOARD numbers:
GPIO.setmode(GPIO.BOARD)

# Setup the PIN 18 as a GPIO output:
GPIO.setup(18,GPIO.OUT)

# Creates the PWM object in PIN 18 with frequency 50Hz:
PWM_object = GPIO.PWM(18,50)

try:

    # Moving Forward, increase duty cycle..
    # Accelerating..
    # Temba, at rest:
    PWM_object.start(7.5)
    sleep(2)
    # Step 1
    PWM_object.ChangeDutyCycle(7.725)
    sleep(5)
    # Step 2
    PWM_object.ChangeDutyCycle(7.95)
    sleep(5)
    # Step 3
    PWM_object.ChangeDutyCycle(8.175)
    sleep(5)
    # Step 4
    PWM_object.ChangeDutyCycle(8.4)
    sleep(5)
    # Step 5: 50% forward speed
    PWM_object.ChangeDutyCycle(8.625)
    sleep(5)
    # Decelerating..
    # Step 6
    PWM_object.ChangeDutyCycle(8.4)
    sleep(5)
    # Step 7
    PWM_object.ChangeDutyCycle(8.175)
    sleep(5)
    # Step 8
    PWM_object.ChangeDutyCycle(7.95)
    sleep(5)
    # Step 9
    PWM_object.ChangeDutyCycle(7.725)
    sleep(5)
    # Step 10: Rest
    PWM_object.ChangeDutyCycle(7.5)
    sleep(5)

    # Moving Backward, decreasing duty cycle..
    # Accelerating..
    # Step 11: Reversing
    PWM_object.ChangeDutyCycle(7.275)
    sleep(5)
    # Step 12:
    PWM_object.ChangeDutyCycle(7.05)
    sleep(5)
    # Step 13:
    PWM_object.ChangeDutyCycle(6.825)
    sleep(5)
    # Step 14:
    PWM_object.ChangeDutyCycle(6.6)
    sleep(5)
    # Step 15: 50% reverse speed
    PWM_object.ChangeDutyCycle(6.375)
    sleep(5)
    # Decelerating..
    # Step 16:
    PWM_object.ChangeDutyCycle(6.6)
    sleep(5)
    # Step 17:
    PWM_object.ChangeDutyCycle(6.825)
    sleep(5)
    # Step 18:
    PWM_object.ChangeDutyCycle(7.05)
    sleep(5)
    # Step 19:
    PWM_object.ChangeDutyCycle(7.275)
    sleep(5)
    # Step 20: Rest
    PWM_object.ChangeDutyCycle(7.5)
    sleep(5)

    PWM_object.stop()
    GPIO.cleanup()

except KeyboardInterrupt:
    PWM_object.stop()
    GPIO.cleanup()

print('Program Complete.')

However, the motor moved clockwise (the first part of the code) but not counterclockwise (the second part).

I've tried, to no luck:

  • Hooking up the battery directly to the motor, with both polarities --> It spins both ways fine.
  • Send the motor straight to full reverse --> Did not move.
  • Change the signal PIN to the speed controller with code above --> Did not move too.
  • Repeat the test with an ARDUINO, and the manufacturer's code --> It moved successfully in both directions, hinting that the problem is with the RPi or my code.

How can I make this motor move both ways?

6
  • Try sending the proper pulses with pigpio. E.g. pigs s 24 1500, pigs s 24 1400 etc. This assumes you have correctly connected to GPIO 24 (pin 18) and ground. abyz.me.uk/rpi/pigpio/pigs.html#S/SERVO
    – joan
    Commented Jun 21, 2023 at 7:27
  • The link you posted to the Speed Controller seems totally devoid of any meaningful data about PWM, and your assumptions seem questionable (I doubt any device would limit itself to such a narrow range). 50Hz seems improbably low.
    – Milliways
    Commented Jun 21, 2023 at 8:06
  • Specifying a PWM signal by duration rather than duty cycle makes no sense (at least without specifying frequency). Arduino usually produces ~500Hz PWM (from memory).
    – Milliways
    Commented Jun 21, 2023 at 8:22
  • I'd also try without the level shifter. That could be wired wrongly (as I guess you did not use one with the Arduino).
    – joan
    Commented Jun 21, 2023 at 19:34
  • Thanks for the input. I'll try the three things as soon I've got some spare time, using pigpio, increasing the PWM frequency and repeating without the level shifter. You are correct @joan, I did not wire the level shifter for the ARDUINO attempt. I could not locate any real documentation for this motor controller except for what is provided in that retail website. I doubt there is one. Commented Jun 23, 2023 at 2:21

1 Answer 1

0

First, I just removed the level shifter, and tried my code. Although the motor did move backward, it started to behave erratically when moving backwards, sometimes even moving when the python code was not being executed.

As per joan suggestion, I used the pigpiod library instead of the code above. Note that the level shifter was removed as well. it worked flawlessly after that, with the motor responding quickly to both directions, at whatever speed was set by the pigs commands.

Thanks for everyone that helped clear this issue, you have no idea how you saved my life. By the way joan should you desire to leave an answer too I'll accept it instead of my own.

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.