I2C does not work on Nucleo 64 STM32F446RE

Post here all questions related to STM32 core if you can't find a relevant section!
Post Reply
ptgon
Posts: 2
Joined: Thu Nov 10, 2022 5:49 pm

I2C does not work on Nucleo 64 STM32F446RE

Post by ptgon »

First of all sorry for my english.
I'm trying to use the Arduino Wire library for the Nucleo F446RE board (STM32 boards groups 2.2.0), but I don't get any signal on the SDA and SCL, both have pullup resistors (I've tested 10k, 1k, 4k7).
I've already tried to configure the SDA and SCL pins with Wire.setSDA() and Wire.setSCL() and still no result.
I monitored all pins of the F446RE on the Oscilloscope and all pins are static, none makes any pulse.
To test I'm doing a simple scanner. Does anyone know how I can use i2C on the F446RE with the Wire library?

This is my code:

Code: Select all

#include <Wire.h>

//TwoWire Wire2(PC0, PC1);

void setup()
{
  Serial.begin(9600);
  Serial.println("===== I2C SCANNER STM32 =====");

  //Wire.setSDA(PB7);
  //Wire.setSCL(PB8);
  //Wire.setClock(100000);
  Wire.begin();
  delay(100);
}


void loop()
{
  uint8_t devices = 0;
  byte addr = 0x00, error = 0x00;

  Serial.println(" ");
  Serial.println("Scanning...");
  for(addr = 1; addr < 127; ++addr)
  {
    Wire.beginTransmission(addr);
    error = Wire.endTransmission();

    if(error == 0)
    {
      Serial.print("I2C Device: 0x");
      if(addr < 16) Serial.print("0");
      Serial.println(addr, HEX);

      ++devices;
    }
    else if(error == 4)
    {
      Serial.print("Unknow error address: ");
      if(addr < 16) Serial.print("0");
      Serial.println(addr, HEX);
    }
  }

  Serial.println(String("Devices Found: ") + String((int) devices));
  delay(1000);
}
ptgon
Posts: 2
Joined: Thu Nov 10, 2022 5:49 pm

Re: I2C does not work on Nucleo 64 STM32F446RE

Post by ptgon »

I managed to solve the problem, for some reason Wire.begin() must be called before Serial.begin(), I also had to reset the SDA and SCL pins to PB9 and PB8.

My code was like this:

Code: Select all

#include <Wire.h>

void setup()
{
  Wire.setSDA(PB9);
  Wire.setSCL(PB8);
  Wire.begin();
  Serial.begin(9600);
  Serial.println("===== I2C SCANNER STM32 =====");
  delay(100);
}


void loop()
{ 
  uint8_t devices = 0;
  byte addr = 0x00, error = 0x00;

  Serial.println(" ");
  Serial.println("Scanning...");
  for(addr = 1; addr < 127; ++addr)
  {
    Wire.beginTransmission(addr);
    error = Wire.endTransmission();

    if(error == 0)
    {
      Serial.print("I2C Device: 0x");
      if(addr < 16) Serial.print("0");
      Serial.println(addr, HEX);

      ++devices;
    }
    else if(error == 4)
    {
      Serial.print("Unknow error address: ");
      if(addr < 16) Serial.print("0");
      Serial.println(addr, HEX);
    }
  }

  Serial.println(String("Devices Found: ") + String((int) devices));
  delay(1000);
}
User avatar
fpiSTM
Posts: 1723
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: I2C does not work on Nucleo 64 STM32F446RE

Post by fpiSTM »

Normally it works. No matter if Serial is called before or after.
For Nucleo F446 the default pins used for Wire instance is PB9/PB8 so setSDA/SCL are not required if you have selected the correct boards (Nucleo64, part num Nucleo F446).
Post Reply

Return to “General discussion”