Page 1 of 1

I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Wed May 31, 2023 12:18 pm
by asking
Hi,

I am trying to use AMS5812 Pressure Sensor, with below code.

Code: Select all

#include <Wire.h>


void setup() {

  Serial.begin(9600);
  Wire.begin();
  //Wire.setClock(400);
  Serial.println("\nI2C Scanner");
}


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(5000);           // wait 5 seconds for next scan
}
on Arduino nano its working fine.

But for STM32 Blue pill it doesn't detect AMS5812 Pressure sensor.

Pull up resistors 4.7K are installed properly.

what could be wrong ?

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Wed May 31, 2023 12:45 pm
by fpiSTM
Except your wiring I don't know.

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Wed May 31, 2023 12:54 pm
by asking
sorry for missing.

using SDA on PB7 and SCL on PB6.

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Wed May 31, 2023 2:14 pm
by fpiSTM
Which core you used?
Tested and it works as expected with STM32 core.
If you use STM32Core, which version and which target you select in arduino menu?

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Sat Jun 10, 2023 9:22 am
by asking
fpiSTM wrote: Wed May 31, 2023 2:14 pm Which core you used?
Tested and it works as expected with STM32 core.
If you use STM32Core, which version and which target you select in arduino menu?
Sorry for late revert,

STM32Core is 2022.9.26

Target board is Generic STM32F103C using Bluepill STM32F103C8T6

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Sat Jun 10, 2023 11:54 am
by fpiSTM
OK. So you used roger's core. Have no idea why it doesn't work.

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Sat Jun 10, 2023 2:06 pm
by ag123
have you tried this code instead?
https://github.com/rogerclarkmelbourne/ ... r_wire.ino
in addition check your connections, make sure it is the hardware pins for I2C1

there are also some subtle differences between some i2c devices, stm32 generally expect 3.3v i2c devices to be connected i.e. pullup is to 3.3v.
some devices may expect a 5v line, those devices may not simply work with a 3.3v device and requires some level translations. e.g. pull up to 5v and supply from 5v, if you try to do that, be wary that on stm32 it needs to be a 5v tolerant pin and in addition use suitably high resistance for pull up e.g. 1k to prevent damage to stm32
this post suggest 4.7k pull up for 5v and 3.3k for 3.3v interfaces.
https://forum.arduino.cc/t/i2c-pullup-t ... on/1008148

it seemed from here that ams5812 is indeed a 5v device, so there'd be more elaborate interfacing requirements as generally 5v i2c is not 'hardware compatible' with 3.3v devices, suitable interfacing between 5v device to 3.3v is needed.
https://www.amsys-sensor.com/products/p ... al-output/

https://learn.adafruit.com/working-with ... -resistors

Re: I2C Scanner working in Arduino nano but Not Able to Detect in STM32 Bluepill

Posted: Mon Jul 10, 2023 2:05 am
by brodg68
I agree with the other posters about the pull ups. Digging through the libraries the internal pull ups are not set in the files I looked at.

They can be turned on in the blue pill with the below added to your setup. I use pins 6/7 out of habit. Change the pin number to what is appropriate for you. Mileage may vary but has been ok for me with LCDs, temp sensors, and adxl345.



const PinMap PinMap_I2C_SDA[] = {
{PB_6, I2C1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULL, GPIO_AF4_I2C1)},
{PB_7, I2C1, STM_PIN_DATA(STM_MODE_AF_OD, GPIO_PULL, GPIO_AF4_I2C1)},
{NC, NP, 0}
};



When in doubt - resistors. but might be worth a try if you stumble on this.