I've recently had some reason to start experimenting with PWM myself, and found that (as pointed out by one of the comments) the frequency seems to vary with duty cycle - bizzare, right? It turns out that Broadcom implemented "balanced" PWM in order to make the on and off PWM pulses as evenly distributed as possible. They give a description of the ...
I finally got a complete(ish) understanding from the bcm2835.h driver header file, so thought I would post and answer my own question for others.
The relevant bits from the header:
The BCM2835 supports hardware PWM on a limited subset of GPIO pins. This bcm2835 library provides functions for configuring and
controlling PWM output on these ...
According to this formula:
pwmFrequency in Hz = 19.2e6 Hz / pwmClock / pwmRange
We can set pwmClock=1920 and pwmRange=200 to get pwmFrequency=50Hz:
50 Hz = 19.2e6 Hz / 1920 / 200
I test it on alarmpi:
$ pacman -S wiringpi
$ gpio mode 1 pwm
$ gpio pwm-ms
$ gpio pwmc 1920
$ gpio pwmr 200 # 0.1 ms per unit
$ gpio pwm 1 15 # 1.5 ms (0º)
$ gpio pwm 1 ...
Recent Pis have two hardware PWM channels. In addition hardware timed PWM pulses may be independently generated on all the GPIO connected to the 40 pin expansion header.
In practice this means there are two highly accurate PWM channels and all the other GPIOs may have Arduino style PWM (800 Hz, 0 off - 255 fully on).
E.g. servoblaster and my pigpio, etc.
You can do this with the PWM pin on the Pi.
Here's a link to a blog post dealing with this that uses the C WiringPi library to do it:
I would paste the code in here but the editor isn't co-operating.
I'm not sure if anyone will write the code for you. It's too broad a question.
You need to acquire a basic understanding of Python programming first, otherwise you'll be forever asking questions.
Personally I'd use the Python curses module (import curses) to handle keyboard entry. There are example of using curses within pigpio at http://abyz.me.uk/rpi/...
According to the docs, GPIO pin 18 supports PWM.
To control it from the shell without root permissions, you need to install the wiringPi library. Take a look at the gpio docs or the man page for more information:
In short: First set the mode of pin 18 to PWM:
gpio -g mode 18 pwm
Then you can set the pin to a PWM value between 0 and 1023:
hardware_PWM >>> set_PWM_dutycycle >>> software PWM
where >>> is orders of magnitude better pulse stability.
rock solid pulses
large choice and range of frequencies
large number of steps between off and fully on
very stable pulses, unlikely to be affected by anything other than sustained heavy network traffic.
18 different ...
On recent Pis (those with the 40 pin expansion header and the compute module) GPIO 12/13/18/19 may be used to provide hardware PWM signals.
The PWM clock source does not need to be the 19.2MHz crystal, pigpio uses the 500MHz PLLD.
For a simple command line method of setting a hardware PWM frequency see http://abyz.me.uk/rpi/pigpio/pigs.html#HP
This is the code I'm using. I'm trying to see what will change as i change the settings.
int main (void)
printf ("Raspberry Pi wiringPi test program\n") ;
if (wiringPiSetupGpio() == -1)
exit (1) ;
I doubt you'll need to use the PWM capability of gpio 18.
There are many ways to provide hardware timed PWM on the Pi on all the user gpios. They are all variations on a theme and use DMA transfers paced by the PWM or PCM peripherals.
You can ignore that as they are all wrapped up in simple wrappers.
My offering is pigpio which will let you send servo/PWM ...
The library needs to be installed for Python 3. The two Python versions 2.7 and 3.x don't share packages (because most code written for Python 3 is not backward compatible.
To install the RPIO library for Python 3 do the following:
sudo apt-get update
sudo apt-get install python3-setuptools
sudo easy_install3 -U RPIO
I am fairly sure it is a bug in the RPi.GPIO module.
Look through https://sourceforge.net/p/raspberry-gpio-python/tickets/
As a workaround I suggest you do not use the start() and stop() methods in a loop, use the ChangeDutyCycle() method instead to set the duty cycle to zero to stop PWM.
The short answer: You CANNOT reliably read PWM on Raspberry Pi.
Reading PWM requires microsecond precision (unless you're reading a very-very slow PWM), and that is not available on Raspberry Pi for userland software without tinkering with kernel modules.
The easiest way to capture PWM would be to get any cheap (< $0.5) microcontroller with serial or ...
ESCs (Electronic Speed Controllers) are typically controlled just like you would control a hobby servo -- with a PWM signal. There are many guides to controlling a servo that should be applicable to the ESC.
Since the Raspberry Pi is a 3.3V device you might need a level shifter if your ESC needs a 5V signal.
Here is a tutorial for Raspberry Pi: https://...
Simple. The maximum number is 0 (zero). You will need at least a transistor or a relay to drive the DC motor, as the GPIO pins on the rPi do not provide enough power to drive the motor. You will also want to put a flyback diode in place, plus a current limiting resistor. Since you said you only want to use a breadboard, rPi and motor, those components do not ...
By default pigpio uses the PCM peripheral to time the DMA leaving the PWM peripheral free for standard audio.
Perhaps your ALSA device is using high quality audio. If that's the case you need to use the PWM peripheral to time the DMA leaving the PCM peripheral free for high quality audio.
To do that from C use gpioCfgClock.
The Pis with 40 pin expansion header (and the compute module) have two hardware PWM channels which you should be able to use with wiringPi.
That would give you control of two ESCs.
From the software side there are several modules which provide DMA timed PWM on any or all GPIO (e.g. my pigpio, servoblaster etc.). These modules provide a pipe interface to ...
Will I need to have a Python script
You do not need python, you just need something with a module that wraps one or more of the following C libraries:
All of which provide PWM for at least the hardware driven pin(s). Python for has wrappers for all three. PHP has one for wiringPi and may have ones for the others. However, ...
PWM should not conflict with I2C.
Everything below presumes the (predominant) Broadcom pin numbering scheme, which does not correlate to the physical arrangement of the pins.
I've never used GPIO 4 for PWM; the normal PWM pins used are 12, 13, 18, and 19, making use of two separate PWM channels derived from one clock.
Have a look at the chart here. Note ...
You are using the wrong tool for the job.
Servos require accurately timed pulses. The RPi.GPIO module uses software timing which leads to all sorts of timing jitter, which leads to a twitching servo, which shortens the life of the servo.
Use something like my pigpio which uses hardware timed pulses.
sudo pigpiod # start daemon
./servo_demo.py 27 # ...
You need to use hardware timed pulses for servos.
Software timed pulses will lead to jitter and a shorter servo lifetime.
Try pigpio, servoblaster, RPIO.GPIO, or similar which use hardware timed pulses.
If you take a look at the spec sheet for that fan (the fan you linked is a 4-pin varient):
You'll see that the PWM signal pin must be 5V. Now why it works with 3.3V (GPIO voltage) is likely because high on 5V is usually not exactly 5V, but that doesn't explain why it only works when the GPIO is held steady on and not PWM.
Anyways, you can use a simple step-...
I have resolved this. Thanks varesa for the note. Your information is correct but there are 2 elements that I think miss in all of the documentation I've ever found on this.
1) The pi is not the right PWM controller to do this if you need it to do anything else at the same time. Maintaining control over the pulse frequency requires its own controller. I ...
It does seem like the GPIO pins get set during boot. This is quite a problem as it is not what you would have expected! The problem is that during boot the GPIO pins are floating and pulled down by anything. So they will have "random" or unexpected values and voltages.
First of all the servos should NOT be connected directly to the GPIO. You ...
I can do fairly accurate pulse width measurement using the piGpio C library:
This library enables you to install a callback function that will trigger on any edge transition on a gpio pin and gives you a microsecond level timestamp for each transition. Don't think you can count on this for microsecond accuracy - but ...
To achieve slow dimming or brightening of your LED you will require (as others have posted before) PWM.
Introduction to PWM
There are primarily two types of modulation software and hardware. There exist several libraries which can achieve a software enabled modulation whose performance lies in between software and hardware. You could read up more about the ...
You could also achieve this effect with a simple 555-timer chip a capacitor and some resistors.
I myself plan on using the following circuit for my tardis project: