Skip to content

Instantly share code, notes, and snippets.

@WestleyR
Created January 21, 2021 04:41
Show Gist options
  • Save WestleyR/60e1a5cd17968f04184f80ba9147f26a to your computer and use it in GitHub Desktop.
Save WestleyR/60e1a5cd17968f04184f80ba9147f26a to your computer and use it in GitHub Desktop.
Log voltage/time with Arduino
// Created by: WestleyR
// Email: westleyr@nym.hush.com
// Url: https://github.com/WestleyR/...
// Date created: 2021-01-20
// Last modified date: 2021-01-20
//
// This is free and unencumbered software released into the public domain.
// For more information, please refer to <https://unlicense.org>
//
/*
# How to use
Conect A0 (or another analog input of your choice) to the voltage divider,
which values you haft to decide yourself. In my case, I used 1K/320, for a
max of ~20.5v
Then, you will need to connect your laptop (or other device that can listen on
the serial port), and redirect/tee the output to a file, which will look like:
```
2.12 0.01
2.12 0.02
2.11 0.03
2.13 0.04
2.12 0.05
[...]
```
From that file, you can use a program (like gnuplot) to create a graph. See:
https://gist.github.com/WestleyR/124ff82093df6d59d308f272a0fbf4ed for a gnuplot
example.
**This code needs lots of improvments!**
This is the first, testing version of the TinyLogger product, which will be
avalibe around a couple months.
*/
// The analog sensor input pin
#define sensorPin A0
// The number of analog readings to make, and avrage it
#define numberOfReadings 25
// The voltage calibration value, this depends on your voltage
// divider. For a 1K/320, its about 4.18, with a max of ~20.5v.
const static float voltageCalibrate = 4.18;
//******
// Setup
//******
void setup() {
Serial.begin(9600);
}
unsigned long timerValue;
void loop() {
// Loop every 0.01s
while ((millis() - timerValue) > 600) {
// Read the voltage on the analog input pin a bunch of
// times, then avrage the value.
float avrageVolts = 0;
for (int i = 0; i < numberOfReadings; i++) {
int sensorValue = analogRead(sensorPin);
float voltage = sensorValue * (5.0 / 1023.0);
voltage *= voltageCalibrate;
avrageVolts += voltage;
}
avrageVolts /= numberOfReadings;
// Print the voltage
Serial.print(avrageVolts);
Serial.print(" ");
// Print the time, in 0.01 minute resolution
Serial.println((float)millis() / 60000);
// Reset the timer value
timerValue = millis();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment