Whenever I use multiple devices on SPI, TFT Screen, and the BME280 breakout board from Adafruit, the Arduino fails to detect the devices.
Code of the screen separately (works)
// include the necessary libraries
#include <SPI.h>
#include <SD.h>
#include <TFT.h> // Arduino LCD library
// pin definitions
#define sd_cs 7
#define lcd_cs 10
#define dc 9
#define rst 8
// Define screen pins
TFT TFTscreen = TFT(lcd_cs, dc, rst);
//Define variables
char sensorPrintout[4];
char sensorPrintout2[4];
// this variable represents the image to be drawn on screen
PImage logo;
void setup() {
Serial.begin(9600);
while (!Serial) { }
Serial.print("Initializing SD card...");
if (!SD.begin(sd_cs)) {
Serial.println("failed!");
return;
} else {
Serial.println("OK!");
}
// 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("Schuif 1: ");
TFTscreen.setCursor(75,25);
TFTscreen.print((char)247);
TFTscreen.print("C");
//Write some text
TFTscreen.setCursor(0,50);
TFTscreen.print("Schuif 2: ");
TFTscreen.setCursor(75,50);
TFTscreen.print((char)247);
TFTscreen.print("C");
}
void loop() {
// Read value of A0
int value = analogRead(A0);
Serial.println(value);
float voltage = value * (5.0/1023.0);
Serial.println(voltage);
String sensorVal = String(analogRead(A0));
String sensorVal2 = String(voltage);
// Change sensor value to a character array
sensorVal.toCharArray(sensorPrintout, 4);
sensorVal2.toCharArray(sensorPrintout2, 4);
// Change text color to white
TFTscreen.stroke(255,255,255);
// Write value of sensor 1
TFTscreen.setCursor(55,25);
TFTscreen.print(sensorPrintout);
// Write value of sensor 2
TFTscreen.setCursor(55,50);
TFTscreen.print(sensorPrintout2);
// wait 500ms
delay(1000);
// Change text color to black
TFTscreen.stroke(0,0,0);
// Write value of sensor 1
TFTscreen.setCursor(55,25);
TFTscreen.print(sensorPrintout);
// Write value of sensor 2
TFTscreen.setCursor(55,50);
TFTscreen.print(sensorPrintout2);
}
Code of the BME280 separately (works)
#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 6
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme(BME_CS); // hardware SPI
unsigned long delayTime;
void setup() {
Serial.begin(9600);
Serial.println(F("BME280 test"));
bool status;
// default settings
// (you can also pass in a Wire library object like &Wire2)
status = bme.begin();
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
Serial.println("-- Default Test --");
delayTime = 1000;
Serial.println();
delay(100); // let sensor boot up
}
void loop() {
printValues();
delay(delayTime);
}
void printValues() {
Serial.print("Temperature = ");
Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
Serial.print(bme.readPressure() / 100.0F);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" m");
Serial.print("Humidity = ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.println();
}
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() {
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 text color 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 sensor value to a character array
sensorVal1.toCharArray(sensorPrintout1, 6);
sensorVal2.toCharArray(sensorPrintout2, 6);
// Change text color 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 text color 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+SDcard and the BME280 separately, 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.
Update 2
After some measurements with the scope I determined that the MOSI is pulled to 0V when I connect the 5V to the TFT screen. I also saw that the MOSI output on the BME280 board is 3.3V instead of 5V.
Could it be that the conflict in voltages is the cause for all my troubles?