Raspberry Pi是I2C中的奴隶,Arduino作为主人



我正在尝试制作一个代码,在该代码中我在Arduino中运行主程序,并在需要时从Raspberry Pi中获取I2C总线数据。因此,我需要将我的Arduino配置为I2C Master和Raspberry Pi作为I2C奴隶。是否可以像将Pi成为主人和Arduino奴隶一样做?如果没有,还有其他方法吗?

p.s。: - 我只进行一对一的沟通,而Arduino是主人,而Raspberry作为奴隶。没有其他设备连接。

感谢您的帮助。

是;这是我在建造一个气象站时所做的事情,在那里我需要Arduino模拟和中断触发输入。在主人上,Python代码看起来像:

i2c_ch = 1
bus = smbus.SMBus(i2c_ch)
#address of the Arduino slave:
i2c_address = 20 
...
readArray  = bus.read_i2c_block_data(i2c_address,8)

然后在arduino上,代码看起来像:

#define I2C_SLAVE_ADDR 20
void setup() {
  Wire.begin(I2C_SLAVE_ADDR);
  Wire.onReceive(receieveEvent); 
  Wire.onRequest(requestEvent);
}
void receieveEvent() { //for reading data from the master
  byte byteRead = 0;
  while(0 < Wire.available()) // loop through all but the last
  {
    byteRead = Wire.read();
  }
}
void requestEvent(){ //for sending data to the master
  long val = millis(); //whatever you want to send, in this case millis()
  byte buffer[4];
  buffer[0] = val >> 24;
  buffer[1] = val >> 16;
  buffer[2] = val >> 8;
  buffer[3] = val;
  Wire.write(buffer, 4);
}

有关此此详细信息的更多详细信息,请在此处查看我为此气象站的代码制作的GitHub存储库:https://github.com/judasgutenberg/i2c-weather-slave

最新更新