As suggested by Alex Chamberlain, the WiringPi library appears to support both hardware PWM output on one or two GPIO pins depending on model, and software PWM on any of the other GPIO pins. Meanwhile the RPIO.PWM library does PWM by DMA on any GPIO pin. Effectively this is a halfway house between hardware and software PWM, providing a 1 µs timing ...
Yes, there is one hardware PWM output on the Raspberry Pi, connected to P1-12 (GPIO18). Further, PWM outputs could be added using an I²C or SPI interface; some people have had success with this (forum post).
You can use the WiringPi library to control the PWM pin; you could look at the code to avoid including the entire library.
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 ...
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.
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 ...
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.
The electrical connections required depends on the servo you have. The GPIO provides 3.3 V and up to 16 mA, which is insufficient for your servo, so you will need to buffer it with a transistor.
There are 8 GPIO pins on the expansion header, though other pins can be reconfigured to give up to 17 GPIO pins in total. If you need more, you will have ...
If you are running a realtime operating system on your Raspberry Pi this is indeed possible. As others have said already you will need an external power source to the servos but except for that you do not need any other additional hardware.
If you are running Xenomai (realtime patch for Linux) you can run a completely familiar Linux system, but with added ...
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:
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/...
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
If you don't mind using a Linux Kernel driver there is servoblaster which exposes the servos as a char device.
I have forked a new version which does not consume all of the gpio pins for servos use. I'm currently using such on a small servo driven robot the I have built and demoed for our robot club using IPGamePad ...
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) ;
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
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 ...
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://...
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 ...
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 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.
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.
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-...
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 ...