/*
*DC_voltage_measure_demo*
Arduino DC Voltage Demo 1
dc-voltage-demo.ino
Use Arduino A/D converter to measure voltage
Use external voltage divider with 30k & 7.5k resistors
Results displayed on Serial Monitor
DroneBot Workshop 2021
https://dronebotworkshop.com
Sketch takes voltage input and through a voltage divider
feeds input to ADC pin. This value is calculated and returns
a reading that is the original voltage in value.
Using 3.3V aref,
ADC 1024 = 16.484V
ADC 383 = 6.171V
*/
// Define analog input
#define ANALOG_IN_PIN A0
// Floats for ADC voltage & Input voltage
float adc_voltage = 0.0;
float in_voltage = 0.0;
// Floats for resistor values in divider (in ohms)
float R1 = 30000.0;
float R2 = 7500.0;
// Float for Reference Voltage
// float ref_voltage = 5.0; // measured from 5V output of Arduino
float ref_voltage = 3.3; // measured from 3.3V output of Arduino
// Integer for ADC value
int adc_value = 0;
void setup() {
Serial.begin(9600);
Serial.println("DC Voltage Test");
// Use 3.3 external voltage reference. 3.3 output to AREF pin.
analogReference(EXTERNAL);
}
void loop(){
// Read the Analog Input
adc_value = analogRead(ANALOG_IN_PIN);
// Determine voltage at ADC input
adc_voltage = (adc_value * ref_voltage) / 1024.0;
// Calculate voltage at divider input
in_voltage = adc_voltage / (R2/(R1+R2));
// Print results to Serial Monitor to 2 decimal places
Serial.print("ADC Value: "); Serial.print(adc_value);
Serial.print(" * Ref Voltage: "); Serial.print(ref_voltage);
Serial.print(" / 1024 = ADC Voltage: "); Serial.println(adc_voltage, 3);
Serial.print("ADC Voltage: / "); Serial.print(R2/(R1+R2));
Serial.print(" = Input Voltage: "); Serial.println(in_voltage, 3);
Serial.println("");
// Short delay
delay(3000);
}