Home › Forums › Mayfly Data Logger › Analog-to- Digital Converter Port on Mayfly Data Logger › Reply To: Analog-to- Digital Converter Port on Mayfly Data Logger
Thanks for the guidance. I went through the examples in the git repo and found the following piece of code (abridged for Campbell OBS3+ turbidity sensor). I have a couple of questions. Could you please help me with them?
1. The OBS3+ I am using operates at 5V and it needs D22 pin. Pin D22 is enabled in the code. But how to mention in the code that it is switched to 5V? Below are the code lines where I could get a reference for D22 and greater than 3.3V(5V in this case)
float getBatteryVoltage()
{
if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update();
return mcuBoard.sensorValues[0];
}
const int8_t sensorPowerPin = 22
// Create the log file, adding the default header to it
// Do this last so we have the best chance of getting the time correct and
// all sensor names correct
// Writing to the SD card can be power intensive, so if we’re skipping
// the sensor setup we’ll skip this too.
if (getBatteryVoltage() > 3.4)
{
dataLogger.turnOnSDcard(true); // true = wait for card to settle after power up
dataLogger.createLogFile(true); // true = write a new header
dataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write
}
2. As the voltage is more than 3V- a voltage regulator is needed. Does the line below help? Does pin A6 work as a voltage divider?
const int8_t resistorDividerPin = A6; //Resistor divider on the battery input of Mayfly board
3.I am trying to connect the sensor to the grove pins ‘Analog to Digital Convreter pins- AA0 and AA1’.
“const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC” — is this statement in the code enough to get output from ports AA0 and AA1.
4. The lines below say that a log file will be created.
“dataLogger.createLogFile(true); // true = write a new header” —>Does this mean I have to write another header in the code or does it print the sample values under a default header?
// Create the log file, adding the default header to it
// Do this last so we have the best chance of getting the time correct and
// all sensor names correct
// Writing to the SD card can be power intensive, so if we’re skipping
// the sensor setup we’ll skip this too.
if (getBatteryVoltage() > 3.4)
{
dataLogger.turnOnSDcard(true); // true = wait for card to settle after power up
dataLogger.createLogFile(true); // true = write a new header
dataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write
}
Could you please guide on what additions and modifications are needed to the code?
/*****************************************************************************
obs3+.ino
Written By: Sara Damiano (sdamiano@stroudcenter.org)
Development Environment: PlatformIO
Hardware Platform: EnviroDIY Mayfly Arduino Datalogger
Software License: BSD-3.
Copyright (c) 2017, Stroud Water Research Center (SWRC)
and the EnviroDIY Development Team
This example sketch is written for ModularSensors library version 0.21.2
This sketch is an example of logging data to an SD card as should be used by
groups involved with The William Penn Foundation’s Delaware River Watershed
Initiative at sites without cellular service.
DISCLAIMER:
THIS CODE IS PROVIDED “AS IS” – NO WARRANTY IS GIVEN.
*****************************************************************************/
// ==========================================================================
// Include the base required libraries
// ==========================================================================
#include <Arduino.h> // The base Arduino library
#include <EnableInterrupt.h> // for external and pin change interrupts
// ==========================================================================
// Data Logger Settings
// ==========================================================================
// The library version this example was written for
const char *libraryVersion = “0.21.2”;
// The name of this file
const char *sketchName = “obs3+.ino”;
// Logger ID, also becomes the prefix for the name of the data file on SD card
const char *LoggerID = “XXXXX”;
// How frequently (in minutes) to log data
const uint8_t loggingInterval = 5;
// Your logger’s timezone.
const int8_t timeZone = -5; // Eastern Standard Time
// NOTE: Daylight savings time will not be applied! Please use standard time!
// ==========================================================================
// Primary Arduino-Based Board and Processor
// ==========================================================================
#include <sensors/ProcessorStats.h>
const long serialBaud = 115200; // Baud rate for the primary serial port for debugging
const int8_t greenLED = 8; // MCU pin for the green LED (-1 if not applicable)
const int8_t redLED = 9; // MCU pin for the red LED (-1 if not applicable)
const int8_t buttonPin = 21; // MCU pin for a button to use to enter debugging mode (-1 if not applicable)
const int8_t wakePin = A7; // MCU interrupt/alarm pin to wake from sleep
// Set the wake pin to -1 if you do not want the main processor to sleep.
// In a SAMD system where you are using the built-in rtc, set wakePin to 1
const int8_t sdCardPwrPin = -1; // MCU SD card power pin (-1 if not applicable)
const int8_t sdCardSSPin = 12; // MCU SD card chip select/slave select pin (must be given!)
const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power (-1 if not applicable)
const int8_t resistorDividerPin = A6; //Resistor divider on the battery input of Mayfly board
// Create the main processor chip “sensor” – for general metadata
const char *mcuBoardVersion = “v0.5b”;
ProcessorStats mcuBoard(mcuBoardVersion);
// ==========================================================================
// Maxim DS3231 RTC (Real Time Clock)
// ==========================================================================
#include <sensors/MaximDS3231.h>
// Create a DS3231 sensor object
MaximDS3231 ds3231(1);
// ==========================================================================
// CAMPBELL OBS 3 / OBS 3+ Analog Turbidity Sensor
// ==========================================================================
#include <sensors/CampbellOBS3.h>
const int8_t OBS3Power = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected)
const uint8_t OBS3numberReadings = 10;
const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC
// Campbell OBS 3+ Low Range calibration in Volts
const int8_t OBSLowADSChannel = 0; // The ADS channel for the low range output
const float OBSLow_A = 0.000E+00; // The “A” value (X^2) from the low range calibration
const float OBSLow_B = 1.000E+00; // The “B” value (X) from the low range calibration
const float OBSLow_C = 0.000E+00; // The “C” value from the low range calibration
// Create a Campbell OBS3+ LOW RANGE sensor object
CampbellOBS3 osb3low(OBS3Power, OBSLowADSChannel, OBSLow_A, OBSLow_B, OBSLow_C, ADSi2c_addr, OBS3numberReadings);
// Campbell OBS 3+ High Range calibration in Volts
const int8_t OBSHighADSChannel = 1; // The ADS channel for the high range output
const float OBSHigh_A = 0.000E+00; // The “A” value (X^2) from the high range calibration
const float OBSHigh_B = 1.000E+00; // The “B” value (X) from the high range calibration
const float OBSHigh_C = 0.000E+00; // The “C” value from the high range calibration
// Create a Campbell OBS3+ HIGH RANGE sensor object
CampbellOBS3 osb3high(OBS3Power, OBSHighADSChannel, OBSHigh_A, OBSHigh_B, OBSHigh_C, ADSi2c_addr, OBS3numberReadings);
// ==========================================================================
// Creating the Variable Array[s] and Filling with Variable Objects
// ==========================================================================
#include <VariableArray.h>
Variable *variableList[] = {
new CampbellOBS3_Turbidity(&osb3low, “12345678-abcd-1234-efgh-1234567890ab”, “TurbLow”),
new CampbellOBS3_Turbidity(&osb3high, “12345678-abcd-1234-efgh-1234567890ab”, “TurbHigh”),
new ProcessorStats_Batt(&mcuBoard, “12345678-abcd-1234-efgh-1234567890ab”),
new MaximDS3231_Temp(&ds3231, “12345678-abcd-1234-efgh-1234567890ab”)
};
// Count up the number of pointers in the array
int variableCount = sizeof(variableList) / sizeof(variableList[0]);
// Create the VariableArray object
VariableArray varArray(variableCount, variableList);
// ==========================================================================
// The Logger Object[s]
// ==========================================================================
#include <LoggerBase.h>
// Create a new logger instance
Logger dataLogger(LoggerID, loggingInterval, &varArray);
// Device registration and sampling feature information
// This should be obtained after registration at http://data.envirodiy.org
// This is needed so the logger file will be “drag-and-drop” ready for manual
// upload to the portal.
const char *registrationToken = “12345678-abcd-1234-efgh-1234567890ab”; // Device registration token
const char *samplingFeature = “12345678-abcd-1234-efgh-1234567890ab”; // Sampling feature UUID
// ==========================================================================
// Working Functions
// ==========================================================================
// Flashes the LED’s on the primary board
void greenredflash(uint8_t numFlash = 4, uint8_t rate = 75)
{
for (uint8_t i = 0; i < numFlash; i++) {
digitalWrite(greenLED, HIGH);
digitalWrite(redLED, LOW);
delay(rate);
digitalWrite(greenLED, LOW);
digitalWrite(redLED, HIGH);
delay(rate);
}
digitalWrite(redLED, LOW);
}
// Read’s the battery voltage
// NOTE: This will actually return the battery level from the previous update!
float getBatteryVoltage()
{
if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update();
return mcuBoard.sensorValues[0];
}
// ==========================================================================
// Main setup function
// ==========================================================================
void setup()
{
// Start the primary serial connection
Serial.begin(serialBaud);
// Print a start-up note to the first serial port
Serial.print(F(“Now running “));
Serial.print(sketchName);
Serial.print(F(” on Logger “));
Serial.println(LoggerID);
Serial.println();
Serial.print(F(“Using ModularSensors Library version “));
Serial.println(MODULAR_SENSORS_VERSION);
if (String(MODULAR_SENSORS_VERSION) != String(libraryVersion))
Serial.println(F(
“WARNING: THIS EXAMPLE WAS WRITTEN FOR A DIFFERENT VERSION OF MODULAR SENSORS!!”));
// Set up pins for the LED’s
pinMode(greenLED, OUTPUT);
digitalWrite(greenLED, LOW);
pinMode(redLED, OUTPUT);
digitalWrite(redLED, LOW);
// Blink the LEDs to show the board is on and starting up
greenredflash();
// Set up some of the power pins so the board boots up with them off
// NOTE: This isn’t necessary at all. The logger begin() function
// should leave all power pins off when it finishes.
if (sensorPowerPin >= 0)
{
pinMode(sensorPowerPin, OUTPUT);
digitalWrite(sensorPowerPin, LOW);
}
// Set the timezone and offsets
// Logging in the given time zone
Logger::setTimeZone(timeZone);
// Offset is the same as the time zone because the RTC is in UTC
Logger::setTZOffset(timeZone);
// Attach information pins to the logger
dataLogger.setLoggerPins(wakePin, sdCardSSPin, sensorPowerPin, buttonPin, greenLED);
dataLogger.setSamplingFeatureUUID(samplingFeature);
// Begin the logger
dataLogger.begin();
// Set up the sensors, except at lowest battery level
if (getBatteryVoltage() > 3.4)
{
Serial.println(F(“Setting up sensors…”));
varArray.setupSensors();
}
// Create the log file, adding the default header to it
// Do this last so we have the best chance of getting the time correct and
// all sensor names correct
// Writing to the SD card can be power intensive, so if we’re skipping
// the sensor setup we’ll skip this too.
if (getBatteryVoltage() > 3.4)
{
dataLogger.turnOnSDcard(true); // true = wait for card to settle after power up
dataLogger.createLogFile(true); // true = write a new header
dataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write
}
// Call the processor sleep
Serial.println(F(“Putting processor to sleep”));
dataLogger.systemSleep();
}
// ==========================================================================
// Main loop function
// ==========================================================================
// Use this short loop for simple data logging and sending
void loop()
{
// Note: Please change these battery voltages to match your battery
// At very low battery, just go back to sleep
if (getBatteryVoltage() < 3.4)
{
dataLogger.systemSleep();
}
// If the battery is OK, log data
else dataLogger.logData();
}
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 |
/***************************************************************************** obs3+.ino Written By: Sara Damiano (sdamiano@stroudcenter.org) Development Environment: PlatformIO Hardware Platform: EnviroDIY Mayfly Arduino Datalogger Software License: BSD-3. Copyright (c) 2017, Stroud Water Research Center (SWRC) and the EnviroDIY Development Team This example sketch is written for ModularSensors library version 0.21.2 This sketch is an example of logging data to an SD card as should be used by groups involved with The William Penn Foundation's Delaware River Watershed Initiative at sites without cellular service. DISCLAIMER: THIS CODE IS PROVIDED "AS IS" - NO WARRANTY IS GIVEN. *****************************************************************************/ // ========================================================================== // Include the base required libraries // ========================================================================== #include <Arduino.h> // The base Arduino library #include <EnableInterrupt.h> // for external and pin change interrupts // ========================================================================== // Data Logger Settings // ========================================================================== // The library version this example was written for const char *libraryVersion = "0.21.2"; // The name of this file const char *sketchName = "obs3+.ino"; // Logger ID, also becomes the prefix for the name of the data file on SD card const char *LoggerID = "XXXXX"; // How frequently (in minutes) to log data const uint8_t loggingInterval = 5; // Your logger's timezone. const int8_t timeZone = -5; // Eastern Standard Time // NOTE: Daylight savings time will not be applied! Please use standard time! // ========================================================================== // Primary Arduino-Based Board and Processor // ========================================================================== #include <sensors/ProcessorStats.h> const long serialBaud = 115200; // Baud rate for the primary serial port for debugging const int8_t greenLED = 8; // MCU pin for the green LED (-1 if not applicable) const int8_t redLED = 9; // MCU pin for the red LED (-1 if not applicable) const int8_t buttonPin = 21; // MCU pin for a button to use to enter debugging mode (-1 if not applicable) const int8_t wakePin = A7; // MCU interrupt/alarm pin to wake from sleep // Set the wake pin to -1 if you do not want the main processor to sleep. // In a SAMD system where you are using the built-in rtc, set wakePin to 1 const int8_t sdCardPwrPin = -1; // MCU SD card power pin (-1 if not applicable) const int8_t sdCardSSPin = 12; // MCU SD card chip select/slave select pin (must be given!) const int8_t sensorPowerPin = 22; // MCU pin controlling main sensor power (-1 if not applicable) const int8_t resistorDividerPin = A6; //Resistor divider on the battery input of Mayfly board // Create the main processor chip "sensor" - for general metadata const char *mcuBoardVersion = "v0.5b"; ProcessorStats mcuBoard(mcuBoardVersion); // ========================================================================== // Maxim DS3231 RTC (Real Time Clock) // ========================================================================== #include <sensors/MaximDS3231.h> // Create a DS3231 sensor object MaximDS3231 ds3231(1); // ========================================================================== // CAMPBELL OBS 3 / OBS 3+ Analog Turbidity Sensor // ========================================================================== #include <sensors/CampbellOBS3.h> const int8_t OBS3Power = sensorPowerPin; // Pin to switch power on and off (-1 if unconnected) const uint8_t OBS3numberReadings = 10; const uint8_t ADSi2c_addr = 0x48; // The I2C address of the ADS1115 ADC // Campbell OBS 3+ Low Range calibration in Volts const int8_t OBSLowADSChannel = 0; // The ADS channel for the low range output const float OBSLow_A = 0.000E+00; // The "A" value (X^2) from the low range calibration const float OBSLow_B = 1.000E+00; // The "B" value (X) from the low range calibration const float OBSLow_C = 0.000E+00; // The "C" value from the low range calibration // Create a Campbell OBS3+ LOW RANGE sensor object CampbellOBS3 osb3low(OBS3Power, OBSLowADSChannel, OBSLow_A, OBSLow_B, OBSLow_C, ADSi2c_addr, OBS3numberReadings); // Campbell OBS 3+ High Range calibration in Volts const int8_t OBSHighADSChannel = 1; // The ADS channel for the high range output const float OBSHigh_A = 0.000E+00; // The "A" value (X^2) from the high range calibration const float OBSHigh_B = 1.000E+00; // The "B" value (X) from the high range calibration const float OBSHigh_C = 0.000E+00; // The "C" value from the high range calibration // Create a Campbell OBS3+ HIGH RANGE sensor object CampbellOBS3 osb3high(OBS3Power, OBSHighADSChannel, OBSHigh_A, OBSHigh_B, OBSHigh_C, ADSi2c_addr, OBS3numberReadings); // ========================================================================== // Creating the Variable Array[s] and Filling with Variable Objects // ========================================================================== #include <VariableArray.h> Variable *variableList[] = { new CampbellOBS3_Turbidity(&osb3low, "12345678-abcd-1234-efgh-1234567890ab", "TurbLow"), new CampbellOBS3_Turbidity(&osb3high, "12345678-abcd-1234-efgh-1234567890ab", "TurbHigh"), new ProcessorStats_Batt(&mcuBoard, "12345678-abcd-1234-efgh-1234567890ab"), new MaximDS3231_Temp(&ds3231, "12345678-abcd-1234-efgh-1234567890ab") }; // Count up the number of pointers in the array int variableCount = sizeof(variableList) / sizeof(variableList[0]); // Create the VariableArray object VariableArray varArray(variableCount, variableList); // ========================================================================== // The Logger Object[s] // ========================================================================== #include <LoggerBase.h> // Create a new logger instance Logger dataLogger(LoggerID, loggingInterval, &varArray); // Device registration and sampling feature information // This should be obtained after registration at http://data.envirodiy.org // This is needed so the logger file will be "drag-and-drop" ready for manual // upload to the portal. const char *registrationToken = "12345678-abcd-1234-efgh-1234567890ab"; // Device registration token const char *samplingFeature = "12345678-abcd-1234-efgh-1234567890ab"; // Sampling feature UUID // ========================================================================== // Working Functions // ========================================================================== // Flashes the LED's on the primary board void greenredflash(uint8_t numFlash = 4, uint8_t rate = 75) { for (uint8_t i = 0; i < numFlash; i++) { digitalWrite(greenLED, HIGH); digitalWrite(redLED, LOW); delay(rate); digitalWrite(greenLED, LOW); digitalWrite(redLED, HIGH); delay(rate); } digitalWrite(redLED, LOW); } // Read's the battery voltage // NOTE: This will actually return the battery level from the previous update! float getBatteryVoltage() { if (mcuBoard.sensorValues[0] == -9999) mcuBoard.update(); return mcuBoard.sensorValues[0]; } // ========================================================================== // Main setup function // ========================================================================== void setup() { // Start the primary serial connection Serial.begin(serialBaud); // Print a start-up note to the first serial port Serial.print(F("Now running ")); Serial.print(sketchName); Serial.print(F(" on Logger ")); Serial.println(LoggerID); Serial.println(); Serial.print(F("Using ModularSensors Library version ")); Serial.println(MODULAR_SENSORS_VERSION); if (String(MODULAR_SENSORS_VERSION) != String(libraryVersion)) Serial.println(F( "WARNING: THIS EXAMPLE WAS WRITTEN FOR A DIFFERENT VERSION OF MODULAR SENSORS!!")); // Set up pins for the LED's pinMode(greenLED, OUTPUT); digitalWrite(greenLED, LOW); pinMode(redLED, OUTPUT); digitalWrite(redLED, LOW); // Blink the LEDs to show the board is on and starting up greenredflash(); // Set up some of the power pins so the board boots up with them off // NOTE: This isn't necessary at all. The logger begin() function // should leave all power pins off when it finishes. if (sensorPowerPin >= 0) { pinMode(sensorPowerPin, OUTPUT); digitalWrite(sensorPowerPin, LOW); } // Set the timezone and offsets // Logging in the given time zone Logger::setTimeZone(timeZone); // Offset is the same as the time zone because the RTC is in UTC Logger::setTZOffset(timeZone); // Attach information pins to the logger dataLogger.setLoggerPins(wakePin, sdCardSSPin, sensorPowerPin, buttonPin, greenLED); dataLogger.setSamplingFeatureUUID(samplingFeature); // Begin the logger dataLogger.begin(); // Set up the sensors, except at lowest battery level if (getBatteryVoltage() > 3.4) { Serial.println(F("Setting up sensors...")); varArray.setupSensors(); } // Create the log file, adding the default header to it // Do this last so we have the best chance of getting the time correct and // all sensor names correct // Writing to the SD card can be power intensive, so if we're skipping // the sensor setup we'll skip this too. if (getBatteryVoltage() > 3.4) { dataLogger.turnOnSDcard(true); // true = wait for card to settle after power up dataLogger.createLogFile(true); // true = write a new header dataLogger.turnOffSDcard(true); // true = wait for internal housekeeping after write } // Call the processor sleep Serial.println(F("Putting processor to sleep")); dataLogger.systemSleep(); } // ========================================================================== // Main loop function // ========================================================================== // Use this short loop for simple data logging and sending void loop() { // Note: Please change these battery voltages to match your battery // At very low battery, just go back to sleep if (getBatteryVoltage() < 3.4) { dataLogger.systemSleep(); } // If the battery is OK, log data else dataLogger.logData(); } |