Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <Wire.h>
- #include <Adafruit_ADS1015.h>
- Adafruit_ADS1115 ads1115(0x48); /* Use this for the 16-bit version */
- double offsetI;
- double filteredI;
- double sqI,sumI;
- int sampleI;
- //int16_t sampleI;
- double Irms;
- double calcIrms(unsigned int Number_of_Samples)
- {
- for (unsigned int n = 0; n < Number_of_Samples; n++)
- {
- sampleI = ads1115.readADC_Differential_0_1();
- Serial.println("Sample read:");
- Serial.println(sampleI);
- // Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
- // then subtract this - signal is now centered on 0 counts.
- offsetI = (offsetI + (sampleI-offsetI)/1024);
- Serial.println("Offset read:");
- Serial.println(offsetI);
- filteredI = sampleI - offsetI;
- // Root-mean-square method current
- // 1) square current values
- sqI = filteredI * filteredI;
- // 2) sum
- sumI += sqI;
- Serial.println("sum");
- Serial.println(sumI);
- Serial.println();
- Serial.println();
- }
- double ICAL;
- //ICAL based on 100A:50mA CT with 22Ohms burden resistor
- ICAL = 90.90;
- int SupplyVoltage=4096;
- int ADC_COUNTS=32768; //16-bit ads1115 i differential modus
- double I_RATIO = ICAL *((SupplyVoltage/1000.0) / (ADC_COUNTS));
- Irms = I_RATIO * sqrt(sumI / Number_of_Samples);
- //Reset accumulators
- sumI = 0;
- //--------------------------------------------------------------------------------------
- return Irms;
- }
- void setup() {
- Serial.begin(9600);
- ads1115.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
- ads1115.begin();
- }
- void loop(){
- double current = calcIrms(172);
- Serial.println();
- Serial.println();
- Serial.println("Calculated current:");
- Serial.println(current); Serial.println("A");
- Serial.println();
- delay(30000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement