Communication between 2 microcontrollers with I2c

Hello,

I have a project I'm working on, but I'm stuck on a problem. I have an stm32 with Arduino bootloader, and I have an Arduino mega. The Arduino Mega has to send integer data to the stm32 using I2c. But the problem is I have a I2c 16x2 LCD connected to the stm32 as well and I don't know how they will work together with the mega, I cant find a solution on the internet and I cant find a decent tutorial on sending integers between 2 Arduinos. The thing I don't understand is can the stm32 receive data from a mega with the Wire library and can it send that data to the LCD. And some example code would be good to get me going because this is the first time I use I2c, with Arduinos!

Please give a link to your STM32 board.

Here, but I think it's discontinued.

Your setup is perfectly alright. All these three devices can work simultaneously using the I2C Bus. Yo may follow these steps:

1. Connect stm32 and LCD using I2C Bus. Upload the following sketch (untested but compiles)and check that the message "Forum" has appeared on the top line of LCD. There is no need to connect extra pull-up resistors for the I2C bus as the LCD Controller has built-in pull-ups.

#include<LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);  //if not working, try with address 0x3F for LCD

void setup() 
{
  lcd.begin();   //if this is not compiled, then try with lcd.init();
  lcd.backlight();
  lcd.setCursor(0, 0);  //DPos (0-15), LPos (0-1)
  lcd.print("Forum"); 
}

void loop() 
{
 
}

2. Connect MEGA with stm32 using the I2C Bus.
(1) Upload the following sketch (untested but compiled) into stm32.

#include<LiquidCrystal_I2C.h>
#include<Wire.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
#define slaveAddress 0x08 //0001000 (7-bit)
void setup() 
{
  Serial.begin(9600);
  Wire.begin();
  lcd.begin();
  lcd.backlight();
  lcd.setCursor(0, 0);  //DPos (0-15), LPos (0-1)
  lcd.print("Forum"); 
  //--------------------
  Wire.requestFrom(slaveAddress, 2); //requesting 2-byte data
  byte lByte = Wire.read();
  byte hByte = Wire.read();
  int x = hByte<<8|lByte;
  //--------------------
  lcd.setCursor(0, 1);  //cursor at bottom line and left most position
  lcd.print(x); //check 1234 has appeared on bottom line of LCD
}

void loop() {}

(2) Upload the following sketch (untested but compiled) into MEGA.

#include<Wire.h>
#define slaveAddress 0x08 //0001000 (7-bit)
int y = 0x1234;

void setup() 
{
  Serial.begin(9600);
  Wire.begin(slaveAddress);
  Wire.onRequest(receiveEvent);   //ISR declaration
}

void loop() {}

void receiveEvent()
{
  Wire.write(lowByte(y)); //sending 0x34 of y via I2C Bus
  Wire.write(highByte(y)); //sending 0x12 of y
}

How far apart are the boards? I2C is pretty short range communication method (<1 meter). Serial has better range. Might be better to take advantage of the STM board's and Mega's multiple hardware serial ports. Could be faster and easier as well.

1 Like

So the LCD works fine, but when I connect the whole system I get "forum 4660" on the LCD.(forum on the first line, 4660 under forum) Also I just realised I will probably need to send more than one integer, but we will deal with that later.

It's under 1 meter. But I also think serial might be too slow...

Hints for seeing 1234 on the LCD:
1234 in hex base = 4660 in decimal base.

lcd.print(x) bears silent decimal base as the 2nd argument.

So you're saying it's working?

You should modify the program based on the hints of post #8 and then tell that you are seeing 1234 at the bottom line of LCD.

Ok, well I got 1234 to show up after some tinkering! I just needed to do this change:

lcd.print(x, HEX);
1 Like

Excellent! Have fun!!

WAIT!!! But how do I now convert a changing integer to bytes, so it can be sent with I2c.

See the following codes of post #4.

int y = 0x1234;
void receiveEvent()
{
  Wire.write(lowByte(y)); //sending 0x34 of y via I2C Bus
  Wire.write(highByte(y)); //sending 0x12 of y
}

So when I add 1, to y the value changes on the LCD. But after 10 it starts going "ABCDEF" then it ads 10 and starts again!

So, what is the problem?
Tell me result of the following add operation:

     0x1234
+    0x9999
-------------------
= ?

It sometimes changes the numbers to letters! I don't really understand this byte stuff I always tried to stay away from it. What I tried to use is:

y = analogRead(A3);

the answer is 0xABCD?

What is this?

Correct!

What is the sum here?

     0x9234
+    0x9999
-------------------
= ?

trying to see if I can change the value of y

That means that you want o connect a potentiometer (Pot) with A3-pin of MEGA, acquire the value from the wiper point of the Pot and then send it to stm32 via I2C. Do you have any code? If yes, post it here.