Logging Sensor Data to an SD Card with NodeMCU

Engineering illustration: Logging Sensor Data to an SD Card with NodeMCU

When you need to log a sensor's data over a long period without relying on a constant internet connection, wiring an SD card to a NodeMCU board offers a practical solution for storing readings locally in a text file that can be reviewed later from any computer.

Components needed

A NodeMCU (ESP8266) board, an SD card reader module running on the SPI protocol, a DHT22 temperature and humidity sensor, an SD card, and connecting wires.

Wiring it up

The SD card reader module connects over SPI, with the chip-select (CS) pin connecting to the D8 pin on the board, while the other SPI pins (MOSI, MISO, and SCK) connect to their default dedicated pins. The DHT22 sensor connects to pin D2 to read temperature and humidity.

How it works

The code relies on the SD library to handle the card's file system, and the DHT library to read the sensor's data. On every 2-second loop cycle, the code reads the current temperature and humidity from the sensor, then opens a text file named LOG.txt on the card in append mode — adding a new line without erasing what was previously recorded — writing the two readings separated by a pipe character "|" to make the file easy to read and parse later. This way, the device can be left running for days or weeks to build a complete environmental data log, after which the card can be removed and the file opened directly on any computer to review the results.

You can find NodeMCU boards, SD card reader modules, and the DHT22 sensor at Juthour Tech.

اترك تعليقا

*
*
يمكن للأعضاء المسجلين فقط ترك تعليقات.