You have several choices when working with I2C devices. The ubiquitous and universal library luma offers a generic serial objectwhich supports I2C connection. Usage is simple: Import the library, define the IC2 registers, then start to send and read data. Here is an example how to make an I2C connection However, the library is intended to create connection objects that are used to instantiate concrete screens, so it might not be suitable if you use other I2C based sensors. Another option is the Python smbus2 library. It supports the i2C protocol and several low-level read and write access methods. It accesses its host built-in smbus kernel module, from which it can get an I2C instance. See full list on The first step in connecting an SPI device is to figure out to which SPI bus you connect a device. On the Raspberry Pi, different SPI bus systemsexist: 3 SPI bus systems for Raspberry Pi up to version 3, and 7 SPI bus systems for the Raspberry Pi 4. Take a note of the concrete bus, because it will map to a device file in your system which you need to use for configuration. The SPI protocol is supported by spidev, an actively maintained library. It makes it easy to connect any attached SPI device, configure this connection with the required specifications (speed in HZ, clock polarity and more), and then read writing data to it. Assuming you want to connect an SPI device on bus 5, the following example connects and writes data to the device. Another option is the Adafruit_Blinka library, an universal library that wraps the CircuitPython API for devices run the Raspberry Pi works with CPython, you can use this library and its many functions. It... See full list on For making UART connections, several options exists. The Python library pyserialprovides a simple, configurable object for making serial connection. You can configure the device file, the baud rate, and other communication aspects like ttesize, parity, timeout and more. It implements Pythons context manager protocols and therefore automatically closes the connection at the end of its program block. Here is an example: The second option is again Adafruit_Blinka library, specifically the busio UART API. Very similar to PySerial, you can create a connection with the default configuration, and then start to read and write data to it. However, it does not work with device files, but directly with pins. See full list on On the Raspberry Pi, several options for working with the protocols I2C, SPI and UART exists. This article is a not-exhaustive list of these libraries. On the one hand, there are very specific libraries, such as pyserial for UART, or spidev for I2C. On the other hand, universal libraries such as luma and busiosupport two or all three protocols. Considering this, I recommend to choose a universal library especially if you intend to connect to a sensor that these libraries already support. See full list on
Get Price