1

Hi I am attempting to test I2C comms between an pi model 4B and teensy 4.1 (running code uploaded from the arduino IDE via usb). While I can read from the slave with a python script if the arduino code has a Wire.write call in setup(), the event handler for onReceive doesn't trigger which makes me suspect that either data is not being written from the pi side correctly or I am not formatting the arduino side correctly.

I am new to I2C and working with arduino/c so am not sure where I am going wrong but hope if I can sort this out it will help me build from this to the functionality I need. Any help appreciated.

Python script on master pi:

from smbus2 import SMBus, i2c_msg


DEVICE_BUS = 1
DEVICE_ADDR_TEST = 0x8

bus = SMBus(DEVICE_BUS)

write = i2c_msg.write(DEVICE_ADDR_TEST, [40,50])
read = i2c_msg.read(DEVICE_ADDR_TEST, 80)

bus.i2c_rdwr(write, read)

print(read)

code on Arduino IDE:

#include <Wire.h>
  
 
void setup() {
  Wire.begin(0x8);               
  Wire.onReceive(receiveEvent); #this doesn't work
  Wire.write("foo"); #this does
}

void receiveEvent(int howMany) {
  while (Wire.available()) {
    Wire.write("foo");
    char c = Wire.read();
    printf("received: %c", c);
  }
}

void loop() {
  delay(100);
}
3
  • How long are your wires. I2C was not intended for module to module communications. Posting a schematic showing exactly how you have wired it and note wire lengths. That will enable us to give you a much better answer. You can see what you have very clearly, I cannot see anything.
    – Gil
    Nov 10, 2022 at 1:50
  • The wires are 15cm. The teensy board is connected on pin 18 (SDA), 19 (SCL) and ground, on the pi the ground wire goes to ground pin 20, the SDA goes to pin 3 and the SCl goes to pin 5. I was advised to use the I2C 1 connectors over I2C 0 on the pi. Nov 10, 2022 at 9:31
  • That is for 1, how many are there? What is the value of the pull up resistors? Waiting on the schematic.
    – Gil
    Nov 10, 2022 at 16:21

2 Answers 2

1

Your Question involves 4 components

  1. Pi Configuration (unspecified)
  2. PythonCode
  3. Connection between Pi & other device (unspecified)
  4. Arduino code

Do not assume anyone on this site knows what a "teensy 4.1" is (although I doubt this is relevant).

Most of us would attempt to debug this systematically.
I would start with i2cdetect or other i2c_tools.

Frankly using I²C between devices is a poor choice (although I have done this).
For a simple message exchange serial would be more suitable (and easier to debug).

For what is is worth the Pi code looks OK (as it appears to have been copied verbatim from the smbus2 website) but is totally unrelated to the Arduino software.

1
  • I managed to find what I was missing, I did find i2c_tools was helpful for verifying the setup was correct though. I am using I2C over serial as while I am currently working with one master/slave initially for testing, I will have multiple slaves eventually and I2C makes it much easier to target communication and allows for any logic written for one slave to be more easily adapted to multiple from what I can tell. Nov 11, 2022 at 10:40
0

Ok so from reading around more I realised that I misunderstood how onReceive related to I2C structure.

onReceive is purely for handling being written data, in this case from the master and will not allow the slave to write back in this scope even if the master has a subsequent read request.

What I was missing was an onRequest event handler:

#include <Wire.h>
  
 
void setup() {
  Wire.begin(0x8);
  Serial.begin(9600);                
  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}

void receiveEvent(int howMany) {
  while (Wire.available()) {
    char c = Wire.read();
    Serial.print(c);
  }
}

void requestEvent() {
  Wire.write("foo");
}

void loop() {
  delay(100);
}

Which is called in the event of a master requesting data from the slave, with this addition the code is now working as intended and I should be able to build from here to get the functionality I want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.