I have two Teensy4.1 Dev boards. I am trying to set up an I2C between them. The hardware connection of these boards is such that both the boards are internally connected, which means they share the same power source. Connecting power cable to one board automatically powers-up the other board.
The issue that I am facing currently is implementing an I2C between both the boards. My aim is to let the master send some data to the slave and the slave should respond back to the master with the received data.
For instance,
1.) Master sends a byte (data = 7 ) to the slave.
2.) Slave receives the data from the master.
3.) Slave re-transmits the data to the master.
4.) Master receives the retransmitted data from the slave.
Currently, I am successful with the first three steps, but got stuck in the 4th step. No matter what, the master never receives any data. I searched for all the topics on this forums, but I couldn't find any solution to this issue.
Below is my code:
I don't know how well the Teensy 4.1 acts as a I2C Slave. I don't know if the Teensy allows to use the Serial.println() inside an interrupt routine (the requestEvent() and receiveEvent() are called from a interrupt routine). At least don't do that in the final version.
Can you use the names that are used by Arduino ?
Such as "receiveEvent()" and "requestEvent()".
The Slave should set the onReceive and the onRequest handlers (the onRequest is not set).
Wire.onRequest(requestEvent);
Can you start with a single byte and remove the while-loops.
If you request just 1 byte in the Master, then there is no need to use a while-loop.
#include <Wire.h>
#define SLAVE_ADDRESS 0x20
byte reTxData;
volatile bool flag = false;
void setup()
{
Serial.begin(9600);
Wire.onReceive(receiveEvent);
Wire.onRequest(sendEvent);
Wire.begin(SLAVE_ADDRESS);
}
void loop()
{
if (flag == true)
{
Serial.print("Data received from Master: ");
Serial.println(reTxData);
flag = false;
}
}
void receiveEvent(int numBytes)
{
reTxData = Wire.read();
flag = true;
}
void sendEvent()
{
Wire.write(reTxData);
}
2. Upload the above sketches in MAster and Slave Arduinos. 3. Press the RESET Buttons of both Arduinos. 4. Check that Serial Moniotr of both Arduinos show 7at 1-sec interval.
Your provided solution works perfect. I could see on the following data displayed on the Master Terminal:
Data Transmission is ok!
Received data: 7
Because both of my Teensy4.1 Dev. boards share a single power source, I cannot monitor the terminals for both the Master and Slave device simultaneously as only one of them is connected to the actual serial cable.
So I tried checking the terminals for each of them independently and what I noticed is that I can see the result getting displayed on the Master's terminal but I do not see any serial data printed on the Slave's terminal.
Would it be okay if I power-up both the boards simultaneously (while keeping the same power source connection) to only check if both the terminals display data simultaneously? Would this attempt burn up the boards?
How long is the distance between the two Teensy boards ?
If one device on a I2C bus has no power, then the I2C bus does not work anymore. The I2C bus is also not plug and play.
The high level of the I2C bus is kept high with a pullup resistor. You can shortcut SDA or SCL to GND at any time and nothing will be damaged.
You can power up one board, and then power up the other board, and they will not be damaged.
If you start the I2C bus on one Teensy board while the other is not powered, then the I2C bus might not work. It is even possible that the I2C bus stays in a stuck state when a I2C device gets confused.
I assume that you have connected the following types of two Boards (I don't on them) using I2C Bus.
You can connect your both boards with two USB Ports of the PC using separate USB Cables.You open two independent IDEs from the Desktop of the PC. After uploading the sketches from the respective IDEs, you can open Serial Monitors for both Master and Slave to monitor the transmitted/received data. (Be sure that the Boards are engaged with seperate virtual COM Ports.)
The distance between two board is very short. Probably 2 inches. They are mounted sideways on a cylindrical structure. Okay, I will check that if the other board gets powered up timely before the first board initiates I2C. Thanks a lot for this information.
As per both of your advices, I was able to test I2C both ways and it now works correctly. I can see the terminals for both the master and slave displaying messages after running them simultaneously.
I appreciate both of your advices and the provided help. Thank you for taking time.
PS:- Sorry @GolamMostafa and @Koepel , I bi-mistakenly deleted the response to your last message.