BME280 and Ultimate GPS won't work in the same sketch

Hi all, (first time posting here, I’ve done my best to put the issue into words)

In this sketch, the BME data (pressure and temp) will write to the SD card, but only when the readGPS(); is commented out? How could you get them to both write to the SD card in separate files?
here’s what i’m using:

BME280

Adafruit Ultimate GPS

Adafruit data shield

Ardiuno Uno

Many thanks & any help appreciated :slight_smile:

here’s the code:

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SD.h>
#define BMP_SCK 6
#define BMP_MISO 7
#define BMP_MOSI 8
#define BMP_CS 9

Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

File GpsFile;

char c;
String NMEA1;
String NMEA2;
float temp;
float Press;

SoftwareSerial mySerial(5,4);
Adafruit_GPS GPS(&mySerial);
const int chipSelect = 10;

void setup()
{
pinMode(2, OUTPUT);
Serial.begin(9600);

pinMode(10, OUTPUT);

if (!SD.begin(chipSelect)) {
Serial.println(“initialization failed!”);

}
bme.begin();

GPS.begin(9600); //Turn on GPS at 9600 baud
GPS.sendCommand("$PGCMD,33,0*6D"); //Turn off antenna update nuisance data
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); //Request RMC and GGA Sentences only
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); //Set update rate to 1 hz
delay(1000);

}
void loop()
{
for (int i = 0; i < 100; i++)
{

readGPS();
GpsFile = SD.open(“GPS.txt”, FILE_WRITE);
GpsFile.println(NMEA1);
GpsFile.println(NMEA2);

delay(100);

GpsFile.close();

GetBME();

GpsFile = SD.open(“Pt.txt”, FILE_WRITE);
GpsFile.println(temp);
GpsFile.println(Press);
GpsFile.close();

}
delay(1000);

while(1);

}

void readGPS() {

clearGPS();
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
NMEA1=GPS.lastNMEA();

while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
NMEA2=GPS.lastNMEA();

Serial.println(NMEA1);
Serial.println(NMEA2);
Serial.println("");

}

void clearGPS() { //Clear old and corrupt data from serial port
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence
while(!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c=GPS.read();
}
GPS.parse(GPS.lastNMEA()); //Parse that last good NMEA sentence

}

void GetBME(){

temp = bme.readTemperature();
Press = bme.readPressure();

}

  GpsFile = SD.open("Pt.txt", FILE_WRITE);

That hardly looks like GPS data you are going to write to the file...

The code does something, when nothing is commented out. You seem to have forgotten to explain what the code actually does.

You are too fond of global variables, and WAY too fond of delay().

Update: thanks for the tips on global variables and delays PaulS, they are now both mostly gone.
I have got the sketch doing what I wanted now, writing NMEA sentences, pressure and temperature to a file on an SD card. I also renamed the files to make it easier reading.

if you wanted it, here’s the updated code:

#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BMP280.h>
#include <SD.h>
#define BMP_SCK 6
#define BMP_MISO 7
#define BMP_MOSI 8
#define BMP_CS 9

Adafruit_BMP280 bme(BMP_CS, BMP_MOSI, BMP_MISO, BMP_SCK);

File DataFile;

char c;

SoftwareSerial mySerial(5, 4);
Adafruit_GPS GPS(&mySerial);
const int chipSelect = 10;

void setup()
{

//Serial.begin(9600);

pinMode(10, OUTPUT);

SD.begin(chipSelect);

bme.begin();

GPS.begin(9600);

}
void loop()
{
for (int i = 0; i < 5; i++)
{

DataFile = SD.open(“GPS.txt”, FILE_WRITE);
DataFile.println(readGPS());

DataFile.close();

DataFile = SD.open(“Pt.txt”, FILE_WRITE);
DataFile.println(getTemp());
DataFile.println(getPress());
DataFile.close();

}
while (1);

}

String readGPS() {

while (!GPS.newNMEAreceived()) { //Loop until you have a good NMEA sentence
c = GPS.read();
}
return GPS.lastNMEA();
}

float getTemp() {
return bme.readTemperature();
}

float getPress() {
return bme.readPressure();
}

File DataFile;

char c;

The File instance is only used in loop(). You should, IMHO, have TWO File instances - one for the GPS data file and one for the temp/humidity data file. Since only one file at a time will be opened, you will not need more memory to have two local variables.

The char variable is only used in readGPS(), so that is where it should be declared.

Finally, you should NOT assume that opening a file for write will succeed. Before actually writing, you should test that the open was successful:

   File gpsData = SD.open("GPS.txt", FILE_WRITE);
   if(gpsData)
   {
      gpsData.println(readGPS());
   
      gpsData.close();
   }

Paul,

Ok, great, I've declared everything where you suggested, and created two files, one for GPS and one for temp/pressure. Also, I added the if() statement to check if the file had been created.
Thanks for all your help,

Tom

Tom
I assume you are using a UNO or similar with 328 chip
If elements of the code function ok but the full code does funny things I would suspect a memory issue.
I would also use hardware serial instead of software serial and just remove the GPS TX
connection whilst you upload the code.
Enjoy

Yes, you assume correctly,
The code works fine now without playing up at all, I looped it 10,000 times (many more than cycles than it will need to do) and everything was fine. To set the gps up using hardware serial, would that involve switching the current TX and RX connections to D1 and D0?
Thanks,
Tom