Problema con pantalla TFT 2.4" SHIELD y el sensor BME280

Buenos días.

Primero saludar a todos los del foro.

Estoy usando una pantalla 2.4" TFT LCD SHIELD ILI9325, tambien el arduino UNO y un sensor GY - BM 280(conectado a los pines D19/SCL y D18/SDA). Adjunto foto de los componenes.

Tengo el siguiente problema:
Cuando quiero imprimir en la pantalla TFT las lecturas bme.readTemperature(), bme.readHumidity y bme.readPressure. Dejo el código abreviado del problema.

//LIBRERIAS SENSOR
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>

//LIBRERIAS TFT
#include <Adafruit_GFX.h>   // Core graphics library
#include <MCUFRIEND_kbv.h>  // Hardware-specific library
#include <Fonts/FreeSans12pt7b.h>
#include <FreeDefaultFonts.h>
#include <TouchScreen.h>

//MACRO TFT
#define BLACK 0x0000        // Color negro
#define WHITE 0xFFFF        // Color blanco
#define GREY 0x8410         // Color griz
#define NUEVO 0xEEEE        // Color dorado
#define VERDEOSCURO 0x03E0  // Color verde oscuro
#define MINPRESSURE 200     // Presiòn mìnima del touch
#define MAXPRESSURE 1000    // Presiòn màxima del touch

//OBJETOS
Adafruit_BME280 bme;
MCUFRIEND_kbv tft;
Adafruit_GFX_Button on_btn, off_btn;


//VARIABLE SENSOR
float temperature;
float pressure;
float humidity;

// Todos las pantalla tàctiles y cableados son diferentes
// Abrir el ejemplo TouchScreen_Calibr_native.ino de MCUFRIEND_kbv
// Recordar que debes mantener apretado en cuadrado con cruz en el interior de color blanco para una buena calibraciòn
// Copia y pega los resultados obtenidos, recordar si usas la pantalla en modo retrato(VERTICAL) o paisaje(HORIZONTAL)
const int XP = 8, XM = A2, YP = A3, YM = 9;  //240x320 ID=0x9328
const int TS_LEFT = 158, TS_RT = 893, TS_TOP = 116, TS_BOT = 909;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

//LECTURA DE COORDENADAS TACTILES
int pixel_x, pixel_y;  //Touch_getXY() updates global vars
bool Touch_getXY() {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);  // Restaura pines compartidos
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);  // Activa la interfaz de la pantalla tàctil
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);  // Verifica si el toque es valido en funciòn de la presiòn
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());   // Mapea las coordenas en el eje X
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());  // Mapea las coordenas en el eje Y
  }
  return pressed;
}

//ACTUALIZA LA PANTALLA POR SI HAY CAMBIO
bool actualizacion = true;

void setup() {
  Serial.begin(9600);
  //INICIAR EL SENSOR BME 280
  if (!bme.begin(0x76)) {
    Serial.println("Could not find a valid BME280 sensor, check wiring!");
    while (1);   
  }
    
  //INICIAR LA PANTALLA TFT
  uint16_t ID = tft.readID();  // Lee el ID de la pantalla gràfica
  Serial.println("found ID = 0x");
  Serial.print(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9325;  // Verifica el ID y lo cambia a 0x9325
  tft.begin(ID);                  //Se configura e inicia la pantalla TFT

  //ROTACION DE LA PANTALLA TFT Y COLOR DE FONDO
  tft.setRotation(0);  // Con el botòn RESET en la parte superior
  tft.fillScreen(BLACK);  // Fondo de pantalla color negro

  //BOTÒN DE MÌNIMOS Y MÀXIMOS
  on_btn.initButton(&tft, 120, 290, 120, 40, RED, RED, WHITE, "MIN/MAX", 2);
  on_btn.drawButton(false);
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg) {
  int16_t x1, y1;
  uint16_t wid, ht;
  //tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(WHITE);
  tft.setTextSize(sz);
  tft.print(msg);
}

void loop(){
  unsigned long currentMillis = millis();
  unsigned long startTime = millis();
  bool down = Touch_getXY();

  //temperature = bme.readTemperature();
  //pressure = bme.readPressure() / 100.0F;
  //humidity = bme.readHumidity();
 
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  if (on_btn.justReleased())
    on_btn.drawButton();
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);  
  }
   
  tft.fillRect(0, 0, 240, 19, GREY);
  showmsgXY(55, 1, 2.2, NULL, "TEMPERATURA");
  tft.fillRect(0, 87, 240, 19, GREY);
  showmsgXY(80, 88, 2.2, NULL, "PRESION");
  tft.fillRect(0, 174, 240, 19, GREY);
  showmsgXY(80, 175, 2.2, NULL, "HUMEDAD");
  
  tft.setCursor(70, 35);
  tft.setTextSize(5);
  tft.print(temperature);
  tft.setCursor(70, 124);
  tft.setTextSize(5);
  tft.print(pressure);
  tft.setCursor(70, 204);
  tft.setTextSize(5);
  tft.print(humidity);
  
  Serial.println(temperature);
  Serial.println(pressure);
  Serial.println(humidity);
  Serial.println("______________");

  delay(2000);
}

Adjunto foto del buen funcionamiento de la pantalla y touch.

Cuando descomente esta parte del código la pantalla se pondrá en color blanco la pantalla TFT, el Monitor Serial registra con normalidad los valores de Temperatura, Presión y Humedad.

  temperature = bme.readTemperature();
  pressure = bme.readPressure() / 100.0F;
  humidity = bme.readHumidity();

Agradezco la ayuda que me puedan brindar, gracias de antemano.

Su publicacion se MUEVE a su ubicacion actual ya que es mas adecuada.

Si, pero el D18, parece ser que esta ocupado por la señal "LCD_RST" de la TFT, en principio esa pantalla es incompatible con el bus I2C y por lo tanto con el BM 280 en modo I2C.
Ese BM280 también puede funcionar dentro de un bus SPI. Mira el siguiente TUTORIAL-1 para identificas pines, (segundo ejemplo).
¿Tiene tu TFT bus SPI?, parece que si, según este otro TUTORIAL-2, la SD utiliza un bus SPI:

Pues ya lo tenemos , el shiel de pantalla compartirá los pines mosi, miso, y clk con los correspondientes del BME280, solo tenemos que buscar un pin libre que no use la tft para el pin CS de bme. Yo solo encuentro libre el A5, (D19); usalo para el CS del BME y nos cuentas que pasa.

1 Like

Gracias por la sugerencia, ya me registra los valores con normalidad en la pantalla TFT.

Esta fueron las conexiones del sensor GY-BM E/P 280 con el Arduino UNO:

Y en la siguiente imagen muestro el buen funcionamiento:

Adjunto el código con las correcciones:

//LIBRERIAS SENSOR
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)

//LIBRERIAS TFT
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h> 
#include <Fonts/FreeSans12pt7b.h>
#include <FreeDefaultFonts.h>
#include <TouchScreen.h>

//COMUNICACION SPI DEL SENSOR GY-BM E/P 280
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS A5


//MACRO TFT
#define BLACK 0x0000        // Color negro
#define RED 0xF800          // Color rojo
#define WHITE 0xFFFF        // Color blanco
#define GREY 0x8410         // Color griz
#define NUEVO 0xEEEE        // Color dorado
#define VERDEOSCURO 0x03E0  // Color verde oscuro
#define MINPRESSURE 200     // Presiòn mìnima del touch
#define MAXPRESSURE 1000    // Presiòn màxima del touch

//OBJETOS
Adafruit_BME280 bme(BME_CS); // hardware SPI
MCUFRIEND_kbv tft;
Adafruit_GFX_Button on_btn, off_btn;

//VARIABLE SENSOR
float temperature;
float pressure;
float humidity;

// Todos las pantalla tàctiles y cableados son diferentes
// Abrir el ejemplo TouchScreen_Calibr_native.ino de MCUFRIEND_kbv
// Recordar que debes mantener apretado en cuadrado con cruz en el interior de color blanco para una buena calibraciòn
// Copia y pega los resultados obtenidos, recordar si usas la pantalla en modo retrato(VERTICAL) o paisaje(HORIZONTAL)
const int XP = 8, XM = A2, YP = A3, YM = 9;  //240x320 ID=0x9328
const int TS_LEFT = 158, TS_RT = 893, TS_TOP = 116, TS_BOT = 909;

TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);

//LECTURA DE COORDENADAS TACTILES
int pixel_x, pixel_y;  //Touch_getXY() updates global vars
bool Touch_getXY() {
  TSPoint p = ts.getPoint();
  pinMode(YP, OUTPUT);  // Restaura pines compartidos
  pinMode(XM, OUTPUT);
  digitalWrite(YP, HIGH);  // Activa la interfaz de la pantalla tàctil
  digitalWrite(XM, HIGH);
  bool pressed = (p.z > MINPRESSURE && p.z < MAXPRESSURE);  // Verifica si el toque es valido en funciòn de la presiòn
  if (pressed) {
    pixel_x = map(p.x, TS_LEFT, TS_RT, 0, tft.width());   // Mapea las coordenas en el eje X
    pixel_y = map(p.y, TS_TOP, TS_BOT, 0, tft.height());  // Mapea las coordenas en el eje Y
  }
  return pressed;
}

void setup() {
  Serial.begin(9600);
  //INICIAR EL SENSOR BME 280
  while(!Serial);    // time to get serial running
  Serial.println(F("BME280 test"));
  unsigned status;  
  status = bme.begin();  
  if (!status) {
    Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
    Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }
    
  //INICIAR LA PANTALLA TFT
  uint16_t ID = tft.readID();  // Lee el ID de la pantalla gràfica
  Serial.println("found ID = 0x");
  Serial.print(ID, HEX);
  if (ID == 0xD3D3) ID = 0x9325;  // Verifica el ID y lo cambia a 0x9325
  tft.begin(ID);                  //Se configura e inicia la pantalla TFT

  //ROTACION DE LA PANTALLA TFT Y COLOR DE FONDO
  tft.setRotation(0);  // Con el botòn RESET en la parte superior
  tft.fillScreen(BLACK);  // Fondo de pantalla color negro

  //BOTÒN DE MÌNIMOS Y MÀXIMOS
  on_btn.initButton(&tft, 120, 290, 120, 40, RED, RED, WHITE, "MIN/MAX", 2);
  on_btn.drawButton(false);
}

void showmsgXY(int x, int y, int sz, const GFXfont *f, const char *msg) {
  int16_t x1, y1;
  uint16_t wid, ht;
  //tft.drawFastHLine(0, y, tft.width(), WHITE);
  tft.setFont(f);
  tft.setCursor(x, y);
  tft.setTextColor(WHITE);
  tft.setTextSize(sz);
  tft.print(msg);
}

void loop(){
  bool down = Touch_getXY();
  temperature = bme.readTemperature();
  pressure = bme.readPressure() / 100.0F;
  humidity = bme.readHumidity();
 
  on_btn.press(down && on_btn.contains(pixel_x, pixel_y));
  if (on_btn.justReleased())
    on_btn.drawButton();
  if (on_btn.justPressed()) {
    on_btn.drawButton(true);  
  }
   
  tft.fillRect(0, 0, 240, 19, GREY);
  showmsgXY(55, 1, 2.2, NULL, "TEMPERATURA");
  tft.fillRect(0, 87, 240, 19, GREY);
  showmsgXY(80, 88, 2.2, NULL, "PRESION");
  tft.fillRect(0, 174, 240, 19, GREY);
  showmsgXY(80, 175, 2.2, NULL, "HUMEDAD");
  
  tft.setCursor(40, 40);
  tft.setTextSize(4);
  tft.print(temperature);
  tft.setCursor(200, 45);
  tft.setTextSize(3);
  tft.print("C");
  tft.setCursor(10, 124);
  tft.setTextSize(4);
  tft.print(pressure);
  tft.setCursor(180, 124);
  tft.setTextSize(3);
  tft.print("hPa");
  tft.setCursor(40, 214);
  tft.setTextSize(4);
  tft.print(humidity);
  tft.setCursor(200, 214);
  tft.setTextSize(3);
  tft.print("%");
  
  Serial.println(temperature);
  Serial.println(pressure);
  Serial.println(humidity);
  Serial.println("______________");

  delay(5000);
}

De nada, me alego que lo hayas solucionado. Me guardo tu código tengo esa misma pantalla y en un futuro no muy lejano, (espero), quiero cambiar una OLED por esa TFT
y prefiero no partir de cero.
Saludos.

2 Likes

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