如何使用SPI与Arduino和Raspberry Pi进行通信

串行外围界面(SPI)是一种同步的串行通信协议,主要用于短途通信,主要用于嵌入式系统。通过SPI将Arduino的多功能性与Raspberry Pi的计算能力相结合可以打开许多项目可能性。在这篇博客文章中,我们将探讨如何在Arduino和Raspberry Pi之间进行设置和使用SPI通信。

了解SPI

SPI是一种以主奴隶模式运行的全双工通信协议。它使用四个主要行:

  • 莫西(主人奴隶): 将数据从主转移到从属。
  • 味o(奴隶的主人): 将数据从从属传输到主。
  • SCLK(串行时钟): 同步由主生成的数据传输。
  • SS/CS(从属选择/芯片选择): 选择从设备。

SPI以其简单性和速度而受到青睐,非常适合需要设备之间快速数据交换的应用程序。

为什么要将SPI与Arduino和Raspberry Pi一起使用?

将Arduino与Raspberry Pi相结合,利用了两个平台的优势。 Arduino在实时的低级硬件控制下擅长,而Raspberry Pi则提供高级处理功能,网络连接和丰富的操作系统环境。使用SPI允许这两个设备有效地通信,从而启用复杂的项目,例如家庭自动化系统,机器人技术和数据记录应用程序。

设置硬件

要在Arduino和Raspberry Pi之间建立SPI通信,您将需要以下组件:

  • Arduino Uno(或任何兼容的Arduino董事会)
  • Raspberry Pi(带有GPIO引脚的任何型号)
  • 跳线
  • 面包板(可选)

将Arduino和Raspberry Pi与SPI接线

仔细的接线对于确保正确沟通至关重要。这是使用SPI连接Arduino和Raspberry Pi的方法:

覆盆子Pi Gpio Pin Arduino Pin 描述
GPIO10(MOSI) 引脚11(MOSI) 掌握奴隶
GPIO9(味o) 引脚12(味o) 大师奴隶
GPIO11(SCLK) 引脚13(SCLK) 串行时钟
GPIO8(CE0) 引脚10(SS) 从选择
gnd gnd 共同点
3.3V 5V 电源(如有必要,使用水平转换)

笔记: Raspberry Pi在3.3V逻辑水平上运行,而Arduino Uno使用5V。建议使用逻辑级转换器来防止对覆盆子PI的潜在损害。

配置Arduino

Arduino将充当SPI从设备。以下是一个示例Arduino草图,以设置它:


// Arduino as SPI Slave

#include 

volatile byte receivedData = 0;

void setup() {
  // Initialize serial communication for debugging
  Serial.begin(9600);

  // Set MISO as output
  pinMode(MISO, OUTPUT);

  // Enable SPI in Slave Mode
  SPCR |= _BV(SPE);
  SPI.attachInterrupt();
}

ISR(SPI_STC_vect) {
  receivedData = SPDR;
}

void loop() {
  if (receivedData) {
    Serial.print("Received: ");
    Serial.println(receivedData);
    receivedData = 0;
  }
}

解释:

  • spi.AttachInterrupt(); 启用S​​PI中断,允许Arduino处理传入数据。
  • 在中断服务程序中 ISR(SPI_STC_vect),收到的数据存储用于处理。
  • loop() 功能检查接收到的数据并将其打印到串行监视器。

配置覆盆子Pi

Raspberry Pi将充当SPI主设备。我们将使用Python与 spidev 库处理SPI通信。首先,确保启用SPI:

  • 打开Raspberry Pi配置工具:
    sudo raspi-config
  • 导航到 接口选项 > spi > 使能够
  • 如果提示,请重新启动覆盆子Pi。

安装 spidev 库如果尚未安装:

sudo apt-get install python3-spidev

这是Raspberry Pi的示例Python脚本:

# Raspberry Pi as SPI Master

import spidev
import time

# Open SPI bus 0, device (CS) 0
spi = spidev.SpiDev()
spi.open(0, 0)

# Set SPI speed and mode
spi.max_speed_hz = 50000
spi.mode = 0

def send_data(data):
    """Send a single byte to the SPI slave"""
    response = spi.xfer2([data])
    return response

try:
    while True:
        data = 42  # Example data byte
        print(f"Sending: {data}")
        resp = send_data(data)
        print(f"Received: {resp[0]}")
        time.sleep(1)

except KeyboardInterrupt:
    spi.close()

解释:

  • spi.open(0,0) 打开SPI总线0,设备0(CE0)。
  • spi.xfer2([数据]) 发送数据字节并同时从从服务器接收数据。
  • 该脚本每秒发送一个字节(例如42),并打印来自Arduino的响应。

测试通信

建立了Arduino和Raspberry Pi之后:

  1. 将Arduino草图上传到Arduino Board。
  2. 通过SPI接线将Arduino连接到Raspberry Pi。
  3. 在Raspberry Pi上运行Python脚本:
    python3 spi_master.py
  4. 打开Arduino串行监视器以查看接收的数据:
    Tools > Serial Monitor

您应该看到Arduino接收由Raspberry Pi发送的数据并将其显示在串行显示器中。同样,Raspberry Pi将显示其发送的数据及其收到的响应。

故障排除提示

  • 检查接线: 确保Arduino和Raspberry Pi之间的所有连接均安全并正确映射。
  • 电压水平: 使用逻辑级转换器将Raspberry Pi的3.3V与Arduino的5V匹配。
  • 启用S​​PI: 验证使用Raspberry Pi启用SPI raspi-config.
  • 许可: 确保您的用户拥有访问SPI设备的必要权限。您可能需要使用Python脚本 sudo.
  • 波特率: 确保串行显示器和Arduino草图使用相同的波特率。
  • SPI设置: 确保Master和Nave都采用相同的SPI模式和速度配置。

结论

使用Arduino和Raspberry Pi之间的SPI通信,您可以有效利用两个平台的优势。无论您是构建复杂的机器人系统,开发传感器网络还是尝试数据记录,了解SPI都是无价的。通过遵循本指南中概述的步骤,您可以建立可靠的SPI通信,并着手进行令人兴奋的嵌入式项目,以利用Arduino和Raspberry Pi的力量。

快乐的修补!

发表评论

Notice an Issue? Have a Suggestion?
If you encounter a problem or have an idea for a new feature, let us know! Report a problem or request a feature here.