I've managed to find some code to help with getting a True RMS reading from the ACS712 modules using the filters library. While this works fine and all with a single sensor, I'm trying to monitor a 3 phase motor for any variation in the phases. I'm not too hot with modifying the code to accommodate the other two sensors and any kind of addition with extra code using a 5kg hammer just completely stops it from working. So far I've only managed to add code for a Nokia display and I've prototyped a board for another two sensors on the A0 and A1 inputs. Any kind of guiding hand would be nice if possible, please.
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
#include <Wire.h>
#include <Filters.h> //This library does a massive work check it's .cpp file
//#include <LiquidCrystal.h>
#define ACS_Pin A2
#define backLight 2
//Sensor data pin on A2 analog input
//const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
//LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
//const int numRows = 2;
//const int numCols = 16;
Adafruit_PCD8544 display = Adafruit_PCD8544(7, 6, 5, 4, 3);
float ACS_Value; //Here we keep the raw data valuess
float testFrequency = 50; // test signal frequency (Hz)
float windowLength = 40.0/testFrequency; // how long to average the signal, for statistist
int Page = 1;
float intercept = -0.1; // to be adjusted based on calibration testing
float slope = 0.0647; // to be adjusted based on calibration testing
//Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below
float Amps_TRMS; // estimated actual current in amps
float amp1Display;
unsigned long printPeriod = 1000; // in milliseconds
// Track time in milliseconds since last reading
unsigned long previousMillis = 0;
void setup() {
analogReference(EXTERNAL);
Serial.begin( 9600 ); // Start the serial port
pinMode(ACS_Pin,INPUT); //Define the pin mode
pinMode(backLight, OUTPUT);
display.begin();
display.setContrast(50);
display.display();
analogWrite(backLight, 150);
}
void loop() {
RunningStatistics inputStats; // create statistics to look at the raw test signal
inputStats.setWindowSecs( windowLength ); //Set the window length
while( true ){
ACS_Value = analogRead(ACS_Pin); // read the analog in value:
inputStats.input(ACS_Value); // log to Stats function
if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation
previousMillis = millis(); // update time
Amps_TRMS = intercept + slope * inputStats.sigma();
Serial.print( "\t without calc: " );
Serial.print(inputStats.sigma());
Serial.print( "\t Amps: " );
Serial.print(Amps_TRMS);
Serial.print( "\t Sigma: " );
Serial.print( inputStats.sigma() * 0.0647 ); //calibrate line
Serial.print( "\t Display 1: " );
Serial.println(amp1Display);
if (Amps_TRMS <= 0){
amp1Display = 0.00;
}
else
amp1Display = Amps_TRMS;
screen1();
}
}
}
void screen1(){
display.setTextSize(1);
display.clearDisplay();
display.setTextColor(BLACK, WHITE);
display.setCursor(5, 2);
display.print("Current Info");
display.drawFastHLine(0,10,83,BLACK);
display.drawFastHLine(0,0,83,BLACK);
display.drawFastHLine(0,47,83,BLACK);
display.drawFastVLine(0,0,50,BLACK);
display.drawFastVLine(83,0,50,BLACK);
display.setCursor(5, 12);
display.print("Phase1:");
display.setCursor(50, 12);
display.print(amp1Display);
display.display();
}