STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post here first, or if you can't find a relevant section!
NickF93
Posts: 5
Joined: Sun May 10, 2020 3:12 am

STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by NickF93 »

I'm going crazy trying to understand some strange reads from BME280 sensor.

With this code:

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

#define SAMPLES_NUM             100
#define SAMPLES_OFF             256

#define GRAPH_X                 (SCREEN_WIDTH - (SAMPLES_NUM + 1))
#define GRAPH_Y                 16
#define GRAPH_W                 (SAMPLES_NUM + 1)
#define GRAPH_H                 47
#define DATA_X                  10
#define DATA_Y                  0

#define SEALEVELPRESSURE_HPA    1000
#define PRESSURE_HPA_MIN        980
#define PRESSURE_HPA_MAX        1045

TwoWire Wire1(PB9, PB8);
TwoWire Wire2(PB11, PB10);
Adafruit_SSD1306 *display;
Adafruit_BME280 *bme;

void setup() {
  pinMode(PC13, OUTPUT);
  digitalWrite(PC13, LOW);

  delay(500);

  Wire1.begin();
  Wire1.setClock(400000);

  Wire2.begin();
  Wire2.setClock(400000);

  delay(100);

  display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2);
  bme = new Adafruit_BME280();

  while (!display->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    delay(500);
  }

  delay(500);

  display->clearDisplay();
  display->dim(0);
  display->setTextSize(1);
  display->setTextColor(WHITE);
  display->setCursor(0, 0);
  display->println("Display initialized");
  display->display();

  int b = 0;
  display->println("init bme...");
  while (b == 0) {
    b = bme->begin(BME280_ADDRESS_ALTERNATE, &Wire1);
    display->print("b=");
    display->print(b);
    display->print("; ");
    display->display();
  }
  display->println();
  display->display();

  if (b == 1) {
    bme->setSampling(Adafruit_BME280::MODE_FORCED,
                    Adafruit_BME280::SAMPLING_X1, // temperature
                    Adafruit_BME280::SAMPLING_X1, // pressure
                    Adafruit_BME280::SAMPLING_X1, // humidity
                    Adafruit_BME280::FILTER_OFF);
  }
  delay(5000);
}

void loop() {
//  bme->takeForcedMeasurement();
  float _t1 = bme->readTemperature();
  float _h1 = bme->readHumidity();
  float _p1 = (bme->readPressure() / ((float)100.0F));

  display->clearDisplay();
  display->setCursor(0, 0);

  display->print("T  : ");
  display->print(_t1, 1);
  display->print((char)247);
  display->println("C");

  display->print("RH : ");
  display->print(_h1, 1);
  display->println("%");

  display->print("P  : ");
  display->print(_p1, 1);
  display->println("hPa");

  display->display();

  delay(5000);
}
And if I uncomment // bme->takeForcedMeasurement(); it wait forever.

I get an obviously erroneous read:

Code: Select all

T  : 66.1°C
RH : 100.0%
P  : -1599.7hPa
With this code:

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

TwoWire Wire1(PB9, PB8);
TwoWire Wire2(PB11, PB10);
Adafruit_SSD1306 *display;
Adafruit_BME280 *bme;

void setup() {
  pinMode(PC13, OUTPUT);
  digitalWrite(PC13, LOW);

  delay(500);

  Wire1.begin();
  Wire1.setClock(400000);

  Wire2.begin();
  Wire2.setClock(400000);

  delay(100);

  display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2);
  bme = new Adafruit_BME280();

  while (!display->begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
    delay(500);
  }

  delay(500);

  display->clearDisplay();
  display->dim(0);
  display->setTextSize(1);
  display->setTextColor(WHITE);
  display->setCursor(0, 0);
  display->println("Display initialized");
  display->display();

  int b = 0;
  display->println("init bme...");
  while (b == 0) {
    b = bme->begin(BME280_ADDRESS_ALTERNATE, &Wire1);
    display->print("b=");
    display->print(b);
    display->print("; ");
    display->display();
  }
  display->println();
  display->display();

  if (b == 1) {
    bme->setSampling(Adafruit_BME280::MODE_FORCED,
                    Adafruit_BME280::SAMPLING_X1, // temperature
                    Adafruit_BME280::SAMPLING_X1, // pressure
                    Adafruit_BME280::SAMPLING_X1, // humidity
                    Adafruit_BME280::FILTER_OFF);
  }
  delay(5000);
}

void loop() {
  bme->takeForcedMeasurement();
  float _t1 = bme->readTemperature();
  float _h1 = bme->readHumidity();
  float _p1 = (bme->readPressure() / ((float)100.0F));

  display->clearDisplay();
  display->setCursor(0, 0);
  display->println(_t1, 1);
  display->println(_h1, 1);
  display->println(_p1, 1);

  display->display();

  delay(5000);
}
I get perfect read:

Code: Select all

24.5
45.7
1009.8
I can't understand the problem.

Wiring it's ok and I can detect on both I2C bus devices address with i2cscanner.
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by fpiSTM »

Well, hard to tell.
If you "wait forever" probably there is a deadlock and about the wrong display value did you check with serial if they are really wrong or if the display print does not convert it properly ?
NickF93
Posts: 5
Joined: Sun May 10, 2020 3:12 am

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by NickF93 »

Yes very hard. If I change something in code, value also change systematically. It seems something like a stack overflow or a bad allocation, because if I convert in int and print to display they are same values but trunked.
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by fpiSTM »

Your best bet is to debug
NickF93
Posts: 5
Joined: Sun May 10, 2020 3:12 am

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by NickF93 »

fpiSTM wrote: Sun May 10, 2020 1:11 pm Your best bet is to debug
Yes, I tried to change μP with another STM32F103C8 and the behavior is the same (just a try, in order to rule out the remote possibility that it might be the processor, maybe with a broken I2C peripheral)
Then I tried to port the code on mbed (with PlatformIO), and it seems to work very well!
So, my question is: could be a bug that introduce a memory leak in stm32duino framework? Or MY code is broken and I'm not able to find out the problem?
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by fredbox »

Code: Select all

  display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2);
  bme = new Adafruit_BME280();
I seem to remember that the Arduino environment discouraged the use of "new."
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by fpiSTM »

Th next release of the core will include several I2C bug fixes.
You can try with the master repo to see if this fixes your issue.
stas2z
Posts: 131
Joined: Mon Feb 24, 2020 8:17 pm
Answers: 8

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by stas2z »

fredbox wrote: Mon May 11, 2020 2:44 am

Code: Select all

  display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2);
  bme = new Adafruit_BME280();
I seem to remember that the Arduino environment discouraged the use of "new."
Only avr-gcc cuz new was not implemented afaik.
All sm32 cores use arm gcc and it have no problems with new
User avatar
fpiSTM
Posts: 1745
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by fpiSTM »

Yes, new can be used.
NickF93
Posts: 5
Joined: Sun May 10, 2020 3:12 am

Re: STM32F103 (Blue Pill) with stm32duino + BME280 + SSD1306 strange behavior

Post by NickF93 »

fpiSTM wrote: Mon May 11, 2020 5:13 am Th next release of the core will include several I2C bug fixes.
You can try with the master repo to see if this fixes your issue.
Sounds very interesting. I was investigating around TwoWire and Print classes, in my opinion one the two classes (more likely the first) do something bad with memory. Thanks for the reply, I will try for sure the new build.
fredbox wrote: Mon May 11, 2020 2:44 am

Code: Select all

  display = new Adafruit_SSD1306(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire2);
  bme = new Adafruit_BME280();
I seem to remember that the Arduino environment discouraged the use of "new."
AFAIK this is only true for AVR, and I don't remember if is still true. However I tried also without pointers, but behavior is the same.
Post Reply

Return to “General discussion”