2 Arduino I2C communication

Post and discuss new components that you have created.
max.tisc
Posts: 77
http://meble-kuchenne.info.pl
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Flowcode v9 2 Arduino I2C communication

Post by max.tisc »

Hi everyone
I'm having difficulty with data transmission between 2 Arduinos with the I2C protocol, I searched the forum but it seems that no one has yet addressed the topic, in practice I have an Arduino mega2560 set as master that reads a sensor in SPI, processes the data collected and then the result is sent to the slave Arduino, a mega328, the SPI part works well but the I2C does not communicate. Do you have any working examples to take as inspiration?
Thank you

max.tisc
Posts: 77
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Re: 2 Arduino I2C communication

Post by max.tisc »

Has anyone done this before or has any ideas on how to do it?
Thank you

mnfisher
Valued Contributor
Posts: 1046
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: 2 Arduino I2C communication

Post by mnfisher »

I created a simple demo that uses the i2c master and slave components.

I have the master as an Arduino Mega and the slave as an Arduino Nano - but this should work with any combination of boards.

Connect SDA, SCL and GND (a common ground is essential) I also added 10k pullups on SDA and SCL - but after getting things working I removed them and nothing seemed to break :-)

I also connected an oscilloscope (logic probe) with i2c decoding to help develop and debug the code. This was especially helpful checking for ACK/NACK (and I'm not sure I really understand the 'mask' property in the slave code.. I use 0 - which should match any address (I think?))

So - a rather 'rough and ready' example - the Nano (slave) receives the address byte sent by the master and then 4 data bytes (and it outputs these to UART) The master just outputs an address, followed by 4 data bytes (which are incremented by one each time) then delays for 1s and repeats.

I initially tried to output 'last byte' as true to stop the master - but didn't succeed - so here it just uses a loop counter. Looking at the trace - it seems to output ACK - even when last byte is set to true. This might be a bug in the component (or my code?)
i2c_send.fcfx
(13.04 KiB) Downloaded 51 times
rec_i2c.fcfx
(15.55 KiB) Downloaded 52 times
LabNation_Screenshot26.png
LabNation_Screenshot26.png (199.69 KiB) Viewed 581 times
Martin

mnfisher
Valued Contributor
Posts: 1046
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: 2 Arduino I2C communication

Post by mnfisher »

I got this working reliably at 100kHz and 400kHz. At 1MHz it worked a while before stopping (I was surprised it worked as well as it did though)

I set the slave address (arbitrarily) as 0x54 - when transmitted this is shifted left one bit (with bit 0 set to 'read' data) - So here address displays in UART as 168 (0x54 x 2 in decimal)

One issue - i2c is fairly short range - UART might be a better fit if you want longer connections.

Because I've slightly messed up my FC installation (oops) - I program both devices from a command prompt - the nano via USB and the Mega via an ISP programmer (hence the Mega doesn't output anything to UART)

Martin

max.tisc
Posts: 77
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Re: 2 Arduino I2C communication

Post by max.tisc »

Thank you very much mnfisher for your example
I also tried a few days ago to create a similar code but I wasn't able to make it work, now I'll try to study yours and tomorrow if I have time I'll try it, for me the distance problem doesn't exist because then the 2 micros will be mounted on the same board and the distances will be a few cm, but your idea of ​​having them communicate via UART is not wrong, but I ask you: the 328 has only one UART, can you create an additional one in software? We hope that the probable bug you found will be investigated soon
Thanks again

max.tisc
Posts: 77
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Flowcode v9 Re: 2 Arduino I2C communication

Post by max.tisc »

Hi mnfisher
I tested your codes and they work as you can see from the screenshot, thank you very much, I wanted to ask you: why does the cycle not repeat every second but only once?
what is the difference for example between initialise and transaction_initialise? What function do transactions have?
if I also have to send data from slave to master and not just from master to slave, do I have to first save these in registers in the slave and then query with the master?
forgive my ignorance
thank you very much
Attachments
Screenshot 2024-05-28 232620.png
Screenshot 2024-05-28 232620.png (177.66 KiB) Viewed 529 times
Screenshot 2024-05-28 231841.png
Screenshot 2024-05-28 231841.png (141.44 KiB) Viewed 529 times

mnfisher
Valued Contributor
Posts: 1046
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: 2 Arduino I2C communication

Post by mnfisher »

It's odd that it just worked one time for you - repeated every second for me. Strange.
Initialise is needed before using the i2c (once only), the transaction routines allow a simpler interface (it handles start and stop conditions. Transactions are also the only available interface on some MCUs but that doesn't affect us here)
It should be possible for the slave to reply with data (master issues a read request) This is typically used to read values from sensors etc.
I'll have a play this evening and see if I can get two way comms working.
Note that the only real difference between master and slave is that the master initiates transactions ( by issuing 'start' ) - so in the case of two MCUs the roles could be reversed.

Martin

max.tisc
Posts: 77
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Re: 2 Arduino I2C communication

Post by max.tisc »

yes Martino usually only does it once a few times 2 max I've seen 3, and to start it again I have to press the reset button
thank's

mnfisher
Valued Contributor
Posts: 1046
Joined: Wed Dec 09, 2020 9:37 pm
Has thanked: 107 times
Been thanked: 538 times

Re: 2 Arduino I2C communication

Post by mnfisher »

I've had one step forwards and one 'to the side'

So - having ascertained that transmit works AOK (for me :-) ) I rewrote the code to use the I2C CAL component and transactions )notice how this makes the code for the master much simpler) I also just use the scope here (and as smartscope has decided to crash on my PC I had to call in a tablet to run it (nothing going well today!)
I then added a 'read' that attempts to read 4 bytes This - sort of works. I tried with a loop - and also 4 individual calls to write - but the trace shows that the master just receives the same 4 bytes (first one sent!) However - I note that it does set ACK/NACK correctly..

So transmit works well (this on hardware - I haven't tried simulation) But receive (unless you want a single byte) doesn't. I suppose it would be possible to reply with individual bytes per read request and maintain a counter in the slave - but a bit of a kludge.
i2c_send2.fcfx
(11.64 KiB) Downloaded 39 times
i2c_rcv2.fcfx
(14.35 KiB) Downloaded 35 times
LabNation_Screenshot5.png
LabNation_Screenshot5.png (271.09 KiB) Viewed 449 times
Not sure why this is happening - I tried a small delay after each byte and also using individual commands (start - tx, etc) without joy.

Martin

max.tisc
Posts: 77
Joined: Thu Dec 10, 2020 5:40 pm
Been thanked: 6 times

Flowcode v9 Re: 2 Arduino I2C communication

Post by max.tisc »

hi Martin, I tried the 2 codes and I get the same results as yours except for the fact that the loop does not repeat and therefore I only see the data exchange once and not every second, maybe if someone else has 2 Arduinos and 10 minutes of time could do a test and post it, if it works it won't just be a Byte to exchange but a few more, it depends on how much I want to exploit the 328 for calculations
thank you
Attachments
Screenshot 2024-05-31 183336.png
Screenshot 2024-05-31 183336.png (116.66 KiB) Viewed 404 times
Screenshot 2024-05-31 183258.png
Screenshot 2024-05-31 183258.png (179.01 KiB) Viewed 404 times

Post Reply