Created
January 15, 2018 13:37
-
-
Save vsefer/0a0412c5ccdb4920edc9dd82f8eaf312 to your computer and use it in GitHub Desktop.
acs711 + arduino
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
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 = 1126400L / result; // Back-calculate AVcc in mV | |
return result; | |
} | |
const int analogIn = A0; | |
int mVperAmp = 90; // from datasheet | |
int RawValue= 0; | |
double Voltage = 0; | |
double avgVoltage = 0; | |
double Amps = 0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
Serial.println( readVcc(), DEC ); | |
RawValue = analogRead(analogIn); | |
Voltage = ((RawValue / 1023.0) * readVcc()) / 1000; // Gets you mV | |
avgVoltage = Voltage / 1.025; // magic | |
Amps = ((Voltage - (readVcc() / 2)) / mVperAmp); | |
Serial.print("A0 = " ); // shows pre-scaled value | |
Serial.print(RawValue); | |
Serial.print("\t V = "); // shows the voltage measured | |
Serial.print(avgVoltage,2); // the '3' after voltage allows you to display 3 digits after decimal point | |
Serial.print("\t Amps = "); // shows the voltage measured | |
Serial.println(Amps,2); // the '3' after voltage allows you to display 3 digits after decimal point | |
delay(1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment