Coding confusion for booting up D1 Mini from battery and putting back into sleep mode

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);
  }


}

Sorry I cannot help on the coding but you did great by posting the schematic and your code. Thanks for reading the Forum Guidelines. Give this link a try, it may help. ESP32 Deep Sleep with Arduino IDE and Wake Up Sources | Random Nerd Tutorials

Thank you! That link is terrific, I will try it later and see if it solved one of our main problems...I think it will go a long way to get us there.

Thanks for the update let us know how you do.

Hello again,
I couldn't make the code work but I think it is for a couple of reasons that I wanted to check before I go to the next step.

1 - I didn't connect the D0 pin to the RST and am nervous to press reset as the code on the rest of the project seems to work and then stop working and I have no idea why. Resetting making me very nervous. I also connected these two pins before and my code no longer worked...

2 - You referred me to the ESP 32 and I have been loading the ESP 8266 board. I was wondering if I have been making a mistake with my board selection. I see a 12 F version on the back of the microprocessor but am only able to find older versions for download. Does this matter?

3- Do I need to attach my battery connection in any special way beside the 3v and ground in order for the D1 mini to wake up when a switch is flipped? Do I need to worry about changing the code for this? The last time I tried to do this, my code stopped working...

If anyone is able to guide me I would be grateful. I just got my code to work again and the project must be submitted by Friday - I am very uncomfortable messing with anything that might affect what is working.

Thank you!

It's not going to work with that battery.

Thank you! (Lot of tears have been shed over this failed effort.)

Will it work with the 3.7 lithium battery pack? I am currently looking at "waking from a deep sleep" with the battery and running a resistor with connection between D0 and RST. I am hoping that will wake it up? (with the right code)

full lithium battery has a voltage 4.2V, too high.
You could do it with 3.2V LiFePO battery.

For prototyping you could try with two brand new AA batteries in series (3V), but the battery life would be really bad.

Okay - thank you! Much appreciated!

Just to close out the discussion...the code worked by moving everything into set up and adding the arduino.h library. (https://www.youtube.com/watch?v=mqCuiFyesq4 )

Loose wiring was the problem with intermittent reading - this was confirmed through careful checking.

The battery issue was resolved with a regulator and wiring shown in this diagram (IoT Based Battery Status Monitoring System using ESP8266) but I couldn't get it to boot up with the resistors(?). Without them, everything seemed to work fine.

Much thanks for the responses. It helped me to keep digging and keep at it.

Post solved.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.