Coder Social home page Coder Social logo

pwmpy's Introduction

pwm.py

A simple Python class to work with the Linux PWM kernel driver.

Written for the Raspberry Pi and the hardware pwm timers, but should work with other SOCs.

Requires that a PWM kernel driver is loaded appropriately which on the RPi normally means a dts overlay.

Instructions for building and loading an appropriate dts for the RPi can be found here.

I'm using a custom Yocto built O/S where pwm overlays are installed by default.

Here is a simple interactive example using the PWM class

root@rpi3:~# python3
Python 3.5.2 (default, Nov 22 2016, 06:54:46)
[GCC 6.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.

Import the PWM class from pwm.py

>>> from pwm import PWM

Create an instance of the class for the PWM0 timer

>>> pwm0 = PWM(0)

Call export() before first use

>>> pwm0.export()

Setup a 1 ms period, 25% duty cycle pulse (period and duty_cycle units are nanoseconds)

>>> pwm0.period = 1000000
>>> pwm0.duty_cycle = 250000

Start the pwm timer

>>> pwm0.enable = True

Change the duty cycle to 50%

>>> pwm0.duty_cycle = 500000

Turn off the timer

>>> pwm0.enable = False

Create an instance for the PWM1 timer

>>> pwm1 = PWM(1)
>>> pwm1.export()

Setup a servo-like pulse with a 50 ms period and 2 ms duty_cycle

>>> pwm1.period = 50000000
>>> pwm1.duty_cycle = 2000000
>>> pwm1.enable = True

Set the servo pulse to 2.5 ms

>>> pwm1.duty_cycle = 2500000

Set the servo pulse to 1.5 ms

>>> pwm1.duty_cycle = 1500000

Back to 2.0 ms

>>> pwm1.duty_cycle = 2000000

Turn it off

>>> pwm1.enable = False

Unexport both timers

>>> pwm0.unexport()
>>> pwm1.unexport()
>>> quit()

pwmpy's People

Contributors

coburnw avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.