Can someone point me to exactly where the PWM pin is on the board and how I can hook this thing up visually as I'm totally lost as to what I would need to make this run. Also what is the snippet of code needed to make it pulse every 2 seconds.
-
It's pin called GPIO18. But since you need one pulse every 2 seconds, you don't seem to need PWM at all. I'm not even sure it can be used with such a small frequency. You can, however, easily do this in software, swapping regular GPIO in every two seconds. – Krzysztof Adamski Mar 06 '13 at 07:37
-
http://elinux.org/Rpi_Low-level_peripherals Not sure what you mean by pwm pin. – lumpynose Mar 06 '13 at 07:00
2 Answers
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:
man gpio
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:
gpio -g pwm 18 512
However, as Krzysztof already noted, you probably don't need PWM if you just want to send a short pulse. You could use a regular pin and simply turn it on and off every 2 seconds.
gpio -g mode 17 out
gpio -g write 17 1
gpio -g write 17 0
You could also control it from a program, e.g. a Python script. You should find information about this in the docs.
All this is untested, but should work. If you have more problems, feel free to comment or look at the or the manpage.
A related question can be found here: Can I use the GPIO for pulse width modulation (PWM)?

- 276
- 2
- 7
I needed to use PWM to control a servo connected to GPIO Pin 18 on an RPi 0W (Same pin setup as the 2x20 header on A+/B+/Pi 2B/Pi 3B). The servo listed the following controls:
Voltage : 3V to 6V DC
Position "0" (1.5ms pulse) is middle, "90" (~2ms pulse) is all the way to the right, "-90" (~1ms pulse) is all the way to the left.
Here is a bash script to control the servo using the pigs cli tool:
sudo apt install pigpio
sudo pigpiod
pigs s 18 2000 #Right (gpio pin 18)
pigs s 18 1500 #Middle
pigs s 18 1500 #Left
#Script to sweep thru the full range
DELAY=0.01 STEP=10
for i in $(seq 1000 $STEP 2000); do pigs s 18 $i; sleep $DELAY; done;
sleep .5;
for i in $(seq 2000 "-$STEP" 1000); do pigs s 18 $i; sleep $DELAY; done;
Instead of a CLI tool you can also use pigpio libraries for Python or C

- 111
- 3