Skip to main content

Python 3 library for working with electronics

Project description

https://travis-ci.org/MartijnBraam/pyElectronics.svg?branch=master https://badge.fury.io/py/pyelectronics.svg

This is a python library for using electronics (like i2c or spi devices) with a unified interface. It currently supports connecting to stuff through the Raspberry Pi gpio with the i2c kernel driver and using the Bus Pirate.

Supported gateways

The gateways are the bridge between Linux and the hardware. Currently supported:

  • Bus Pirate v3

  • Raspberry Pi / other linux i2c_dev supported interfaces

Supported chips/devices

Read the contributing guide to add devices

  • Bosch BMP180 Digital pressure sensor

  • Honeywell 3-Axis Digital Compass IC HMC5883L

  • MPU-6050 Six-Axis (Gyro + Accelerometer) MEMS MotionTracking™ Device

  • NXP LM75A Digital temperature sensor

  • Microchip MCP23017 16-Bit I/O Expander

  • 7-segment displays without multiplexing

Installation

Install it from pypi:

$ pip3 install pyelectronics

The various gateways have their own dependencies. Install the dependencies for the gateway you require:

# Requirements for the Raspberry Pi
$ pip3 install pysmbus

# Requirements for the Bus Pirate
$ pip3 install pyserial

To run the doctests you need all dependencies. You can install them with the doctest requirements file:

$ pip3 install -r docs/doctest-requirements.txt

Usage

Read the full docs at pythonhosted.

First create a instance of a gateway:

from electronics.gateways import BusPirate
from electronics.gateways import LinuxDevice

# Use a BusPirate to connect to a bus
gw = BusPirate('/dev/ttyUSB0')

# Use a i2c bus with a linux driver (like the raspberry pi)
gw = LinuxDevice(1) # /dev/i2c-1

Create instances for components connected to the gateway:

from electronics.devices import BMP180
from electronics.devices import MPU6050I2C

barometer = BMP180(gw, address=0x77) # Address is optional
inertia = MPU6050(gw)

# Do chip specific initialisation
barometer.load_calibration()
inertia.wakeup()

Read values from sensors:

temperature = barometer.temperature()
pressure = barometer.pressure()
acceleration = inertia.acceleration()
rotation = inertia.angular_rate()

Example with GPIO:

# Red led conected to the aux pin of the bus pirate
red_led = gw.get_aux_pin()
# Turn the led on
red_led.write(True)

# An Microchip port expander connected through I2C
expander = MCP23017I2C(gw, address=0x21)

# Get the pins for the two other leds that are connected
# to the port expander
green_led = expander.get_pin('A0')
blue_led = expander.get_pin('A1')

# Bundle the leds to a bus so you can address 8 colours as an int
# the pins on the port expander are open drain, they are inverted with the invert operator (~)
bus = GPIOBus([red_led, ~green_led, ~blue_led])
bus.write(0) # Black
bus.write(7) # White

# Disco!
while True:
    for i in range(0,8):
        bus.write(i)
        sleep(0.2)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyelectronics-0.1.4.tar.gz (13.2 kB view details)

Uploaded Source

File details

Details for the file pyelectronics-0.1.4.tar.gz.

File metadata

File hashes

Hashes for pyelectronics-0.1.4.tar.gz
Algorithm Hash digest
SHA256 9931399172462c753658b6d5cd7ea1cf40c9576cdb769f5e40b1614cf64ea0c1
MD5 40de074d29d8b4edb72707614adf7a41
BLAKE2b-256 b83f52003d90d83137dfb02bae6e58fa71f95fc49483ed85c93b62f48703f487

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page