Arduino Uno
Adafruit 1141 Data Logging Shield
CR1220 3V Coin Cell Battery
SD Card (formatted as FAT32)
Maxbotix MB7078 depth sensor
Adafruit Ultimate GPS Breakout shield
Adafruit Passive GPS Antenna
The Data Logging Shield is stacked on top of the Uno, SD Card and battery inserted. 5 V and GND connected to pins V+ and GND of the depth sensor, respectively, Pin 3 of the depth sensor connected to the Arduino using pin A0 on the Uno (an example is given in https://www.maxbotix.com/Arduino-Ultrasonic-Sensors-085/). 5 V and GND also connected to pins VIN and GND of the GPS, respectively, TX of the GPS is connected to Pin 2 of the Arduino and the RX is connected to Pin 3.
The following is the code to be uploaded to the Arduino Uno. This code was cobbled together by J. C. Riddick and S. N. Riddick over the past 7 years. Much of the code has been repurposed from other projects and may not be the most elegant (some lines may be superfluous), but it works! This code reads output from the depth sensor and the GPS, logs them to an SD card (as a back up) and sends the data to the Raspberry Pi.
Several libraries need to be included to make it work: SoftwareSerial, SPI, SD, Wire, DHT, RTClib. Then copy and paste the code below into a sketch and upload to the Arduino.
// Program Log_GPS_Depth
// Development date 10 Feb 2019
// This software initialises 10 bit ADC and Sd card
// The RTC is set using Set_RTC software
// Test data is then written to the SD card every minute
// In format 2018 6 28 19 58 40 0 726 0 1023
// Date Time ch 1, ch2, ch3
// Filenames are LOGGER00.txt, LOGGER01.txt number incremented by 1 every time
// the Reset PB is pressed
//
// Port 4 is set to 1 to drive the excitation LED's
// Blue LED always on
// Port 5 is indicator LED
// Data are output using MAX232 ports 2,3
//
// 10 Samples are taken every minute
// * RX is digital pin 2 (connect to TX of other device)
// * TX is digital pin 3 (connect to RX of other device)
//
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include <DHT.h>
#include "RTClib.h"
// set up variables using the SD utility library functions:
#define LOG_INTERVAL 1000 // mills between entries (reduce to take more/faster data)
#define SYNC_INTERVAL 1000 // mills between calls to flush() - to write data to the card
uint32_t syncTime = 0; // time of last sync()
#define SYNC_SIZE 512
uint32_t syncSize = 0;
#define ECHO_TO_SERIAL 1 // echo data to serial port
RTC_DS1307 RTC; // define the Real Time Clock object
// the logging file
File logfile;
void error(char *str)
{
Serial.print("error: ");
Serial.println(str);
while (1);
}
const int chipSelect = 10;
int t = 0;
int millisec = 0;
int filecount = 0;
int Flag = 0;
int j;
uint16_t i = 0;
int anPin = A0;
int anVolt, cm;
char g;
char gpsin[200];
int counter[12];
int char_pos = 0;
int count_in ;
SoftwareSerial GPSSerial(2, 3); // RX, TX
void setup()
{
Serial.begin(9600);
GPSSerial.begin(9600);
// initialize the SD card
Serial.print("Initializing SD card...");
//lcd.print("Initializing SD card...");
pinMode(10, OUTPUT);
if (!SD.begin(chipSelect)) {
Serial.print("Card failed, or not present");
}
Serial.println("Card initialized");
// create a new file
char filename[] = "LOGGER00.TXT";
for (uint8_t i = 0; i < 100; i++) {
filename[6] = i / 10 + '0';
filename[7] = i % 10 + '0';
if (! SD.exists(filename)) {
// only open a new file if it doesn't exist
logfile = SD.open(filename, FILE_WRITE);
break; // leave the loop!
}
}
if (! logfile) {
error("couldnt create file");
}
Wire.begin();
delay(200);
}
void loop()
{
// delay for the amount of time we want between readings
delay((LOG_INTERVAL - 1) - (millis() % LOG_INTERVAL));
// log milliseconds since starting
uint32_t m = millis();
count_in = GPSSerial.findUntil("$GPRMC", ";");
count_in = GPSSerial.readBytesUntil(0x0A, gpsin, 70);
// Count Commas and Check GPS Valid Flag
do {
if (gpsin[char_pos] == ',') {
counter[i] = char_pos;
i = i + 1;
}
char_pos = char_pos + 1;
} while (gpsin[char_pos] != '*');
for ( i = 1; i < 56; i++)
{
if (i < 7 || i > 12 && i < 23|| i > 24 && i < 36){
Serial.print(gpsin[i]);
}
logfile.print(gpsin[i]);
}
//Serial.print(" ");
logfile.print(" ");
GPSSerial.flush();
// Read Depth sensor
anVolt = analogRead(anPin); // this is for the standard range sensors
cm = anVolt;
Serial.print(",");
Serial.println(cm);
//Serial.println( " ");
logfile.print(",Depth ,");
logfile.print(cm);
logfile.println( ",");
delay(1000);
// Now we write data to disk! Don't sync too often - requires 2048 bytes of I/O to SD card
// which uses a bunch of power and takes time
if ((millis() - syncTime) < SYNC_INTERVAL) return;
syncTime = millis();
// if ((logfile.size() - syncSize) < SYNC_SIZE) return;
// syncSize = logfile.size();
logfile.flush();
delay(4000);
}