###raspberry pi とarduino間でI2C通信がしたい
Arduinoでアナログデータを取得し、Raspberry piの方に送ることを考えています。そこでhttp://revetronique.blog.fc2.com/blog-entry-16.htmlのプログラムを借り、I2C通信が可能であるかチェックを試みました。しかしRaspberry piでプログラムを実行すると、以下のエラーメッセージが表示されました。この問題の解決方法が知りたいです。
[Errno 121] Remote I/O error>
###Raspberry pi側のプログラム
python
1#! /usr/bin/env /usr/bin/python 2 3import smbus 4import time 5#for RPI ver1, use "bus=smbus.SMBus(0)" 6bus=smbus.SMBus(1) 7 8#This is the address we setup in the <a href="http://blog.fc2.com/tag/Arduino" class="tagword">Arduino</a> Program 9address = 0x04 10 11def writeNumber(value): 12 bus.write_byte(address, value) 13 #bus.write_byte_data(address, 0, value) 14 return -1 15 16def readNumber(): 17 #get the sensor value from the <a href="http://blog.fc2.com/tag/Arduino" class="tagword">Arduino</a>(signed 16bit little-endian)... 18 #number=bus.read_byte(address) 19 number=bus.read_word_data(address, 0) 20 return number 21 22def main(): 23 while True: 24 writeNumber(0) 25 #sleep one second 26 time.sleep(1) 27 28 number=readNumber() 29 print number 30 #time.sleep(1) 31 32if __name__ == '__main__': 33 main()
###Arduino側のプログラム
Arduino
1#include <Wire.h> 2 3#define SLAVE_ADDRESS 0x04 //address of <a href="http://blog.fc2.com/tag/Arduino" class="tagword">Arduino</a> via i2c connection 4 5void setup() { 6 //initialize i2c as slave 7 Wire.begin(SLAVE_ADDRESS); 8 9 //define callbacks for i2c communication 10 Wire.onReceive(receiveData); 11 Wire.onRequest(sendData); 12} 13 14void loop() { 15 // wait 100ms 16 delay(100); 17} 18 19//callback for received data 20void receiveData(int byteCount){ 21 //Read all remaining data sent from <a href="http://blog.fc2.com/tag/Raspberry" class="tagword">Raspberry</a> <a href="http://blog.fc2.com/tag/Pi" class="tagword">Pi</a> 22 while(Wire.available()){ 23 int buf = Wire.read(); 24 } 25} 26 27//callback for sent data 28void sendData(){ 29 int val = 1023 - analogRead(1); //convert sensor output to 10-bit digital value 30 byte adc[] = {val & 0xff, (val >> 8) & 0xff}; //separate the value to lower 8-bit and upper 8-bit. 31 32 //Transfer ADC value to <a href="http://blog.fc2.com/tag/Raspberry" class="tagword">Raspberry</a> <a href="http://blog.fc2.com/tag/Pi" class="tagword">Pi</a> 33 Wire.write(adc, sizeof(int)); 34}
###接続ピン
Arduinoピン | Raspberry piピン |
---|---|
SCL | 5 |
SDA | 3 |
GND | GND |
3.3V | 3.3V |
#補足情報
raspberry pi側で$sudo i2cdetect -y 1によりarduinoが認識されているか,
チェック済みです。
回答1件
あなたの回答
tips
プレビュー