Unable to print sensor BMP280 data sensor data to KMR-1.8 SPI LCD

So I am trying to print BMP280 sensor data onto my brand new KMR-1.8 SPI LCD screen, only thing is its coming out garbage. Can someone suggest why? Below is my code. BTW, all regular text prints fine


#include <TFT.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>
#include "Adafruit_HTU21DF.h"

#define cs 10
#define dc 9
#define rst 8

char AM[8];

TFT TFTscreen = TFT(cs,dc,rst);

/* //for other smaller lcd
//#define BMP_SCK  (13) 
//#define BMP_MISO (12)
//#define BMP_MOSI (11)
//#define BMP_CS   (10)
*/
Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);
Adafruit_HTU21DF htu = Adafruit_HTU21DF();


void setup() {
  Serial.begin(9600);

  TFTscreen.begin();
  TFTscreen.background(0,0,0);
  
  TFTscreen.setTextSize(2);
  
  if (!htu.begin()) {
    Serial.println("Couldn't find sensor!");
    while (1);
  }
  
  Serial.println(F("BMP280 test"));
  if (!bmp.begin()) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring!"));
    while (1);
  }
  /* Default settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /* Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp. oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering. */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}
void loop() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");
    Serial.print(F("Pressure = "));
    Serial.print((bmp.readPressure())*0.000986923);
    Serial.println("% Sea Level");//not Pa
    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
    Serial.println(" m");
    Serial.println();

    float temp = htu.readTemperature();
    float rel_hum = htu.readHumidity();
    Serial.print("Second Sensor Temp: "); Serial.print(temp); Serial.print(" C");
    Serial.print("\t\t");
    Serial.print("Humidity: "); Serial.print(rel_hum); Serial.println(" \%");

    //int AltMtrs = bmp.readAltitude(1013.25);
    //int AltFt = (bmp.readAltitude(1013.25))*3.28;
    dtostrf(bmp.readAltitude(1013.25), 6, 2, AM);
   TFTscreen.stroke(255,255,255);
    TFTscreen.text("Alt ", 30, 10);
    
    TFTscreen.text(AM, 30, 30);
    //TFTscreen.text("mtrs / ", 30, 50);
    //TFTscreen.text(AltFt, 30, 40);
    //TFTscreen.text("ft.", 30, 50);
    
    delay(2000);
}

Below is my screen status:

Hi,

What garbage, all the screen or the numbers you want to display?

If the whole screen have you got some example code, or simple code that JUST uses to screen, to see if you have control of it.
Isolating you hardware will make it easier to see where the problem is.

Thanks.. Tom.. :smiley: :+1: :coffee: :australia:

Its just those numbers coming out all wrong...if I try to print just a string between " " then its fine.

Hi,
Does Adafruit library have some examples in the IDE Examples tab?

Tom... :smiley: :+1: :coffee: :australia:

I'm going to guess that the above function expects text rather than an integer.

@TomGeorge Thanks, I've resolved it.

@TomGeorge @markd833 but a new problem presents itself, when the numbers change the screen doesn't refresh but just overwrites the previous text. making it garbage again. How to rectify that? I am updating the code in the question.

Hi,
Before writing the new data, write in blank spaces to clear that segment of the screen for over writing.

Tom... :smiley: :+1: :coffee: :australia:

@TomGeorge nope, tried it, not working

TFTscreen.text("                       ", 30, 30);
    TFTscreen.text(AM, 30, 30);

@TomGeorge Ok, sorted that out too, thanks for the suggestions

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