Skip to content

SPI Loopback Test in Python

This section demonstrates how to perform a basic SPI loopback test using Python. This example is useful for verifying the SPI interface's functionality before connecting external devices such as displays. The SPI loopback test involves connecting the MOSI (Master Out Slave In) pin to the MISO (Master In Slave Out) pin, allowing transmitted data to be immediately received. This setup is purely for testing purposes and does not involve any external module.

Hardware Connection

MOSI/pin-19 ↔ MISO/pin-21

Running the Python Example

Clone the modified example from GitHub

git clone https://github.com/kneron/kneopi-examples.git
cd kneopi-examples
cd peripherals/Python/spidev

Run the test

How to Use chmod to Make a Script Executable?

Before running the Python script, ensure you have execute permission.

You can grant permission using the command:

chmod +x spidev_loopback.py

Then, you can execute it directly by:

./spidev_loopback.py

If you prefer running it with python3, you can still use:

python spidev_loopback.py

./spidev_loopback.py

Expected Output

Sent data: 85, Received data: 85
Sent data: 85, Received data: 85
Sent data: 85, Received data: 85
...

Additional Information

Refer to the official spidev documentation for further details and advanced usage.