Hi! I am reaching out as a mom for my younger son (g.4) who is preparing a science project that is designed to take readings (from a graphene voltaic) and log those readings in space. To do so, he is using the Arduino D1 Mini and a data logging shield as well as the BME280 sensor and the Arduino voltage sensor as the cube is less than 4cmx4cmx4cm in size.
Because we have been short on time, I have tried to help by preparing wiring and coding - learning as I go and sharing with my son who is also new to electronics and coding. Thankfully, this has been an amazing experience of open source sharing of knowledge and I've cobbled together and tried to manipulate code to read a voltage sensor, the BME280 temp/pressure and to log the readings. It has more or less worked, although we find that the BME280 glitches and the data logging is not always successful. I am assuming it is because we have not woven the source code together correctly (and our wiring is loose).
Our larger issue though is with booting up the D1 Mini on battery power. I had tried a switch, but it hasn't worked. We were also just informed that there will be a general switch on from battery at the site where the projects are launched. The project will be encased in a box and out of reach and will cycle through for about a day, so we are trying to get it to go to sleep for 5 min. wake up and take a couple of readings and go back to sleep to save the battery. The code doesn't seem to be putting the microprocessor to sleep - it runs continuously without a pause and if we try to run the data logger on battery power it fails. We have been using a 3V 2032 button battery as a power source.
The code is attached and the equipment is as follows:
Microprocessor: WEMOS D1 Mini ESP8266 WiFi IoT Module 4MB
Data logging shield: D1 Mini Data Logger Shield with DS1307 RTC and micro-SD slot
Sensors: Arduino DC0-25V with Code (Voltage Sensor), BME280 Digital 5V Temperature Humidity Sensor Atmospheric Barometric Pressure Board IIC I2C Breakout for Arduino
Power Supply: JST PH2-Pin Cable – Male Header 200mm Product ID 3814 to a 3.7 lithium battery pack.
Constraints - out of wifi range and running on battery power for 1 day.
Any guidance or direction would be greatly appreciated.
We have attached a wiring diagram and the code for data logging from both sensors, as well as our attempt to power up from off with a switch and to set the D1 mini to sleep for 5 min. and to wake up and take a reading to extend the life of the battery.
Thank you for your posts and sharing of code and electronics! They are terrific and is why we were able to get this far.
#include <SD.h> //Load the SD library
#include <SPI.h> //Load the SPI communication library
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // Create BME280 object
const int voltageSensorPin = A0;
float vIn;
float vOut;
float voltageSensorVal;
const float factor = 3;
const float vCC = 5;
int chipSelect = D8;//Set Chip Select = D8
File mySensorData; //Variable for working with our file object
int buttonPin=D0;
int buttonValue;
int dt=100;
void setup() {
Serial.begin(9600);
pinMode(buttonPin,INPUT);
digitalWrite(buttonPin,HIGH);
Wire.begin();
pinMode(10,OUTPUT);//Reserve pin 10 as an output, don't use it for other parts of the circuit
SD.begin(chipSelect); //Initialize the SD card with chipSelect connected to pin D8
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}// put your setup code here, to run once:
}
const unsigned long AWAKE_SECONDS = 30000;
static uint32_t millis_last_reset = 0;
void loop() {
buttonValue=digitalRead(buttonPin);
Serial.print("Your Button is: ");
Serial.println(buttonValue);
delay(dt);
voltageSensorVal = analogRead(voltageSensorPin);
vOut = (voltageSensorVal / 1024) * vCC;
vIn = vOut * factor;
mySensorData= SD.open("VData.txt", FILE_WRITE); //Open VData.txt on the SD card as a file to write to
if (mySensorData) {
delay(10000); // Adjust the delay as needed
}
{
Serial.print("Voltage = ");
Serial.println(vIn);
Serial.println("BME280 mySensor Data:");
Serial.print("Temperature: ");
Serial.print(bme.readTemperature());
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(bme.readHumidity());
Serial.println(" %");
Serial.print("Pressure: ");
Serial.print(bme.readPressure() / 100.0F); // hPa to Pa conversion
Serial.println(" hPa");
Serial.print("Altitude: ");
Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.println(" meters");
Serial.println();
mySensorData.print(vIn); //Write vIn to the SD card
mySensorData.print(", "); //Write comma to the line
mySensorData.print(bme.readTemperature()); //Write Temperature to the SD card
mySensorData.print(", "); //Write comma to the line
mySensorData.print(bme.readHumidity()); //Write Humidity to the SD card
mySensorData.print(", "); //Write comma to the line
mySensorData.println(bme.readAltitude(SEALEVELPRESSURE_HPA)); //Write Altitude to the SD card
mySensorData.close();//Close the file
}
if (millis()-millis_last_reset > AWAKE_SECONDS * 10000) {
Serial.println("Going to sleep");
//Sleep until RESET pin is triggered
ESP.deepSleep(0);
}
}
