I/O Error [121] between Arduino and Rpi using i2c communication

不羁的心 提交于 2020-02-07 04:04:04

问题


I have connected Rpi with Arduino using i2c communication. Pin Configuration: Rpi ---- Arduino Mega SDA SDA SCL SCL GND GND

I also have an encoder attached to Rpi. Encoder `------ Rpi 5V 5V Gnd Gnd Output A Gpio 17 (BCM layout) Output B Gpio 18 (BCM Layout)

Code is working perfectly fine, however after sometime, I am getting an error: 121, Remote I/O Error.

Also, before the error (before starting the communication), on executing i2cdetect -y 1, I see only one slave address (20) same address as my Pi.

However, after the error, on executing i2cdetect -y 1, I get 2 addresses, even tho only Arduino is connected.

Output for before communication:
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f  
00           --  -- -- -- -- -- -- -- -- -- -- -- --
10   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20   20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
Output after error, on executing i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f  
00           03  -- -- -- -- -- -- -- -- -- -- -- --
10   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20   20 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70   -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --

Python:
# -*- coding: utf-8 -*-
import RPi.GPIO as GPIO
import time
import serial
import os
import smbus
#import time
bus = smbus.SMBus(1)
time.sleep(1)
address = 0x20


GPIO.setmode (GPIO.BCM)
GPIO.setwarnings(True)

i=0
pin_A = 17
pin_B = 18

######################################################################
def writeNumber(value):
    bus.write_byte(address, value)
    return -1

def readNumber():
    number = bus.read_byte(address)

    return number
###################################################################
Encoder_Count = 0
A_Pos2=0
GPIO.setup (pin_A, GPIO.IN)
GPIO.setup (pin_B, GPIO.IN)
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)#Button to GPIO23
A_Pos = 0
A_Last = "00"
STATE = {"0001":1,"0010":-1,"0100":-1,"0111":1,"1000":1,"1011":-1, "1101":-1, "1110":1}     

def Encoder1(channel1):
    global Encoder_Count,A_Pos,A_Last,STATE
    now = str(GPIO.input(17)) + str(GPIO.input(18))
    key = A_Last + now
    if key in STATE:
            direction = STATE[key]
            A_Last = now
            A_Pos +=direction


GPIO.add_event_detect (pin_A, GPIO.BOTH, callback=Encoder1)  
GPIO.add_event_detect (pin_B, GPIO.BOTH, callback=Encoder1)
i=0
#while(i<10):
while(1):
    var = 100
        button_state = GPIO.input(23)
   # ser.write(b'1')
        A_Pos2= A_Pos/(1600)
        time.sleep(0.01)
    print A_Pos2
    writeNumber(A_Pos2)
        if (A_Pos2 ==-6):
        #writeNumber(7)

        A_Pos = A_Pos % 6
        time.sleep(0.01)
    if (A_Pos2 ==6):

        #writeNumber(A_Pos2)
        #A_Pos=0
        A_Pos = A_Pos % 6
        time.sleep(0.01)

    #else:


        if button_state == False:
            # GPIO.output(24, True)
            os.system("sudo shutdown now -h")


GPIO.cleanup()


Arduino:
#include <Wire.h>
#define SLAVE_ADDRESS 0x20
int number = 0;
void setup()
{

    Wire.begin(SLAVE_ADDRESS);
  Wire.onReceive(receiveData);

  delay(2000);

}

void loop()
{
if (number == 10)
{
// do something
}

// callback for wire communication:
void receiveData(int byteCount) {

  if (Wire.available()) {
     digitalWrite(5,HIGH);
    number = Wire.read();

    Serial.println (number);
    delay(100);

}

来源:https://stackoverflow.com/questions/57972271/i-o-error-121-between-arduino-and-rpi-using-i2c-communication

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!