Pages: [1]   Go Down
Author Topic: GPS(EM-406A) + SPI SD card module  (Read 1591 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi all!

I'm an arduino newbie smiley-razz and I'm trying to build my GPS tracker/logger.

The GPS is this one (EM-406A SiRF III): http://www.sparkfun.com/products/465
The shield that holds it is this one: http://www.sparkfun.com/products/10710
And the SD card shield SPI compatible is this one: http://www.aliexpress.com/fm-store/300680/211218687-492983592/SD001-SD-card-modules-LC-STUDIO-Arduino-SD-card-reader-module-for-read-and-write.html

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! smiley

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.

Code:
#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);
    delay(1000);
  }
  digitalWrite(5, LOW);
 
  uart_gps.begin(GPSBAUD);
}

void loop()
{
  if(uart_gps.available())
  {
      int c = uart_gps.read();
      if(gps.encode(c))
      {
        bling(5);
        logGpsData(gps);
        bling(5);
      }
  }
}

void logGpsData(TinyGPS &gps)
{
  digitalWrite(6, HIGH);
  delay(500);
 
  write();
 
  digitalWrite(6, LOW);
  delay(500);
}

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

  if (dataFile) {
    bling(7);   
   
    number++;
    String dataString = number;
    dataFile.println(dataString);
    dataFile.close();

    bling(7);
  } 
}

void bling(int cs)
{
  digitalWrite(cs, HIGH);
  delay(250);
  digitalWrite(cs, LOW);
  delay(250);
}

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 = SD.open("datalog.txt", FILE_WRITE);" I can't use "uart_gps.read()" 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!!!!

Regards!!!!
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
** 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.

Quote
BUT when I invoke "File dataFile = SD.open("datalog.txt", FILE_WRITE);" I can't use "uart_gps.read()" 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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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??  smiley-eek-blue

Thanks again, regards.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
But I'm not using it for the SD card module
You're not using what for the SD card module?

Quote
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.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?

Thanks!
« Last Edit: October 31, 2011, 05:36:37 am by mrcatacroquer » Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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?
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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);
« Last Edit: October 31, 2011, 06:24:58 am by SurferTim » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Yes, it's an arduino UNO.
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
SD.begin(5);
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 6
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Thanks!!
Logged

Miramar Beach, Florida
Offline Offline
Faraday Member
**
Karma: 140
Posts: 5873
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Add:
Quote
** 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.
Quote
  // 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.
« Last Edit: October 31, 2011, 08:38:30 am by SurferTim » Logged

Pages: [1]   Go Up
Jump to: