Author Topic: Moteino Mega connecting with SDcard Reader/Writer?  (Read 752 times)

hexibot43

  • Jr. Member
  • **
  • Posts: 78
Moteino Mega connecting with SDcard Reader/Writer?
« on: June 01, 2019, 06:21:13 PM »
     Currently I have been working on a project with an Arduino Mega that I decided I wanted to do some data logging.  Bought a SD card connection board.  And had issues.  Did some reading to find the Arduino Mega has issues with connecting to SD cards.  I have a spare Moteino Mega swap in.  Are there issues with Moteino Mega and reading SD cards?  Any pitfalls to look out for?  Figured I'd better ask before I start moving all my code over to find problems here too. 

Thanks,

MAB

TomWS

  • Hero Member
  • *****
  • Posts: 1892
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #1 on: June 02, 2019, 09:52:47 PM »
There are issues whenever you try to share an SD card with another SPI device. Some work arounds work most of the time, but what works all of the time is to put the SD card on its own SPI bus with dedicated SCK. I've done this with Moteino Mega without any problems.
Alternative is to not use any other SPI device when you're using the SD card. That is, Init SD, open file, do file io, close file, flush SD, and uninit SD. Then do any other SPI  devices.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5996
  • Country: us
    • LowPowerLab
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #2 on: June 03, 2019, 09:22:34 AM »
Since Moteinos can have a radio module and SPI FLASH-MEM chip, both using SPI with separate SPI_CS pins, why would a third SPI device with another SPI_CS pin not work?
Anything special about SD cards?

hexibot43

  • Jr. Member
  • **
  • Posts: 78
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #3 on: June 03, 2019, 10:30:13 AM »
     Felix,
    I have a MotenioMega I was going to use as my main Main, and it would need to always be listening.  Radio "On"  And of course that would be the same unit I'm using to Log to the SD card.  Now would that be where it would problematic?  Somewhere around here I use to have a chip dedicated to driving a memory card.  I would just pass it data via serial port.  I just can't find it, or remember where I got it. Ahhh.  Still looking.  Thanks.

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5996
  • Country: us
    • LowPowerLab
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #4 on: June 03, 2019, 10:36:56 AM »
Now would that be where it would problematic?
No, I don't know of any problems.
Maybe Tom can give some details how an SD used with native SPI would be problematic.
BTW - MoteinoMEGA uses the Atmega1284p AVR processor and is not the same as Arduino MEGA.

TomWS

  • Hero Member
  • *****
  • Posts: 1892
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #5 on: June 03, 2019, 05:03:59 PM »
@Felix, SD card protocol is not simple SPI sequence. There are modes where clk and sdi are meaningful WITHOUT CS.  Mixed SPI transactions can mess up SD configuration.  There is at least one thread on this forum discussing it from a couple of years ago.  I'm traveling so can't dig it up easily.

hexibot43

  • Jr. Member
  • **
  • Posts: 78
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #6 on: June 03, 2019, 10:03:17 PM »
Well, it sounds like at least have a chance.  I'm already realizing I'm enjoying the smaller package size.  And the radios instead of cabling is so much nicer.  Thanks everyone.

chribru

  • Newbie
  • *
  • Posts: 2
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #7 on: August 12, 2019, 04:51:30 AM »
@hexibot, did you have any luck with your SD card project?
I've been trying to initialize SD card with the SD card adapter from lowpower lab and a Moteino Mega with no luck. I was able to configure the SPI pins as they are not standard Arduino Mega pins using this code but no luck. This code works on a Teensy with on board SD card so it's not the card itself.

Code: [Select]
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_CS  0
#define SD_PWR 1
#define FLASH_CS 23
#define RF_CS 4

#define SPI_MOSI 5
#define SPI_MISO 6
#define SPI_CLK  7

void setup()
{
  //************ SD Card  *************
  pinMode(SS, OUTPUT);
  pinMode(SD_PWR,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  pinMode(FLASH_CS,OUTPUT);
  pinMode(RF_CS,OUTPUT);
 
  digitalWrite(SD_PWR,LOW);   //turn power to SD module on
  digitalWrite(FLASH_CS,LOW); //pull CS line for on boards SPI flash low
  digitalWrite(RF_CS,LOW);    //pull CS line for on board RF low
  digitalWrite(SD_CS,HIGH);   //pull CS line for SD card module high
 
  Serial.begin(9600);
  delay(1000);

  if (SD.begin(SD_CS,SPI_MOSI,SPI_MISO,SPI_CLK)) {
      Serial.println("SD CARD initialised.....");         
  }
  Serial.println("SD CARD failed.....");
  delay(2000);
}
 
void loop() {
}


Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5996
  • Country: us
    • LowPowerLab
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #8 on: August 12, 2019, 09:41:28 AM »
Did you try to use the hardware SPI instead?
Do you power the SD adapter from 3.3V of the MoteinoMEGA?

chribru

  • Newbie
  • *
  • Posts: 2
Re: Motenio Mega connecting with SDcard Reader/Writer?
« Reply #9 on: August 13, 2019, 10:07:14 PM »
I'm not sure what you mean. I'm using the hardware SPI using pin 5-MOSI, 6-MISO and 7-SCK. I got it to work however. Looked at the datasheet of ATmega1284P and saw that the pin 4 on the MotMega is going to SS pin (PB4) which is active low. It's also the CS line for the on board RF but needs to be set HIGH (to make it LOW) if you want to talk to other SPI slaves. Below the working test code.


Code: [Select]
#include <Arduino.h>
#include <SPI.h>
#include <SD.h>

#define SD_CS  0
#define SD_PWR 1
#define FLASH_CS 23
#define RF_CS 4

void setup()
{
  //************ SD Card  *************
  pinMode(SD_PWR,OUTPUT);
  pinMode(SD_CS,OUTPUT);
  pinMode(FLASH_CS,OUTPUT);
  pinMode(RF_CS,OUTPUT); //setup this pin as output also sets the uC to be SPI master
 
  digitalWrite(SD_PWR,LOW);   //turn power to SD module on
  digitalWrite(FLASH_CS,LOW); //pull CS line for on boards SPI flash low
  digitalWrite(RF_CS,HIGH);   //active LOW therefore pull CS line for on board RF HIGH
 
  Serial.begin(9600);
  delay(1000);

  if (SD.begin(SD_CS)) { 
      Serial.println("SD CARD initialised.....");
  }
  else{
    Serial.println("SD CARD failed.....");
  }
}
 
void loop() {
}
« Last Edit: August 13, 2019, 10:12:08 PM by chribru »

Felix

  • Administrator
  • Hero Member
  • *****
  • Posts: 5996
  • Country: us
    • LowPowerLab
Re: Moteino Mega connecting with SDcard Reader/Writer?
« Reply #10 on: August 14, 2019, 09:24:00 AM »
Great that you got it working.
Note that to cut power off to the SD card completely, you will likely need to set the MOSI pin (possibly SCK and MISO too) to INPUT, then calling your sleep code, then back to OUTPUT, something like this (in addition to setting the SD_PWR to HIGH):

Code: [Select]
  pinMode(MOSI, INPUT); //ADD THIS BEFORE SLEEPING
  LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);   // everything is eleeping, just a few uA
  pinMode(MOSI, OUTPUT); //ADD THIS AFTER SLEEPING

Otherwise depending on the SD card, the MOSI pin might drain a few hundred uA.