First attempt at writing my own code? Doesn't work. Not sure where to go with it

Soooo I finally decided to try writing my own code. It is for a BMP280.

The goal is to have the BMP 280 print the pressure and altitude on a I2C 16*2 screen that has a I2C adapter. In theory it will run from an Arduino Nano. The code is below. I honestly don’t know enough about error codes to know what to do with them… or how to fix them.

…and I’m sure there will be many as this is the first time I’ve tried to write code myself.

#include <LiquidCrystal_I2C.h>      //Libraries for the screen and BMP280
#include <Adafruit_BMP280.h>

LiquidCrystal_I2C lcd(0x27, 16, 2);  // I2C address 0x27, 16 column and 2 rows

void setup()
  Adafruit_BMP280 begin();     // initialize the sensor
  lcd.init();      // initialize the lcd
  lcd.backlight(); // open the backlight 

void loop()
  delay(2000); // wait a few seconds between measurements

  float T = bmp.readTemperature()*9/5 + 32;    //Read temperature in C and conversion to F
    float P = bmp.readPressure()/3386.39;        //Read Pressure in Pa and conversion to inches of mercury
    float A = bmp.readAltitude(1019.66)*3.28084; //Calculating the Altitude, the "1019.66" is the pressure in(hPa) at sea level

} else {
    lcd.setCursor(0, 0);  // start to print at the first row
    lcd.print("Press: ");
    lcd.print(P);     // print the pressure

    lcd.setCursor(0, 1);  // start to print at the second row
    lcd.print("Alt: ");
    lcd.print(A);      // print the altitude
    lcd.print(" ft");

maybe.ino (1.2 KB)

setup() doesn't have a closing brace.
In loop() there is no if preceding the else.

Look at the examples that come with the BMP library to see how to construct and initialize the bmp object. You are not doing it right.

For an I2C LCD display to work, the I2C address and the I2C backpack to LCD pin mapping must be correct. If the library default settings for either or both are not correct the LCD will not work. You can try to figure out the right pin mapping and use an I2C scanner to find the address, but if you install and use the hd44780 library that is done automatically by the library.

To install the hd44780 library. The hd44780 library is the best available for I2C LCDs. The library is available in the Library Manager. Go to Library Manager (in the IDE menus, Sketch, Include Libraries, Manage Libraries) and in the Topics dropdown choose Display and in the Filter your search box enter hd44780. Select and install the hd44780 library by Bill Perry.

The class that you want to use is the hd44780_I2Cexp class. There are examples to show how to use the library. The nice thing about the hd44780 library is that it will autodetect the I2C address and the I2C backpack to LCD pin mapping.

In the examples, there is a diagnostic sketch that will help us to help you if you still have trouble with the display. Run the diagnostic sketch and post the results.

I honestly don't know enough about error codes to know what to do with them... or how to fix them.

Did you run code for BMP280 Sensor and LCD separately?
If not, please run separate code for sensor/LCD to make sure that there is nothing wrong with each sensors (such as wiring, coding).

I think there needs to be a #include "Wire.h" near the top and a Wire.begin(); in setup to use the I2C buss !!

62Tom, the Adafruit_BMP280.h file includes Wire.h and the Adafruit_BMP280.cpp file has the begin() function that calls Wire.begin().

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