Code works in nano atmega328p but not in atmega328pb

I made a barometer with bmp180 sensor and Arduino anno, using display 16x2 lcd, shows temperature and pressure, it is working perfectly in Arduino nano(atmega328p) but when the same code uploaded in another Arduino nano(atmega328pb) the temperature value is not showing, only shows pressure... here is my code

    /////////////////////////////////////////////////////////////////
   //            Arduino Weather Station Project #2     v1.02     //
  //         Get the latest version of the code here:            //
 //         http://educ8s.tv/arduino-weather-station-2          //
/////////////////////////////////////////////////////////////////


#include <LiquidCrystal.h>
#include <SFE_BMP180.h>  //https://github.com/sparkfun/BMP180_Breakout
#include <Wire.h>

SFE_BMP180 pressure;

float temperature;

#define ALTITUDE 50.44 // 



LiquidCrystal lcd(8,9,4,5,6,7); 

void setup(void) {
  lcd.begin(16, 2);
  lcd.print(" BMP180 Sensor");
  lcd.setCursor(0, 1);
  lcd.print("Temp. & Pressure");
  lcd.setCursor(0, 2);

  pressure.begin();
}

void loop() {
  
 float pressure;
  
 
 pressure = readPressureAndTemperature();
 
 delay(3000);
 
 lcd.clear(); 
 
 char tempF[6]; 
 
 char pressF[7];
 
 dtostrf(temperature, 5, 1, tempF);
 
 dtostrf(pressure, 7, 2, pressF);

 //Printing Temperature
 lcd.print("Temp:"); 
 lcd.print(tempF);
 lcd.print((char)223);
 lcd.print("C ");
 

 
 //Printing Pressure
 lcd.setCursor(0,1);
 lcd.print("P: ");
 lcd.print(pressF);
 lcd.print(" Mb ");
}

float readPressureAndTemperature()
{
  char status;
  double T,P,p0,a;

  status = pressure.startTemperature();
  if (status != 0)
  {
    delay(status);
    status = pressure.getTemperature(T);
    if (status != 0)
    { 
      temperature = T;
      status = pressure.startPressure(3);
      if (status != 0)
      {
        delay(status);
        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          p0 = pressure.sealevel(P,ALTITUDE);       
          return p0;
        }
      }
    }
  }
}



That's not particularly well written code, and that's being kind. Mixing globals and locals willy nilly, having global and local variables with the same name and different types? Ugh. I'm not surprised you are having trouble with it. Why you should only have trouble with a 328PB is unclear, and I'm unwilling to spend the time to try and wrap my head around that badly written piece of code to try and figure it out.

And your description of the problem you're seeing is lacking in both precision and detail. Both of which are necessary to successfully diagnose a problem. What exactly do you mean when you say "the temperature value is not showing"? Is its value blank on the LCD? Is it zero? What? Be precise. Be detailed. No one can see what you're seeing. If you don't tell us, if you don't show us, you're just wasting everyone's time. Yours and ours.

1 Like

This is guaranteed to fail, because pressF is declared too short.

 char pressF[7];
 ...
 dtostrf(pressure, 7, 2, pressF);
1 Like

And what in the name of sanity is this kind of construct intended do to? Delaying by the value of the returned status code? Explain!

temperature value shows nothing in atmega328pb, (Temp: c) it's blank, but shows well in atmega328p arduino nano Temp:23 C

And? What debugging have you tried? What's the value of the variables when it doesn't work? Before and after? At various stages? Don't make others constantly drag information out of you.

I try debugging using serial monitor, my BMP180 well works with atmega328pb Arduino nano and shows temperature and pressure value on serial monitor, but it fails to print temperature value on my 1602 lcd... I am surprized that my other Arduino nano atmega328p works well , but I used that in my other radio project....

So am I, given the errors already pointed out.

1 Like

And yet no details of what you saw. Sigh. I have no more time to spend on this.

Hi @van_der_decken ,

I was puzzled like you but found this on Github

https://github.com/sparkfun/BMP180_Breakout/blob/master/Libraries/Arduino/examples/BMP180_altitude_example/BMP180_altitude_example.ino

      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);

The "start.." functions return the "time to wait" in msecs

char SFE_BMP180::startTemperature(void)
// Begin a temperature reading.
// Will return delay in ms to wait, or 0 if I2C error

the "get..." functions return 1 if successful, 0 if I2C error.

So this is a feature ...

:wink:

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