Introduction to the PWM pulse width modulation function of Raspberry Pi

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.