使用Arduino Mega作为I2C Slave和RPi3

问题描述 投票:0回答:2

我正在尝试使用Arduino Mega 2560来扩展带有PWM和模拟输入的RPi3的I / O.事实上,我根本不使用RPi3 GPIO引脚,因为输入3.3和5 V很难保持两个电压。

基本上,我想:

  • 从RPi3发送一个数组来设置Arduino和中的输出
  • 从Arduino发送一个数组到RPi3,给出输入状态。

数组中的某些值可能高达10000。

我已经能够达到上面的数字1而没有高于255的值。

Python代码

bus = smbus.SMBus(1)
address = 0x06

def writeNumber(value):
    bus.write_i2c_block_data(address, 1, [5,0,1,255, 6]) #dummy array as of now. This can go upto 50 values
    return -1


def readNumber():
    # number = bus.read_byte(address)
    data_received_from_Arduino = bus.read_byte(address)
    for i in data_received_from_Arduino:
       print(i)

    return number

while i1:
    writeNumber(1)
    readNumber()

Arduino代码

#include <Wire.h>

#define SLAVE_ADDRESS 0x06
int number[50] = {0};
int inputs[100] = {0};

int state = 0;
int p=0; 

void setup() {
pinMode(13, OUTPUT);

Serial.begin(9600); // start serial for output
// initialize i2c as slave
Wire.begin(SLAVE_ADDRESS);

// define callbacks for i2c communication
Wire.onReceive(receiveData);
Wire.onRequest(sendData);

Serial.println('Ready!');
}

void loop() {

//delay(1);
}

// callback for received data
void receiveData(int byteCount){
  Serial.println(byteCount);
  int p=0;
  while(Wire.available()) {
      number[p] = Wire.read();
      p++;
  }
    for(int k=0; k < 5; k++)  { 
    Serial.print( k);
    Serial.print( ":");
    Serial.println(number[k]); 
  }

}


// callback for sending data
void sendData(){  
  for(int k=0; k < 56;k++) {
  inputs[k] = digitalRead(k);
  Serial.print( k ); Serial.print(" : "); Serial.print(inputs[k]); 
  Serial.println(digitalRead(k));
  }
  Wire.write( inputs,56);
}

有人可以指导吗?有没有人知道实现上述的Git示例。即使样本是用于小型数组,我也可以为我的应用程序构建它。

arduino raspberry-pi i2c
2个回答
0
投票

我一直在尝试通过I2C发送和接收从Raspberry Pi到Arduino的四个16位数字,并得到以下工作。

请注意,我不是SMBus或I2C的专家,我不知道是否有更简单的方法可以做到这一点。如果有人知道的话,我很乐意收回我的答案!

这是Raspberry Pi的代码,它只发送四个16位数字100,200,1000,10000然后再读回来。

#!/usr/bin/env python3
from smbus import SMBus
from time import sleep

bus = SMBus(1)
address = 0x08

def split(v):
    """Split 16-bit value into low and high bytes"""
    lobyte = v & 0xff
    hibyte = (v >> 8) & 0xff
    return lobyte, hibyte

def join(lo,hi):
    return lo | (hi << 8)

def Transmit():
    """Send 100, 200, 1000, 10000 on I2C"""
    a,b = split(100)
    c,d = split(200)
    e,f = split(1000)
    g,h = split(10000)
    bus.write_i2c_block_data(address, a,[b, c, d, e, f, g, h])

def Receive():
    block = bus.read_i2c_block_data(address, 0)
    i = join(block[0],block[1])
    j = join(block[2],block[3])
    k = join(block[4],block[5])
    l = join(block[6],block[7])
    print("{} {} {} {}".format(i,j,k,l))

Transmit()
sleep(1)
Receive()

在Arduino方面,我只是从I2C中读取了4个16位数字,将它们存储在一个数组中并递增每个数字。当读取请求进入时,我发回四个递增的数字:

#include <Wire.h>

const int address= 8;

#define N 4

// Last four 16-bit values we received
int16_t values[N];

void setup() {
  Serial.begin(9600);
  Serial.print("Starting on i2c address:");
  Serial.println(address,DEC);
  Wire.begin(address);
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void loop() {
  delay(100);
}

// callback for when data are received
void receiveEvent(int nBytes) {
  Serial.print("Received: ");
  Serial.println(nBytes);

  if(nBytes != 2 *N){
     Serial.print("I was expecting 8 bytes");

     return;
  }
  unsigned char *p = (unsigned char *)&values;
  for(int i=0;i<2*N;i++){
    *p++ = Wire.read();
  }
  // Increment all the values we received
  for(int i=0;i<N;i++){
    values[i]++;
  }

}

// Callback for when data are read
void requestEvent() {

  Serial.println("Data requested");
  // Send back
 Wire.write((const uint8_t*)&values, N*2);
}

当我在Raspberry Pi上运行Python代码时,我得到:

./i2c.py 
101 201 1001 10001

0
投票

与raspberry pi和arduino进行通信的最简单方法是使用串行协议。我一直都在用这个。

在python中有一个用于串行通信pyserial的模块。

https://www.electronicwings.com/raspberry-pi/raspberry-pi-uart-communication-using-python-and-c

© www.soinside.com 2019 - 2024. All rights reserved.