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!
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.
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.
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.