6

I need a signal at the output of the GIPO of approximately this shape.(sub-pulse in pulse)enter image description here

How can this be implemented using PWM on PI? Im trying do it with RPIO,but his ancient GPIO pinout maybe not working for my Rpi 3 b+.

from RPIO import PWM
servo = PWM.Servo()
servo.set_servo(12, 10000)
PWM.add_channel_pulse(0, 12, start=200, width=2000)

Not Signal on pin. enter image description here I'm confused in it and would like to try the built-in library to work with PWM, but I did not find there the possibility of sub-cycles. How else i can a signal of this form be output from different GPIO?

Dmitrii
  • 93
  • 10
  • Errm, in your code the gpio pin is toggled every 2ms, so that is 500Hz-ish signal being generated and definitely not 1000Hz. I say 500Hz-ish because time.sleep only guarantees a minimum delay, not the maximum - with Linux not being a realtime OS, expecting sleep accuracy in the millisecond level is very optimistic. Oh and your code `time.sleep(0.1)` turns the 500Hz-ish signal off for 0.1s, not 0.01s as stated in your text. – DisappointedByUnaccountableMod Sep 06 '17 at 06:55
  • Right! Thanks! Fixed! – Dmitrii Sep 06 '17 at 07:01
  • BTW. If you want any sort of frequency accuracy, you might want to investigate using [PWM](http://pythonhosted.org/RPIO/pwm_py.html), rather than `time.sleep()`. – SiHa Sep 06 '17 at 07:11
  • @SiHa Thanks! It's fine! If I want to get a frequency of 1 kHz and turn it off every 10 ms (frequency 100 Hz), then the GIPO 17 code should be about this `PWM.setup() PWM.init_channel(0) PWM.add_channel_pulse(0, 17, 0,10000) PWM.add_channel_pulse(0, 17, 100,1000)` or not? Please, if you can, arrange your answer as an answer, and tell us about this opportunity in more detail, with an example in this case. Maybe I misunderstood, if we added fast and slow pulsations, will the slow ones turn off the fast ones? How will this work at all? – Dmitrii Sep 09 '17 at 08:28

3 Answers3

2

The documentation suggests that simply passing a list of channels as the first argument to both GPIO.setup and GPIO.output will accomplish what you are asking.

chan_list = [11,12]    # add as many channels as you want!
                       # you can tuples instead i.e.:
                       #   chan_list = (11,12)
GPIO.setup(chan_list, GPIO.OUT)
GPIO.output(chan_list, GPIO.LOW)                # sets all to GPIO.LOW
1

I have had much better PWM experience with pigpio as compared to RPi.GPIO. Wiringpi is also good but pigpio's PWM support is much better IMO.

The documentation has a few functions for generating PWM on any pin:

http://abyz.co.uk/rpi/pigpio/python.html#set_servo_pulsewidth http://abyz.co.uk/rpi/pigpio/python.html#set_PWM_dutycycle

Do you *have* to use RPi.GPIO? I understand that this is not an exact answer, but I hope it at least points you in the right direction.

pixeld
  • 101
  • 5
  • Please, add python code for this waveform - 5 Khz, turn off-on every 10 ms(100 Hz), turn its all on-off every 10 sec(0,1Hz). I know this library, but not understand how do this waveform. – Dmitrii Sep 20 '17 at 05:06
1

It seems, you should use code like this. Unfortunately, I have no chance to test it since I have no frequency meter or oscillograph.

import time
import pigpio

GPIO=12

pulse = []

#                          ON       OFF    MICROS
pulse.append(pigpio.pulse(1<<GPIO, 0,       5))
pulse.append(pigpio.pulse(0,       1<<GPIO, 5))
pulse.append(pigpio.pulse(1<<GPIO, 0,       5))
pulse.append(pigpio.pulse(0,       1<<GPIO, 1e7))

pi = pigpio.pi() # connect to local Pi

pi.set_mode(GPIO, pigpio.OUTPUT)

pi.wave_add_generic(pulse)

wid = pi.wave_create()

if wid >= 0:    
    pi.wave_send_repeat(wid)
    time.sleep(60)   # or another condition for stop processing
    pi.wave_tx_stop()
    pi.wave_delete(wid)

pi.stop()
Roman Mindlin
  • 852
  • 1
  • 8
  • 12