Closed Sorin-Jayaweera closed 2 years ago
To anyone who sees this, the issue is fixed. Both of my boards are teensy 4.0. I connected the i2c wires directly to the boards without pullup resistors, and used code another user sent me:
reciever:```
struct TransmitData { float x; float y; };
TransmitData data;
void setup() {
Wire.begin(); // join i2c bus Serial.begin(9600); // start serial for output while(!Serial) { } }
void loop() { Serial.print("requesting ("); Serial.print(sizeof data); Serial.print(" bytes)... "); if (Wire.requestFrom(0x40, sizeof data)) { Wire.readBytes((byte*) &data, sizeof data); Serial.println("done");
Serial.println(data.x);
Serial.println(data.y);
} else { Serial.println("could not connect"); } // delay(500); }
the other Teensy 4.1 SENDER:
struct TransData
{
float x;
float z;
};
TransData data;
void requestEvent();
void setup() {
Wire.begin(0x40); // join i2c bus with address #8 Wire.onRequest(requestEvent); // register event //Wire.setClock(1000000);
Serial.begin(9600); while(!Serial) { }
}
void loop() { data.x = 1.0; data.y = 2.0; }
// function that executes whenever data is requested by master // this function is registered as an event, see setup() void requestEvent() { Serial.print("sending ("); Serial.print(sizeof data); Serial.println(" bytes)");
Wire.write((byte *)&data, sizeof data); }
also, for sender sending whenever and reciever listening, this is code that works for me:
reciever:
struct TransmitData { float x; float y; };
TransmitData data; void recieveEvent(int howMany);
void setup() {
Wire.begin(9);
Wire.onReceive(recieveEvent); // join i2c bus
Serial.begin(9600); // start serial for output
while(!Serial) { }
}
void loop(){}
void recieveEvent(int howMany){ if (Wire.available() > 0) { Wire.readBytes((byte*) &data, sizeof data); Serial.println("done");
Serial.println(data.x);
Serial.println(data.y);
} }
sender
struct TransData
{
float x;
float y;
};
TransData data;
void requestEvent();
void setup() {
Wire.begin();
Serial.begin(9600); while(!Serial) { }
}
void loop() { Serial.println("sending"); data.x = 1.0;
data.y = 2.0; Wire.beginTransmission(9); Wire.write((byte *) &data, sizeof data); Wire.endTransmission(); delay(100);
}
// function that executes whenever data is requested by master // this function is registered as an event, see setup()
Hi finncat62. I'm glad you've solved the problem. Thanks for posting details of your solution. I'm sure it'll help someone else. cheers, Richard
I'm using two Teensy 4.0, on 4.5ish volts and the power doesn't fluxuate. Pins 18 and 19(sda and scl) are both connected to chained resistors(1000, 1000, 200 ohm) to 3.3v output from the teensy OHM resistors, The pins also each connect to the corresponding sda scl
This is the slave reciever code
and this is the master sender code
Please excuse the poor photos of my circuit.
Power in is usbc, stays at around 4.5-4.7, I feed it to the teensies. They are both on the same ground line, so I believe it should be fine. The third from top pin on the teensy is 3.3 out, which goes to the first long common line. That is what I put the resistors connected to pins 18 and 19 to. At the teensy after the wire with the resistors, the pins each connect to their own long common line, which then connects to the other teensy. SDA to SDA, SCL to SCL. Sorry for all the white wires, I had only had some leftover scraps to work with.