helpme
Posts: 119
Joined: Thu May 16, 2013 2:20 am

Python libraries for hardware ports like i2c, spi

Sat Jul 13, 2013 1:25 pm

Are there python libraries for controlling hardware ports like i2c and spi on the Raspberry Pi? Besides i2c/spi masters, is it possible that there are libraries that allow us to use Raspberry Pi as i2c/spi slaves?

ghp
Posts: 1401
Joined: Wed Jun 12, 2013 12:41 pm
Location: Stuttgart Germany
Contact: Website

Re: Python libraries for hardware ports like i2c, spi

Sat Jul 13, 2013 8:47 pm

Hello,
for SPI, use "spidev". You also need to start the driver "sudo modprobe spi_bcm2708".
There are good tutorials at adafruit, demonstrating SPI for python.
The code snipped is just copied and shorted from one of may programs. It just demonstrates the basic usage.

Code: Select all

# -*- coding: utf-8 -*-
    import spidev
    -- os.system("modprobe spi_bcm2708")

        self.spi = spidev.SpiDev()
        self.spi.open(bus, device)
        self.spi.max_speed_hz = 1000000

        self.spi.xfer (list(bytesS))

Greetings
Gerhard

User avatar
TonyD
Posts: 447
Joined: Thu Sep 08, 2011 10:58 am
Location: Newcastle, UK
Contact: Website

Re: Python libraries for hardware ports like i2c, spi

Mon Jul 15, 2013 2:28 pm

for I2C bus search for smbus and for GPIO search for RPi.GPIO
Tony

Return to “Python”