Last active
January 15, 2018 13:57
-
-
Save vsefer/c415ac85edbcdc21c447920779b051a9 to your computer and use it in GitHub Desktop.
arduino-voltmeter
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; | |
double avgVoltage = 0; | |
double Voltage = 0; | |
int RawValue= 0; | |
void setup() | |
{ | |
Serial.begin(9600); | |
} | |
void loop() | |
{ | |
Serial.print("Vcc = "); | |
Serial.print(readVcc()); | |
Serial.print(" mV \t"); | |
RawValue = analogRead(analogIn); | |
Voltage = ((RawValue / 1023.0) * readVcc()) / 1000; | |
avgVoltage = Voltage / 1.025; | |
Serial.print("Measured voltage = "); | |
Serial.print(avgVoltage,2); | |
Serial.println(" V"); | |
delay (1000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment