STM32F103 (original) I2C2 won't work

Hello community,

I'm trying to use I2C device on the 2nd hardware I2C avaible on this chip, on pin PB10 and PB11.
I found something on this forum that doesn't work for me. I guess it's because the topic concerned a fake STM32F103.

As i have a working screen on I2C1 (SD1306), I re-use some of the code of this topic to make an address scanner. It works (almost*) fine on I2C1 and it found all my devices.

When i plug the same devices on I2C2, it found nothing (Another screen and a MPU6050).

The full code :

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64

#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C /// on the screen's back, it's write 0x78 ans 0x7A, with the resistor  on 0X78... but it works on 0x3C !?!?

TwoWire Wire2(PB11, PB10);
// TwoWire Wire2 (2, I2C_FAST_MODE); // generate error 'I2C_FAST_MODE' was not declared

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

//const int MPU_ADDR = 0x68;

uint16_t tmp = 0;
void setup() {

  //Wire2.setSCL(PB_10); // doesn't change anything
  //Wire2.setSDA(PB_11);
  
  display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS);
  display.clearDisplay();
  display.display();
}

void loop() {
  display.clearDisplay();
  display.setTextSize(1);      // Normal 1:1 pixel scale
  display.setTextColor(SSD1306_WHITE); // Draw white text
  display.cp437(true);         // Use full 256 char 'Code Page 437' font
  
  if(tmp > 55) tmp = 0;
  byte error, address;
  int nDevices;
  for(address = 1; address < 127; address++) {
    Wire2.beginTransmission(address);
    error = Wire2.endTransmission();
    if (error == 0) {
      display.setCursor(5, tmp); tmp += 9;
      display.print("found at 0x");
      if (address < 16) 
        display.print("0");
      display.print(address, HEX);

      nDevices++;
    }
    else if (error == 4) {}    
  }
  display.setCursor(5, tmp); tmp += 9;
  if (nDevices == 0)
    display.print("nothing found");
  else
    Serial.println("done");
display.display();
delay(1000);
}

What's going wrong ?

Thank you very much

  • it works almost fine because when i put a 2nd device on the same I2C than screen, i have glitterings on the screen and when try to use the MPU6050, it end crashing quickly after 2 or 3 requests. Don't understand why.

Oh, and serial don't works to debug. Guess i need a UART things or something ? (I use a STlinkV2)

When you post on such things as "blue pill", you must specify the "core" you are using... last time I counted, there were greater than 5.

If you are using the Official STM32duino.com core, then a search of their forum has an I2C2 scanner.

The older Roger Clark core, your search would look like:
I2C2 site:stm32dauinoforum.com - Google Search

Thank you very much for your answer.

Sorry for asking noobs questions, but how should i know what core i use ? Is it the link i specified in boards manager ?

https://raw.githubusercontent.com/stm32duino/BoardManagerFiles/master/STM32/package_stm_index.json,https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json

Thank you again

This looks like you have the URL for the latest official ST Microsystems core:
https://github.com/stm32duino/BoardManagerFiles/raw/main/package_stmicroelectronics_index.json

If you go into the boards manager and search for stm32 you should see an entry for the STMicroelectronics version 2.0.x core and you'll probably see also an entry for the deprecated core because of the other STM32 Board Manager URL you have

To use the USB cable connection for the serial monitor, simply do this when configuring the board type and you can use Serial.

Found it... i mean them !

  • one of my wire had a bad contact and i guess it can be one of the reasons
  • added Wire2.begin() and it worked :')

That was stupid from me.

However, i learned a lot and your tip to use serial may be a life changer ! i'm going to check it !

Thank you very much.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.