Display BME280 Sensor Data to 20X4 LCD

Hello hackers, tinkerers, and DIY’ers.

I am a newbie to Arduino, but have a basic understanding of programming.
I am an electrical engineering student and am completing an Automated Aeroponics System for my senior project.
I have chose to use Arduino over PLC’s because of large support communities, like this one.

Now to the heart of the matter.

I have an Adafruit BME280 Temp/Humidity/Pressure sensor that I would like to output data onto a 20X4 LCD via I2C.
I am able to connect to both devices and run my sketches fine.
I am able to output sensor data to the serial monitor, but like i said, I would like to output it to the LCD

I would like to merge these two sketches, to run as one, as I have tried multiple attempts going from other peoples projects (which use different sensors and LCD’s), but kept getting riddled with errors.
Any help getting this up and running would be greatly appreciated.


Code for the BME280 Sensor:

This is a library for the BME280 humidity, temperature & pressure sensor

Designed specifically to work with the Adafruit BME280 Breakout
----> http://www.adafruit.com/products/2650

These sensors use I2C or SPI to communicate, 2 or 4 pins are required
to interface. The device's I2C address is either 0x76 or 0x77.

Adafruit invests time and resources providing this open source code,
please support Adafruit andopen-source hardware by purchasing products
from Adafruit!

Written by Limor Fried & Kevin Townsend for Adafruit Industries.
BSD license, all text above must be included in any redistribution

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

#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10

#define SEALEVELPRESSURE_HPA (1037.5895)

Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

unsigned long delayTime;

void setup() {
Serial.println(F("BME280 test"));

bool status;

// default settings
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);

Serial.println("-- Default Test --");
delayTime = 1000;


void loop() { 

void printValues() {
Serial.print("Temperature = ");
Serial.print((((bme.readTemperature()*9)/5)+32)); //added this to convert to farenheit and add to string
Serial.println(" *F");

Serial.print("Pressure = ");

Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");

Serial.print("Approx. Altitude = ");
Serial.println(" m");

Serial.print("Humidity = ");
Serial.println(" %");


And this is the sketch for the 20x4 LCD

/* YourDuino.com Example Software Sketch
20 character 4 line I2C Display
Backpack Interface labelled "YwRobot Arduino LCM1602 IIC V1"
Connect Vcc and Ground, SDA to A4, SCL to A5 on Arduino
terry@yourduino.com */

/*-----( Import needed libraries )-----*/
#include <Wire.h> // Comes with Arduino IDE
// Get the LCD I2C Library here: 
// https://bitbucket.org/fmalpartida/new-l ... /downloads
// Move any other LCD libraries to another folder or delete them
// See Library "Docs" folder for possible commands etc.
#include <LiquidCrystal_I2C.h>

/*-----( Declare Constants )-----*/
/*-----( Declare objects )-----*/
// set the LCD address to 0x27 for a 20 chars 4 line display
// Set the pins on the I2C chip used for LCD connections:
// addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // Set the LCD I2C address

/*-----( Declare Variables )-----*/

void setup() /*----( SETUP: RUNS ONCE )----*/
Serial.begin(9600); // Used to type in characters

lcd.begin(20,4); // initialize the lcd for 20 chars 4 lines, turn on backlight

// ------- Quick 3 blinks of backlight -------------
for(int i = 0; i< 3; i++)
lcd.backlight(); // finish with backlight on 

//-------- Write characters on the display ------------------
// NOTE: Cursor Position: Lines and Characters start at 0 
lcd.setCursor(3,0); //Start at character 4 on line 0
lcd.print("Hello little man,");
lcd.print("Your dad has been");
lcd.print("tellng me you play");
lcd.print("a lot of games.");
// Wait and then tell user they can start the Serial Monitor and type in characters to
// Display. (Set Serial Monitor option to "No Line Ending")
lcd.setCursor(0,0); //Start at character 0 on line 0
lcd.print("You can type stuff in");
lcd.print("the serial monitor"); 

}/*--(end setup )---*/

void loop() /*----( LOOP: RUNS CONSTANTLY )----*/
// when characters arrive over the serial port...
if (Serial.available()) {
// wait a bit for the entire message to arrive
// clear the screen
// read all the available characters
while (Serial.available() > 0) {
// display each character to the LCD

}/* --(end main loop )-- */

/* ( THE END ) *//tt]

Most of the lcd sketch like the "hello little man" stuff and the stuff to do with serial input is irrelevant but surely you see that there's an lcd.print() method which is analogous to the Serial.print() one?

So copy the lcd configuration stuff from above setup into the are above setup in the sensor sketch. Copy the lcd.begin() from setup() to setup() with the backlight blinker if you want it.

Then use lcd.setCursor() and lcd.print() to do what you want withe the sensor output, instead of using Serial.print().

If you are using the I2C interface LCD you need to comment this command:
//LiquidCrystal_I2C lcd(0x3f, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); It will give an error like ‘positive’ was not declare in this scope. Any way you do not need it. I think this is if you are not using the I2C and connected the output pins directly to the LCD.

then add these:

#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F, 20, 4); // Set the LCD I2C address 20 = character per lines 4 = numbers of lines

Wire the lcd display as follow: Connect Vcc and Ground, SDA to A4, SCL to A5 on Arduino
You should be able to run the sketch without errors.