Go Down

Topic: Voltage measuring system (Read 92 times) previous topic - next topic

piet_v

Aug 11, 2018, 11:12 pm Last Edit: Aug 11, 2018, 11:17 pm by piet_v
Hello everyone,

My project code:

Code: [Select]

#include <SPI.h>
#include <SD.h>
#include "RTClib.h"


//Indications:
int function_ok = 5; // green LED
int function_nok = 6; // red LED
int fault_detect = 7; // orange LED

//Probe 0:
int analog_in0 = 0; // analog read current
float R1 = 100000.0; // resistance R1 (100K)
float R2 = 10000.0; // resistance R2 (10K)

//Probe 1:
int analog_in1 = 1; // analog read current
float R3 = 100000.0; // resistance R3 (100K)
float R4 = 10000.0; // resistance R4 (10K)

//Probe 2:
int analog_in2 = 2; // analog read current
float R5 = 100000.0; // resistance R5 (100K)
float R6 = 10000.0; // resistance R6 (10K)

//Compare inputs
float deviation_1 = 10; // deviation from previous value (1)
float voltage[3] = {0.0, 0.0, 0.0}; // analog value
float Vin_save[3] = {0.0, 0.0, 0.0}; // saved voltage value
int Trigger = 0;

//Real Time Clock module (RTC)---------------------------------------------------------------------------
/*
  The circuit:
 ** VCC - 5V pin
 ** GND - GND pin
 ** SDA - SDA pin
 ** SCL - SCL pin
*/

RTC_DS3231 rtc;
char timestamp[30];
// call back for file timestamps
void dateTime(uint16_t* date, uint16_t* time) {
  DateTime now = rtc.now();
  sprintf(timestamp, "%02d/%02d/%2d %02d:%02d:%02d ", now.day(), now.month(), now.year() - 2000, now.hour(), now.minute(), now.second());
  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());
}


//SD-Card---------------------------------------------------------------------------
/*
  The circuit:
   analog sensors on analog ins 0, (1), and (2)
   SD card attached to SPI bus as follows:
 ** MOSI - pin 11
 ** MISO - pin 12
 ** CLK - pin 13
 ** CS - pin 4
*/

const int chipSelect = 4;
char filename[] = "00000000.CSV";
void getFileName() {
  DateTime now = rtc.now();
  sprintf(filename, "%02d%02d%02d%02d.CSV", now.day(), now.month(), now.year() - 2000, Trigger);
}

//measuring function---------------------------------------------------------------------------
int measure(int input, float Rfirst, float Rsecond) {
  //Local variables for measuring function
  float Vout = 0.0; // 1/10 analog value
  float a_read = 0.0;

  //Analog -> digital convert
  a_read = analogRead(input); // read analog value
  Vout = (a_read * 4.93) / 1024.0; // convert digital -> analog
  voltage[input] = Vout / (Rsecond / (Rfirst + Rsecond)); // convert 1/10 -> real value

  Trigger = input;
}

//Compare function + datalogging---------------------------------------------------------------------------
int action (float go, float deviation) {
  //local variables compare function
  float Vin_max = Vin_save[Trigger] + ((Vin_save[Trigger] / 100) * deviation);
  float Vin_min = Vin_save[Trigger] - ((Vin_save[Trigger] / 100) * deviation);

  if (go != 0) {
    if (go >= Vin_max or go <= Vin_min) {
      digitalWrite(fault_detect, HIGH);
      digitalWrite(function_ok, LOW);
      digitalWrite(function_nok, LOW);
      Vin_save[Trigger] = go;

      //SD-Card-------------------------------
      //String for assembling the data to log
      String dataString = "Voltage logged = ";
      dataString += String(go);
      getFileName();

      // open the file
      File dataFile = SD.open(filename, FILE_WRITE);

      // if the file is available, write to it
      if (dataFile) {
        dataFile.println(timestamp + dataString);
        dataFile.close();
        // print same to the serial port
        Serial.println(voltage[Trigger]);
      }
      // if the file isn't open, pop up an error
      else {
        Serial.println("error opening datalog");
      }
      delay(300);

    } else if (go <= Vin_max or go >= Vin_min) {
      digitalWrite(fault_detect, LOW);
      digitalWrite(function_ok, HIGH);
      digitalWrite(function_nok, LOW);
      Vin_save[Trigger] = go;
      delay(300);
    } else {
      digitalWrite(fault_detect, LOW);
      digitalWrite(function_ok, LOW);
      digitalWrite(function_nok, HIGH);
      Vin_save[Trigger] = go;
      delay(300);
    }
  } else {
    digitalWrite(function_nok, HIGH);
    digitalWrite(function_ok, LOW);
    digitalWrite(fault_detect, LOW);
    delay(300);
  }
}

void setup() {
  //Arduino pin definition
  pinMode(analog_in0, INPUT);
  pinMode(analog_in1, INPUT);
  pinMode(analog_in2, INPUT);
  pinMode(function_ok, OUTPUT);
  pinMode(function_nok, OUTPUT);
  pinMode(fault_detect, OUTPUT);

  //SD-card setup---------------------------------------------------------------------------
  // Open serial communications and wait for port to open:
  Serial.begin(9600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.print("Initializing SD card...");

  // see if the card is present and can be initialized:
  if (!SD.begin(chipSelect)) {
    Serial.println("Card failed, or not present");
    // don't do anything more:
    return;
  }
  Serial.println("card initialized.");

  //RTC Setup---------------------------------------------------------------------------
  if (! rtc.begin()) {
    Serial.println("Couldn't find RTC");
    while (1);
  }
  // set date time callback function
  SdFile::dateTimeCallback(dateTime);

  if (rtc.lostPower()) {
    Serial.println("RTC lost power, lets set the time!");
    // following line sets the RTC to the date & time this sketch was compiled
    rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  }
}

void loop() {

  measure(analog_in0, R1, R2); //Measure probe 0
  action (voltage[Trigger], deviation_1);

  measure(analog_in1, R3, R4); //Measure probe 1
  action (voltage[Trigger], deviation_1);

  measure(analog_in2, R5, R6); //Measure probe 2
  action (voltage[Trigger], deviation_1);

}



Materials used:

- Arduino Uno
- SD card (waveshark)
- DS3231
- 3 x 100 Kohm & 3 x 10 Kohm
- 2 x 1,5 V batteries

I have a few questions:

1) There's a +- 15 seconds difference when i compare the timestamp of the data with the time of my computer clock. Does anyone have an idea why?

2) I looked but couldn't find a clear answer, so can someone explain me why i need following code?

Code: [Select]

  // return date using FAT_DATE macro to format fields
  *date = FAT_DATE(now.year(), now.month(), now.day());

  // return time using FAT_TIME macro to format fields
  *time = FAT_TIME(now.hour(), now.minute(), now.second());


3) General feedback?

In attachment i added 3 log files from my last test run + a copy of the console information.
I also added a picture of the values i measured with my volt meter (i wrote the results of 2 test runs on the paper, only the second run can be compared with the log files).

There are some differences between the log files and the measurements i did, not yet clear why... . Changed the resistor values, will do a new test tomorrow.

Looking forward to receiving some constructive remarks.

Greetz,
Piet



Paul_KD7HB

Would it not be a whole lot more effective to just tell us what your project is rather than give dozens of clues for us to guess at?

Paul

Go Up