Setting up SPI on Raspberry Pi

In this tutorial we learn how to setting up SPI (Serial Peripheral Interface) on Raspberry Pi. By default, Raspbian is not configured for the Raspberry Pi’s SPI interface to be enabled. If yo want to enable it. The procedure is simple and easy. just using the Raspberry Pi Configuration tool that you will find on the main menu under preference. Just check the box for SPI and click OK. You will be prompted to restart.
On older versions of raspbian, the raspi-config tool does the same job.

 $ sudo raspi-config 

then select advanced, followed by SP, and then Yes before rebooting your RPi.

Why need SPI ?

We used SPI for serial communication. SPI allows serial transfer of data between the raspberry Pi and peripheral devices, such as
analog-to-digital converter (ADC) and port expander chips, among other devices.

You may like also: How To Use Raspberry pi in a truely headless mode


Setting up SPI on Raspberry Pi


Installing PySerial for Access to the Serial Port from Python

If you want to use the serial port (Rx and Tx pins) on the RPi using Python.
Install the PySerial library:

$ sudo apt-get install python-serial

Now create connection

ser = serial.Serial(DEVICE,BAUD)

where DEVICE is the device for the serial port (/dev/ttyACM0) and BAUD is the baud rate as a number.

ser = serial.Serial('/dev/ttyACM0', 9600)

Once connection established, you can transmit data over serial like this:

ser = ser.write('write some msg')

Listening for a response normally involves a loop that reads and prints, as illustrated in this example:


while True:
    print(ser.read())
    

If you want to control LED with Raspberry Pi, Visit these tutorials:


Installing Minicom to test the Serial Port

You can use Minicom If you want to send and receive serial commands from a terminal session.

Install Minicom:

$ sudo apt-get install minicom

after installation you can communicate with serial device connected to the Rx and Tx pins of the GPIO connector by using this command:

$ minicom -b 9600 -o -D /dev/ttyACM0 

where -b is baud rate, -D is the serial port. Remember to use the same baud rate as the one the device you are communicating with.

Now your minicom session started. first turn on

 local echo

so you can see the command that you are typing. To do this
press CTRL+A and then CTRL+Z; you will see the command list. Now Pres CTRL+E to turn on local echo.
Now you sending/receiving messages will also be displayed.

I hope you like this tutorial Setting up SPI on Raspberry Pi.


You may like also:


Buy now : Raspberry PI 3 Model B+ Motherboard

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. I am a tech blogger and an IoT Enthusiast. I am eager to learn and explore tech related stuff! also, I wanted to deliver you the same as much as the simpler way with more informative content. I generally appreciate learning by doing, rather than only learning. Thank you for reading my blog! Happy learning! Follow and send tweets me on @harshvardhanrvm

8 thoughts on “Setting up SPI on Raspberry Pi

Leave a Reply

%d bloggers like this: