Two STM32 Nucleo G071RB boards communicate via I2C issue.

Post here first, or if you can't find a relevant section!
Post Reply
nathankuo0821
Posts: 3
Joined: Tue Jan 05, 2021 2:53 am

Two STM32 Nucleo G071RB boards communicate via I2C issue.

Post by nathankuo0821 »

Hello everyone,
I have two identical STM32 Nucleo G071RB boards. I want to establish communication between them using I2C, with Board one acting as the I2C master and Board two as the I2C slave. The current issue is that the I2C scanner sample code successfully detects the passive sensor when configured as an I2C master. However, whenconfigured as an I2C slave, it cannot be detected by the I2C master. Below is my code.

I2C master

Code: Select all

#include <Arduino.h>
#include <Wire.h>
#define I2C1_SCL PB8
#define I2C1_SDA PB9

void setup()
{
  Wire.setSDA(I2C1_SDA);
  Wire.setSCL(I2C1_SCL);
  Wire.begin();
  Serial.begin(115200);
}

void loop()
{
  byte error, address;
  int nDevices;

  Serial.println("Scanning...");

  nDevices = 0;
  for (address = 1; address < 127; address++)
  {
    // The i2c_scanner uses the return value of
    // the Write.endTransmisstion to see if
    // a device did acknowledge to the address.

    Wire.beginTransmission(address);
    error = Wire.endTransmission();

    if (error == 0)
    {
      Serial.print("I2C device found at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);

      nDevices++;
    }
    else if (error == 4)
    {
      Serial.print("Unknown error at address 0x");
      if (address < 16)
        Serial.print("0");
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0)
    Serial.println("No I2C devices found");
  else
    Serial.println("done");
  delay(2000); // wait 5 seconds for next scan
}
I2C Slave

Code: Select all

#include <Arduino.h>
#include <Wire.h>
#define I2C1_SCL PB8
#define I2C1_SDA PB9
#define I2C_ADDR 0x40

void slaveWrite()
{
  Wire.write("hello\n"); // respond with message of 6 bytes
                          // as expected by master
}

void slaveRead(int howMany)
{
  while (1 < Wire.available()) // loop through all but the last
  {
    char c = Wire.read(); // receive byte as a character
    Serial.print(c);       // print the character
  }
  int x = Wire.read(); // receive byte as an integer
  Serial.println(x); // print the integer
}


void setup()
{
  Wire.setSDA(I2C1_SDA);
  Wire.setSCL(I2C1_SCL);
  Wire.begin(I2C_ADDR );
  Wire.onRequest(slaveWrite); // register event
  Wire.onReceive(slaveRead);  // register event
  Serial.begin(115200);
}

void loop()
{
}
by fpiSTM » Tue Dec 19, 2023 8:14 am
Do you have pullup in each i2c lines?
Go to full post
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: Two STM32 Nucleo G071RB boards communicate via I2C issue.

Post by fpiSTM »

Do you have pullup in each i2c lines?
nathankuo0821
Posts: 3
Joined: Tue Jan 05, 2021 2:53 am

Re: Two STM32 Nucleo G071RB boards communicate via I2C issue.

Post by nathankuo0821 »

Thank you for your advice. It is now working.
Post Reply

Return to “General discussion”