-
Notifications
You must be signed in to change notification settings - Fork 32
/
Arduino_voltmeter_final.ino
51 lines (45 loc) · 1.51 KB
/
Arduino_voltmeter_final.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* DESCRIPTION:
* ------------
* This program calculates and prints the voltage and measurement number to a Serial Monitor,
* from which a Python script can scrape the data and write it to a CSV. The program is
* intended to be run on an Arduino/Genuino Uno and uses the analog pin A0.
*
* CONTACT:
* -----------
* Jack O'Grady
*/
const int sensorPin = A0;
// from voltage divider equation
float voltageFactor = 1;
// set initial data point as 1
long measurementNumber = 1;
void setup() {
Serial.begin(9600);
}
void loop() {
/*
* The Arduino reads the analog input from 0 to 1023, so we must convert that to a voltage
* scale, which is 5V for this board. Once we have that voltage value, we multiply by the
* voltage factor from the voltage divider in the circuit. We need a voltage divider for
* voltages greater than 5V as that is the Arduino's max.
*
* The program takes the average of 10000 voltage measurements for each data point. This avoids
* voltage reading fluctations from the Arduino that were not present on my DMM.
*/
int count = 0;
float periodVoltageSum = 0;
while (count < 10000) {
int rawVoltRead = analogRead(sensorPin);
float voltage = (rawVoltRead / 1024.0) * 5.0 * voltageFactor;
periodVoltageSum += voltage;
count++;
}
float voltageAve = periodVoltageSum / count;
// prints the output to Serial in a CSV format
Serial.print(measurementNumber);
Serial.print(',');
Serial.println(voltageAve, 3);
measurementNumber++;
}