
Arduino Mega’s and associated peripherals
During the recent shut down I decided to get into a few Arduino projects related to solar energy using the Arduino Mega to measure data, collect it and carry out necessary actions from these measurements. For example relay switches, servo motors and stepper motors all of these are requirements for solar energy measurement and control systems such as solar trackers, PV battery chargers and output of your solar system. The Arduino Mega 2560 rev 3 whilst not as popular as its smaller relations within the Arduino family has the largest number of pins with 16 analogue inputs and 54 digital pins which are used as either inputs or outputs. The problem is the standard Arduino libraries are mostly tailored towards the Arduino Uno so many of the supplied programs require adaption for successful operation. A good starting example of this is the production of your own data acquisition system which will require an SD card and an RTC module to enable measurements to be recorded with a time step so that trends in your own system can be identified.
Starting with the SD card if the standard Arduino program is used without a minor adaption it will not work on the Arduino Mega. Typically when you want to integrate an SD card shield with an Arduino you got to the built in example in the SD library called “card info”.
The standard version is:
/*
SD card test
This example shows how use the utility libraries on which the’
SD library is based in order to get info about your SD card.
Very useful for testing a card when you’re not sure whether its working or not.
The circuit:
SD card attached to SPI bus as follows:
** MOSI – pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO – pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK – pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS – depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples
created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
// Adafruit SD shields and modules: pin 10
// Sparkfun SD shield: pin 8
// MKRZero SD: SDCARD_SS_PIN
const int chipSelect = 4;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“\nInitializing SD card…”);
// we’ll use the initialization code from the utility libraries
// since we’re just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println(“initialization failed. Things to check:”);
Serial.println(“* is a card inserted?”);
Serial.println(“* is your wiring correct?”);
Serial.println(“* did you change the chipSelect pin to match your shield or module?”);
while (1);
} else {
Serial.println(“Wiring is correct and a card is present.”);
}
// print the type of card
Serial.println();
Serial.print(“Card type: “);
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println(“SD1”);
break;
case SD_CARD_TYPE_SD2:
Serial.println(“SD2”);
break;
case SD_CARD_TYPE_SDHC:
Serial.println(“SDHC”);
break;
default:
Serial.println(“Unknown”);
}
// Now we will try to open the ‘volume’/’partition’ – it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println(“Could not find FAT16/FAT32 partition.\nMake sure you’ve formatted the card”);
while (1);
}
Serial.print(“Clusters: “);
Serial.println(volume.clusterCount());
Serial.print(“Blocks x Cluster: “);
Serial.println(volume.blocksPerCluster());
Serial.print(“Total Blocks: “);
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print(“Volume type is: FAT”);
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we’ll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print(“Volume size (Kb): “);
Serial.println(volumesize);
Serial.print(“Volume size (Mb): “);
volumesize /= 1024;
Serial.println(volumesize);
Serial.print(“Volume size (Gb): “);
Serial.println((float)volumesize / 1024.0);
Serial.println(“\nFiles found on the card (name, date and size in bytes): “);
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
Now if you upload the above code to a Mega it won’t work without a minor modification the line of code:
const int chipSelect = 4;
has to be modified to:
const int chipSelect = 53; //// Changed to 53 for Mega
Also note that the line I’ve modified I’ve added a comment to remind myself what has/hasn’t been changed, this is a typical example of good coding practice, as one it shows anyone else who uses the program what the modification is and number 2 if your code becomes damaged or you make a mistake its easy to see where you have made changes.
At the start of the card info program the wiring configuration for the Uno not the Mega is described:
SD card attached to SPI bus as follows:
** MOSI – pin 11 on Arduino Uno/Duemilanove/Diecimila
** MISO – pin 12 on Arduino Uno/Duemilanove/Diecimila
** CLK – pin 13 on Arduino Uno/Duemilanove/Diecimila
** CS – depends on your SD card shield or module.
Pin 4 used here for consistency with other Arduino examples.
I modified this for the Mega as follows:
The circuit:
SD card attached to SPI bus as follows:
** MOSI – pin 51 on Arduino Mega
** MISO – pin 50 on Arduino Mega
** CLK – pin 52 on Arduino Mega
** CS – pin 53 on Arduino Mega
So my modified Mega card info sketch is as follows:
SD card test
This example shows how use the utility libraries on which the’
SD library is based in order to get info about your SD card.
Very useful for testing a card when you’re not sure whether its working or not.
The circuit:
SD card attached to SPI bus as follows:
** MOSI – pin 51 on Arduino Mega
** MISO – pin 50 on Arduino Mega
** CLK – pin 52 on Arduino Mega
** CS – pin 53 n Arduino Mega
created 28 Mar 2011
by Limor Fried
modified 9 Apr 2012
by Tom Igoe
modified May 2020 by David Redpath
*/
// include the SD library:
#include <SPI.h>
#include <SD.h>
// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;
// change this to match your SD shield or module;
// Arduino Ethernet shield: pin 4
const int chipSelect = 53; //// Changed to 53 for Mega
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
Serial.print(“\nInitializing SD card…”);
// we’ll use the initialization code from the utility libraries
// since we’re just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println(“initialization failed. Things to check:”);
Serial.println(“* is a card inserted?”);
Serial.println(“* is your wiring correct?”);
Serial.println(“* did you change the chipSelect pin to match your shield or module?”);
while (1);
} else {
Serial.println(“Wiring is correct and a card is present.”);
}
// print the type of card
Serial.println();
Serial.print(“Card type: “);
switch (card.type()) {
case SD_CARD_TYPE_SD1:
Serial.println(“SD1”);
break;
case SD_CARD_TYPE_SD2:
Serial.println(“SD2”);
break;
case SD_CARD_TYPE_SDHC:
Serial.println(“SDHC”);
break;
default:
Serial.println(“Unknown”);
}
// Now we will try to open the ‘volume’/’partition’ – it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println(“Could not find FAT16/FAT32 partition.\nMake sure you’ve formatted the card”);
while (1);
}
Serial.print(“Clusters: “);
Serial.println(volume.clusterCount());
Serial.print(“Blocks x Cluster: “);
Serial.println(volume.blocksPerCluster());
Serial.print(“Total Blocks: “);
Serial.println(volume.blocksPerCluster() * volume.clusterCount());
Serial.println();
// print the type and size of the first FAT-type volume
uint32_t volumesize;
Serial.print(“Volume type is: FAT”);
Serial.println(volume.fatType(), DEC);
volumesize = volume.blocksPerCluster(); // clusters are collections of blocks
volumesize *= volume.clusterCount(); // we’ll have a lot of clusters
volumesize /= 2; // SD card blocks are always 512 bytes (2 blocks are 1KB)
Serial.print(“Volume size (Kb): “);
Serial.println(volumesize);
Serial.print(“Volume size (Mb): “);
volumesize /= 1024;
Serial.println(volumesize);
Serial.print(“Volume size (Gb): “);
Serial.println((float)volumesize / 1024.0);
Serial.println(“\nFiles found on the card (name, date and size in bytes): “);
root.openRoot(volume);
// list all files in the card with date and size
root.ls(LS_R | LS_DATE | LS_SIZE);
}
void loop(void) {
}
So when I upload this to my Mega after wiring it up to the correct ports and open my serial monitor at 9600 baud I got the following readout:
19:41:39.333 -> Initializing SD card…Wiring is correct and a card is present.
19:41:39.504 ->
19:41:39.504 -> Card type: SDHC
19:41:39.538 -> Clusters: 485936
19:41:39.572 -> Blocks x Cluster: 64
19:41:39.606 -> Total Blocks: 31099904
19:41:39.640 ->
19:41:39.640 -> Volume type is: FAT32
19:41:39.640 -> Volume size (Kb): 15549952
19:41:39.674 -> Volume size (Mb): 15185
19:41:39.708 -> Volume size (Gb): 14.83
19:41:39.743 ->
19:41:39.743 -> Files found on the card (name, date and size in bytes):
19:41:39.812 -> LOGGER00.CSV 2000-01-01 01:00:00 1242
19:41:39.846 -> LOGGER01.CSV 2000-01-01 01:00:00 26475
19:41:39.879 -> LOGGER02.CSV 2000-01-01 01:00:00 244918
19:41:39.914 -> LOGGER03.CSV 2000-01-01 01:00:00 1691
19:41:39.983 -> LOGGER04.CSV 2000-01-01 01:00:00 97930
19:41:40.017 -> LOGGER05.CSV 2000-01-01 01:00:00 4116
19:41:40.051 -> LOGGER06.CSV 2000-01-01 01:00:00 661
19:41:40.086 -> LOGGER07.CSV 2000-01-01 01:00:00 3612
19:41:40.121 -> LOGGER08.CSV 2000-01-01 01:00:00 2289
19:41:40.189 -> LOGGER09.CSV 2000-01-01 01:00:00 289
19:41:40.223 -> LOGGER10.CSV 2000-01-01 01:00:00 61647
19:41:40.258 -> LOGGER11.CSV 2000-01-01 01:00:00 73936
19:41:40.292 -> LOGGER12.CSV 2000-01-01 01:00:00 5364945
19:41:40.360 -> LOGGER13.CSV 2000-01-01 01:00:00 91128
19:41:40.395 -> LOGGER14.CSV 2000-01-01 01:00:00 9704
19:41:40.429 -> LOGGER15.CSV 2000-01-01 01:00:00 4927
19:41:40.464 -> LOGGER16.CSV 2000-01-01 01:00:00 9064
19:41:40.533 -> LOGGER17.CSV 2000-01-01 01:00:00 7527
19:41:40.567 -> LOGGER18.CSV 2000-01-01 01:00:00 8544
19:41:40.601 -> LOGGER19.CSV 2000-01-01 01:00:00 217470
19:41:40.635 -> LOGGER20.CSV 2000-01-01 01:00:00 11530243
19:41:40.704 -> LOGGER21.CSV 2000-01-01 01:00:00 15258
19:41:40.738 -> LOGGER22.CSV 2000-01-01 01:00:00 5376
19:41:40.772 -> LOGGER23.CSV 2000-01-01 01:00:00 10588
19:41:40.807 -> LOGGER24.CSV 2000-01-01 01:00:00 22052
19:41:40.842 -> LOGGER25.CSV 2000-01-01 01:00:00 2995766
19:41:40.910 -> LOGGER26.CSV 2000-01-01 01:00:00 5994493
19:41:40.944 -> LOGGER27.CSV 2000-01-01 01:00:00 576754
19:41:40.978 -> LOGGER28.CSV 2000-01-01 01:00:00 7139040
19:41:41.047 -> LOGGER29.CSV 2000-01-01 01:00:00 721
19:41:41.082 -> LOGGER30.CSV 2000-01-01 01:00:00 3334
19:41:41.115 -> LOGGER31.CSV 2000-01-01 01:00:00 1208
19:41:41.150 -> LOGGER32.CSV 2000-01-01 01:00:00 619
19:41:41.185 -> LOGGER33.CSV 2000-01-01 01:00:00 407
19:41:41.253 -> LOGGER34.CSV 2000-01-01 01:00:00 3184
Please note I used this card previously for data logging on the Mega, so after clearing it:
19:53:16.859 -> Initializing SD card…Wiring is correct and a card is present.
19:53:17.063 ->
19:53:17.063 -> Card type: SDHC
19:53:17.063 -> Clusters: 1942208
19:53:17.097 -> Blocks x Cluster: 16
19:53:17.132 -> Total Blocks: 31075328
19:53:17.166 ->
19:53:17.166 -> Volume type is: FAT32
19:53:17.200 -> Volume size (Kb): 15537664
19:53:17.200 -> Volume size (Mb): 15173
19:53:17.234 -> Volume size (Gb): 14.82
19:53:17.268 ->
19:53:17.268 -> Files found on the card (name, date and size in bytes):
19:53:17.335 -> SYSTEM~1/ 2020-05-31 19:50:06
19:53:17.369 -> WPSETT~1.DAT 2020-05-31 19:50:06 12
19:53:17.402 -> INDEXE~1 2020-05-31 19:50:06 76
The SD card is now ready and integrated with the Mega to collect data as per requirements.