1

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

asked Oct 21, 2014 at 1:22
2
  • 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. Commented Oct 21, 2014 at 6:10
  • 6.28 seconds from 0% to 100% at 500 Hz Commented Oct 21, 2014 at 23:05

1 Answer 1

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.

webm video

#!/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.
answered Oct 22, 2014 at 0:09

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.