Go Down

Topic: BME280 not working with Mega powered with 9v battery (Read 314 times) previous topic - next topic

jaggeritoshico

Hello,

This is my first message and I would like to thank this community in advance.

I've built a small Temperature/Pressure/Humidity reader with an Arduino Mega, a BME280 sensor, a I2C 16x2 LCD and a button to light up the lcd screen.

Everything is working fine while the system is powered through the USB cable but when I switch to a 9v battery the LCD shows nonsense characters.

The code I've been using is this:

Code: [Select]
#include <cactus_io_BME280_I2C.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>



BME280_I2C bme(0x76);
LiquidCrystal_I2C lcd(0x27,16,2);

int boton = 0;

void setup()
{
  pinMode(22, INPUT);
  lcd.begin(16,2);
  lcd.backlight();
 
  if (bme.begin()) {
    lcd.print("OK");
    delay(2000);
    lcd.clear();
  }
  else
  {
    lcd.print("ERROR");
  }
  bme.setTempCal(-1);
 
}

void loop()
{
  boton = digitalRead(22);
  if(boton == HIGH) {
    lcd.backlight();
  }
  else {
    lcd.noBacklight();
  }
  bme.readSensor();
  lcd.setCursor(0,0);
  lcd.print("P:");
  lcd.print(bme.getPressure_MB());
  lcd.print("mbar");
  lcd.setCursor(0,1);
  lcd.print("T:");
  lcd.print(bme.getTemperature_C());
  lcd.print(" ");
  lcd.print("H:");
  lcd.print(bme.getHumidity());
  lcd.print("%");

  delay(1000);
}


I've tried replacing the BME280 with a BMP180 (same wiring, just replacing the sensor) and this code:

Code: [Select]
#include <LiquidCrystal_I2C.h>
#include <SFE_BMP180.h>
#include <Wire.h>


SFE_BMP180 bmp180;
LiquidCrystal_I2C lcd(0x27,16,2);

int boton = 0;

void setup()
{
  pinMode(22, INPUT);
  lcd.init();
  lcd.backlight();
 
  if (bmp180.begin()) {
    lcd.print("OK");
    delay(2000);
    lcd.clear();
  }
  else
  {
    lcd.print("ERROR");
  }
}

void loop()
{
  boton = digitalRead(22);
  if(boton == HIGH) {
    lcd.backlight();
  }
  else {
    lcd.noBacklight();
  }
  char status;
  double T,P;
  T = (T-1);

  status = bmp180.startTemperature();//Inicio de lectura de temperatura
  if (status != 0)
  {   
    delay(status); //Pausa para que finalice la lectura
    status = bmp180.getTemperature(T); //Obtener la temperatura
    if (status != 0)
    {
      status = bmp180.startPressure(3); //Inicio lectura de presión
      if (status != 0)
      {       
        delay(status);//Pausa para que finalice la lectura       
        status = bmp180.getPressure(P,T); //Obtenemos la presión
        if (status != 0)
        {                 
          lcd.setCursor(0,0);
          lcd.print("T: ");
          lcd.print(T,2);
          lcd.print(" *C , ");
          lcd.setCursor(0,1);
          lcd.print("P: ");
          lcd.print(P,2);
          lcd.print(" mbar");         
        }     
      }     
    }   
  }
  delay(1000);
}


This works perfect with both USB and 9V battery.

Any idea what's going wrong with BME280 and 9V battery?

Thanks again for your help.

groundFungus

#1
Jul 08, 2020, 07:36 pm Last Edit: Jul 08, 2020, 07:36 pm by groundFungus
Have you measured the actual value of Vcc (5V) with the 9V battery connected.  9V smoke alarm batteries have low current capability and the voltage may be dragged down by too much current draw.
You will save everyone's time if you read and follow the forum guidelines.https://forum.arduino.cc/index.php/topic,148850.0.html

Koepel

Can you give a link to your BME280 module ? (a link to where you bought it). The Mega board has 10k pullup resistors on SDA and SCL to 5V and the display module might also have pullup resistors to 5V. The BME280 does not like 5V on its SDA and SCL pins.

Sometimes a 9V battery is used in examples, but no one actually thinks that is a good idea. We never use 9V batteries for a Arduino project.

jaggeritoshico

Hello,

Thank you both for your answers.

This is the BME280 I bought:

https://www.amazon.es/gp/product/B07RJT3NMZ/ref=ppx_yo_dt_b_asin_title_o02_s00?ie=UTF8&psc=1

I will measure Vcc and post the result, but reading your comments I will try to power the Mega board with AA batteries and let you know how it works.

Thanks again.

Koepel

Very good, that BME280 module has a voltage regulator and levels shifters for SDA and SCL :D
When using a 5V Arduino board (as the Arduino Mega 2560 board) then you should power that module with 5V to VIN. If you would power it with 3.3V then the levels shifters don't work.

jaggeritoshico

Hello again,

I'm still struggling with this. I've realized that it's not an issue when the system is powered with batteries ( I've measured and I get 5v). This happens every time I remove power for any reason, when I connect power again it simply does not work.

I've tried to replace Mega with Uno but it happens the same, and it only happens with BME280, with BMP180 it works fine everytime.

Attached there are some pictures, maybe they can help.

Once again, thanks for your time and help.

Koepel

Can you try a delay at the start of setup() ?
You can send a message to the serial monitor and then do the delay.

Code: [Select]
void setup()
{
  Serial.begin( 9600);          // 9600 is default but slow, 115200 baud is better
  Serial.println( "The sketch has started");

  delay( 1000);

  ...
}

jaggeritoshico

Hi Koepel.

Same result. Once I switch off power and on again LCD shows those symbols. I've sent as well the pressure reading to the serial monitor and it works when the LCD is "failing", so I guess it's not an issue with the BME sensor. I've tried to replace the LCD with another one and keeps doing the same.

LCD is this one:

https://www.amazon.es/gp/product/B07PGZ9B51/ref=ppx_od_dt_b_asin_title_s00?ie=UTF8&psc=1

Thanks!!

Code: [Select]
#include <cactus_io_BME280_I2C.h>
#include <LiquidCrystal_I2C.h>
#include <Wire.h>



BME280_I2C bme(0x76);
LiquidCrystal_I2C lcd(0x27,16,2);

int boton = 0;

void setup()
{
  pinMode(2, INPUT);
  lcd.begin(16,2);
  lcd.backlight();
 
  if (bme.begin()) {
    lcd.print("OK");
    delay(2000);
    lcd.clear();
  }
  else
  {
    lcd.print("ERROR");
  }
  bme.setTempCal(-1);
 
}

void loop()
{
  Serial.begin( 9600);          // 9600 is default but slow, 115200 baud is better
  Serial.println( "The sketch has started");

  delay( 1000);
  boton = digitalRead(2);
  if(boton == HIGH) {
    lcd.backlight();
  }
  else {
    lcd.noBacklight();
  }
  bme.readSensor();
  Serial.println(bme.getPressure_MB()); // To test if the BME is sending data or not
  lcd.setCursor(0,0);
  lcd.print("P:");
  lcd.print(bme.getPressure_MB());
  lcd.print("mbar");
  lcd.setCursor(0,1);
  lcd.print("T:");
  lcd.print(bme.getTemperature_C());
  lcd.print(" ");
  lcd.print("H:");
  lcd.print(bme.getHumidity());
  lcd.print("%");

  delay(1000);
}



Koepel

I'm afraid that I'm overlooking something. We should have a direction to look for the problem by now. I have read this thread from the start. I'm very sorry, but I can still not tell in what direction to look :'(

A I2C device can disturb another I2C device when something is wrong. It is common that two I2C devices work on their own, but not together. It is even possible that a third I2C device prevents two other I2C devices from working together. The BME280 is returning the baromic pressure, but that does not mean that it is okay.

Weird things happen with:

1. Not enough sram, buffer overflow.
I don't know the cactus_io library, you could try another one.

2. Low voltage. The Mega 2560 board really needs a voltage above 4.5V and a LCD display is even more critical, I suggest at least 4.7V for the display.
How much do you measure at the 5V pin of the Arduino board and at the 5V of the display ? Please don't tell me it is 5v, but tell what you have measure (4.89 or 5.02 or something like that).

3. Bad hardware I2C bus. Too much pullup, too little pullup. Voltage level differences. A cable. Forgetting to connect the GNDs. SDA and SCL next to each other in a cable. A breadboard with bad contacts. The SDA and SCL going through two level shifters.
Can you confirm that you power the BME280 module with 5V to VIN ? Have you tested each wire that it is not broken ? Can you move everything to a different location on the breadboard ?

4. Something is broken, for example the Arduino or a sensor. The microcontroller on a Arduino board can be damaged partially.

Do you know someone that can come to your place an have a look at it ?

jaggeritoshico

Thanks Koepel.

I will test all this things you suggest and post the results.

Somehwere there will be something

Go Up