Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering

Interfacing Raspberry Pi with Arduino

February 22, 2021 By Usman ali Butt

Raspberry Pi (RPi) is a small, low-cost, single-board computer that offers more than just a Linux operating system. It also has communication buses and general-purpose input/output (GPIOs) that can be used to interface external sensors and devices.

One example is the serial peripheral interface or SPI. SPI communication is based on a shift register, where a master and multiple slaves acquire data by shifting one another’s registers.   

An SPI protocol has four wires:

1. Master out slave in (MOSI): A master sends data to a slave
2. Master in slave out (MISO): A slave sends data to a master
3. Clock (CLK): A master and a slave must be operating at the same clock speed
4. Chip select (CS): Multiple devices can be connected to a single master. However, to access each device, the CS must first be enabled, which ensures all other devices on the bus remain disabled.

The RPi header SPI (including the MOSI, MISO, and CLK) is present on GPIO 9, 10, 11. The CS is present on the GPIO’s 8 and 9. Two chip selects (CE0 and CE1) mean that the default RPI hardware can control two slaves.

Additionally, it’s possible to increase the number of slaves simply by declaring any other GPIO pin as the output — and, then, controlling the CS slaves with it.   

Raspberry Pi’s typical SPI master and slave arrangement are shown below. Three slaves are commonly attached with one master. Two of the slaves are controlled with RPi’s default chip select pins and a dedicated RPi GPIO pin is used to control the third slave.

By default, RPi’s SPI bus is disabled, so it’s necessary to enable it to communicate. The hard-coded manner to do so is to modify the root file. Another option is to enable it during the boot-up.

However, the easiest way is to enable it through RPi’s configuration window. Go to the menubar -> configuration -> RPI configuration and then check the “Enable” box next to the SPI.

Once RPi’s SPI bus is enabled, type this command into the console:

Lsmod | grep spi_

You should get the spi_bcm chip name, which means that the SPI is enabled. (If you fail to get this message, then the SPI is not enabled and you’ll need to try again.)

Once the SPI is enabled, it’s possible to interface any external SPI device with RPi. Raspberry Pi offers pre-defined packages that are a great resource when working with the SPI hardware. These packages provide libraries and interfaces that are easy and straightforward to use.  

The RPi Python bundle
One of the most popular SPI packages for RPi is Spidev, a Python bundle that can easily access, program, and support the development of applications. Installing Spidev is also easy. In the console, simply type the following commands (in this order):

Sudo apt update
Sudo apt install python3-pip
Sudo pip3 install spidev

In the above installation procedure, python3 was used for all of the commands (because Python 3 was the program installed on the Raspberry Pi).

First, the RPi packages are updated and then the python3 pip manager is installed. Pip is Python’s package manager, so you can install any Python packages using it. The last step is to install Spidev by using the pip3 package manager.

To locate Spidev, switch to the /usr/lib/python3/dist-packages and list the packages. The compilation is another test, ensuring that the installation and compilation are successful. To do so, it’s necessary to import the package in the code as per below…

An example of the SPI code for Raspberry PI is shown below. First, the Spidev package is imported and its instance is created. With this instance, it’s possible to access the public variables and methods in the package.

The SPI parameters — such as the clock, transfer bit pattern, chip select, or the bits per transfer — can each be set individually. Data can also be transferred byte by byte or as a list.

To purchase RPi and practice using the SPI, go to:

  • Raspberry Pi: Mouser

You may also like:


  • The pros and cons of the Raspberry Pi 400
  • beginners guide
    Basic Electronics 01 – Beginners guide to setting up an…

  • Getting started with Raspberry Pi
  • Raspberry Pi 4
    RPi Python Programming 01: Introduction to Raspberry Pi 4

Related Articles Read More >

Mathematical equations executed by an MCU
Clap sensor using STM32 MCU
Electro nonmechanical relays characteristics and how-to DIY
How to build an object-following robot using Arduino

Featured Tutorials

  • Remote Controlling Multiple Loads Using RF Module (Part 2/23)
  • Increasing Range of RF Module by Increasing Transmission Power (Part 3/23)
  • Increasing range of RF module by using antenna (Part 4/23)
  • Increasing the Range of RF Module by Using Antenna and Increasing Transmission Power (Part 5/23)
  • Controlling Multiple RF Receivers by Universal RF Transmitter (Part 6/23)
  • Multiple RF Receivers Controlled by a Universal RF transmitter based on Address Pin Configuration (Part 7/23)

Stay Up To Date

Newsletter Signup

EE Training Center Classrooms

“ee

“ee

“ee

“ee

“ee

Recent Articles

  • IAR Systems announces IEC and ISO certifications of its RISC-V development tools
  • Mathematical equations executed by an MCU
  • Infineon’s new Wi-Fi 6 brings reliable connectivity to smart homes
  • Toshiba releases compact LDO regulators for stabilizing DC power lines
  • Clap sensor using STM32 MCU
...

RSS EDABOARD.com Discussions

  • Stumped with buck converter simulation Pspice
  • CMCON0 and ANSEL registers
  • Smart high side switch is a 200mA current clamp device?
  • Finding Difficulty in Selection LED MATRIX
  • MCLR as digital input is resetting the device

RSS Electro-Tech-Online.com Discussions

  • Identify Crystal on Ford Immobilizer antenna board
  • GPU price, lol
  • Diode that only allows continuity at 13.5v?
  • ZVS driver circuit
  • Cutting D-shaped and double flat holes
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2021 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Circuit Design
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • EE Design News
    • DIY Reviews
    • Guest Post
    • Sponsored Content
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • Video
    • White Papers
    • Webinars
  • EE Learning Center
  • Women in Engineering