mikecrowe
Published © GPL3+

Arduino Uno Brownout Detector and Logger

Detect and record brownouts even when you're not home!

IntermediateFull instructions provided281
Arduino Uno Brownout Detector and Logger

Things used in this project

Hardware components

Resistor 100k ohm
Resistor 100k ohm
×1

Story

Read more

Schematics

Breadboard layout Brownout detector

Schematic brownout detector

Parts list

Code

Sketh for Brownout detector

Arduino
/* FILE:    Data_Logger_Shield_HCARDU0093_Example
   DATE:    24/02/15
   VERSION: 0.1
   
Modified 01/01/20

BROWNOUT DETECTOR AND RECORDER.

Modified code by: Mike Makes It. Find my YouTube channel at:

https://www.youtube.com/channel/UCRXtjq0foKPM0F0xsBGd1Sw/

This is an example of how to use a Geek Robot RTC and data logger shield with an Arduino Uno as a Brownout detector and recorder.

The Geek Robot shield contains a battery backed DS1307 real time clock and an SD card interface. 
This allows any data read by your Arduino to be stored with an accurate time stamp to an SD card. 
In this example sketch we will read the value at pin A0 every 10th of a second. The voltage presented to it is from the cut down mains supply to our house.
If this voltage, when measured, is below the predetermined level, a record is written to the SD card as a CSV file. The file can then be read when loaded into a spreadsheet such as Excel.

To use this sketch you will require the HCTRC library which is available for
download in the software section of Hobby Components or see my notes for a link and further information on Hobby Components. 

You will need to connect the shield to the Uno then connect the shield as per information provided on the link to my Google drive.

You may copy, alter and reuse this code in any way you like, as I have but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.

*/




/* Include the wire library */
#include <Wire.h>
/* Include the Hobby Components RTC library */
#include <HCRTC.h>
/* Include the standard SD card library */
#include <SD.h>

/* The RTC has a fixed addresses of 0x68 */
#define I2CDS1307Add 0x68

/* DIO pin used to control the SD card CS pin */
#define SD_CS_DIO 10 

/* Define the analogue pin used to read the voltage sensor (A0) */
#define VoltageInPin 0

/* Create an instance of HCRTC library */
HCRTC HCRTC;

/* Create an instance of the standard SD card library */
File DataFile;

/* This will store the current voltage reading */
float CurrentVoltage;

/* Defines the boundary range that if the voltage strays outside it will write to file. You will need to alter these values to 
suit your input voltage from the PSU. 580 worked for me although in the video I said 595 */
int LowerVoltageBoundary = 580;
int UpperVoltageBoundary = 700;

void setup()
{
  /* Initialise the serial port */
  Serial.begin(9600);

  
  
  // Set the SD card CS pin to an output */ 
  pinMode(SD_CS_DIO, OUTPUT);
  
  /* Use the RTCWrite library function to set the time and date. 
     Parameters are: I2C address, year, month, date, hour, minute, second, 
     day of week. You would normally only need to do this once */
   //HCRTC.RTCWrite(I2CDS1307Add, 20, 01, 07, 12, 35, 0, 2);
  
  /* Initialise the SD card */
  if (!SD.begin(SD_CS_DIO)) 
  {
    /* If there was an error output this to the serial port and go no further */
    Serial.println("ERROR: SD card failed to initialise");
    while(1);
  }else
  {
    Serial.println("SD Card OK");
  }
}

/* Main Loop */
void loop()
{
  /* Read the voltage meter and save its value to variable */
  CurrentVoltage = analogRead(VoltageInPin);  //cal in here for formular 
    
  /* Read the current time from the RTC module */
  HCRTC.RTCRead(I2CDS1307Add);
    
  /* Lets output this data to the serial port */ 
  Serial.print(HCRTC.GetDateString());
  Serial.print(", ");
  Serial.print(HCRTC.GetTimeString());
  Serial.print(", ");
  Serial.println(CurrentVoltage);

  if(CurrentVoltage < LowerVoltageBoundary || CurrentVoltage > UpperVoltageBoundary)
  {
    Serial.println("---------- !!Brown Out!! --------- ");

    /* Open the data.csv file to save our data to. 
     If the file already exists it will just tag our new data onto the end of it */
    DataFile = SD.open("data.csv", FILE_WRITE);
  
    if (DataFile) 
    {
      DataFile.print(HCRTC.GetDateString());
      DataFile.print(", ");
      DataFile.print(HCRTC.GetTimeString());
      DataFile.print(", ");
      DataFile.println(CurrentVoltage);  
      DataFile.close();
    } 
  }

  /* Wait 10th second before reading again. Reading at this speed should catch most voltage variations. Any slower and you could miss something. Ive not tried it any quicker! */
  delay(100);
}

Credits

mikecrowe

mikecrowe

0 projects • 0 followers

Comments