Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2017
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.86 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <Wire.h>
  3. #include <Adafruit_ADS1015.h>
  4.  
  5. Adafruit_ADS1115 ads1115(0x48); /* Use this for the 16-bit version */
  6.  
  7. double offsetI;
  8. double filteredI;
  9. double sqI,sumI;
  10. int sampleI;
  11. //int16_t sampleI;
  12. double Irms;
  13.  
  14. double calcIrms(unsigned int Number_of_Samples)
  15. {
  16. for (unsigned int n = 0; n < Number_of_Samples; n++)
  17. {
  18. sampleI = ads1115.readADC_Differential_0_1();
  19.  
  20. Serial.println("Sample read:");
  21. Serial.println(sampleI);
  22.  
  23. // Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
  24. // then subtract this - signal is now centered on 0 counts.
  25. offsetI = (offsetI + (sampleI-offsetI)/1024);
  26.  
  27. Serial.println("Offset read:");
  28. Serial.println(offsetI);
  29.  
  30. filteredI = sampleI - offsetI;
  31.  
  32. // Root-mean-square method current
  33. // 1) square current values
  34.  
  35. sqI = filteredI * filteredI;
  36. // 2) sum
  37. sumI += sqI;
  38.  
  39. Serial.println("sum");
  40. Serial.println(sumI);
  41. Serial.println();
  42. Serial.println();
  43. }
  44.  
  45. double ICAL;
  46. //ICAL based on 100A:50mA CT with 22Ohms burden resistor
  47. ICAL = 90.90;
  48.  
  49. int SupplyVoltage=4096;
  50. int ADC_COUNTS=32768; //16-bit ads1115 i differential modus
  51.  
  52. double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
  53. Irms = I_RATIO * sqrt(sumI / Number_of_Samples);
  54.  
  55. //Reset accumulators
  56. sumI = 0;
  57. //--------------------------------------------------------------------------------------
  58.  
  59. return Irms;
  60. }
  61.  
  62. void setup() {
  63. Serial.begin(9600);
  64.  
  65. ads1115.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
  66. ads1115.begin();
  67.  
  68. }
  69.  
  70. void loop(){
  71. double current = calcIrms(172);
  72.  
  73. Serial.println();
  74. Serial.println();
  75. Serial.println("Calculated current:");
  76. Serial.println(current); Serial.println("A");
  77. Serial.println();
  78.  
  79.  
  80. delay(30000);
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement