whenever i use multiple devices on spi : tft screen, https://www.arduino.cc/en/Guide/TFT) and the bme 280 (breakout board from adafruit) the Arduino fails
to detect the devices
Code of the Tft screen with the BME280 (doesn’t work)
// include the necessary libraries
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <SPI.h>
#include <SD.h>
#include <TFT.h> // Arduino LCD library
#include <Wire.h>
// pin definitions
#define bme_cs 6
#define sd_cs 7
#define rst 8
#define dc 9
#define lcd_cs 10
#define mosi 11
#define miso 12
#define sck 13
//var definitions
#define SEALEVELPRESSURE_HPA (1013.25)
//Define hardware SPI connection with BME280
Adafruit_BME280 bme(bme_cs); // hardware SPI
// Define screen pins
TFT TFTscreen = TFT(lcd_cs, dc, rst);
//Define variables
char sensorPrintout1[6];
char sensorPrintout2[6];
unsigned long delayTime;
// this variable represents the image to be drawn on screen
PImage logo;
void setup()
{
// Start serial communication
Serial.begin(9600);
while (!Serial) { }
bool status;
// Initializing SD card
Serial.print("Initializing SD card...");
if (!SD.begin(sd_cs))
{
Serial.println("failed!");
//while (1);
}
else
{
Serial.println("OK!");
}
//Initializing BME280
Serial.print("Initializing BME280");
status = bme.begin();
if (!status)
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
else
{
Serial.println("BME 280 sensor found");
}
// Start and clear TFT screen
TFTscreen.begin();
TFTscreen.background(0, 0, 0);
// Lookup image on TFT screen
logo = TFTscreen.loadImage("Logo.bmp");
if (!logo.isValid())
{
Serial.println("error while loading Logo.bmp");
}
if (logo.isValid() == false)
{
return;
}
//Draw logo on the screen
Serial.println("drawing image");
TFTscreen.image(logo, 0, 0);
// Change textcolor to white
TFTscreen.stroke(255,255,255);
//Write some text
TFTscreen.setCursor(0,25);
TFTscreen.print("temperatuur 1: ");
TFTscreen.setCursor(100,25);
TFTscreen.print((char)247);
TFTscreen.print("C");
//write some text
TFTscreen.setCursor(0,50);
TFTscreen.print("luchtvochtigheid 2: ");
TFTscreen.setCursor(100,50);
TFTscreen.print((char)37);
TFTscreen.print("C");
}
void loop() {
// Read value of temperature
int value1 = bme.readTemperature();
int value2 = bme.readHumidity();
String sensorVal1 = String(value1);
String sensorVal2 = String(value2);
// Change sensorvalue to a character array
sensorVal1.toCharArray(sensorPrintout1, 6);
sensorVal2.toCharArray(sensorPrintout2, 6);
// Change textolor to white
TFTscreen.stroke(255,255,255);
// Write value of temperature sensor
TFTscreen.setCursor(55,25);
TFTscreen.print(sensorPrintout1);
// Write value of humidity sensor
TFTscreen.setCursor(55,50);
TFTscreen.print(sensorPrintout2);
// wait 500ms
delay(1000);
// Change textolor to black (aka overwrite text)
TFTscreen.stroke(0,0,0);
// Write value of temperature sensor
TFTscreen.setCursor(55,25);
TFTscreen.print(sensorPrintout1);
// Write value of humidity sensor
TFTscreen.setCursor(55,50);
TFTscreen.print(sensorPrintout2);
}
Now this is the serial output
Initializing SD card...failed!
Initializing BME280Could not find a valid BME280 sensor, check wiring!
at first it only showed the first line, then it stopped because of the while(1)
loop. so i put it in comment to see if the BME initializes on its own.
I also tested the screen+SD card and the BME280 seperately which worked just fine
update
I tried to test the programs with the complete circuit. The program of the TFT screen works fine that way but when i try the program of the BME280 it doesn’t work.
I have to remove the TFT screen from the breadboard to make it work.