I'm trying to make an energy efficiencies sensor. I use the feather M0 adalogger and it is working fine with 3 i2c sensors. The veml 7700 lux, amc2315c sensor and ds3231 RTC for an outdoor project.
I need some guidance of the best way to minimize the power consumption of the EEMB 3.7V Li-ION 1800mAh Lipo Battery.
Code: Select all
#include <RTClib.h>
#include <SD.h>
#include <Wire.h>
#include <Adafruit_AHTX0.h>
#define VBATPIN A7
Adafruit_AHTX0 aht;
Adafruit_Sensor *aht_humidity, *aht_temp;
RTC_DS3231 rtc;
const int chipSelect = 4; // SPI chip select for SD card
const int cardDetect = 7; // pin that detects whether the card is there
const int writeLed = 8; // LED indicator for writing to card
const int errorLed = 13; // LED indicator for error
long lastWriteTime = 0; // timestamp for last write attempt
long interval = 300000; // time between readings
char fileName[] = "datalog.csv"; // filename to save on SD card
void setup()
{
Serial.begin(9600);
Wire.begin();
rtc.begin();
rtc.adjust(DateTime(F(__DATE__),F(__TIME__)));
//rtc.adjust(DateTime(2019, 1, 21, 5, 0, 0));
// initialize LED and cardDetect pins:
pinMode(writeLed, OUTPUT);
pinMode(errorLed, OUTPUT);
pinMode(cardDetect, INPUT_PULLUP);
if (! aht.begin()) {
Serial.println("Could not find AHT? Check wiring");
while (1) delay(10);
}
if (!veml.begin()) {
Serial.println("Sensor not found");
while (10);
}
Serial.println("Sensor found");
Serial.println("AHT10 or AHT20 found");
// startSDCard() blocks everything until the card is present
// and writable:
if (startSDCard() == true) {
Serial.println("card initialized.");
delay(100);
// open the log file:
File logFile = SD.open(fileName, FILE_WRITE);
// write header columns to file:
if (logFile) {
logFile.println("Date,Hour,Batt,Humi,Temp");
logFile.close();
}
}
}
void loop()
{
// if the cart's not there, don't do anything more:
if (digitalRead(cardDetect) == LOW) {
digitalWrite(errorLed, HIGH);
return;
}
// turn of the error LED:
digitalWrite(errorLed, LOW);
DateTime now = rtc.now();
char dateBuffer[16];
char hourBuffer[16];
sprintf(dateBuffer,"%02u-%02u-%04u ",now.day(),now.month(),now.year());
sprintf(hourBuffer,"%02d:%02d:%02d ",now.hour(),now.minute(),now.second());
float measuredvbat = analogRead(VBATPIN);
measuredvbat *= 2;
measuredvbat *= 3.3;
measuredvbat /= 1024;
aht_humidity = aht.getHumiditySensor();
sensors_event_t humidity;
aht_humidity->getEvent(&humidity);
float rHumidity = humidity.relative_humidity;
aht_temp = aht.getTemperatureSensor();
sensors_event_t temp;
aht_temp->getEvent(&temp);
float temperature = temp.temperature;
// read the lux value from the sensor:
float readLux = veml.readLux();
// read sensors every 5 minutes
if (millis() - lastWriteTime >= interval) {
File logFile = SD.open(fileName, FILE_WRITE); // open the log file
if (logFile) { // if you can write to the log file,
digitalWrite(writeLed, HIGH); // turn on the write LED
logFile.print(dateBuffer);
logFile.print(", ");
logFile.print(hourBuffer);
logFile.print(", ");
logFile.print(measuredvbat);
logFile.print(", ");
logFile.println(rHumidity);
logFile.print(", ");
logFile.println(temperature);
logFile.print(", ");
logFile.println(readLux);
logFile.flush();
Serial.print(dateBuffer);
Serial.print(",");
Serial.print(hourBuffer);
Serial.print(",");
Serial.print(measuredvbat);
Serial.print(",");
Serial.print(rHumidity);
Serial.print(",");
Serial.print(temperature);
Serial.print(",");
Serial.print(readLux);
Serial.print('\n');
// update the last attempted save time:
lastWriteTime = millis();
}
digitalWrite(writeLed, LOW); // turn off the write LED
}
delay(5000);
}
boolean startSDCard() {
// Wait until the card is inserted:
while (digitalRead(cardDetect) == LOW) {
Serial.println("Waiting for card...");
digitalWrite(errorLed, HIGH);
delay(750);
}
// wait until the card initialized successfully:
while (!SD.begin(chipSelect)) {
digitalWrite(errorLed, HIGH); // turn on error LED
Serial.println("Card failed");
delay(750);
}
return true;
}