Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to log sensor data using SD and Micro SD card with Arduino

By Nikhil Agnihotri April 22, 2022

It is common to use different sensors with microcontrollers in embedded applications. Microcontrollers are equipped with parallel data ports and several serial data communication interfaces that allow them to interface and communicate with sensors. There are times when the data fetched from sensors need to be stored for future use. This may be just for logging sensor data or deriving high-level analytics. Many microcontrollers have built-in or on-board EEPROM to store external data, while the flash memory always remains reserved for storing the bootloader and the firmware. This EEPROM is usually a few kilobytes in size and can only store a limited amount of data.

A practical solution to this problem is storing sensor data on SD or Micro SD cards. SD and Micro SD card modules can be easily interfaced with Arduino (or any microcontroller) via the SPI interface. SD and Micro SD cards facilitate a storage capacity in gigabytes that is more than sufficient to log data from any number of sensors or any amount of data.

In this project, we will interface a Micro SD card module with Arduino and use an 8 GB Micro SD card to log data from the ADXL345 accelerometer sensor. Before continuing this project, learn to interface the ADXL345 accelerometer with Arduino. You may also be interested in working on the ADXL345 accelerometer sensor.

SD and Micro SD card modules
SD and Micro SD cards are widely used memory cards. Both are different in technology and origin. SD cards are a bit bulky in size (24 mm x 32 mm x 2.1 mm) compared to Micro SD cards. Their maximum storage capacity is limited to 2 GB. SD cards are commonly used in handheld computers, digital cameras, and baby monitors. Micro SD cards are smaller (15 mm x 11 mm x 1 mm). Their maximum storage capacity is 16 GB. Micro SD cards are commonly used in mobile phones, microcomputers, drones, dash cameras, etc.

Both SD and Micro SD card readers usually have an SPI interface for data communication with a microcontroller/microcomputer. Except that the SD card reader reads/writes an SD card and Micro SD card reader reads/writes a Micro SD card, both types of modules are interfaced with Arduino (or any other microcontroller/microcomputer) similarly, and the same library in Arduino is used to read or write from both type of modules.

Examples of SD and Micro SD card readers for Arduino

SD library
Arduino can communicate with SD and Micro SD card readers using its built-in SD library, as both modules use the SPI interface for data communications. The library also supports FAT16 and FAT32 file systems on SD/Micro SD cards, but the file names must be in 8.3 formats. Only one period is allowed, although the filenames can include PATHs separated by forward slashes. The library provides the following methods to work with SD/Micro SD cards.

SD.begin(): This method initializes the SD/Micro SD card. It takes the pin connected to the SS pin of the reader as a parameter.
SD.exists(): This method tests the existence of a file or directory in the SD/Micro SD card. It takes the filename as a parameter.
SD.mkdir(): This method creates a directory on the SD/Micro SD card. It takes the filename, including the path, as a parameter.
SD.rmdir(): This method removes a directory from the SD/Micro SD card. It takes the filename, including the path, as a parameter.
SD.open(): This method opens a file for reading or writing. It takes two parameters filename and mode. The mode determines if the file has to be read or written. The file will open in read mode by default if the mode is not specified. If the file is opened for writing, if it does not already exist, it will be created by the specified filename and path.
SD.remove(): This method removes a file from SD/Micro SD card. It takes the filename, including the path, as a parameter.

Many methods of the built-in file class are also used while reading from or writing to SD/Micro SD cards. Some of the useful techniques of the built-in file class are as follows.

File.name(): This method returns the file name.
File.available(): This method checks if a file with given filename exists.
File.read(): This method reads the contents of the file.
File.write(): This method writes data to the file.
File.size(): This method returns the size of the file in bytes.
File.print(): This method prints data to the file.
File.println(): This method prints data to the file followed by a carriage return and newline.
File.close():      This method closes the file and ensures any data written to it is physically saved in the SD/Micro SD card.

Note that until the file.close() method is called, data written to a file is not saved. Therefore, a file must be closed after writing data to it. Otherwise, the logged data will be lost.

Interfacing SD/Micro SD card module with Arduino
The SD/Micro SD card is interfaced with Arduino via the SPI interface. The module has six pins – VCC, GND, MISO, MOSI, SCK, and CS. The SPI ports in Arduino UNO are shown in the image below.

SPI port in Arduino UNO

Note that the SS pin of the SD/Micro SD card reader should be connected to pin 10 of Arduino UNO.

Writing data to SD/Micro SD card with Arduino
Interface the SD/Micro SD card reader with Arduino UNO as described above. Writing data to SD/Micro SD card involves use of SD.begin(), SD.mkdir() (if a new directory is created for the data file), SD.open(), File.write(), File.print(), File.println(), and File.close() methods. The following sample code writes a list of websites belonging to the electronics engineering network of EEWORLDONLINE to a text file. Note that data is written to the SD/Micro SD card in the setup() function because it must be written only once in this case. If it had been sensor data with a time stamp, the writing to SD/Micro SD card would have to be done in the loop() function. Also, note that the file class only needs instantiating a file object.

The result of the above sample code is shown below.

Example of writing data to SD card with Arduino

Reading data from SD/Micro SD card with Arduino
Reading data from SD/Micro SD card involves use of SD.begin(), SD.open(), File.available(), and File.read() methods. Note that File.read() method reads a single line at a time. It needs to be repeated either until reading the desired number of lines or until the file ends.

The result of the above sample code is shown below.

Example of reading data from SD card with Arduino

Logging sensor data in SD card with Arduino
Now equipped with the knowledge of logging data into SD/Micro SD card using a card reader, we can now log sensor data for future use. Let us interface the ADXL345 accelerometer sensor with Arduino and log ten consecutive acceleration readings on the x, y, and z-axis at 500 milliseconds.

Components required

  1. Arduino UNO x1
  2. SD/Micro SD card reader x1
  3. SD/Micro SD card x1
  4. ADXL345 accelerometer sensor x1
  5. Connecting wires/Jumper wires
  6. Breadboard

Circuit connections
The SD or Micro SD card reader will interface with Arduino via SPI port. The circuit connections between the SD/Micro SD card reader and Arduino are summarized below.

Arduino UNO SD/MicroSD Card Reader
5V out VCC
GND GND
13 SCK
12 MISO
11 MOSI
10 SS

The ADXL345 accelerometer is interfaced with Arduino via an I2C port. The circuit connections between ADXL345 and Arduino are shown in the image below.

The final circuit looks like as shown in the image below.

Circuit diagram for logging sensor data to SD or Micro SD card with Arduino

Arduino Sketch

How it works
Arduino senses the acceleration along the x, y, and z-axis with the help of the ADXL345 accelerometer sensor. The code runs once and reads through the sensor ten times at 500-millisecond intervals. The readings are written to a text file on a Micro SD card and saved.

Result
The following video shows Arduino logging ten consecutive readings from ADXL345 to an 8GB Micro SD card.

https://www.engineersgarage.com/wp-content/uploads/2022/04/P30-DV01.mp4

The following video shows the verification of data logged in the micro SD card.

https://www.engineersgarage.com/wp-content/uploads/2022/04/P30-DV02.mp4

 

 

 

 

 

You may also like:


  • MicroPython – Digital input/output in ESP8266 and ESP32

  • How to display data from an ultrasonic distance-measurement sensor on…

  • How to use a DHT sensor to show humidity and…

  • What is MicroPython?

  • How to display LDR and soil-moisture sensor values using an…

  • How to use interrupts with Arduino

Filed Under: Electronic Projects, Featured
Tagged With: Arduino, Arduino projects, logging sensor data
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: Internet of Things
Explore practical strategies for minimizing attack surfaces, managing memory efficiently, and securing firmware. Download now to ensure your IoT implementations remain secure, efficient, and future-ready.

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • Will this TL084C based current clamp circuit work?
  • ADS optimization cockpit window does not open
  • Voltage mode pushpull is a nonsense SMPS?
  • How to determine the maximum PAD frequency ?
  • Xiaomi Mijia 1C Robot problem of going backwards while working

RSS Electro-Tech-Online.com Discussions

  • Curved lines in PCB design
  • using a RTC in SF basic
  • PIC KIT 3 not able to program dsPIC
  • Siemens large industrial PLC parts
  • Parts required for a personal project

Featured – RPi Python Programming (27 Part)

  • RPi Python Programming 21: The SIM900A AT commands
  • RPi Python Programming 22: Calls & SMS using a SIM900A GSM-GPRS modem
  • RPi Python Programming 23: Interfacing a NEO-6MV2 GPS module with Raspberry Pi
  • RPi Python Programming 24: I2C explained
  • RPi Python Programming 25 – Synchronous serial communication in Raspberry Pi using I2C protocol
  • RPi Python Programming 26 – Interfacing ADXL345 accelerometer sensor with Raspberry Pi

Recent Articles

  • AC-DC power supply extends voltage range to 800 V DC
  • Infineon’s inductive sensor integrates coil system driver, signal conditioning circuits and DSP
  • Arm Cortex-M23 MCU delivers 87.5 µA/MHz active mode
  • STMicroelectronics releases automotive amplifiers with in-play open-load detection
  • Convection-cooled power controller integrates EtherCat connectivity

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe