Pages: [1]   Go Down
Author Topic: Wiring needed for Arduino Mega 2560 Rev3 and Arduino Ethernet/microSD Shield  (Read 1249 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 2
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Rather than continue beating myself up over an issue I have with a Mega/Ethernet/microSD combo I figured I
would first eliminate the correct setup.

Does the stock Arduino Mega 2560 R3 and Arduino Ethernet/microSD Shield require ANY special wiring?  I assumed it does not.

I understand there are code considerations when sharing the SPI pins.  I'm currently using 1.0.4 of the IDE
and sdFAT, and Ethernet libraries.

I've tried a few of the examples after running into what appeared to be SD card corruption on the device.
The problem was intermittent. 

I've made sure to use:

Code:
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);

at the beginning during setup(). 

If the board sits in the OFF state for a while, next time I turn it on it appears as though the Card is fine again.
Switching cards when the problem occurs doesn't solve the problem.  Resetting doesn't appear to solve the problem.

I've used examples like SDInfo when the problem occurs and upload, the same error (from memory it's FF 01 I believe).
I think this is SPI SS related.  The Ethernet never appears to be affected, only the SD card.

If anyone has suggestions, like a sample program (example) that used both Ethernet AND the microSD card simultaneously.
That way I can rule out something Hardware or Power related.  I suspect I'm simply missing something REALLY simple, there
is a problem with this board's RESET/RESTART/bootloader or a power issue (heat related?).

I'll post more information if I can in a bit, away from the project right now.

OH BTW, hello everyone (first post lol).

Chris
rocketman49


« Last Edit: April 29, 2013, 02:23:28 pm by rocketman49 » Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2302
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the stock Arduino Mega 2560 R3 and Arduino Ethernet/microSD Shield require ANY special wiring?  I assumed it does not.
I've made sure to use:
Code:
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);

at the beginning during setup(). 

If anyone has suggestions, like a sample program (example) that used both Ethernet AND the microSD card simultaneously.

You can be sure no extra wiring is needed and your problems are all in software. The Ethernet and SD operations are never simultaneous but can be in sequence in the same loop cycle to the point where they look simultaneous.

Your problem could be in this vein, i.e. the sequence of commands, and easily fixed.

Here is come code, typical of datalogging - read, display, record on SD, and send to internet. Note that the SD operation is tightly confined and is to exclusion of all else. Note too that the code you mention

Code:
pinMode(53, OUTPUT);
digitalWrite(53, HIGH);

is not used. I can't recommend this, even though I get away with it. I guess the reason why I do is simply because of the rigorous confinement of the SD operations. Having said that, the pin 53 HIGH/LOW sequence is a likely place to look.

Code:
/*
//  This Arduino sketch reads DS18B20 "1-Wire" digital
 //  temperature sensors.
 //  Copyright (c) 2010 Mark McComb, hacktronics LLC
 //  License: http://www.opensource.org/licenses/mit-license.php (Go crazy)
 //  Tutorial:
 //  http://www.hacktronics.com/Tutorials/arduino-1-wire-tutorial.html
 //  code uses Arduino LCD stuff, for shield on Freetronics EtherTen.
 //  Serial print commands are for PLX-DAQ
 
 From cosm library example and lifts from a lot of others
 particularly from Stanley in Kuala Lumpur.
 Use your own DS18B20 addresses, keys etc.
 Note that the feed id is in line 42, or somewhere nearby...
 */
#include <OneWire.h>
#include <DallasTemperature.h>
#include <SPI.h>                 
#include <Ethernet.h>            // Ethernet
#include <HttpClient.h>          // Cosm lib
#include <Cosm.h>                // Cosm lib
#include <PCD8544.h>             // Nokia 5110
#include <SD.h>                  // SD card
#include <string.h>              // from Date As Filename
#include "RTClib.h"              // from Date As Filename
#include "Wire.h"                // MUST HAVE lib for LCD disp, SD card, and serial

#define DS1307_ADDRESS 0x68

RTC_DS1307 RTC;
static PCD8544 lcd;

File myFile;
char filename[] = "00000000.CSV";

// Custom symbols
static const byte DEGREES_CHAR = 1;
static const byte degrees_glyph[] = { 0x00, 0x07, 0x05, 0x07, 0x00 };
static const byte SLASH_CHAR = 2;
static const byte slash_glyph[] = {0x00,0x20,0x10,0x08};

byte InThermo[8] =  {
  0x28, 0x69, 0xC2, 0xB0, 0x03, 0x00, 0x00, 0X9F};
byte OutThermo[8] = {
  0x28, 0x7A, 0x8B, 0xC0, 0x03, 0x00, 0x00, 0x2F};
byte DrainThermo[8] = {
  0x28, 0x09, 0xA9, 0xC0, 0x03, 0x00, 0x00, 0x95};

#define ONE_WIRE_BUS 3
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);

byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };

char cosmKey[] = "l6absmJtIHkul48Y7zXwmpNQaJuSAKw3cFAxZUt1U0t0Zz0g";

int  second, minute, hour, weekDay, monthDay, month, year;

int k=0;
float InTemp, OutTemp, DrainTemp;   

// Define the strings for our datastream IDs
char sensorId0[] = "InThermo";
char sensorId1[] = "OutThermo";
char sensorId2[] = "DrainThermo";
char calcId1[] = "diff";

const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
CosmDatastream datastreams[] = {
  CosmDatastream(sensorId0, strlen(sensorId0), DATASTREAM_FLOAT),
  CosmDatastream(sensorId1, strlen(sensorId1), DATASTREAM_FLOAT),
  CosmDatastream(sensorId2, strlen(sensorId2), DATASTREAM_FLOAT),
  CosmDatastream(calcId1, strlen(calcId1), DATASTREAM_FLOAT),
};
// Finally, wrap the datastreams into a feed
CosmFeed feed(83153, datastreams, 4);    //put your number of feeds here

EthernetClient client;
CosmClient cosmclient(client);

void setup() {
   lcd.begin(84, 48);
     // Register the custom symbols...
  lcd.createChar(DEGREES_CHAR, degrees_glyph);
  lcd.createChar(SLASH_CHAR, slash_glyph);
  Wire.begin();
  Serial.begin(9600);
  Serial.print("    filename   ");
  delay(300);//Wait for newly restarted system to stabilize
  lcd.setCursor (0,0);
  lcd.print("Initializing");
  delay(2000);
  lcd.setCursor (0,1);

  pinMode(10, OUTPUT);

  if (!SD.begin(4))
  {
    lcd.print("failed!");
    delay (2000);
    return;
  }
  lcd.print("init. OK!");
  delay(2000);
      getFileName();
      Serial.println(filename);
  lcd.clear();

  Serial.println("LABEL,Time,InTemp,OutTemp,diff,DrainTemp");

  sensors.setResolution(InThermo, 12);
  sensors.setResolution(OutThermo, 12);
  sensors.setResolution(DrainThermo, 12);

  Serial.println("Starting multiple datastream upload to Cosm...");
  Serial.println();

  while (Ethernet.begin(mac) != 1)
  {
    Serial.println("Error getting IP address via DHCP, trying again...");
    delay(10000);
  }
}

void loop() {
  running();
  GetClock();
  if (hour == 0 && minute == 0 && second <2)
  {
    getFileName();
  }
 Serial.print("DATA,TIME,       ");
  int ret=0;
  //get the values from the DS8B20's
  sensors.requestTemperatures();

  float InTemp = (sensorValue(InThermo));
  float OutTemp = (sensorValue(OutThermo)); 
  float DrainTemp = (sensorValue(DrainThermo));

  float diff = OutTemp - InTemp;

  datastreams[0].setFloat(InTemp);
  datastreams[1].setFloat(OutTemp);
  datastreams[2].setFloat(DrainTemp);
  datastreams[3].setFloat(diff);
  Serial.print(InTemp);
  Serial.print(" ,  ");
  Serial.print(OutTemp);
  Serial.print(" ,  ");
  Serial.print(DrainTemp);
  Serial.println(" ,  ");

  lcd.setCursor(49,0);
  lcd.print(InTemp);
  lcd.setCursor(49,1);
  lcd.print (OutTemp);
  lcd.setCursor(49,2);
  lcd.print(DrainTemp);

  k=k+1; 

  if (k>9 )
  {   
      ret = cosmclient.put(feed, cosmKey);    // SEND FEED TO COSM
     
      myFile = SD.open(filename, FILE_WRITE);//<<<<<<<<<<<<< OPEN
  myFile.print(hour);
  myFile.print(":");
  myFile.print(minute);
  myFile.print(":");
  myFile.print(second);
  myFile.print(",");

  myFile.print(InTemp);
  myFile.print(",");
  myFile.print(OutTemp);
  myFile.print(",");
  myFile.print(DrainTemp);
  myFile.print(",");
  myFile.println();
       myFile.close();//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>CLOSE
       
      k=0;
  }
  delay(850);
}  // loop ends here

//sensorValue function
float sensorValue (byte deviceAddress[])
{
  float tempC = sensors.getTempC (deviceAddress);
  return tempC;
}

byte bcdToDec(byte val)  {
  // Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void GetClock(){
  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();
  Wire.requestFrom(DS1307_ADDRESS, 7);

  second = bcdToDec(Wire.read());
  minute = bcdToDec(Wire.read());
  hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  monthDay = bcdToDec(Wire.read());
  month = bcdToDec(Wire.read());
  year = bcdToDec(Wire.read());
}

void getFileName(){

  DateTime now = RTC.now();

  filename[0] = (now.year()/1000)%10 + '0'; //To get 1st digit from year()
  filename[1] = (now.year()/100)%10 + '0'; //To get 2nd digit from year()
  filename[2] = (now.year()/10)%10 + '0'; //To get 3rd digit from year()
  filename[3] = now.year()%10 + '0'; //To get 4th digit from year()
  filename[4] = now.month()/10 + '0'; //To get 1st digit from month()
  filename[5] = now.month()%10 + '0'; //To get 2nd digit from month()
  filename[6] = now.day()/10 + '0'; //To get 1st digit from day()
  filename[7] = now.day()%10 + '0'; //To get 2nd digit from day()
}

void running(){
  lcd.setCursor(0,0);
  lcd.print("In");
  lcd.setCursor(31,0);
  lcd.print("\001C ");
  lcd.setCursor(0,1);
  lcd.print("Out");
  lcd.setCursor(31,1);
  lcd.print("\001C ");
  lcd.setCursor(0,2);
  lcd.print("Drain");
  lcd.setCursor(31,2);
  lcd.print("\001C ");
  lcd.setCursor(0,3);
  lcd.print("F");
  lcd.setCursor(5,3);
  lcd.print("l");
  lcd.setCursor(10,3);
  lcd.print("ow");
  lcd.setCursor(24,3);
  lcd.print("l");
  lcd.setCursor(28,3);
  lcd.print("\002");
  lcd.print("m");
  lcd.setCursor(39,3);
  lcd.print("i"); 
  lcd.setCursor(44,3);
  lcd.print("n");
  lcd.setCursor(14,4);
  lcd.print("Conv %");
  lcd.setCursor(21,5);
  lcd.print("kW");

}
Logged

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

This setup should start both the SD and W5100.
Code:
void setup() {
  Serial.begin(9600);

  // disable W5100 while starting SD
  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);

  Serial.print(F("Starting SD..."));
  if(!SD.begin(4)) Serial.println(F("failed"));
  else Serial.println(F("ok"));

  Serial.print(F("Starting W5100..."));
  Ethernet.begin(mac,ip);
  Serial.println(Ethernet.localIP());
}
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 48
Posts: 2302
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This setup should start both the SD and W5100.
Code:
void setup() {......

  pinMode(10,OUTPUT);
  digitalWrite(10,HIGH);


Are we talking Mega here?

There have been a couple of related threads of late and I have wondered if I have  been working in a fools' paradise. I always have used


  pinMode(10,OUTPUT);

When I was obliged to move from Uno to mega I simply carried this code over and everything worked so I didn't question it. I now recognise that pin 10 is no longer on the SPI and assume it isn't a good idea, even though my Ethernet and SD operations are all OK.

http://arduino.cc/forum/index.php/topic,162167.0.html           amongst others
Logged

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

Thanks for the responses, just got a chance to look back at my post.

I was under the impression that it was necessary to keep Pin53 as an OUPUT otherwise it causes issues with SPI?

I'll see if the code gives me any issues, if it doesn't I'll have to start looking deeper into my code.

I noticed this is ONLY a problem with the microSD card.init which fails (FF/01 from SDinfo I think)
...suspecting that Pin 4 is likely High when the error occurs, but I took out a logic analyzer and pin 4 appears to be
low when making the calls.  Ethernet never seems to have any issues.

When looking through the Ethernet Library I noticed that Slave Select for the Ethernet is LOW, then
reset to HIGH whenever there is anything going on with the W5100. I mean LOW, do ethernet stuff, then
reset to HIGH.  So I'm confused how the two can get mixed up.

Thanks again,
Chris
« Last Edit: April 30, 2013, 12:17:44 am by rocketman49 » Logged

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

Quote
Are we talking Mega here?

There have been a couple of related threads of late and I have wondered if I have  been working in a fools' paradise. I always have used


  pinMode(10,OUTPUT);

When I was obliged to move from Uno to mega I simply carried this code over and everything worked so I didn't question it. I now recognise that pin 10 is no longer on the SPI and assume it isn't a good idea, even though my Ethernet and SD operations are all OK.
It doesn't matter if a Mega, Uno, Leonardo. The ethernet shield W5100 slave select is D10. The default slave select for the Mega is D53 and the Uno is D10, but that does not change the W5100 slave select. Still D10. My code is NOT setting the SPI to MASTER mode, it is disabling the W5100 SPI so it won't trash up the SPI bus when initializing the SD card.

Quote
I noticed this is ONLY a problem with the microSD card.init which fails (FF/01 from SDinfo I think)
...suspecting that Pin 4 is likely High when the error occurs, but I took out a logic analyzer and pin 4 appears to be
low when making the calls.  Ethernet never seems to have any issues.
The W5100 does have issues if the SD is not disabled or initialized, but sometimes they won't manifest themselves right away. Normally you will notice error on UDP more frequently than TCP, but both suffer from SPI corruption if not initialized correctly. You will see a lot of my posts that refer to this. Like this one:
http://arduino.cc/forum/index.php/topic,162799.msg1220013.html#msg1220013
edit: The code in the link above works fine with the SD card in the slot if you add this to setup().
Code:
void setup() {
  Serial.begin(9600);

  // disable SD SPI
  pinMode(4,OUTPUT);
  digitalWrite(4,HIGH);

  // then everything else
}
« Last Edit: April 30, 2013, 07:31:38 am by SurferTim » Logged

Pages: [1]   Go Up
Jump to: