The document discusses using Arduino for datalogging. It explains the structure of Arduino programs, called sketches, which include a setup() and loop() function. It emphasizes controlling the scale triplet of support, spacing and extent when collecting data. This involves using timing functions to implement scan and recording intervals that determine how often to sample sensors and record observations. Strategies like averaging can reduce noise and control data collection rates.
2. Objectives
• Learn basic data collection concepts for hydrologic
data
• Examine more closely observation dimensionality,
including the scale triplet of support, spacing, and
extent
• Learn the basic datalogger program structure for
logging data
3. Arduino Programs – “Sketches”
• Sketch = Program
• Unit of code uploaded to and run on an Arduino
board
• 5 parts:
1. A descriptive header comment block
2. Definition of global variables
3. Setup() function
4. Loop() function
5. User-defined functions
• Arduino Language Reference:
https://www.arduino.cc/en/Reference/HomePage
4. Arduino Language
• Roughly based on C
• All statements must end with a ;
• Variables are storage compartments for numbers
• Variables must be defined before use
• // Comments are preceded by two forward slashes
What’s the best way to learn the language?
Study the examples and other people’s code!!!
5. Exercise – The “Blink” Sketch
• Plug your Arduino into your computer via the USB
cable
• Open the Arduino IDE software
• From the Tools drop down menu select:
Board Arduino/Genuino UNO
Port <whichever port is labeled Arduino/Genuino Uno>
• From the File drop down menu select:
Examples 01. Basics Blink
7. Exercise – The “Blink” Sketch
• Plug your Arduino into your computer via the USB
cable
• Open the Arduino IDE software
• From the Tools drop down menu select:
Board Arduino/Genuino UNO
Port <whichever port is labeled Arduino/Genuino Uno>
• From the File drop down menu select:
Examples 01. Basics Blink
• On the toolbar, click the “verify” button
• On the toolbar, click the “upload” button
8. Closer Look – the setup() function
• Run once when you power the Arduino or when
you send a new program
• Put everything in the setup() function that you
want to happen before the main program loop
starts
9. Closer look – the loop() function
• Runs continuously as long as the Arduino is
powered
• Yep – its an endless loop!
• Put everything in the loop() function that you want
to execute continuously
• Measurements
• Control logic
• Data output
• Etc.
10. How do we turn an Arduino into a
datalogger?
•Some things we have to figure out:
o Debugging
o Timing
o Interfacing with sensors and making
measurements
o Recording data to a file
11. Debugging Using the Output Pane
• When you
compile your
code, errors
will show up
here
12. Exercise – “Blink_Example2”
• Send the program, then open the serial monitor by
clicking the button on the toolbar (top right)
Start a serial
port and print a
line of text to it.
Print a line of
text with LED
status
13. Debugging with
Serial Output
• Useful when you want to
see what’s going on as
the program executes
• Print values and
messages to a serial
monitor
• The Arduino IDE has it’s
own serial monitor
• Super helpful for
debugging
Make sure the baud rate
matches your Serial.begin()
statement in your sketch
14. Timing
• The loop() function runs indefinitely as fast as it can
• The loop itself takes a couple of clock cycles, but
total time is dependent on what is in the loop
• Arduino UNO has no realtime clock (bummer!)
• But, it has some useful timing functions:
• millis() – number of milliseconds since the Arduino
board began running the current program
• micros() – same as millis, but for microseconds
• delay() – pauses the program for an amount of time (in
milliseconds)
• delayMicroseconds() – same as delay but for
microseconds
NOTE: millis() and micros() will overflow and go back to zero after a certain
period of time
16. Timing
• millis() and delay() are great, but not exact
• Plus, delay() pauses the program and you can’t do
anything else at the same time
• What if I want the Arduino to do something (like
make a measurement) on a more precise, set time
interval?
• What if I want to do multiple things at the same
time?
17. Measurement Concepts
• Remember the scale triplet – support, spacing, and
extent
• We want to control these with our program
The Scale Triplet of Measurements
length or time
quantity
(a) Extent
length or time
quantity
(b) Spacing
length or time
quantity
(c) Support
18. Measurement Concepts - Spacing
How often should we record
values to capture this signal?
19. “When one can verify that additional
measurements will merely ‘connect
the dots’ between the existing
observations, the hydrochemical
record can be considered continuous
for all practical purposes.”
Kirchner, J.W., Feng, X., Neal, C., Robson, A.J. (2004). The fine structure of water-
quality dynamics: the (high-frequency) wave of the future, Hydrological Processes,
18(7), 1353-1359, http://dx.doi.org/10.1002/hyp.5537.
20. Remember this Slide?
Sampling Frequency
• Half hourly data
subsampled
-Hourly
-Daily
-Weekly
-Monthly
Half hourly
Hourly
Daily
Weekly
Monthly
21. Measurement Concepts - Spacing
Looks good…
But, what do we record?
One instantaneous value
every 5 seconds?
24. Time Support and Spacing
• Generally handled using a scan interval and a
recording interval
• Scan Interval = the time between sensor measurements
• Recording Interval = the time between recorded
observations
What is the difference?
25. Measurement Concepts
Scan Interval vs. Recording Interval
• Recording Interval = spacing
• Scan Interval = what we do within a recording
interval that determines what values we record
• How many times we sample the sensor(s)
• What statistic (if any) we calculate
27. Measurement Concepts
Scan Interval vs. Recording Interval
• There are several reasons to scan sensors more
frequently than you record data:
oReduce error in sensor observations by aggregation
(e.g., calculate an average value)
oReduce noise in the observed phenomena by
aggregation – again, averaging
oAdaptive sampling – record values at a slow rate until
something interesting happens (e.g., a storm event),
then increase recording frequency
• Tradeoff: Scanning sensors requires power
28. Measurement Concepts
Time Support
• The time window / footprint
over which an observation is
made
• Not always equal to the recording interval
• Depends on scanning and recording strategy
oInstantaneous values: time support = 0
oAverage (or other aggregate statistic) values: time
support = whatever time window over which you
calculate the statistic
• Regular averaging
• Burst sampling
29. Exercise – “Timing_Example2”
Pseduo-code of loop:
1. Get the current value of the micros() function
2. Check to see if a scan interval has passed
3. If a scan interval has passed
a. Perform scan instructions (e.g.,
measurements, calculations, etc.)
b. Check to see if a recording interval has passed
c. If a recording interval has passed
i. Perform necessary calculations
ii. Record an output record
30. Some Notes about Timing
• On the Arduino UNO, the micros() function has a
resolution of 4 microseconds
• The “Timing_Example2” sketch will accumulate
error in timing
• More sophisticated examples might use interrupts
and the UNO’s internal timers, but for our work,
this example will suffice
31. How do we turn an Arduino into a
datalogger?
•Some things we have to figure out:
Debugging
Timing
o Interfacing with sensors and making
measurements
o Recording data to a file
32. Summary
• Every hydrologic observation, regardless of how it was
created, has support, spacing, and extent
• The scale triplet determines how we interpret and use
data
• Arduino gives us an inexpensive prototyping platform
for environmental sensors and datalogging
• Arduino’s IDE and programming language provide a
coding environment for measurement and control
• Arduino sketches rely on setup() and loop() functions
• Controlling time support and spacing of observations
relies on Arduino’s timing functions (or an external
realtime clock)
• Timing/sampling strategies can be used to
capture/overcome noisy signals and sensors