Notifications
Clear all

Raspberry pi with Arduino using i2c

3 Posts
2 Users
1 Likes
748 Views
(@mohamedmym)
Member
Joined: 2 years ago
Posts: 2
Topic starter  

Hi,

I'm trying to connect raspberry pi with Arduino using I2C communication protocol, But I'm have a problem.

when I'm trying to write data to Arduino (as it's the slave device) from the pi (the master device),

the data arrives perfectly.

but when I'm trying to read data from the Arduino I always get 0.

here's my code plz some one help me.

//Arduino Code:

#include <Wire.h>
int dataSize;
char data[32];
char response[32];
char Buffer[80];
String answer = "Hi from Arduino";

void setup() {
    pinMode(13, OUTPUT);
    digitalWrite(13, 0);
    Serial.begin(9600);
    Wire.begin(0x8);

    Wire.onReceive(receiveEvent);
    Wire.onRequest(requestEvent);
    delay(500);
    Serial.println("Welcome To I2c test.");
    for (byte i = 0; i < 32; i++) {
        data[i] = 0x00;
    }
    for (byte i = 0; i < 32; i++) {
        response[i] = 0x00;
    }

}

void loop() {
    if (Serial.available()) {
        answer = Serial.readString();
    }
    delay(10);
}

int counter = 0;
char x;

void receiveEvent(int howMany) {
    dataSize = Wire.read();

 

    while (Wire.available()) {
        x = Wire.read();
        data[counter] = x;
        counter++;
    }

    Serial.print("Data Size: ");
    Serial.println(dataSize);
    Serial.print("Data Received: ");
    Serial.println(data[0]);
    Serial.println(data);
    counter = 0;

    for (int i = 0; i < 30; i++)
        data[i] = 0;
    }

    void requestEvent(){

    char response[32];

    if (sizeof(answer) < 32) {
        for (byte i = 0; i < 32; i++) {
            if (answer.charAt(i) != 0)
                response[i] = answer.charAt(i);
            else
                response[i] = 0xff;
        }
    }    

    dataSize = Wire.read();
    Wire.write(response);
    sprintf(Buffer, "Data: \n[%03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d]"
, response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7]
, response[8], response[9], response[10], response[11], response[12], response[13], response[14], response[15]
, response[16], response[17], response[18], response[19], response[20], response[21], response[22], response[23]
, response[24], response[25], response[26], response[27], response[28], response[29], response[30], response[31]);

    Serial.println("Request Event");
    Serial .println(Buffer);

    for (byte i = 0; i < 32; i++) {
        response[i] = 0xff;
    }
    for (byte i = 0; i < 32; i++) {
        data[i] = 0xff;
    }

}

 

/***************************************************/

#Raspberry Pi Code (Python):

from smbus import SMBus
import time
name = "Welcome to my test"

data = []
bus = SMBus(1)

time.sleep(0.5)
data1 = 48
for i in name:
    data.append(i)

for i in range(len(data)):
    try:
        data [i] = ord(data[i])
    except:
        data[i] = 0

time.sleep(0.1)
#print (data)

#print(len(data), "Bytes")
#bus.write_byte_data(0x8,len(data),data1+2)
#print ("data sent")
time.sleep(1)
#received = bus.read_i2c_block_data(0x8,20,20)
#print(received)e
rec = bus.read_i2c_block_data(0x8,32,32);
print(rec)

##################################

I've tried the read_i2c_block_data () function, and the read_byte_data() function but still the same.

 


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 3 years ago
Posts: 6970
 

Please read the help/faq on how to post code, and before posting use Auto Format. Thank you.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
Sure you can learn to be a programmer, it will take the same amount of time for me to learn to be a Doctor.


   
ReplyQuote
(@mohamedmym)
Member
Joined: 2 years ago
Posts: 2
Topic starter  
Posted by: @mohamedmym

Hi,

I'm trying to connect raspberry pi with Arduino using I2C communication protocol, But I'm have a problem.

when I'm trying to write data to Arduino (as it's the slave device) from the pi (the master device),

the data arrives perfectly.

but when I'm trying to read data from the Arduino I always get 0.

here's my code plz some one help me.

//Arduino Code:

#include <Wire.h>
int dataSize;
char data[32];
char response[32];
char Buffer[80];
String answer = "Hi from Arduino";

void setup() {
    pinMode(13, OUTPUT);
    digitalWrite(13, 0);
    Serial.begin(9600);
    Wire.begin(0x8);

    Wire.onReceive(receiveEvent);
    Wire.onRequest(requestEvent);
    delay(500);
    Serial.println("Welcome To I2c test.");
    for (byte i = 0; i < 32; i++) {
        data[i] = 0x00;
    }
    for (byte i = 0; i < 32; i++) {
        response[i] = 0x00;
    }

}

void loop() {
    if (Serial.available()) {
        answer = Serial.readString();
    }
    delay(10);
}

int counter = 0;
char x;

void receiveEvent(int howMany) {
    dataSize = Wire.read();

 

    while (Wire.available()) {
        x = Wire.read();
        data[counter] = x;
        counter++;
    }

    Serial.print("Data Size: ");
    Serial.println(dataSize);
    Serial.print("Data Received: ");
    Serial.println(data[0]);
    Serial.println(data);
    counter = 0;

    for (int i = 0; i < 30; i++)
        data[i] = 0;
    }

    void requestEvent(){

    char response[32];

    if (sizeof(answer) < 32) {
        for (byte i = 0; i < 32; i++) {
            if (answer.charAt(i) != 0)
                response[i] = answer.charAt(i);
            else
                response[i] = 0xff;
        }
    }    

    dataSize = Wire.read();
    Wire.write(response);
    sprintf(Buffer, "Data: \n[%03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d,\n %03d,%03d,%03d,%03d,\t%03d,%03d,%03d,%03d]"
, response[0], response[1], response[2], response[3], response[4], response[5], response[6], response[7]
, response[8], response[9], response[10], response[11], response[12], response[13], response[14], response[15]
, response[16], response[17], response[18], response[19], response[20], response[21], response[22], response[23]
, response[24], response[25], response[26], response[27], response[28], response[29], response[30], response[31]);

    Serial.println("Request Event");
    Serial .println(Buffer);

    for (byte i = 0; i < 32; i++) {
        response[i] = 0xff;
    }
    for (byte i = 0; i < 32; i++) {
        data[i] = 0xff;
    }

}

 

/***************************************************/

#Raspberry Pi Code (Python):

from smbus import SMBus
import time
name = "Welcome to my test"

data = []
bus = SMBus(1)

time.sleep(0.5)
data1 = 48
for i in name:
    data.append(i)

for i in range(len(data)):
    try:
        data [i] = ord(data[i])
    except:
        data[i] = 0

time.sleep(0.1)
print (data)

print(len(data), "Bytes")
bus.write_byte_data(0x8,len(data),data1+2)
print ("data sent")
time.sleep(1)

rec = bus.read_i2c_block_data(0x8,32,32);
print(rec)

##################################

I've tried the read_i2c_block_data () function, and the read_byte_data() function but still the same.

 

 


   
ReplyQuote