STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post here first, or if you can't find a relevant section!
Orion1398
Posts: 15
Joined: Tue May 04, 2021 5:28 pm
Answers: 2

STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by Orion1398 »

Hi!
(sorry for my english I'm not a native speaker)
I have a problem with connecting the BME280 sensor to the board.
Using standard libraries I cannot specify which pins I will use.
The only thing I could achieve was using begin (0x76); print incomprehensible data like (tft.print (bme.readTemperature ()); I get = -106.82).
Connected the BME280 sensor to pins PB9 - SDA, PB8 - SCL.
I have already connected the SPI TFT screen (ILI9341 controller and XPT2046 controller).
First I was try to use a combination of 10-DOF IMU sensors V2 from Waveshare.
But I could not do anything at all.
If I can manage with BME280, then in the future I wanted to use the module from Waveshare, which also has MPU9255 and do a compass.
Maybe someone have idea how to do it? (maybe someone knows some library where you can specify which pins you use)
Below I will throw the code which I use (there is a part where I test and calibrate the touch screen ) :

Code: Select all

#include "Adafruit_GFX.h"     
#include "Adafruit_ILI9341.h"
#include "URTouch.h"          
#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"
#include <Wire.h>
#include <SPI.h>

#define SEALEVELPRESSURE_HPA (1015.92)

Adafruit_BME280 bme; // I2C

#define TFT_DC PD5             
#define TFT_CS PD7             
#define TFT_RST -1         
#define TFT_MISO PB14           
#define TFT_MOSI PB15         
#define TFT_CLK PB13            

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);  

#define t_SCK PB3             
#define t_CS PB7                
#define t_MOSI PB5              
#define t_MISO PB4             
#define t_IRQ PB8    
          
URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);

void setup() {
  Serial.begin(9600);
  while (!Serial);  
  unsigned status;
  status = bme.begin(0x76);
  //bme.begin();
  tft.begin();                      
  tft.setRotation(0);          
  tft.fillScreen(ILI9341_BLACK);
  ts.InitTouch(PORTRAIT);                  
  ts.setPrecision(PREC_EXTREME);    
  tft.setTextColor(ILI9341_WHITE);  
  tft.setTextSize(2);              
  tft.setCursor(25, 5);             
  tft.print("Testing TouchTFT"); 
  tft.setTextColor(ILI9341_GREEN);  
  tft.setCursor(45, 300);          
  tft.print("POWER OF STM");   
  tft.setTextColor(ILI9341_WHITE);
  tft.setTextSize(1);
  tft.setCursor(2, 300);
  tft.print("X= ");
  tft.setCursor(200, 300);
  tft.print("Y= ");

  if (!status)
  {
    tft.setTextColor(ILI9341_WHITE); 
    tft.setTextSize(1);               
    tft.setCursor(20, 45);
    tft.print("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    tft.setCursor(20, 65);
    tft.print("SensorID was: 0x");
    tft.print(bme.sensorID(), 16);
    tft.setCursor(20, 75);
    tft.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    tft.setCursor(20, 95);
    tft.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    tft.setCursor(20, 105);
    tft.print("        ID of 0x60 represents a BME 280.\n");
    tft.setCursor(20, 115);
    tft.print("        ID of 0x61 represents a BME 680.\n");
    tft.setCursor(20, 125);
    while (1) delay(10);
    }

}

void loop()
{
  int x, y;                        
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(10, 165);
  tft.print(bme.readTemperature());
  tft.setCursor(10, 185);
  tft.print(bme.readPressure() / 100);
  tft.setCursor(10, 205);
  tft.print(bme.readAltitude(SEALEVELPRESSURE_HPA));

  while (ts.dataAvailable())       
  {
    ts.read();                     
    x = ts.getX();                 
    y = ts.getY();                  

    tft.fillRect(13, 300, 20, 8, 0x0000);
    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(1);
    tft.setCursor(13, 300);
    tft.print(x);

    tft.fillRect(213, 300, 20, 8, 0x0000);
    tft.setTextColor(ILI9341_WHITE);
    tft.setTextSize(1);
    tft.setCursor(213, 300);
    tft.print(y);
    if ((x != -1) && (y != -1))     
    {
      x += 4;                      
      y += 0;                      
      int radius = 1;              
      tft.fillCircle(x, y, radius, ILI9341_YELLOW); 
    }
  }
}
by Orion1398 » Wed May 05, 2021 7:59 pm
Heh :shock:
They work.
I don't know how but they work.
I just steamed the SDA and SCL outputs.
I know it's wrong but it work.
https://drive.google.com/file/d/10n4UB7 ... sp=sharing
Thanks Everybody. For now, that will be enough.
Go to full post
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by mlundin »

But there is data returned from the BME is seems ?

Try just using the single BME280 sensor and print data through a serial console.

What board are you using ?
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by AndrewBCN »

Hi,
I had all sorts of weird problems connecting a BMP280 (similar to BME280) to a STM32F411CEU6 through I2C, but then I decided to use SPI and everything works fine - and SPI is much faster than I2C.
I am using the Adafruit BMP280 library.
Orion1398
Posts: 15
Joined: Tue May 04, 2021 5:28 pm
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by Orion1398 »

mlundin wrote: Tue May 04, 2021 7:18 pm But there is data returned from the BME is seems ?

Try just using the single BME280 sensor and print data through a serial console.

What board are you using ?
I'm using STM32 F407VET6.
I use single BME280 sensor and I get -106.82 for Temperature and etc.
I don't know how to use serial console using STM32 in Arduino IDE
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by fredbox »

See this post: viewtopic.php?p=2219#p2219

Also, run the i2c_scanner from the Arduino examples menu and make sure it can see your device at the right address.
Orion1398
Posts: 15
Joined: Tue May 04, 2021 5:28 pm
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by Orion1398 »

AndrewBCN wrote: Tue May 04, 2021 7:37 pm Hi,
I had all sorts of weird problems connecting a BMP280 (similar to BME280) to a STM32F411CEU6 through I2C, but then I decided to use SPI and everything works fine - and SPI is much faster than I2C.
I am using the Adafruit BMP280 library.
I understand that but i have BMP280 with 4 ports vcc gnd sda scl
Orion1398
Posts: 15
Joined: Tue May 04, 2021 5:28 pm
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by Orion1398 »

fredbox wrote: Tue May 04, 2021 8:16 pm See this post: viewtopic.php?p=2219#p2219

Also, run the i2c_scanner from the Arduino examples menu and make sure it can see your device at the right address.
I read that post but it dosen't help me.
I use i2c_scaner and get this -> No I2C devices found
Code what I use

Code: Select all

#include "Adafruit_GFX.h"
#include "Adafruit_ILI9341.h"
#include "URTouch.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_BME280.h"
#include <Wire.h>
#include <SPI.h>

#define SEALEVELPRESSURE_HPA (1015.92)
Adafruit_BME280 bme; // I2C

#define TFT_DC PD5
#define TFT_CS PD7
#define TFT_RST -1
// Uno Hardware SPI
#define TFT_MISO PB14
#define TFT_MOSI PB15
#define TFT_CLK PB13

Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_CLK, TFT_RST, TFT_MISO);

#define t_SCK PB3
#define t_CS PB7
#define t_MOSI PB5
#define t_MISO PB4
#define t_IRQ PB8

URTouch ts(t_SCK, t_CS, t_MOSI, t_MISO, t_IRQ);


void setup()
{
  Wire.begin();
  Serial.begin(9600);
  while (!Serial);
  bme.begin(0x76);

  tft.begin();
  tft.setRotation(0);
  tft.fillScreen(ILI9341_BLACK);
  ts.InitTouch(PORTRAIT);
  ts.setPrecision(PREC_EXTREME);
}
void loop()
{
  int nDevices = 0;
  tft.setTextColor(ILI9341_RED);
  tft.setTextSize(1);
  tft.setCursor(10, 65);
  tft.print("Scanning...");

  for (byte 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);
    byte error = Wire.endTransmission();

    if (error == 0)
    {
      tft.setTextColor(ILI9341_RED);
      tft.setTextSize(1);
      tft.setCursor(10, 75);
      tft.print("I2C device found at address 0x");
      if (address < 16)
      {
        tft.setTextColor(ILI9341_RED);
        tft.setTextSize(1);
        tft.setCursor(10, 85);
        tft.print("0");
      }
      tft.setTextColor(ILI9341_RED);
      tft.setTextSize(1);
      tft.setCursor(90, 85);
      tft.print(address, HEX);

      ++nDevices;
    } else if (error == 4)
    {
      tft.setTextColor(ILI9341_RED);
      tft.setTextSize(1);
      tft.setCursor(10, 95);
      tft.print("Unknown error at address 0x");
      if (address < 16) {
        tft.setTextColor(ILI9341_RED);
        tft.setTextSize(1);
        tft.setCursor(10, 95);
        tft.print("0");
      }
      tft.setTextColor(ILI9341_RED);
      tft.setTextSize(1);
      tft.setCursor(90, 95);
      tft.print(address, HEX);
    }
  }
  if (nDevices == 0) {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(1);
    tft.setCursor(10, 105);
    tft.print("No I2C devices found\n");
  } else {
    tft.setTextColor(ILI9341_RED);
    tft.setTextSize(1);
    tft.setCursor(90, 105);
    tft.print("done\n");
  }
  delay(5000); // Wait 5 seconds for next scan
}
User avatar
fpiSTM
Posts: 1738
Joined: Wed Dec 11, 2019 7:11 pm
Answers: 91
Location: Le Mans
Contact:

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by fpiSTM »

Do you have pullups resistors on each I2C lines? If not then add them. Usually 4.7K.
Orion1398
Posts: 15
Joined: Tue May 04, 2021 5:28 pm
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by Orion1398 »

fpiSTM wrote: Tue May 04, 2021 9:44 pm Do you have pullups resistors on each I2C lines? If not then add them. Usually 4.7K.
I will try it. Thanks.
fredbox
Posts: 125
Joined: Thu Dec 19, 2019 3:05 am
Answers: 2

Re: STM32 F407VET6 and BME280 I2C how to choose SDA and SCL pins?

Post by fredbox »

Here is a nice picture of the data lines without pull-up resistors: https://electronics.stackexchange.com/q ... al-pullups. With the right pull-up, the signals are nice and square.
Post Reply

Return to “General discussion”