Difference between revisions of "Programming Power Fail Logger with Arduino IDE"
Line 173: | Line 173: | ||
Unplug '''USBTiny Programmer''' | Unplug '''USBTiny Programmer''' | ||
Remove '''Jumper''' from '''J8''' | Remove '''Jumper''' from '''J8''' | ||
− | + | ||
==[[Initializing the Microcontroller]]== | ==[[Initializing the Microcontroller]]== | ||
==[[Setting Time and Date for the DS3231 RTC]]== | ==[[Setting Time and Date for the DS3231 RTC]]== | ||
---- | ---- | ||
==[[ Power Fail Logger by Microrusty ]]== | ==[[ Power Fail Logger by Microrusty ]]== | ||
+ | ==[[ Main Page]]== |
Revision as of 18:42, 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