0

I am controlling a stepper motor using PWM on a RPi 4 and python. The motor has a controller/driver (BLDC-8015A) that accepts 0-3.3V to set the rotational speed. According to the documentation, this signal can be analog or a PWM signal. Raspi does not have an analog option. That's why I'm going for the PWM approach.

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
pwm = GPIO.PWM(23,1000) #pwm on pin 23 at 1kHz
pwm.start(0) #should start with 0% == 0V, is 0.1A

However, this gives me something around 0.1 Amps and my motor rotates slowly. I assume its something similar to that: https://arduino.stackexchange.com/questions/75602/very-small-pwm-when-timer-says-zero

Is there a similar way for RPis, or a completely different reason and solution?

EDIT: TLDR: use proper power supply... While following some of the suggestions below I found that if I set a pin to 'low' it is not actually 0. Same thing again, I get a small voltage

GPIO.setmode(GPIO.BCM)
GPIO.setup(23,0)
GPIO.output(23,0)

Now it gets weird. I noticed that the voltage becomes higher if the Raspi is busy with other even minor tasks. For example, if I scroll quickly through my program in Thonny (python editor) the voltage increases from about 30mA to above 100mA. That doesn't sound much but is 3% duty. I then tried the original raspi power supply. No problem anymore. Before, I used a DC/DC converter to get from 24V battery voltage to 5.1V for the raspi. I guess I'll have to figure that out now.

1
  • 1
    Please provide a complete script which shows the problem.
    – joan
    Jul 12 at 9:43

2 Answers 2

0

You don't make it clear, but your incomplete code fragment seems to be using RPi.GPIO.

This uses software PWM, which is imprecise (always produces slight jitter) and unsuitable for a servo. It is unclear how "a stepper motor" would be controlled by PWM. Stepper motors are normally controlled by sequenced digital signals.

If you want precise PWM use a library which supports hardware PWM such as pigpio or my enhanced Pi.GPIO https://raspberrypi.stackexchange.com/a/117593/8697

1
  • Thanks, I now also tried pigpio using py = pigpio.py() py.hardware_PWM() I basically get the same result as with RPi.GPIO: small voltage and slow rotation. I also found that the problem might already occur before I use PWM. I will add something about that in the actual question Jul 12 at 19:25
0

I doubt you can rely on the GPIO having any particular state after the script exits. It exits after the pwm.start(0) of your script.

The GPIO is possibly reset to be an input which would leave the value floating.

1
  • That's something I didn't think about. Thanks. Although, most of the times I had a time.sleep() after the PWM start. But not always so this definitely will help future debugging. Jul 13 at 16:10

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.