【发布时间】:2012-12-14 17:43:28
【问题描述】:
我正在尝试使用 I²C 通过I²C 接口将数据从Arduino Uno 发送到Raspberry Pi。这是我使用的代码。
在 Arduino 中:
#include <Wire.h>
unsigned int watt;
unsigned int watt1;
byte watt2;
byte watt3;
void setup()
{
Wire.begin(30);
Wire.onRequest(requestEvent);
Serial.begin(9600);
}
void loop() {
delay(100);
int sensorValue = analogRead(A0);
int sensorValue1 = analogRead(A1);
watt = sensorValue * (5 / 1023) * 2857;
watt1 = sensorValue1 * (5 / 1023) * 2857;
watt2 = map(watt, 0, 4294967295, 0, 255);
watt3 = map(watt1, 0, 4294967295, 0, 255);
Serial.println(watt2);
Serial.println(watt3);
}
void requestEvent()
{
Wire.write(watt2);
delay(30);
Wire.write(watt3);
}
在树莓派中:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x1e
while (1):
watt=bus.read_byte_data(address,1)
watt2=bus.read_byte_data(address,2)
我收到以下错误。
回溯(最近一次通话最后一次):
中的文件“/home/pi/i2ctest.py”,第 8 行 watt = bus.read_byte_data(地址,1)
IOError: [Errno 5] 输入/输出错误
我该如何解决这个问题?另外,除了 SMBus 库之外,在 Raspberry Pi 中使用 I²C 是否还有其他选择?
【问题讨论】:
标签: python arduino raspberry-pi i2c