1

I'm trying to connect a stm32F103C8 board (master) to an Arduino Mega (slave) over I2C. I have a master and slave program which runs perfectly fine when the clock frequency for I2C communication is set to standard (100000L) which is default.

I wanted the communication to happen a bit faster. So I changed the clock frequency to fast mode i.e., 400000L. I am seeing some weird behavior.

I can send data from master to slave (I can see the data appearing on slave) but when the master requests data from the slave, I don't receive any data on master.

I have tried the same code out on an Arduino Uno (master) and Mega (slave). The code works fine for both standard and fast mode.

I run the code in standard mode by commenting the following line in the master code.

Wire.setClock(400000L);

Here's the code I have for the Master and Slave:

//Master Code

#include <Wire.h>

int data = 1;
void setup() {

  Wire.begin(); 
  Wire.setClock(400000L);
  Serial.begin(9600);
}
void loop() {
  Wire.beginTransmission(9);
  Wire.write(data);
  Wire.endTransmission();

  Wire.requestFrom(9, 1);
  int x = Wire.read();
  Serial.print(x);
  delay(500);

}




//Slave Code

#include <Wire.h>

int send_data = 1;
void setup() {

  Serial.begin(9600);

  Wire.begin(9);

  Wire.onReceive(receiveEvent);
  Wire.onRequest(requestEvent);
}
void requestEvent()
{
  Wire.write(send_data);
}

void receiveEvent(int bytes) {
  int x = Wire.read();
  Serial.println(x);
}
void loop() {
delay(500);
}

I'm having hard time understanding what's causing the issue. Please help me make sense as to why it's doing this and how I can resolve it.

  • try setting the i2c clock speed in the slave alsp – jsotola Jan 29 at 19:53
  • tried it. it didn't work. – Sushant Rasalkar Jan 30 at 3:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.