GPS(EM-406A) + SPI SD card module

Hi all!

I’m an arduino newbie :stuck_out_tongue: and I’m trying to build my GPS tracker/logger.

The GPS is this one (EM-406A SiRF III):
The shield that holds it is this one:
And the SD card shield SPI compatible is this one:

I’ve run the SD card sketch example and everything is fine.
I’ve run the SparkFun GPS example with serial output and also everything fine, cool! :slight_smile:

So the next step is put everything together, so basically I merged the two examples in a single one.
And the same for the pin configuration.

** MOSI - pin 11
** MISO - pin 12
** CLK - pin 13
** CS - pin 4
** The pin 10 set as output (Not sure why, the SD example sketch says that it’s needed)
** Pin 2 and 3 are the GPS RXPIN and TXPIN, the GPS shield is working in DLINE mode.
** Pin 5, 6 and 7 are just led debug pins.

#include <NewSoftSerial.h>
#include <TinyGPS.h>
#include <SD.h>

#define RXPIN 2
#define TXPIN 3

int number = 0;
const int chipSelect = 4;
#define GPSBAUD 4800

TinyGPS gps;
NewSoftSerial uart_gps(RXPIN, TXPIN);

void setup()
  pinMode(10, OUTPUT);
  pinMode(5, OUTPUT); 
  pinMode(6, OUTPUT); 
  pinMode(7, OUTPUT); 
  while (!SD.begin(chipSelect))
    digitalWrite(5, HIGH);
  digitalWrite(5, LOW);

void loop()
      int c =;

void logGpsData(TinyGPS &gps)
  digitalWrite(6, HIGH);
  digitalWrite(6, LOW);

void write()
  File dataFile ="datalog.txt", FILE_WRITE);

  if (dataFile) {
    String dataString = number;


void bling(int cs)
  digitalWrite(cs, HIGH);
  digitalWrite(cs, LOW);

The sketch is not logging the GPS data since it’s a debug project.

The strange thing comes when I try to use both devices at the same time. I can initialize both devices with “SD.begin(chipSelect))” and “uart_gps.begin(GPSBAUD)” BUT when I invoke “File dataFile =“datalog.txt”, FILE_WRITE);” I can’t use “” and viceversa. Seems that something is blocking something! xD

I think I’m stuck in a newbie mistake but I really can’t see it!!! Arrrrrrrr.

Please can somebody help me with this? Thanksssssss!!!


** The pin 10 set as output (Not sure why, the SD example sketch says that it's needed)

Because the default slave select pin is pin 10. That pin must be defined as an output pin, so that it can be set LOW (the default setting for output pins), so that you don't talk to the default (non-existant) slave instead of the intended slave.

BUT when I invoke "File dataFile ="datalog.txt", FILE_WRITE);" I can't use "" and viceversa

This statement implies that you are not allowed to use both in the same sketch, which is rubbish. It would be better if you explained what actually happens when you try to use both in the same sketch.

Hi PaulS!

Thank you!!

1) But I'm not using it for the SD card module, the basic example runs fine without it. Can be a problem with the GPS merged sketch?

2) Ok, what happens is that the file is not available or the gps output data is not a correct one, I can't gps.encode(c) it. What can I do if I can't use both calls in the same sketch?? :fearful:

Thanks again, regards.

But I'm not using it for the SD card module

You're not using what for the SD card module?

What can I do if I can't use both calls in the same sketch?

You should be able to. It may be necessary to enable/disable the SD card. Disable it to read the GPS data, then enable it to log the data, then disable it again.

I mean that I haven't used that pin for anything. There is nothing plugged to it.

Do you know how can I enable/disable the SD module? Are you familiar with it? Maybe releasing the SD object?


How is the SD reader connected to your Arduino? That could be the trouble. What pin on the Arduino does the SD reader "CS" pin connect? That is your "Slave Select" pin for the SD reader.

this is the SD module pin configuration: ** MOSI - pin 11 ** MISO - pin 12 ** CLK - pin 13 ** CS - pin 4

The CS es the pin number 4. And it's pluged from arduino pin number 4 to the sd card.

Do I need something more?

The GPS is on a serial interface, correct? That will not interfere with the SPI interface. And this is an Uno, correct? The Mega2560 SPI pins are different.

Edit: If you have the switch on the shield to dline instead of uart, move the SD "cs" connector to Arduino pin 5. It appears pin 4 is being used by the GPS shield in that mode.

Then use SD.begin(5);

Yes I think the GPS communication is serial. It’s using the library. And the 2 and 3 pins are used as rx and tx.

Yes, it’s an arduino UNO.

That means you have the switch in the uart position?

To avoid a possible data collision, I would move the SD reader “CS” pin to the Arduino pin 5. It appears pin 4 is used by the GPS shield in one of the two modes.

Then use

That makes sense! I'll try it.

I'm using the gps in DLINE mode, with the pins 2 and 3 for data transmission. I think that in uart mode the pins are 0 and 1 but I'm not sure. I know that in uart mode you can't upload the sketch to the board.


My bad. I see the pins are numbered differently on the two schematics. The shield numbers the pins 1-8, and the Uno is 0-7. So you should be right.


** The pin 10 set as output (Not sure why, the SD example sketch says that it's needed)

Normally, the SD reader does use pin 10 (default slave select). But on the ethernet shield, the SD slave select pin was moved to pin 4 because pin 10 is used by the ethernet SPI interface. The pin 10 as an output on the Uno is required by the SPI. The default pin must remain an output for the SPI interface to work.

The comment in the source code (spi.cpp) states the default slave select (SS) must remain an output for the SPI to remain a master.

// Warning: if the SS pin ever becomes a LOW INPUT then SPI // automatically switches to Slave, so the data direction of // the SS pin MUST be kept as OUTPUT.