I want my Raspberry Pi B+ Model to generate a triangle wave and output a PWM signal from one of the GPIO pins, but I'm not sure how to go about doing this. All I want it to do is add some small length to every next pulse until it hits 100%, then it subtracts some amount from every pulse width. So if the triangle wave is at its high peak the PWM is at 100% duty cycle, and if it's at its low peak the PWM is at 0% duty cycle. Any ideas or suggestions? Thank you in advance!
New note: For 6.28 seconds from 0% to 100% at 500 Hz
-
You need to clarify your requirements. As a minimum the length of the maximum pulse, the number of steps between 0 and maximum pulse, the time spent at each step.joan– joan2014年10月21日 06:10:39 +00:00Commented Oct 21, 2014 at 6:10
-
6.28 seconds from 0% to 100% at 500 Hzashlyk321– ashlyk3212014年10月21日 23:05:28 +00:00Commented Oct 21, 2014 at 23:05
1 Answer 1
There may be many ways of doing what you want. You still need to tighten up your requirements. There are many variables which are not clear.
Here is a possible solution using my pigpio library.
#!/usr/bin/env python
# 2014年10月22日
# sweep-pwm.py
# Public Domain
import time
import pigpio
FREQUENCY=500
SWEEP=6.28
GPIO=4
LOOPS=10
pi = pigpio.pi() # Connect to local Pi.
f = pi.set_PWM_frequency(GPIO, FREQUENCY)
if f != FREQUENCY:
print("Can't set frequency {}.".format(FREQUENCY))
pi.stop()
exit()
# Find real number of steps from min to max
# and use this value in the loop.
r = pi.get_PWM_real_range(GPIO)
pi.set_PWM_range(GPIO, r)
for i in range(LOOPS):
for s in range(r+1):
pi.set_PWM_dutycycle(GPIO, s)
time.sleep(SWEEP/r)
pi.set_PWM_dutycycle(GPIO, s) # Stop PWM on gpio.
pi.stop() # Close connection.