Data Logger for geography class - ESP8266, BME280 with DS3231 RTC and SD Card

Hello everybody,

for an educational project in a geography class we want to create a couple of data loggers.
They should record time, air temperature, rel. air humidity and atmospheric pressure on a SD card (to make it easier for the students to read out the data). No WiFi will be available at the locations.
To be able to compare the data between the single loggers, the data should be logged at every exact minute - though every 5, 10 or 15 minutes would also be sufficient.

As micro controller the ESP8266 NodeMCU 12E (Arduino Nano or so could be an alternative?) was chosen, as sensor the BME280, as RTC the DS3231 and a standard SD card adapter.
The energy is currently supplied with 4x AAA batteries via micro USB.

Below, I have posted code which works fine so far… but also with modem sleep after abt. 25 hours the batteries need to be replaced / recharged. :-\

Maybe, somebody can help to make it last longer? … 3-4 days recording time would already be enough.
Thank you very much in advance!! :slight_smile:

/*
Wiring:

BME280, DS3231 <> ESP8266 NodeMCU 12E
SDA <> D3
SCL <> D4
VIN,VCC <> 3V3
GND <> GND
SQW (just DS3231) <> D1

SD Card Module <> ESP8266 NodeMCU 12E
CS <> D8
SCK <> D5
MOSI <> D7
MISO <> D6
VCC <> VIN
GND <> GND

Power Supply via microUSB
*/

#include <Wire.h>
#include <RtcDS3231.h>
#include <Adafruit_BME280.h>
#include <Adafruit_Sensor.h>
#include <ESP8266WiFi.h>
#include <SD.h>

#define CS_PIN D8

Adafruit_BME280 bme;

const byte interruptPin = D1;
volatile bool alarm = 0;

RtcDS3231 rtcObject(Wire);

void setup() {
Serial.begin(115200);
Wire.begin(D3,D4);
WiFi.forceSleepBegin();
bme.begin();
SD.begin(CS_PIN);

pinMode(interruptPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(interruptPin), handleInterrupt, FALLING);

rtcObject.Begin();
RtcDateTime timestamp = RtcDateTime(DATE, TIME);
rtcObject.SetDateTime(timestamp);

rtcObject.Enable32kHzPin(false);
rtcObject.SetSquareWavePin(DS3231SquareWavePin_ModeAlarmOne);

DS3231AlarmOne alarm1(
0,
0,
0,
0,
DS3231AlarmOneControl_SecondsMatch);

rtcObject.SetAlarmOne(alarm1);
rtcObject.LatchAlarmsTriggeredFlags();
}

void loop() {
if (alarm == true) {
handleAlarm();
}
}

void handleAlarm() {
alarm = false;

RtcDateTime timestamp = rtcObject.GetDateTime();
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure();

char time[20];

sprintf(time, “%d/%d/%d %d:%d:%d”,
timestamp.Day(),
timestamp.Month(),
timestamp.Year(),
timestamp.Hour(),
timestamp.Minute(),
timestamp.Second()
);

File dataFile = SD.open(“tthp.CSV”, FILE_WRITE);
if (dataFile) {
Serial.println(“Data logged.”);
dataFile.print(time);
dataFile.print(",");
dataFile.print(t);
dataFile.print(",");
dataFile.print(h);
dataFile.print(",");
dataFile.println(p);

dataFile.close();
}

rtcObject.LatchAlarmsTriggeredFlags();
}

void handleInterrupt() {
alarm = true;
}

Since no WiFi is available and you want to compare measurements from sensors that are not connected I would recommend you use a GPS unit as your time base. You can get GPS boards on ebay for less than $10 on ebay, make sure that there is pulse per second (PPS) output on the boards you buy. The PPS signal will arrive at the individual GPS units very close to the same time +/- 10's nanoseconds that way the measurements made by the individual sensors will be very close to the same time.

"Maybe, somebody can help to make it last longer? ... 3-4 days recording time would already be enough."

Well, the simple answer is to get bigger better (preferable rechargeable) batteries to power the project. Surprised nobody thought of that.

ESP8266 is a poor choice, quite a high running current.

An Arduino Pro Mini, modified, running from a LiFePO4 battery direct, so no regulator needed, would be a better choice.

Choose the BME280 board right and they have a deep sleep current under 1uA.

A PCF8563 RTC has a very low running current well under 1uA, you can set an alarm to wake the processor every minute.

Setting the time for the RTC could be done with a GPS, just remove it when the RTC has the time or set the time manually.

The easiest 'logger' to use is an Openlog, cheap on eBay, just plug it into the Serial program port on the Pro Mini and it records whatever you print to the serial port.

Now you might be lucky and find a MicroSD card that only takes a few mA, in which case with the rest of the stuff only using about 2 - 3uA when asleep, you have a battery life of several weeks.

Do the 'logging' bit remotely, via say a LoRa link, and battery life stretches to months or years.

Hi, Keno2020!
I have spent weeks relearning the Arduino IDE after I was out of it for about 5 years.

I,too, am looking at the same type of data logger setup using a Nano instead of the ESP8266-12E,
Three things I noticed right off:

  1. Using the ESP instead of a Nano. I have run across a couple of data loggers that disabled the on-board LEDs (Nano) in order to cut current draw.
  2. You are using the two I2C devices differently and without the <Wire.h> library. I know you said it works as is which lets me know I’m almost there with my coding. I know you can use A4 (SDA) and A5 (SCL) with both the DS3231 and the BME280 devices but I haven’t learned how yet. The RTC’s I’ve seen need the <SPI.h> like you have in your sketch. I haven’t gotten that far yet, myself.
  3. You are using 4 AA batteries instead of 2 Lion batteries in a 2-cell Li Ion battery holder. The Nano input is from 7-12Vdc. Double the battery packs (in parallel) double the usable time. Same can be said about the 4-AA battery packs, parallel as many as you need, to the length of time you need it to record data.

Also, double check the USB port as you power insertion location. The USB port draws power, less usable recording time.
Instead, look at the Vin pin. That pin takes in more voltage than the 5V or 3v3 pins. Vin will always be higher than other voltages. Instead of using Vcc (Voltage, common collector) use the V5 or V3.3 instead.
Vcc has stumped me for a while outside of the Discrete Component use of schematics.
Hope this helps.

esp8266-12e dev board.jpg

You'll need to measure what's drawing current between readings, but I would predict that the SD card would be a major culprit. This will vary considerably with the brand of SD card, but will probably be in the range of about 2mA to as much as 20mA in an idle state. So you might consider rigging up a transistor to power the card, and shutting its power down completely between readings.

Thank you for all the answers.

Bigger better batteries (and more in parallel) is only the last option... since we want to keep the data loggers as tiny as possible.

The GPS is sadly not a real option as well. Some data loggers might be placed in locations like narrow backyards, street canyons or in "natural" canyons, close to water falls etc. where a good satellite signal could be a problem.
And then I am not sure if there is a GPS module which has the interrupt functionality?
(Normally an easy way would be to apply deep sleep code with the delay function and log the GPS time. But that would just give an accurate time for one microcontroller, but not enable simultaneous measurements.)

The Openlog and PCF8563 RTC sound interesting and I will for sure consider that for further projects.
Besides I will check the above using the Arduino Nano.

"So you might consider rigging up a transistor to power the card, and shutting its power down completely between readings." ... that sounds reasonable. Thanks! :wink:

Apart from that, does somebody have an idea if just by an optimization of the code more power can be saved?
E.g. if there is a certain function to send just single parts like the SD card module to some kind of sleep mode?

Starting with a large program and trying to 'optimise' it for low current operation is a mistake in my view.

Start wit the basic microcontroler with nothing else connected.

Measure the idle current.

Put the microcontroller into a deep sleep to wakeup in say 60 seconds, measure the current.

Add the sensor, measure the current etc.

Put the sensor into deep sleep etc, measure the current.

Etc, Etc.

Yes, though it is not really the start. We conducted the project in a larger park (with WIFI coverage) before and started up with the very basic code… from putting sensor readings to SEMO up to deepsleep and just every few minutes sending the data to a web server.
But (additionally to the issue with the now non-WIFI-covered locations) the WIFI antenna/sending process guzzles a lot of energy and then the internal ‘clock’ of the ESP is not accurate enough for simultaneous measurements etc…
So, that is why we came up with the RTC and since reading out internal memory is considered to be a bit too tricky for the students (with a rather non-technical background), we connected the SD card adapter.

But yeah, perhaps it is possible to put the above - using the RTC and SD card - in some sleep status…
or maybe there is a library containing a function to set the processor speed to a more power saving mode etc. etc…
that would be awesome. :slight_smile:

You may want to visit the Underwater data Logger.org, also know as the cave pearl project.I have read about Ed Mallon's Cave Pearl Logger sketch and hardware. My greatest stumbling block was matching cheap boards with working sketches. I'm trying to coordinate the use of BME280 mini purple I2c mini PCB with the ZS-042 RTC in I2c mode with the HW-203 SD card module in SPI mode.