I'm trying to use the i2c bus between Arduino nano 33 IOT and the Jetson Nano 2gb. I'm using the i2c bus and I want to send an array of integers to the Jetson Nano but when I receive data over the bus it's gibberish and destroys the data sent from the Jetson to the Arduino.
Jetson Nano pins: GND, 27 (SDA), 28 (SDL) Arduino Nano 33 IoT pins: GND, A4 (SDA), A5 (SCL)
Arduino code:
#include <Wire.h>
int data [4];
int x = 0;
void setup() {
Serial.begin(9600);
Wire.begin(0x6a);
Wire.onReceive(receiveData);
Wire.onRequest(sendData);
}
void loop () {
//sendData();
delay(100);
}
void sendData() {
int arr[4] = { 0, 23, 41, 19 };
Serial.println("Sending Data to Jetson ...");
//sendI2C((byte*) arr, sizeof(arr));
Wire.write( (byte*)&arr, sizeof(arr));
Serial.print("Sent...");
Serial.println();
}
//void sendI2C(byte *data, int size) {
// Wire.beginTransmission(0x6a);
// for(int i = 0; i < size; i ) {
// Wire.write(data[i]);
// }
// Wire.endTransmission();
//}
void receiveData(int byteCount) {
while(Wire.available() && x < 4) { //Wire.available() returns the number of bytes available for retrieval with Wire.read(). Or it returns TRUE for values >0.
data[x]=Wire.read();
x ;
}
if(x == 4) { x = 0; }
Serial.println("----");
Serial.print(data[0]);
Serial.print("\t");
Serial.print(data[1]);
Serial.print("\t");
Serial.print(data[2]);
Serial.print("\t");
Serial.println(data[3]);
Serial.print("----");
//sendData();
}
Jetson Nano - Python3 code:
import smbus
import time
bus = smbus.SMBus(0)
address = 0x6a
def writeArray(a, b, c, d):
bus.write_i2c_block_data(address, a, [b, c, d])
return -1
def readArray(bytes_nr):
values = bus.read_i2c_block_data(address, 0x00, bytes_nr)
return values
while True:
writeArray(14,42,95,0)
time.sleep(1)
values = readArray(8)
print(values)
There are 2 things that happen:
- When I only send data from the jetson nano to the arduino, on the Serial monitor of the arduino the data is received
correctly:
[14, 42, 95, 0]
- When I try to sendData() to the Jetson Nano on the Jetson Nano console the data received 'print(values)' is like this:
[0, 0, 0, 0, 0, 0, 42, 105, 0 , 0, 4, 0 , 0 ,0 ,0 , 56, 0 , 0 , 0 ,0 ,0, 187, 0 , 0 ,0, 0, 0 , 0]
-- And on the Arduino console the data shifts from left to right so instead of receiving `[14, 42, 95, 0]`, It prints
[8, 14, 42, 95]
I'm just interested in sending an array of 4 integers from both sides.
Can someone extend a helping hand?
Thank you!
CodePudding user response:
Arduino code uses raw I2C protocol, Jetson uses SMBus on top of I2C. SMBus block structure implies 1 additional byte of length to be transferred.
CodePudding user response:
Thank you for the response @Dymyto Bagrii, I will read about the SMBus library and maybe come to a solution, I will post it if it works.
KR, Armand