Help please!: temperature and shutter event logging

I am an Arduino novice - and generally deficient in electronics literacy - however, due to the lack of an off-the-shelf device, or devices to achieve my objective, it has been recommended that I apply Arduino - and Arduino-compatible - hardware and software to provide a solution.

I own an electric vehicle whose high voltage battery temperature is managed by a relatively sophisticated cooling system which includes (amongst other components) 5 coolant pumps, numerous coolant hoses, an automotive radiator and a shutter system, which controls airflow through the radiator.

I want to monitor shutter activation and the external temperature of 3 of the coolant hoses, plus ambient temperature, in real time.

To this end, I have purchased some hardware, which includes 2 Arduino UNO boards, 2 DS1307 real time clock modules, a micro-SD card module, 4 DS18B20 waterproof temperature sensors, and a generic lever-action micro-switch. I have other Arduino-related componentry, such as breadboards, jumper leads, various sensors, etc., but the above are the principal items relevant to this project. I have also just ordered from Core Electronics 2 Adafruit data logging shields, for convenience.

Due to my inexperience in this field, I intend to break the project into two sub-projects: shutter activation monitoring and temperature monitoring. I then intend to consolidate the captured data (recorded over say 6-12 hour time periods) using Excel.

Shutter activation: the shutters (in front of the radiator) are actuated by a mechanism whose open/closed state (the shutters are either fully open or fully closed) I can monitor using the micro switch, digitally logging the shutter state as either ‘open’ or ‘closed’ with the Arduino UNO / Adafruit data logger combination. I have adapted a couple of existing sketches to - more or less - record those events on a desktop system with which I have been experimenting. Ultimately, I intend to convert ‘open’ to a value of say 1 and ‘closed’ to a value of say 10 so that in my composite Excel graph of shutter opening state and coolant temperatures v time, the shutter curve presents as a set of horizontal lines connected by verticals when the opening state changes. I want to assess how shutter opening/closing relates to coolant temperature during the time period of a journey in various scenarios (e.g. different ambient temperatures, urban v country journeys, topographic influences, etc) - and during high voltage battery charging in my home garage.

I have a sketch suitable for monitoring the 4 temperature sensors, but I need to integrate that with suitable time data logging code.

I am open to suggestions as to improvements to any of the abovementioned methodology and of course if anyone is aware of existing code which would be readily adaptable to my project, I would be pleased to learn about it.

Thank you, in anticipation.

Chris

1 Like

I have made some progress with this project since my previous post. At this stage at least, I have decided to split the project into two components and combine my data in Excel.

Using my Arduino Uno board, and Elegoo DS1307 RTC and a micro SD card shield, I have run some code which I’ve modified over several days. Essentially, it combines elements of two sketches I’ve found in the internet.

I hope the following two uploads are legible. The first is the current version of my code and the second is a screen shot showing some Serial Monitor data.

#include "SD.h"  // SD card library
#include "Wire.h"  // I2C
#include "Time.h"  // Time Manipulation
#include "DS1307RTC.h"  // DS1307 RTC

// constants won't change. They're used here to set pin numbers:
const int SWITCH_PIN = 2;       // the number of the microswitch pin
char timedatebuf[65];  // Time and Date string buffer
int year4digit;  // 4 digit year

void setup()
{
  Serial.begin(9600);  // Serial monitor used for testing
  // Micro switch code
  pinMode (SWITCH_PIN, INPUT_PULLUP);
  Serial.println("Micro switch project");
  pinMode(10, OUTPUT);
  
  if (!SD.begin(10)) {  // check if card is installed
    Serial.println("No SD Card present in module");
    return;
  }
  Serial.println("SD Card Ready");
}

void loop()
{
   tmElements_t tm;
  // Micro switch code
  int microSwitch = digitalRead(SWITCH_PIN);
  if(microSwitch == LOW){
      Serial.println("Shutters Closed");
    }
  
    if (RTC.read(tm)) {  // Get Time/Date from RTC1307
     
      year4digit = tm.Year + 1970;  // 4 digit year variable
      
      // Format Time & Date string in timedatebuf
      sprintf(timedatebuf, "Time: %02d:%02d:%02d   Date:  %02d/%02d/%02d SHUTTERS CLOSED",tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, year4digit);
      
      File dataFile = SD.open("SHUTTERS.txt", FILE_WRITE);  // Open or Create file 

      if (dataFile) {  // Check if file exist on SD Card
         dataFile.println(timedatebuf);
         dataFile.close();  // Close file
         Serial.println(timedatebuf);
      }  
    }
  else if (microSwitch == HIGH){
    Serial.println("Shutters Open");
    }
    
    if (RTC.read(tm)) {  // Get Time/Date from RTC1307
     
      year4digit = tm.Year + 1970;  // 4 digit year variable
      
      // Format Time & Date string in timedatebuf
      sprintf(timedatebuf, "Time: %02d:%02d:%02d   Date:  %02d/%02d/%02d SHUTTERS OPEN",tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, year4digit);
      
      File dataFile = SD.open("SHUTTERS.txt", FILE_WRITE);  // Open or Create file 

      if (dataFile) {  // Check if file exist on SD Card
         dataFile.println(timedatebuf);
         dataFile.close();  // Close file
         Serial.println(timedatebuf);
      }  
      
      else {
        Serial.println("error opening shutters.txt"); // if file not on SD Card
      } 
      delay(1000);
    }

}

Shutter monitoring sketch with DS1307RTC and microSD card - v5.pdf (136.9 KB)

The attachment, in particular, reflects the flaws - or at least some of the flaws in my sketch. They are as follows:

  1. Shutters Closed and Shutters Open is being recorded each second, irrespective of the position of my controller (a micro-switch) connected to pin 2 (and ground). The correct record should be: Shutters Closed when the micro-switch circuit is open and Shutters Open when that circuit is closed.
  2. A simple Shutters Closed entry is displayed in the Serial Monitor when the switch is open, which is correct, but when the switch is closed the Shutters Closed entry is omitted and there is no Shutters Open entry.

These flaws demonstrate my lack of programming capability. I would very much appreciate it if someone could set me right.

Do you mean that the microswitch connected to pin 2 connects that pin to ground when it closes? In that case the closed state will be LOW and the open state will be HIGH (due to INPUT_PULLUP). So a LOW should indicate Shutters Open and a HIGH should indicate Shutters Closed. You have the messages the other way around.

Your formatting puts the start of each block at the end of a line. I would recommend moving each of your block start brackets (“{”) to the start of the next line, and autoformat. That will make it much clearer that your if and else if are not matched up the way that you think they are. Rearrange your blocks so that the testing of the switch occurs once right at the start, and the testing for both high and low follows immediately. Set your descriptive text into a variable according to the test result. Then write your file update routine once only, using the prepared variable as your file text and your console output.

Thanks for your prompt response, Jeff.

My microswitch is connected as follows: normally open to pin 2 and common to ground. So yes, when the switch closes it connects pin 2 to ground (my logic). I understand your advice and will re-label accordingly.

Due to my poor knowledge of the Arduino language, I will need to do some further research to properly comprehend the advice you’ve provided in your second paragraph.

Thank you, again.

1 Like

Hi Chris,

Another thing you may want to look into is interrupts. They are very good at responding to rising or falling edges:

Another thing to consider is switch debouncing. This can be done in hardware or software, but may be the cause of the spurious readings you are seeing.

It’s also worth mentioning that EVs have enormous chemical energy stored in their batteries, just waiting to become a fire you can’t put out for days. Personally, I wouldn’t put my warranty and safety on the line, but I admire your desire to learn more about the way your car works.

This is an example of what I was suggesting.

char shutterStatus[24];

void loop()
{
  tmElements_t tm;
  // Micro switch code
  int microSwitch = digitalRead(SWITCH_PIN);
  if (microSwitch == LOW)
  {
    strcpy(shutterStatus, "Shutters Open");
  }
  else
  {
    strcpy(shutterStatus, "Shutters Closed");
  }
  if (RTC.read(tm))   // Get Time/Date from RTC1307
  {
    year4digit = tm.Year + 1970;  // 4 digit year variable

    // Format Time & Date string in timedatebuf
    sprintf(timedatebuf, "%s Time: %02d:%02d:%02d   Date:  %02d/%02d/%02d", shutterStatus, tm.Hour, tm.Minute, tm.Second, tm.Day, tm.Month, year4digit);

    File dataFile = SD.open("SHUTTERS.txt", FILE_WRITE);  // Open or Create file
    if (dataFile)   // Check if file exist on SD Card
    {
      dataFile.println(timedatebuf);
      dataFile.close();  // Close file
      Serial.println(timedatebuf);
    }
    else
    {
      Serial.println("error opening shutters.txt"); // if file not on SD Card
    }
  }
  delay(1000);
}

Thank you, Jeff.

I will copy that section of code and study carefully what you’ve done. Some new terms for me, e.g. strcpy (which I’ve briefly researched).

Fantastic, Jeff - thank you immensely!

Just one small tweak when I uploaded your code and this is the result!

Jeff’s suggestion 1 - Core Electronics Forum 10.01.2023.pdf (123.1 KB)

I’ll fine tune the chart format in due course, but this (see page 2 of the attachment) is the sort of result I want - then combine it with temperature data from a separate sketch.

Thanks, again for your generous assistance.

Next step: to construct durable hardware for this part of my project.

Thank you, James.

I’m mindful of the high energy levels running around my EV, which is why I’m ensuring that anything I do with this project is peripheral to that.