1

Following commands work well as full speed PWM.

echo "s 18 2000" >/dev/pigpio

pigs s 18 2000

But following code do nothing. I am using Raspberry Pi4 8G B model

import RPi.GPIO as GPIO
import time

# use for overwrite testing
GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)

print("start")
soft_pwm = GPIO.PWM(12, 100)
soft_pwm.start(100)
time.sleep(5)
print("finish")
soft_pwm.stop()
GPIO.cleanup()

3 Answers 3

0

Try soft_pwm.start(50). I think 100 means fully on (i.e always high).

See https://sourceforge.net/p/raspberry-gpio-python/wiki/PWM/

0

The code you listed will NOT do ANYTHING useful.

It will print start & stop then finish a few milliseconds later and restore GPIO state.

(I haven't verified whether the settings are correct - presumably this will set PWM 100% i.e. HIGH level.)

If you are actually trying to use a servo RPi.GPIO.PWM is not appropriate. It can be done with hardware PWM (which RPi.GPIO does not support).

Mixing 2 different libraries which operate on different principles is a bad idea; stick to ONE.

I suggest you try gpiozero (a frontend for RPi.GPIO) which is well documented and has extensive tutorials.

0

Following code did my job.

import RPi.GPIO as GPIO
import time
import pigpio

pinNum = 23

# use for overwrite testing
GPIO.setwarnings(False)

GPIO.setmode(GPIO.BOARD)

print("start")
pi = pigpio.pi()
pi.set_servo_pulsewidth(pinNum, 2000)
time.sleep(5)
pi.set_servo_pulsewidth(pinNum, 1000)
time.sleep(5)
pi.set_servo_pulsewidth(pinNum, 0)
pi.stop()
print("finish")
1
  • 2
    Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Apr 20, 2022 at 20:38

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.