Author Topic: MoteinoMEGA and SD card  (Read 7083 times)

syrinxtech

  • Sr. Member
  • ****
  • Posts: 347
  • Country: us
    • Syrinx Technologies
Re: MoteinoMEGA and SD card
« Reply #15 on: February 27, 2017, 09:49:54 PM »
Hi,

I'm trying to get SD card (SPI) working with MoteinoMEGA paired with RFM69HW (915Mhz).
My SD card's CS pin is connected to D3 on MoteinoMEGA. Using standard unmodified libraries for both RFM69 and SD.

For some reason I cannot get both(SDCard and RFM69HW) to work together.
Radio fails to initialize when SD Card adapter is connected to MoteinoMEGA even if I comment out SD-related code.

Is there anything specific that I need to do in the code in order to make both play nicely with each other?
Anything specific on the hardware side?

Thanks in advance!

Code: [Select]
#include <RFM69.h>
#include <RFM69_ATC.h>
#include <SD.h>

#define LED                   15 // LED is on D15
#define chipSelect_SD   3

#define NODEID        1    //unique for each node on same network
#define NETWORKID     100  //the same on all nodes that talk to each other
//Match frequency to the hardware version of the radio on your Moteino (uncomment one):
#define FREQUENCY     RF69_915MHZ
#define ENCRYPTKEY    "sampleEncryptKey" //exactly the same 16 characters/bytes on all nodes!
#define IS_RFM69HW    //uncomment only for RFM69HW! Leave out if you have RFM69W!
#define ENABLE_ATC    //comment out this line to disable AUTO TRANSMISSION CONTROL
//#define PROMISCOUS_MODE  //uncomment to sniff all packets on the same network

#ifdef ENABLE_ATC
  RFM69_ATC radio;
#else
  RFM69 radio;
#endif

void setup()
{
  //************ SD Card  *************
  pinMode(SS, OUTPUT);
  if (SD.begin(chipSelect_SD)) {

      // Blink 4 times to indicate SD card successful init
      Blink(LED, 200, 4);     
  }
  delay(2000);

  //************ RFM69HW  *************
  delay(10);
  if (radio.initialize(FREQUENCY, NODEID, NETWORKID)) {

    #ifdef IS_RFM69HW
      radio.setHighPower();
    #endif
    radio.encrypt(ENCRYPTKEY);
    #ifdef PROMISCOUS_MODE
      radio.promiscuous(true);
    #else
      radio.promiscuous(false);
    #endif
    //radio.setFrequency(919000000); //set frequency to some custom frequency
 
    //Auto Transmission Control - dials down transmit power to save battery (-100 is the noise floor, -90 is still pretty good)
    //For indoor nodes that are pretty static and at pretty stable temperatures (like a MotionMote) -90dBm is quite safe
    //For more variable nodes that can expect to move or experience larger temp drifts a lower margin like -70 to -80 would probably be better
    //Always test your ATC mote in the edge cases in your own environment to ensure ATC will perform as you expect
    #ifdef ENABLE_ATC
      radio.enableAutoPower(-70);
    #endif

    // Blink 6 times to indicate radio successful init
    Blink(LED, 200, 6); 
  }

  delay(2000);
 
  // Blink to indicate end of Setup
  Blink(LED, 300, 3);
}

void loop()
{
}

void Blink(byte PIN, int DELAY_MS, byte loops)
{
  pinMode(PIN, OUTPUT);
  while (loops--)
  {
    digitalWrite(PIN,HIGH);
    delay(DELAY_MS);
    digitalWrite(PIN,LOW);
    delay(DELAY_MS); 
  }
}


fmark, I am doing exactly what you're doing with a 220 Ohm resistor between the SD card MISO pin and the Mega.

TomWS

  • Hero Member
  • *****
  • Posts: 1930
Re: MoteinoMEGA and SD card
« Reply #16 on: February 27, 2017, 11:27:20 PM »
Do you have a pullup on the SD CS pin?  If not, then, at least, you need to initialize the SD CS pin to HIGH AND OUTPUT, even if the SD begin code is not called.   And if SD begin code is called, then you need to ensure that you UNSELECT the SD card before radio.initialize().  Also, setting SS pin to OUTPUT is not enough, you should set it HIGH and probably call SPI.begin() prior to calling SD.begin.

In my experience, getting the two to work peacefully together isn't trivial.  In the worst case, I've had to make all SD card accesses atomic (no other SPI transactions within a file open, seek, read/write, file close cycle).


perky

  • Hero Member
  • *****
  • Posts: 873
  • Country: gb
Re: MoteinoMEGA and SD card
« Reply #17 on: February 28, 2017, 06:42:25 AM »
fmark, I am doing exactly what you're doing with a 220 Ohm resistor between the SD card MISO pin and the Mega.

Yep, some SD card refuse to release MISO when de-selected, that resistor allows other devices to overdrive MISO. I've seen that behaviour before and the series resistor fixed it.

Mark.

syrinxtech

  • Sr. Member
  • ****
  • Posts: 347
  • Country: us
    • Syrinx Technologies
Re: MoteinoMEGA and SD card
« Reply #18 on: February 28, 2017, 02:04:19 PM »
As soon as I get a little free time I'm going to post some findings that my partner and I have made after spending 4-5 days working with both regular Moteino's and Megas along with SD cards (2 different cards) and various other accessories (GPS, LCD, OLED, RTC, etc).  Turns out in our case we didn't need to add ANY special code and it works just great as long as you add the 220 Ohm resistor as I mentioned before.

I must add that we are opening the file, writing the log entry and closing the file in one atomic function, but otherwise, no special pinMode, digitalWrites or anything else was required.  The only problem we had with the regular Moteino is that once you've added the radio, RTC and the SD card you only have about 7K left which doesn't leave much for the sensor that is supposedly going to produce the stats that you're logging. 

It's funny....the ruggedness of the 328P chip.....there were many time when I ran code with 10-15 FREE bytes left without a problem.  The lowest I saw was 2 free bytes.