Last active
June 9, 2016 09:17
-
-
Save wisewolf/deb67ae31c91c213d19be23109c890cc to your computer and use it in GitHub Desktop.
Max471 Voltage/Current Sensor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MAX471 | |
*/ | |
long milisec = millis(); // calculate time in milliseconds | |
long time = milisec/1000; // convert milliseconds to seconds | |
const int max471VT = A0; | |
const int max471AT = A1; | |
unsigned int RawVT= 0; | |
unsigned int RawAT= 0; | |
float Voltage = 0; | |
float Current = 0; | |
float Vcc; | |
float Power = 0; | |
float Energy = 0; | |
float SumCurrent = 0; | |
float AvgCurrent = 0; | |
float CurrentHour = 0; | |
long readVcc() { | |
long result; | |
// Read 1.1V reference against AVcc | |
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); | |
delay(2); // Wait for Vref to settle | |
ADCSRA |= _BV(ADSC); // Convert | |
while (bit_is_set(ADCSRA,ADSC)); | |
result = ADCL; | |
result |= ADCH<<8; | |
result = 1125300L / result; // Back-calculate AVcc in mV | |
return result; | |
} | |
void setup(){ | |
pinMode(max471VT, INPUT); | |
pinMode(max471AT, INPUT); | |
Serial.begin(115200); | |
} | |
void loop(){ | |
RawVT = analogRead(max471VT); | |
RawAT = analogRead(max471AT); | |
Vcc = readVcc() / 1000.0; | |
Voltage = (RawVT / 1023) * Vcc; // scale the ADC | |
Current = (RawAT / 1023) * Vcc; // scale the ADC | |
Power = Voltage * Current; // Ohm's Law | |
Energy = (Power * time) / 3600; // Watt-sec is again convert to Watt-Hr by dividing 1hr (3600sec) | |
// Energy = (Power * time) / (1000*3600); for reading in kWh | |
SumCurrent += Current; // calculate total current | |
AvgCurrent = SumCurrent / time; // average current | |
CurrentHour = (AvgCurrent * time) / 3600; // Ah | |
Serial.print("Voltage = "); // shows the voltage measured | |
Serial.print(Voltage,3); // 3 digits after decimal point | |
Serial.println(" V"); // 3 digits after decimal point | |
Serial.print("Current = "); // shows the voltage measured | |
Serial.print(Current,3); // 3 digits after decimal point | |
Serial.println(" A"); // 3 digits after decimal point | |
Serial.print("Power = "); // shows the voltage measured | |
Serial.print(Power,3); // 3 digits after decimal point | |
Serial.println(" W"); // 3 digits after decimal point | |
Serial.print("Energy = "); // shows the voltage measured | |
Serial.print(Energy,3); // 3 digits after decimal point | |
Serial.println(" Wh"); // 3 digits after decimal point | |
delay(1500); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment