with File dataFile = SD.open(“datalog.txt”, FILE_WRITE);
where datalog.txt is the name of the file we wish to write.
We can then print and println to that file object — much
like we would to the serial port:
Once the writing is done, we need to close the file
dataFile.close();. While opening and closing the file is not a
particularly fast process, it is better than risking a corrupt or
partially written file. Our data rate of 1 Hz is very slow, so
we won’t have any problems opening the file reference
each time we wish to write to it.
If using these sensors on our quad was going to be
routine, we could make a more permanent setup with a
custom PCB and nice enclosure. Given that this is really a
proof-of-concept application of these sensors, I elected to
build up the circuit on a breadboard and temporarily affix it
to the top of the quad’s flight deck.
First, I soldered 6” lead wires ( 22 ga solid) to the
power, ground, data, and clock pins of the IR sensors, as
well as the color sensor. Both of these sensors are 3. 3 VDC
logic, but the Wildfire is 5 VDC logic; we need a logic level
I hooked the clock and data lines to the LV1 and LV2
ports of a SparkFun logic level converter
( https://www.sparkfun.com/products/12009). I
connected the LV and GND ports to the 3. 3 VDC and
ground header pins on the Wildfire, and then connected
the sensor’s power as well. Next, I connected the 5 VDC
power output of the Wildfire to the HV pin of the logic
converter, and connected SCL and SDA to HV1 and HV2.
The GPS will vary some from module to module, but
my GPS unit requires the \RAW pin be tied to low to output
raw NMEA strings. Otherwise, it operates in a “smart”
call/response mode. I powered the GPS with 5 VDC and
hooked the serial pin to pin D2 on the Wildfire. This is the
receive port for the second onboard UART.
If you are using an external SD card breakout board
instead of the Wildfire, be sure to follow the hookup guide
for that board. For those using the Wildfire, you’re all set!
We now have a complete hardware setup (Figure 2). Next,
we need to test out the various sensors to be sure that we
don’t have anything hooked up incorrectly.
Most of the sensor’s libraries come with example
sketches that test out the sensor, but I’m perpetually
frustrated by their output format. Often, I want to log the
output for a few hours to make sure the sensor is
responding like I think it should or to perform a simple
calibration. Logging and parsing the output of many of
these example sketches is frustrating! They are a multi-line
poorly delimited display that is slightly more human
readable, but mostly just odd looking.
Maybe I’ve spent too much time watching data scroll
across screens in various labs, but I prefer looking at tabular
data. In the code repository and the zip file accompanying
this article at the link, I’ve included improved examples that
print simple tabular data in a comma separated values
32 SERVO 08.2017
Figure 2: The completed instrument, ready for benchtop testing.
Though there are a lot of wires, it really is a relatively
straightforward application of the sensors and level shifter.
Figure 3: IR sensor
unit test sketch.