Difference between revisions of "Programming Power Fail Logger with Arduino IDE"

From media_wiki
Jump to: navigation, search
 
(18 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[File:DS3231.PNG |200px|right]]
+
'''Test the connectivity from the programmer to the Power Fail Logger board'''
 
+
  Using '''AVRDUDE''' validate the '''USBTiny''' is communicating with the '''Power Fail''' Module Microcontroller
 
+
   Connect the '''USBTinyISP''' to the '''AVR-ISP-6 pin header J7''' on the '''Power Fail Logger PCB''' .
'''Setting up the ATMega328 chip'''
+
   On laptop using Windows CMD terminal type the following. Make sure you are in the AVRDude directory.
   Plug the USBTiny Programmer into the AVR-ISP 6 pin header J7.
 
   Using AVRDUDE Validate the USBTiny is communicating with the Power Fail Module Microcontroller
 
 
 
 
   '''avrdude -c usbtiny -p m328p -v'''
 
   '''avrdude -c usbtiny -p m328p -v'''
 
    
 
    
   The Microcontroller is set to 1HMz and needs to changed to '''16 MHz Default'''
+
For more information about using AVRDude refer back to this link
   Using AVRDude enter the follow command to set the fuse bits on the Microcontroller.
+
   ==[[ AVRDUDE Class]]==
    
+
'''Connect Serial Port to the RS232 Port'''
 
+
  Plug into your laptop USB port the '''Prolific USB-to-Serial Comm Port'''  
  '''avrdude -p m328p -c usbtiny -v -U lfuse:w:0xFF:m -U hfuse:w:0xDE:m -U efuse:w:0xFF:m'''
+
   Connect the '''Prolific USB-to-Serial Comm Port''' to the '''RS232 Port''' on the Power Fail Board
 +
  Connect the '''TX''', '''RX''', and '''Ground''' Pins to '''J4'''.
 +
          Prolific        J4 Power Logger
 +
              TX      -    RX
 +
              RX      -    TX
 +
              GND    -    GND
 +
   Use Windows '''Device Manager''' to validate which Comm Port is used. You will need this later.
 +
 
 +
'''Set up the Arduino IDE '''
 
    
 
    
   Make sure it matches these settings:
+
   Connect the '''USBTinyISP''' to the '''AVR-ISP-6 pin header J7''' on the '''Power Fail Logger PCB'''.
+
  Select your Programmer.
   '''avrdude: safemode: hfuse reads as DE'''
+
   In the Arduino IDE click on '''Tools''' Tab - Select '''Programmer''' then select '''USBTinyISP'''.<br>
   '''avrdude: safemode: efuse reads as FF'''
+
   Select your comm Port.
   '''avrdude: safemode: Fuses OK (E:FF, H:DE, L:FF)'''
+
  In the Arduino IDE click on '''Tools''' Tab - Select '''Port''' then select the comm-port that is using the '''Prolific Port'''
 +
   Select the correct target board.
 +
  In the Arduino IDE click on '''Tools''' Tab - Select '''Board''' then select the '''Arduino Uno'''.<br>
 +
'''Validate the Arduino IDE has the following libraries installed before proceeding with the flash process.'''
 +
#include <Wire.h>                  // For I2C Interface SDA SCK.
 +
#include <ds3231.h>                // For the DS3231 RTC library.
 +
#include <SPI.h>                    // SPI Interface that the SD card module will use.
 +
#include <SD.h>                    // For the SD card.
  
 
+
'''Flash the Microcontroller''' with '''Power-Fail-Logger''' Program
  For more information about using AVRDude refer back to this link
+
   Paste the program below '''Power-Fail-Logger Program.ino''' into the '''Arduino IDE'''.
  ==[[ AVRDUDE Class]]==
+
   In the '''Arduino IDE''' click on '''Sketch''' Tab - Select '''Upload Using Programmer'''
 
+
----
'''Setting up the DS3231 RTC'''
+
  /* Power_Fail_Logger.ino Jim Merkle and Rusty Cain 
   DS3231 RTC Datasheet: https://datasheets.maximintegrated.com/en/ds/DS3231.pdf
+
  * Microrusty.com Copyright 4-12-2021
  Insert a coin cell battery into DS3231. Either CR2032 or LI2032 coin cell battery will work.
+
  */
   Insert '''DS3231 module''' into the '''Power Logger PCB''' at J5.  
+
  #include <Wire.h>                   // For I2C Interface SDA SCK
   
+
   #include <ds3231.h>                // For the DS3231 RTC library
To Set the date and time use the follow program and paste in the Arduino IDE.<br>  
+
   #include <SPI.h>                   // SPI Interface that the SD card module will use
   //This code can be found here:https://github.com/rodan/ds3231
+
   #include <SD.h>                     // for the SD card
   #include <Wire.h>
+
  const int chipSelect = 4;          // Set Chip Select for SD card read/write
   #include <ds3231.h>
+
  File myFile;                        // Create a SD file to store the data
   struct ts t;  
+
  // our state values
 +
  #define POWER_NORMAL 0              // power has been good a long time
 +
  #define POWER_GONE  2              // Where's the power?????
 +
  #define POWER_UP  1
 +
  #define POWER_OFF 0
 +
  #define POWER_BAD_COUNT 3
 +
  #define POLL_NORMAL  5000          // 5000 ms = 5 seconds
 +
  #define POLL_BAD    5000          // 5000 ms = 5 seconds
 +
  int Vresistor = A0;                // A0 Pin on Arduino used to sample the power.
 +
                                      // Either +5 Volts (1023) or 0 volts (0)
 +
   struct ts t;
 +
  int currentpower = POWER_UP;        //currentpower is the value used to check power status
 +
  char time_as_text[80];              // build strings into this buffer
 
   void setup() {
 
   void setup() {
   Serial.begin(9600);
+
  // Setting up system
   Wire.begin();
+
   Serial.begin(9600);               // Start the Serial print for use with Serial monitor
 +
  while(!Serial );                  // Wait for process to start
 +
   Wire.begin();                     //Start the I2C interface. Pins A5 and A4
 +
  //  int number;                    // variable for the print2functions - needs to be removed
 +
                                    // Initialize the RTC.
 
   DS3231_init(DS3231_CONTROL_INTCN);
 
   DS3231_init(DS3231_CONTROL_INTCN);
  /*----------------------------------------------------------------------------
+
                                    // Print start up status for system to Console monitor
  In order to synchronise your clock module, insert timetable values below !
+
   Serial.println("POWER Logger Copyright 2021 Microrusty.com");
   ----------------------------------------------------------------------------*/
+
   gettime();                       //Read RTC DS3231 and display on Console Monitor
  t.hour=20;  
+
    Serial.print("\nInitializing SD card...");
   t.min=40;
+
                                    //  Check the SD card is there before writing to it
  t.sec=0;
+
    if(!SD.begin(chipSelect)) {
  t.mday=11;
+
      Serial.println("initialization failed!");
  t.mon=4;
+
        return;
  t.year=2021;
 
  DS3231_set(t); //Comment out after setting RTC with time & date
 
 
   }
 
   }
   
+
                                    // "initialization done."
 +
                                    //open file to record how many time it has rebooted
 +
    myFile=SD.open("Reboots.txt", FILE_WRITE); 
 +
    if (myFile) {                  // if the file opened ok, write to it:
 +
                                    // Reboots.txt File opened ok
 +
                                    //Writes "System Rebooted and gets time stamp.
 +
        myFile.print("System Rebooted ");
 +
        myFile.println(time_as_text);
 +
        myFile.close();            // Closes file Reboots.txt
 +
  }
 +
      Serial.println("Setting up logging time");
 +
                                  //  gettime(); //Remove ??
 +
      Logging();  
 +
  }
 +
                                    // Return POWER_UP: for power OK, POWER_OFF: power gone
 +
                                    // Initial power state and period values
 +
  int powerstate = POWER_NORMAL;
 +
  int pollperiod=POLL_NORMAL;
 +
  int powerbadcount=0;
 
   void loop() {
 
   void loop() {
   DS3231_get(&t);
+
  currentpower = checkpower();    //Check Power status and read value from A0
  Serial.print("Date : ");
+
                                  // Power Logging State Machine
  Serial.print(t.mon);
+
   switch(powerstate) {
  Serial.print("/");
+
      case POWER_NORMAL:
  Serial.print(t.mday);
+
                                  // Power is good
  Serial.print("/");
+
        if(currentpower == POWER_UP)
   Serial.print(t.year);
+
          break;                 // do nothing
   Serial.print("\t Hour : ");
+
                                  // we read a bad power reading, transition to POWER_GONE
   Serial.print(t.hour);
+
        powerstate = POWER_GONE;
  Serial.print(":");
+
        pollperiod = POLL_BAD;
  Serial.println(t.min);
+
        Serial.println("Transition to POWER_GONE");
   Serial.print(":");
+
                                  //Serial.print("Power Unstable: ");
   Serial.println(t.sec);
+
        gettime();
   delay(60000);
+
        Logging();
 +
        break;
 +
        case POWER_GONE:
 +
        if(currentpower == POWER_UP) {
 +
                                  // transition to power good
 +
          powerstate = POWER_NORMAL;
 +
          pollperiod = POLL_NORMAL;      
 +
          Serial.println("Transition to POWER_NORMAL");
 +
          Serial.print("Power Restored: ");
 +
          gettime();
 +
          Logging();
 +
            }
 +
        break;
 +
    }                            // end of switch state machine
 +
    delay(pollperiod);            // sleep(pollperiod); // 1s
 +
  }                              // end loop()
 +
  void gettime() {     
 +
   DS3231_get(&t);      
 +
   sprintf(time_as_text,"Date : %02u/%02u/%u    Time : %02u:%02u:%02u",
 +
    t.mon,t.mday,t.year,t.hour,t.min,t.sec);
 +
   Serial.println(time_as_text);  
 +
  // This Function Reads RTC and gets current time and Date then converts to text string.  
 +
  // DS3231_get(&t); Call to DS3231 library and reads the RTC
 +
  // Serial.println(time_as_text); Prints the converted String data from the RTC.
 +
  }
 +
  int checkpower()                          //Check Power status and read value from A0
 +
  {
 +
  int rawpower = analogRead(Vresistor);   // check Power at Pin A0
 +
   // check
 +
  if(rawpower > 250) {
 +
    Serial.println("Power Good ");
 +
    return POWER_UP;
 +
   }
 +
  else {
 +
    Serial.println("Power Failure: ");
 +
      return POWER_OFF;
 +
   }
 
   }
 
   }
   void print2digits(int number){
+
   void Logging() {
   if (number >= 0 && number < 10) {
+
  myFile = SD.open("DATALOG.txt", FILE_WRITE); // opens the file DATALOG, which is the text file, for writing the Pot value.
     Serial.write ('0');
+
  if (myFile) {
 +
   if (currentpower == POWER_UP) {
 +
     Serial.println("Power Good");              // Print Power Good to Console monitor
 +
    myFile.print("Power Good  : ");           // Print Power Good to SD Card
 
   }
 
   }
   Serial.print(number);
+
  else { 
   }  
+
   Serial.println("Power Failure");            // Print Power Failure to Console monitor
 
+
  myFile.print("Power Failure: ");           // Print Power Failure to SD Card
Connect the '''USBTinyISP''' to the '''AVR-ISP-6 pin header J7''' on the '''Power Fail Logger PCB''' .<br>
+
   }
In the Arduino IDE click on '''Tools''' Tab - Select '''Programmer''' then select '''USBTinyISP'''.<br>
+
  //sprintf(time_as_text,"Date : %u/%u/%u    Time : %u:%u:%u",
In the Arduino IDE click on '''Sketch''' Tab - Select '''Upload Using Programmer'''<br>
+
  //t.mon,t.mday,t.year,t.hour,t.min,t.sec)
Program should download. <br>
+
  myFile.println(time_as_text);
 +
  }
 +
  myFile.close();
 +
  Serial.println("Data written:");
 +
  }
 +
----
 +
'''Validate the Power-Fail-Logger Program is running'''
 +
  After program has uploaded  Validate
 +
  In the Arduino IDE click on '''Tools''' Tab - Select '''Serial Monitor'''<br>
 +
  Once the program has finished loading hit the Reset button on the Power Fail Logger Board.
 +
  You should see the program running in the display of the Serial Monitor.
 +
 
 +
  Unplug '''USBTiny Programmer'''
 +
  Remove '''Jumper''' from '''J8'''  
  
 +
'''Next Step:''' Installation and use. Click on link below <br>
 +
==[[ Power Fail Logger by Microrusty ]]==
 +
==[[Initializing the Microcontroller]]==
 +
==[[Setting Time and Date for the DS3231 RTC]]==
 +
==[[ Programming Power Fail Logger with Arduino IDE]]==
 +
==[[Installation and use]]==
 
----
 
----
==[[ Power Fail Logger by Microrusty ]]==
+
==[[ Main Page ]]==

Latest revision as of 21:46, 17 August 2021

Test the connectivity from the programmer to the Power Fail Logger board

 Using AVRDUDE validate the USBTiny is communicating with the Power Fail Module Microcontroller
 Connect the USBTinyISP to the AVR-ISP-6 pin header J7 on the Power Fail Logger PCB .
 On laptop using Windows CMD terminal type the following. Make sure you are in the AVRDude directory.
 avrdude -c usbtiny -p m328p -v
 
For more information about using AVRDude refer back to this link 
 ==AVRDUDE Class==

Connect Serial Port to the RS232 Port

 Plug into your laptop USB port the Prolific USB-to-Serial Comm Port 
 Connect the Prolific USB-to-Serial Comm Port to the RS232 Port on the Power Fail Board 
 Connect the TX, RX, and Ground Pins to J4.
         Prolific         J4 Power Logger
             TX      -    RX
             RX      -    TX
             GND     -    GND
 Use Windows Device Manager to validate which Comm Port is used. You will need this later.

Set up the Arduino IDE

 Connect the USBTinyISP to the AVR-ISP-6 pin header J7 on the Power Fail Logger PCB.
 Select your Programmer.
 In the Arduino IDE click on Tools Tab - Select Programmer then select USBTinyISP.
Select your comm Port. In the Arduino IDE click on Tools Tab - Select Port then select the comm-port that is using the Prolific Port Select the correct target board. In the Arduino IDE click on Tools Tab - Select Board then select the Arduino Uno.

Validate the Arduino IDE has the following libraries installed before proceeding with the flash process.

#include <Wire.h>                   // For I2C Interface SDA SCK.
#include <ds3231.h>                 // For the DS3231 RTC library.
#include <SPI.h>                    // SPI Interface that the SD card module will use.
#include <SD.h>                     // For the SD card. 

Flash the Microcontroller with Power-Fail-Logger Program

 Paste the program below Power-Fail-Logger Program.ino into the Arduino IDE.
 In the Arduino IDE click on Sketch Tab - Select Upload Using Programmer

 /* Power_Fail_Logger.ino Jim Merkle and Rusty Cain  
  *  Microrusty.com Copyright 4-12-2021
  */
 #include <Wire.h>                   // For I2C Interface SDA SCK
 #include <ds3231.h>                 // For the DS3231 RTC library
 #include <SPI.h>                    // SPI Interface that the SD card module will use
 #include <SD.h>                     // for the SD card
 const int chipSelect = 4;           // Set Chip Select for SD card read/write
 File myFile;                        // Create a SD file to store the data
 // our state values
 #define POWER_NORMAL 0              // power has been good a long time
 #define POWER_GONE   2              // Where's the power?????
 #define POWER_UP  1
 #define POWER_OFF 0
 #define POWER_BAD_COUNT 3
 #define POLL_NORMAL  5000           // 5000 ms = 5 seconds
 #define POLL_BAD     5000           // 5000 ms = 5 seconds
 int Vresistor = A0;                 // A0 Pin on Arduino used to sample the power. 
                                     // Either +5 Volts (1023) or 0 volts (0) 
 struct ts t;
 int currentpower = POWER_UP;        //currentpower is the value used to check power status
 char time_as_text[80];              // build strings into this buffer
 void setup() {
 // Setting up system
 Serial.begin(9600);               // Start the Serial print for use with Serial monitor
 while(!Serial );                  // Wait for process to start 
 Wire.begin();                     //Start the I2C interface. Pins A5 and A4
 //  int number;                     // variable for the print2functions - needs to be removed
                                   // Initialize the RTC.
 DS3231_init(DS3231_CONTROL_INTCN);
                                   // Print start up status for system to Console monitor
 Serial.println("POWER Logger Copyright 2021 Microrusty.com");
 gettime();                        //Read RTC DS3231 and display on Console Monitor
   Serial.print("\nInitializing SD card...");
                                   //  Check the SD card is there before writing to it 
   if(!SD.begin(chipSelect)) {
     Serial.println("initialization failed!");
       return;
 }
                                   // "initialization done."
                                   //open file to record how many time it has rebooted
    myFile=SD.open("Reboots.txt", FILE_WRITE);   
   if (myFile) {                   // if the file opened ok, write to it:
                                   // Reboots.txt File opened ok
                                   //Writes "System Rebooted and gets time stamp.
       myFile.print("System Rebooted "); 
       myFile.println(time_as_text);
       myFile.close();             // Closes file Reboots.txt
 }
     Serial.println("Setting up logging time");
                                 //   gettime(); //Remove ??
     Logging();  
 }
                                   // Return POWER_UP: for power OK, POWER_OFF: power gone
                                   // Initial power state and period values
 int powerstate = POWER_NORMAL;
 int pollperiod=POLL_NORMAL;
 int powerbadcount=0;
 void loop() {
  currentpower = checkpower();    //Check Power status and read value from A0
                                  // Power Logging State Machine
 switch(powerstate) {
     case POWER_NORMAL:
                                  // Power is good
       if(currentpower == POWER_UP)
         break;                  // do nothing
                                 // we read a bad power reading, transition to POWER_GONE
       powerstate = POWER_GONE;
       pollperiod = POLL_BAD;
       Serial.println("Transition to POWER_GONE");
                                 //Serial.print("Power Unstable: ");
       gettime();
       Logging();
       break;
       case POWER_GONE:
       if(currentpower == POWER_UP) {
                                 // transition to power good
         powerstate = POWER_NORMAL;
         pollperiod = POLL_NORMAL;       
         Serial.println("Transition to POWER_NORMAL");
         Serial.print("Power Restored: ");
         gettime();
         Logging();
            }
       break;
   }                             // end of switch state machine
   delay(pollperiod);            // sleep(pollperiod); // 1s
 }                               // end loop()
 void gettime() {       
 DS3231_get(&t);       
 sprintf(time_as_text,"Date : %02u/%02u/%u    Time : %02u:%02u:%02u",
    t.mon,t.mday,t.year,t.hour,t.min,t.sec);
 Serial.println(time_as_text); 
  // This Function Reads RTC and gets current time and Date then converts to text string. 
  // DS3231_get(&t); Call to DS3231 library and reads the RTC 
  // Serial.println(time_as_text); Prints the converted String data from the RTC.
 }
 int checkpower()                          //Check Power status and read value from A0
 {
 int rawpower = analogRead(Vresistor);   // check Power at Pin A0 
 // check
 if(rawpower > 250) {
   Serial.println("Power Good ");
   return POWER_UP;
 }
 else {
   Serial.println("Power Failure: ");
      return POWER_OFF;
 }
 }
 void Logging() {
  myFile = SD.open("DATALOG.txt", FILE_WRITE); // opens the file DATALOG, which is the text file, for writing the Pot value.
 if (myFile) {
 if (currentpower == POWER_UP) {
   Serial.println("Power Good");               // Print Power Good to Console monitor
   myFile.print("Power Good   : ");            // Print Power Good to SD Card
 }
 else {  
 Serial.println("Power Failure");            // Print Power Failure to Console monitor
 myFile.print("Power Failure: ");            // Print Power Failure to SD Card
 }
 //sprintf(time_as_text,"Date : %u/%u/%u    Time : %u:%u:%u",
 //t.mon,t.mday,t.year,t.hour,t.min,t.sec)
 myFile.println(time_as_text);
 }
 myFile.close();
 Serial.println("Data written:");
 }

Validate the Power-Fail-Logger Program is running

 After program has uploaded   Validate 
 In the Arduino IDE click on Tools Tab - Select Serial Monitor
Once the program has finished loading hit the Reset button on the Power Fail Logger Board. You should see the program running in the display of the Serial Monitor. Unplug USBTiny Programmer Remove Jumper from J8

Next Step: Installation and use. Click on link below

Power Fail Logger by Microrusty

Initializing the Microcontroller

Setting Time and Date for the DS3231 RTC

Programming Power Fail Logger with Arduino IDE

Installation and use


Main Page