sdCardModule
sdCardModule
Difficulty
Medium
Steps
22
An SD (Secure Digital) card is ideal for both data storage as well
as for data transfer. However, by itself, an Arduino board is not
able to use SD cards. With an SD card module, this is made
possible.
Take a look at the SD card module and you'll notice that there are
eight pins:
GND: Ground
+3.3V: Power pin. Attach to 3.3V on the Arduino
+5: Another power pin, we recommend using a voltage level
converter when connecting to a 5V Arduino.
CS: Chip Select
MOSI: Master Out Slave In
SCK: Clock signal
MISO: Master In Slave Out
GND: Another ground pin
Step 3 Prepare SD card
Attach a F-F black jumper wire from GND on the SD card module
to GND on the Pro Mini.
Step 6 3.3V to VCC
Attach a F-F red jumper wire from 3.3V on the SD card module
to VCC on the Pro Mini.
Step 7 Connect CS to digital pin 10
Next, insert the DHT11 temperature and humidity sensor into the
mini breadboard.
Then, remove the F-F red jumper wire from VCC on the Pro mini.
Plug this end of the jumper wire into the breadboard, instead.
Step 12 Insert resistor into breadboard
Insert the 10k ohm resistor into the breadboard, so that pins 1
and 2 of the DHT11 are bridged.
Step 13 Connect to VCC
Now connect a red jumper wire from pin 1 of the DHT11 sensor to
the same breadboard row as the other red jumper wire connected
to the SD card module.
Insert another red jumper wire from the breadboard to
the VCC pin on the Pro Mini.
Step 14 Connect GND to GND
First, the SD card module uses SPI, so you will need to import the
SPI library with: #include <SPI.h>
Next, there is a built-in SD library which depends on three other
internal libraries that can handle card and filesystem-specific
functions. The library is easy to use, import it with: #include
<SD.h>
Finally, include the DHT library.
Step 18 Define constant variable
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
#include <DHT.h> // Include DHT sensor library
File dataFile;
Now create a File object, which can then be opened as well as
have other file actions done to it.
Step 20 Setup
#include <SPI.h> // Include SPI library (needed for the SD card)
#include <SD.h> // Include SD library
#include <DHT.h> // Include DHT sensor library
File dataFile;
void setup() {
// Start serial communications
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
Serial.print("Initialising SD card...");
if (!SD.begin()) {
Serial.println("initialisation failed!");
while (1);
}
Serial.println("initialisation done.");
delay(1000);
}
void setup() {
// Start serial communications
Serial.begin(9600);
while (!Serial)
; // wait for serial port to connect.
Serial.print("Initialising SD card...");
if (!SD.begin()) {
Serial.println("initialisation failed!");
while (1);
}
Serial.println("initialisation done.");
delay(1000);
}
uint16_t line = 1;
void loop()
{
const unsigned long fiveMinutes = 5 * 60 * 1000UL;
static unsigned long lastSampleTime = 0 - fiveMinutes; // initialise
such that a reading is due the first time through loop()
}
// if the file didn't open, print an error:
else
Serial.println("error opening DHT11Log.txt");
}
// add code to do other stuff here
}
Add the following code to get a temperature reading every 5
minutes, and record this temperature measurement into a text
file on the SD card.
Step 22 Serial monitor