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

Hi,

We are trying to use AMS5812 Pressure Sensor, with below code.

#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 ?

what pins you are using for AMS5812 ?

as shown in image. Pin 4 and Pin 5.

Is that an Arduino layer over Mbed ? Mbed has trouble with a I2C Scanner that uses write mode with zero data length.
Maybe a I2C read will work with zero data length, but the Wire library probably does not accept that.
Some manufacturer's don't implement the I2C read mode or the I2C write mode, because that is not used in that chip.

I suggest to read all 4 bytes.

Replace this:

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

    if (error == 0) {

with this:

    int n = Wire.requestFrom(address,4);

    if (n == 4) {

I think that you also have a voltage conflict on the I2C bus. Is the sensor a 5V chip (with a 5V I2C bus) and the STM32 is a 3.3V chip (with a 3.3V I2C bus).

ok let me check with modified code.

I am using Arduino IDE 2.0.4 with stm32duino core version 2022.09.26

Pins on stm32f103 ?

STM32 Bluepill pins B6 and B7. on that pins... through normal i2c scanner code. SM9541 Pressure gets detected easily.

Test this code

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

#define LCD_ADDRESS 0x27      //Cambiar si es necesario

LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);

void setup() {

    lcd.init ();
    lcd.setCursor (0,0);
    lcd.print ("Hola Mundo!");

 }

 void loop() {

 }

show the code for sensor code

But the same simple original code works fine with Arduino Nano I2C. so i doubt will it work or not.

This is the address for stm32

https://wildlab.org/index.php/2019/03/01/stm32-i2c-scanner/

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