Components and supplies
Arduino UNO
LCD 16x2 i²c
OLED i²c 128x32
ACS712 30A
Project description
Code
Code_3.ino
arduino
This code uses statistics formulas to calculate the TRMS of a signal whatever the sape and displays it on the serial monitor
1/* This code works with ACS712 current sensor, it permits the calculation of the signal TRMS 2 * Visit www.surtrtech.com for more details 3 */ 4 5#include <Filters.h> //This library does a massive work check it's .cpp file 6 7#define ACS_Pin A0 //Sensor data pin on A0 analog input 8 9float ACS_Value; //Here we keep the raw data valuess 10float testFrequency = 50; // test signal frequency (Hz) 11float windowLength = 40.0/testFrequency; // how long to average the signal, for statistist 12 13 14 15float intercept = 0; // to be adjusted based on calibration testing 16float slope = 0.0752; // to be adjusted based on calibration testing 17 //Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below 18 19 20float Amps_TRMS; // estimated actual current in amps 21 22unsigned long printPeriod = 1000; // in milliseconds 23// Track time in milliseconds since last reading 24unsigned long previousMillis = 0; 25 26void setup() { 27 Serial.begin( 9600 ); // Start the serial port 28 pinMode(ACS_Pin,INPUT); //Define the pin mode 29} 30 31void loop() { 32 RunningStatistics inputStats; // create statistics to look at the raw test signal 33 inputStats.setWindowSecs( windowLength ); //Set the window length 34 35 while( true ) { 36 ACS_Value = analogRead(ACS_Pin); // read the analog in value: 37 inputStats.input(ACS_Value); // log to Stats function 38 39 if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation 40 previousMillis = millis(); // update time 41 42 Amps_TRMS = intercept + slope * inputStats.sigma(); 43 44 Serial.print( "\ Amps: " ); 45 Serial.print( Amps_TRMS ); 46 47 } 48 } 49} 50 51/* About the slope and intercept 52 * First you need to know that all the TRMS calucations are done by functions from the library, it's the "inputStats.sigma()" value 53 * At first you can display that "inputStats.sigma()" as your TRMS value, then try to measure using it when the input is 0.00A 54 * If the measured value is 0 like I got you can keep the intercept as 0, otherwise you'll need to add or substract to make that value equal to 0 55 * In other words " remove the offset" 56 * Then turn on the power to a known value, for example use a bulb or a led that ou know its power and you already know your voltage, so a little math you'll get the theoritical amps 57 * you divide that theory value by the measured value and here you got the slope, now place them or modify them 58 */ 59
Code_1.ino
arduino
This code is to test the module and visualize the signal shap
1/*This code works with ACS712 Current sensor, it permits to read the raw data 2 It's better to use it with Serial Plotter 3 More details on www.surtrtech.com 4*/ 5 6#define Current_sensor A0 //The sensor analog input pin 7 8float i; 9 10 11void setup() { 12 13Serial.begin(9600); 14pinMode(Current_sensor, INPUT); 15 16} 17 18void loop() { 19 i = analogRead(Current_sensor); 20 Serial.println(i); 21 delay(100); //Modifying or removing the delay will change the way the signal is shown 22 //set it until you get the correct sinewave shap 23 24} 25
Code_ACS712_OLED.ino
arduino
This code uses the TRMS calculation method and displays the value on the OLED.
1/* This code works with ACS712 and OLED ic 2 * It measure the TRMS of an Alternating Current and displays the value on the screen 3 * Visit www.SurtrTech.com for more details 4 */ 5 6 7#include <Filters.h> //This library does a huge work check its .cpp file 8#include <Adafruit_GFX.h> //OLED libraries 9#include <Adafruit_SSD1306.h> 10 11#define ACS_Pin A0 12 13#define SCREEN_WIDTH 128 // OLED display width, in pixels 14#define SCREEN_HEIGHT 32 // OLED display height, in pixels 15#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin) 16 17float testFrequency = 50; // test signal frequency (Hz) 18float windowLength = 40.0/testFrequency; // how long to average the signal, for statistist 19 20float intercept = 0; // to be adjusted based on calibration testing 21float slope = 0.0752; // to be adjusted based on calibration testing 22 //Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below 23 24float Amps_TRMS; 25float ACS_Value; 26 27unsigned long printPeriod = 1000; 28unsigned long previousMillis = 0; 29 30Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display) 31 32void setup() { 33 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display 34 display.clearDisplay(); 35 display.display(); 36} 37 38void loop() { 39 RunningStatistics inputStats; // create statistics to look at the raw test signal 40 inputStats.setWindowSecs( windowLength ); 41 42 while( true ) { 43 ACS_Value = analogRead(ACS_Pin); // read the analog in value: 44 inputStats.input(ACS_Value); // log to Stats function 45 46 if((unsigned long)(millis() - previousMillis) >= printPeriod) { //Do the calculations every 1s 47 previousMillis = millis(); // update time 48 49 Amps_TRMS = intercept + slope * inputStats.sigma(); 50 display.clearDisplay(); 51 display.setTextSize(3); 52 display.setTextColor(WHITE); 53 display.setCursor(15,10); 54 display.print(Amps_TRMS); 55 display.println(" A"); 56 display.display(); 57 } 58 } 59} 60 61/* About the slope and intercept 62 * First you need to know that all the TRMS calucations are done by functions from the library, it's the "inputStats.sigma()" value 63 * At first you can display that "inputStats.sigma()" as your TRMS value, then try to measure using it when the input is 0.00A 64 * If the measured value is 0 like I got you can keep the intercept as 0, otherwise you'll need to add or substract to make that value equal to 0 65 * In other words " remove the offset" 66 * Then turn on the power to a known value, for example use a bulb or a led that ou know its power and you already know your voltage, so a little math you'll get the theoritical amps 67 * you divide that theory value by the measured value and here you got the slope, now place them or modify them 68 */ 69
Code_ACS712_LCD.ino
arduino
This code uses the TRMS calculation method and displays it on the LCD i²c
1/* This code works with ACS712 and LCD ic 2 * It measure the TRMS of an Alternating Current and displays the value on the screen 3 * Visit www.SurtrTech.com for more details 4 */ 5 6#include <Filters.h> //This library does a huge work check its .cpp file 7#include <LiquidCrystal_I2C.h> //LCD ic library 8 9 10#define ACS_Pin A0 //ACS712 data pin 11 12#define I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) here 13#define BACKLIGHT_PIN 3 // Declaring LCD Pins 14#define En_pin 2 15#define Rw_pin 1 16#define Rs_pin 0 17#define D4_pin 4 18#define D5_pin 5 19#define D6_pin 6 20#define D7_pin 7 21 22LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); //Declaring the lcd 23 24 25float testFrequency = 50; // test signal frequency (Hz) 26float windowLength = 40.0/testFrequency; // how long to average the signal, for statistist 27 28float intercept = 0; // to be adjusted based on calibration testing 29float slope = 0.0752; // to be adjusted based on calibration testing 30 //Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below 31 32float Amps_TRMS; 33float ACS_Value; 34 35unsigned long printPeriod = 1000; 36unsigned long previousMillis = 0; 37 38 39 40void setup() { 41 digitalWrite(2,HIGH); 42 lcd.begin (16,2); 43 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 44 lcd.setBacklight(HIGH); //Lighting backlight 45 lcd.home (); 46 47 48} 49 50void loop() { 51 RunningStatistics inputStats; // create statistics to look at the raw test signal 52 inputStats.setWindowSecs( windowLength ); 53 54 while( true ) { 55 ACS_Value = analogRead(ACS_Pin); // read the analog in value: 56 inputStats.input(ACS_Value); // log to Stats function 57 58 if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation 59 previousMillis = millis(); // update time 60 61 Amps_TRMS = intercept + slope * inputStats.sigma(); //Calibrate the values 62 lcd.clear(); //clear the lcd and print in a certain position 63 lcd.setCursor(2,0); 64 lcd.print(Amps_TRMS); 65 lcd.print(" A"); 66 67 } 68 } 69} 70 71/* About the slope and intercept 72 * First you need to know that all the TRMS calucations are done by functions from the library, it's the "inputStats.sigma()" value 73 * At first you can display that "inputStats.sigma()" as your TRMS value, then try to measure using it when the input is 0.00A 74 * If the measured value is 0 like I got you can keep the intercept as 0, otherwise you'll need to add or substract to make that value equal to 0 75 * In other words " remove the offset" 76 * Then turn on the power to a known value, for example use a bulb or a led that ou know its power and you already know your voltage, so a little math you'll get the theoritical amps 77 * you divide that theory value by the measured value and here you got the slope, now place them or modify them 78 */ 79
Code_1.ino
arduino
This code is to test the module and visualize the signal shap
1/*This code works with ACS712 Current sensor, it permits to read the raw 2 data 3 It's better to use it with Serial Plotter 4 More details on www.surtrtech.com 5*/ 6 7#define 8 Current_sensor A0 //The sensor analog input pin 9 10float i; 11 12 13void 14 setup() { 15 16Serial.begin(9600); 17pinMode(Current_sensor, INPUT); 18 19} 20 21void 22 loop() { 23 i = analogRead(Current_sensor); 24 Serial.println(i); 25 delay(100); 26 //Modifying or removing the delay will change the way the signal 27 is shown 28 //set it until you get the correct 29 sinewave shap 30 31} 32
Code_ACS712_OLED.ino
arduino
This code uses the TRMS calculation method and displays the value on the OLED.
1/* This code works with ACS712 and OLED ic 2 * It measure the TRMS of 3 an Alternating Current and displays the value on the screen 4 * Visit www.SurtrTech.com 5 for more details 6 */ 7 8 9#include <Filters.h> //This library 10 does a huge work check its .cpp file 11#include <Adafruit_GFX.h> //OLED libraries 12#include 13 <Adafruit_SSD1306.h> 14 15#define ACS_Pin A0 16 17#define SCREEN_WIDTH 128 // 18 OLED display width, in pixels 19#define SCREEN_HEIGHT 32 // OLED display height, 20 in pixels 21#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset 22 pin) 23 24float testFrequency = 50; // test signal frequency 25 (Hz) 26float windowLength = 40.0/testFrequency; // how long to average the 27 signal, for statistist 28 29float intercept = 0; // to be adjusted based on calibration 30 testing 31float slope = 0.0752; // to be adjusted based on calibration testing 32 33 //Please check the ACS712 Tutorial video by SurtrTech to see 34 how to get them because it depends on your sensor, or look below 35 36float Amps_TRMS; 37 38float ACS_Value; 39 40unsigned long printPeriod = 1000; 41unsigned long 42 previousMillis = 0; 43 44Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, 45 &Wire, OLED_RESET); //Declaring the display name (display) 46 47void setup() { 48 49 display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start the OLED display 50 display.clearDisplay(); 51 52 display.display(); 53} 54 55void loop() { 56 RunningStatistics inputStats; 57 // create statistics to look at the raw test signal 58 inputStats.setWindowSecs( 59 windowLength ); 60 61 while( true ) { 62 ACS_Value = analogRead(ACS_Pin); 63 // read the analog in value: 64 inputStats.input(ACS_Value); // log to Stats 65 function 66 67 if((unsigned long)(millis() - previousMillis) >= printPeriod) 68 { //Do the calculations every 1s 69 previousMillis = millis(); // update 70 time 71 72 Amps_TRMS = intercept + slope * inputStats.sigma(); 73 74 display.clearDisplay(); 75 display.setTextSize(3); 76 77 display.setTextColor(WHITE); 78 display.setCursor(15,10); 79 80 display.print(Amps_TRMS); 81 display.println(" 82 A"); 83 display.display(); 84 } 85 } 86} 87 88/* About the slope 89 and intercept 90 * First you need to know that all the TRMS calucations are done 91 by functions from the library, it's the "inputStats.sigma()" value 92 * At first 93 you can display that "inputStats.sigma()" as your TRMS value, then try to measure 94 using it when the input is 0.00A 95 * If the measured value is 0 like I got you 96 can keep the intercept as 0, otherwise you'll need to add or substract to make that 97 value equal to 0 98 * In other words " remove the offset" 99 * Then turn on 100 the power to a known value, for example use a bulb or a led that ou know its power 101 and you already know your voltage, so a little math you'll get the theoritical amps 102 103 * you divide that theory value by the measured value and here you got the slope, 104 now place them or modify them 105 */ 106
Code_ACS712_LCD.ino
arduino
This code uses the TRMS calculation method and displays it on the LCD i²c
1/* This code works with ACS712 and LCD ic 2 * It measure the TRMS of 3 an Alternating Current and displays the value on the screen 4 * Visit www.SurtrTech.com 5 for more details 6 */ 7 8#include <Filters.h> //This library 9 does a huge work check its .cpp file 10#include <LiquidCrystal_I2C.h> //LCD 11 ic library 12 13 14#define ACS_Pin A0 //ACS712 data pin 15 16#define 17 I2C_ADDR 0x27 //I2C adress, you should use the code to scan the adress first (0x27) 18 here 19#define BACKLIGHT_PIN 3 // Declaring LCD Pins 20#define En_pin 2 21#define 22 Rw_pin 1 23#define Rs_pin 0 24#define D4_pin 4 25#define D5_pin 5 26#define 27 D6_pin 6 28#define D7_pin 7 29 30LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin); 31 //Declaring the lcd 32 33 34float testFrequency = 50; // 35 test signal frequency (Hz) 36float windowLength = 40.0/testFrequency; // how 37 long to average the signal, for statistist 38 39float intercept = 0; // to be 40 adjusted based on calibration testing 41float slope = 0.0752; // to be adjusted 42 based on calibration testing 43 //Please check the ACS712 44 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, 45 or look below 46 47float Amps_TRMS; 48float ACS_Value; 49 50unsigned long 51 printPeriod = 1000; 52unsigned long previousMillis = 0; 53 54 55 56void setup() 57 { 58 digitalWrite(2,HIGH); 59 lcd.begin (16,2); 60 lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE); 61 62 lcd.setBacklight(HIGH); //Lighting backlight 63 lcd.home (); 64 65 66} 67 68void 69 loop() { 70 RunningStatistics inputStats; // create statistics 71 to look at the raw test signal 72 inputStats.setWindowSecs( windowLength ); 73 74 75 while( true ) { 76 ACS_Value = analogRead(ACS_Pin); // read the 77 analog in value: 78 inputStats.input(ACS_Value); // log to Stats function 79 80 81 if((unsigned long)(millis() - previousMillis) >= printPeriod) { 82 //every second we do the calculation 83 previousMillis = millis(); // update 84 time 85 86 Amps_TRMS = intercept + slope * inputStats.sigma(); //Calibrate 87 the values 88 lcd.clear(); //clear the lcd and print in a certain 89 position 90 lcd.setCursor(2,0); 91 lcd.print(Amps_TRMS); 92 lcd.print(" 93 A"); 94 95 } 96 } 97} 98 99/* About the slope and intercept 100 * First 101 you need to know that all the TRMS calucations are done by functions from the library, 102 it's the "inputStats.sigma()" value 103 * At first you can display that "inputStats.sigma()" 104 as your TRMS value, then try to measure using it when the input is 0.00A 105 * If 106 the measured value is 0 like I got you can keep the intercept as 0, otherwise you'll 107 need to add or substract to make that value equal to 0 108 * In other words " remove 109 the offset" 110 * Then turn on the power to a known value, for example use a bulb 111 or a led that ou know its power and you already know your voltage, so a little math 112 you'll get the theoritical amps 113 * you divide that theory value by the measured 114 value and here you got the slope, now place them or modify them 115 */ 116
Code_3.ino
arduino
This code uses statistics formulas to calculate the TRMS of a signal whatever the sape and displays it on the serial monitor
1/* This code works with ACS712 current sensor, it permits the calculation of the signal TRMS 2 * Visit www.surtrtech.com for more details 3 */ 4 5#include <Filters.h> //This library does a massive work check it's .cpp file 6 7#define ACS_Pin A0 //Sensor data pin on A0 analog input 8 9float ACS_Value; //Here we keep the raw data valuess 10float testFrequency = 50; // test signal frequency (Hz) 11float windowLength = 40.0/testFrequency; // how long to average the signal, for statistist 12 13 14 15float intercept = 0; // to be adjusted based on calibration testing 16float slope = 0.0752; // to be adjusted based on calibration testing 17 //Please check the ACS712 Tutorial video by SurtrTech to see how to get them because it depends on your sensor, or look below 18 19 20float Amps_TRMS; // estimated actual current in amps 21 22unsigned long printPeriod = 1000; // in milliseconds 23// Track time in milliseconds since last reading 24unsigned long previousMillis = 0; 25 26void setup() { 27 Serial.begin( 9600 ); // Start the serial port 28 pinMode(ACS_Pin,INPUT); //Define the pin mode 29} 30 31void loop() { 32 RunningStatistics inputStats; // create statistics to look at the raw test signal 33 inputStats.setWindowSecs( windowLength ); //Set the window length 34 35 while( true ) { 36 ACS_Value = analogRead(ACS_Pin); // read the analog in value: 37 inputStats.input(ACS_Value); // log to Stats function 38 39 if((unsigned long)(millis() - previousMillis) >= printPeriod) { //every second we do the calculation 40 previousMillis = millis(); // update time 41 42 Amps_TRMS = intercept + slope * inputStats.sigma(); 43 44 Serial.print( "\ Amps: " ); 45 Serial.print( Amps_TRMS ); 46 47 } 48 } 49} 50 51/* About the slope and intercept 52 * First you need to know that all the TRMS calucations are done by functions from the library, it's the "inputStats.sigma()" value 53 * At first you can display that "inputStats.sigma()" as your TRMS value, then try to measure using it when the input is 0.00A 54 * If the measured value is 0 like I got you can keep the intercept as 0, otherwise you'll need to add or substract to make that value equal to 0 55 * In other words " remove the offset" 56 * Then turn on the power to a known value, for example use a bulb or a led that ou know its power and you already know your voltage, so a little math you'll get the theoritical amps 57 * you divide that theory value by the measured value and here you got the slope, now place them or modify them 58 */ 59
Code_2.ino
arduino
This code uses the peak-to-peak measuring method to calculate the RMS of a sinewave signal
1/* This code works with ACS712 current sensor, it permits to calculate the RMS of a sinewave Alternating Current 2 * it uses the Peak to Peak method to calculate the RMS 3 * For more information check www.surtrtech.com 4 */ 5 6#define SAMPLES 300 //Number of samples you want to take everytime you loop 7#define ACS_Pin A0 //ACS712 data pin analong input 8 9 10float High_peak,Low_peak; //Variables to measure or calculate 11float Amps_Peak_Peak, Amps_RMS; 12 13 14void setup() 15{ 16 Serial.begin(9600); 17 pinMode(ACS_Pin,INPUT); //Define pin mode 18} 19 20void loop() 21{ 22 23 read_Amps(); //Launch the read_Amps function 24 Amps_RMS = Amps_Peak_Peak*0.3536*0.06; //Now we have the peak to peak value normally the formula requires only multiplying times 0.3536 25 //but since the values will be very big you should multiply by 0.06, you can first not use it, 26 //do your calculations and compare them to real values measured by an Ammeter. eg: 0.06=Real value/Measured value 27 28 Serial.print(Amps_RMS); //Here I show the RMS value and the peak to peak value, you can print what you want and add the "A" symbol... 29 Serial.print("\ "); 30 Serial.println(Amps_Peak_Peak); 31 delay(200); 32} 33 34void read_Amps() //read_Amps function calculate the difference between the high peak and low peak 35{ //get peak to peak value 36 int cnt; //Counter 37 High_peak = 0; //We first assume that our high peak is equal to 0 and low peak is 1024, yes inverted 38 Low_peak = 1024; 39 40 for(cnt=0 ; cnt<SAMPLES ; cnt++) //everytime a sample (module value) is taken it will go through test 41 { 42 float ACS_Value = analogRead(ACS_Pin); //We read a single value from the module 43 44 45 if(ACS_Value > High_peak) //If that value is higher than the high peak (at first is 0) 46 { 47 High_peak = ACS_Value; //The high peak will change from 0 to that value found 48 } 49 50 if(ACS_Value < Low_peak) //If that value is lower than the low peak (at first is 1024) 51 { 52 Low_peak = ACS_Value; //The low peak will change from 1024 to that value found 53 } 54 } //We keep looping until we take all samples and at the end we will have the high/low peaks values 55 56 Amps_Peak_Peak = High_peak - Low_peak; //Calculate the difference 57} 58
Downloadable files
Wiring_OLED
Use OLED 128x32
Wiring_OLED
Wiring 1
Wiring used of tests 1,2 and 3
Wiring 1
Wiring_LCD
Use LCD i²c
Wiring_LCD
Wiring_OLED
Use OLED 128x32
Wiring_OLED
Wiring 1
Wiring used of tests 1,2 and 3
Wiring 1
Wiring_LCD
Use LCD i²c
Wiring_LCD
Comments
Only logged in users can leave comments
SurtrTech
0 Followers
•0 Projects
18
0