magicalDuck
Posts: 25
Joined: Mon Oct 09, 2017 6:45 am

Can I make PWM with GPIO

Fri Oct 20, 2017 9:22 am

Hi,

yesterday I tried a usb->gpio board, basically it turned my high end PC into a raspberry pi.

But :

I tried to make a 5Khz PWM with a 20% duty cycle, it did not go well, my scope could not lock onto the signal because each period was different, I'm guessing this is due to usb lag.

ex of program used :

Code: Select all

def makePwm20Percent5KhzPeriod(pin_number):
	# 5Khz = 0.0002 seconds periods, here making a 20% pwm
	ft232h.output(pin_number, GPIO.HIGH)
	time.sleep(0.00004)
	ft232h.output(pin_number, GPIO.LOW)
	time.sleep(0.00016)
 

FT232H.use_FT232H()
ft232h = FT232H.FT232H()

C0_PIN = 8
ft232h.setup(C0_PIN , GPIO.OUT)

while True:
	makePwm20Percent5KhzPeriod(C0_PIN)
Will I have this kind of problem with a raspberry pi? or can I reliably control the gpio outputs.

Thanks.

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 10:12 am

this program will work as bad as on your pc even on a Raspi
but if you write a better program and use proper tools (for example: pigpio) you'll get good pwm output

magicalDuck
Posts: 25
Joined: Mon Oct 09, 2017 6:45 am

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 12:58 pm

Why would it works badly? what is special about this library?

Is there a maximum speed for the GPIO before they start to behave erraticly? for example wait 100ms minimum before re-changing a GPIO state?

Massi
Posts: 1691
Joined: Fri May 02, 2014 1:52 pm
Location: Italy

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 1:18 pm

magicalDuck wrote:
Fri Oct 20, 2017 12:58 pm
Why would it works badly? what is special about this library?

Is there a maximum speed for the GPIO before they start to behave erraticly? for example wait 100ms minimum before re-changing a GPIO state?
linux is not a real time os
you can't be sure of the correctness of all those sleep times
and the command that toggles gpio's is not "time consumption free"
and so on

btw, invest some time in a good google research.
You'll find out a lot of interesting information about PWM and raspberry. How to do it in the "hardware" way.
How to do it in the "software" way
and so on..

User avatar
procount
Posts: 2330
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 1:22 pm

magicalDuck wrote:
Fri Oct 20, 2017 12:58 pm
Why would it works badly? what is special about this library?
Is there a maximum speed for the GPIO before they start to behave erraticly? for example wait 100ms minimum before re-changing a GPIO state?
(@Massi beat me to it, but I'll send this anyway...)

1. You have written it in Python, which is an interpreted language not compiled, therefore it will be comparatively slow and will not be able to reach the higher frequencies that a compiled program would. (But Python is still ok to do the high level control, leaving the libraries to do the time dependent stuff)
2. Linux is not real-time. It is not like an Arduino that only does what your program asks. Even with a compiled program, it is subject to task switching and interrupt processing as the processor manages the rest of the devices and peripherals in the system.
3. Libraries such as pigpio use hardware DMA and/or high priority kernel drivers that do not suffer the erratic task latencies that a user mode program would suffer, as you have experienced.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

magicalDuck
Posts: 25
Joined: Mon Oct 09, 2017 6:45 am

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 3:19 pm

Oh yes, I had forgotten about that, the OS will gives priority to some process and your actual process might get slowed. (my os classes were 10 years ago)

Well... how are you suposed to make a real time embeded system that demand a lot of power? (the pwm thingy was just a test)

I mean, you have the arduino that do exactly what you tell them but who are not really powerfull.
And you have the computers who are really fast but the OS will not give all it's ressources to your program.

Is there some kind of "arduino on steroids" out there?

scotty101
Posts: 4106
Joined: Fri Jun 08, 2012 6:03 pm

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 3:48 pm

magicalDuck wrote:
Fri Oct 20, 2017 3:19 pm
Well... how are you suposed to make a real time embeded system that demand a lot of power? (the pwm thingy was just a test)
There are real time operating systems (such as FreeRTOS) that can run on more powerful processors than those on arduinos.
You can offload the time critical elements to lower level hardware such as a counter/timer which controls the PWM of a GPIO pin without having to wait for the CPU to be available.
Electronic and Computer Engineer
Pi Interests: Home Automation, IOT, Python and Tkinter

User avatar
procount
Posts: 2330
Joined: Thu Jun 27, 2013 12:32 pm
Location: UK

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 4:10 pm

It all depends on what level of "real-time" you need. Many people have success with pwm on raspbian, but you need to use the right libraries that use the hardware in the best way to minimise the latencies.

There is also linux-preempt-rt and Xenomai. Also checkout linux cnc, but most serious real time operating systems are commercial offerings.
PINN - NOOBS with the extras... https://www.raspberrypi.org/forums/viewtopic.php?f=63&t=142574

User avatar
rpdom
Posts: 17741
Joined: Sun May 06, 2012 5:17 am
Location: Chelmsford, Essex, UK

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 4:16 pm

You could go Bare Meta, which will give you much more control of the timings. You can use low-level interrupts to give precise times to event, but to go to that level takes some dedication and a lot of the standard devices (like USB and SD card) won't be available to you unless you get some driver code. There are some other things that are close to bare metal, but you will probably need to learn a lot to be able to use them.

MarkR
Posts: 156
Joined: Fri Jan 25, 2013 1:55 pm

Re: Can I make PWM with GPIO

Fri Oct 20, 2017 4:27 pm

There are hardware PWM generators. On the Pi you can use the DMA controller (by a library like pigpio) to pulse the GPIO lines nice and accurately. You can't do that with a usb dongle on your PC, no matter how powerful it is, the usb lag will be too much (unless the dongle has hardware PWM, which it might?)

I think that systems that need a powerful CPU and PWM would generally either use a hardware pwm generator, or use a microcontroller to control the PWM lines and communicate with it over usb or other serial

steveb4pi
Posts: 62
Joined: Sun Aug 11, 2013 6:12 pm

Re: Can I make PWM with GPIO

Sat Oct 21, 2017 9:44 am

'Yes' == the Pi GPU has 2 PWM circuits built in, however they are normally used for 'Audio out' (stereo jack) - but the frequency response is clipped to audio by external circuits (you MIGHT get an accetable 5kHz waveform out of it). Depending on what you need it for, you could just 'play' .wav music (pure sinewave tones) using one of the Pi audiio players :-). It is possible to 'redirect' the audio to some other i/o pins (so you can get access without the frequency limiting curcuits)

If you need PWN for motor control etc. from the Pi, use a dedicated PWM Motor control pcb (go look on eBay = theres' a good selection at the low-end 99p upward (all post free from China)) - you will pay a bit more for those that can be controlled by I2C (a serial protocol) available from the Pi on it's i.o pins (I2C is what most 'Hats' use)

PWM doesn't need a GUI, a CLI or even an operting system == to buld a custom (software controlerable) PWM eg. because you need some specilist soft start (or fast start) ramp-up / stop ramp-down (or some other clever PWM control like variable frequency), you use a dedicated chip like one of the PIC series == see, for example, http://microchipdeveloper.com/8bit:pwm

User avatar
B.Goode
Posts: 10727
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Can I make PWM with GPIO

Sat Oct 21, 2017 10:07 am

yesterday I tried a usb->gpio board, basically it turned my high end PC into a raspberry pi.
So, basically, this is about an Add-on board for a 'conventional' PC? Not actually anything to do with Raspberry Pi hardware or recommended Operating Systems?

Nevertheless, perhaps if you specify or provide a link to the hardware you are using someone might have the knowledge to assist. My hunch is that this is likely to be a restriction or bottleneck in the API used to communicate between your PC operating system and the Add-on board.

Return to “Beginners”