Recently want to use Raspberry Pi control model of the ESC, so studied the next PWM, seemingly control the ESC is troublesome, because the ESC needs to send a few specific signals to start, and then to enter the control mode, today first understand PWM, slowly toss. The following program pro-test is available, I use the Raspberry Pi model B,12gpio port is the 6th pin outside.
Pulse width modulation (PWM) refers to the digital output of the microprocessor to control the analog circuit, is a digital coding method of analog signal level. On the Raspberry Pi, the PWM can be implemented by programming the GPIO.
Create a PWM instance:
GPIO. PWM (channel, frequency)
Enable PWM:
P.start (DC) # DC represents duty-free (range: 0.0 <= DC >= 100.0)
Change frequency:
P.changefrequency (Freq) # freq is the new frequency set, in Hz
Change duty Duty:
P.changedutycycle (DC) # Range: 0.0 <= DC >= 100.0
Stop PWM:
P.stop ()
Note that if the variable "P" in the instance goes out of range, it can also cause a PWM stop.
The following is an example that causes the LEDs to blink once every two seconds:
Importrpi.gpio as Gpiogpio.setmode (GPIO. BOARD) Gpio.setup (GPIO). Out) P =gpio. PWM (0.5) P.start (1) Input (' Click enter Stop: ') # in Python 2 you need to use Raw_inputp.stop () Gpio.cleanup ()
The following is an example of switching LEDs between light and dark:
Import Timeimport Rpi.gpio as Gpiogpio.setmode (GPIO. BOARD) Gpio.setup (one, GPIO). Out) Gpio.setup (GPIO). Out) p = GPIO. PWM (P11) = GPIO. PWM (one, P.start) (0) p11.start (0) Try: While 1: for DC in range (0, 101, 5): p.changedutycycle (DC) P11. Changedutycycle (DC) Time.sleep (0.1) for DC in range (1,-5): p.changedutycycle (DC) P11. Changedutycycle (DC) Time.sleep (0.1) except Keyboardinterrupt: passp11.stop () Gpio.cleanup ()
Copyright NOTICE: This article is the original blogger article, reproduced please retain the source Http://blog.csdn.net/offbye
Introduction to the PWM pulse width modulation function of Raspberry Pi