#define max471v A2 #define max471a A1 #define acs A0 #define max_sn "max0" #define acs_sn "acs0" int mVperAmp = 66; // use 100 for 20A Module and 66 for 30A Module int acs_offset = 2500; long readVcc() { // Read 1.1V reference against AVcc // set the reference to Vcc and the measurement to the internal 1.1V reference #if defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) ADMUX = _BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); #elif defined (__AVR_ATtiny24__) || defined(__AVR_ATtiny44__) || defined(__AVR_ATtiny84__) ADMUX = _BV(MUX5) | _BV(MUX0); #elif defined (__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__) ADMUX = _BV(MUX3) | _BV(MUX2); #else ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); #endif delay(2); // Wait for Vref to settle ADCSRA |= _BV(ADSC); // Start conversion while (bit_is_set(ADCSRA,ADSC)); // measuring uint8_t low = ADCL; // must read ADCL first - it then locks ADCH uint8_t high = ADCH; // unlocks both long result = (high<<8) | low; result = 1125300L / result; // Calculate Vcc (in mV); 1125300 = 1.1*1023*1000 return result; // Vcc in millivolts } void show_max() { float voltage = readVcc() / 1000.0; // Serial.println(voltage); int rawmax_v = analogRead(max471v); int rawmax_a = analogRead(max471a); double max_a = rawmax_a * (voltage*2 / 1024.0); double max_v = rawmax_v * 5.0 / 1024.0 * 5; // Serial.println(rawmax_v); //3 digits after decimal point // Serial.println(rawmax_a); //3 digits after decimal point //w_sensor=type:VOLT,sn:max0,curr:xxx,volt:xxx; Serial.print("type:VOLT,sn:"); Serial.print(max_sn); Serial.print(","); Serial.print("curr:"); Serial.print(max_a,3); //3 digits after decimal point Serial.print(",volt:"); Serial.print(max_v,3); //3 digits after decimal point Serial.print(",rawa:"); Serial.print(rawmax_a); //3 digits after decimal point Serial.print(",rawv:"); Serial.print(rawmax_v); Serial.println(";"); } void show_acs() { int i; int rawacs; for (i = 0, rawacs = 0; i < 10; i++) { rawacs += analogRead(acs); } rawacs = rawacs / 10; // rawacs += 2; //calibration double v = rawacs * 5.0 / 1024.0; //Serial.println(v); double acs_a = (v - 2.5) / 0.066; Serial.print("type:VOLT,sn:"); Serial.print(acs_sn); Serial.print(",curr:"); Serial.print(acs_a,3); //3 digits after decimal point Serial.print(",raw:"); Serial.print(rawacs); //3 digits after decimal point Serial.println(";"); } void setup() { pinMode(LED_BUILTIN, OUTPUT); pinMode(max471v, INPUT); pinMode(max471a, INPUT); pinMode(acs, INPUT); Serial.begin(9600); } void loop() { show_max(); delay(2500); show_acs(); delay(2500); }