How to timestamp sensor data and send to SD card

Hello,

I have a Honeywell differential pressure sensor HSCDDRD005PD3A3. I have programmed this sensor (without using HSC library) and am able to read data from it.

I want to time stamp this data using DS3231 (Overview | Adafruit DS3231 Precision RTC Breakout | Adafruit Learning System)
and send this data to an SD card using 5V Micro -SD breakout board (MicroSD card breakout board+ : ID 254 : $7.50 : Adafruit Industries, Unique & fun DIY electronics and kits).

Can anyone help me with the code and give me suggestions or links which can help me programming.

I can post the sensor code if required.

Thank you.

dpoornima:
I have programmed this sensor (without using HSC library) and am able to read data from it.

Whatever means you do that may ber applied similarly to SD

Serial.print(data;     // to monitor
lcd.print(data);       // to display
myFile.print(data);    //to SD

There is a swag of RTC libraries, all with examples, or you can get by without. I use this. It is for DS12307 but works with DS3231.

This datalogging code included in the examples in the IDE.

There is a working example here which includes SD file management.

If you aren't married to the idea of an SD card or shield, then using CoolTerm (free, with option to donate) for your Serial Monitor has an option of adding timestamps as well as capture to textfile.

I have used it in projects with very satisfactory results.

Hello,

There is a working example here which includes SD file management.

I am not able to open this link. Can you please post it again?

If you aren't married to the idea of an SD card or shield, then using CoolTerm (free, with option to donate) for your Serial Monitor has an option of adding timestamps as well as capture to textfile.

I specifically want to transfer the data on to a SD card.

Thanks

Hello,

I have connected the A4 (SDA) and A5 (SCL) pins of the arduino to the pressure sensor and from the pressure sensor, I have connected these pins to DS3231.
Is there any other way to connect?

Thank you

Sorry about that

http://homepages.ihug.com.au/~npyner/Arduino/Bluetooth_graphic_5110_with_file_retrieve_DY.ino

Note that Weeman makes a good point. You only need SD for backup or when operating remote form a PC.

Also, I have connected the A4 (SDA) and A5 (SCL) pins of the arduino to the pressure sensor and from the pressure sensor, I have connected these pins to DS3231.
Is there any other way to connect?

No. Both devices are I2C, and are meant to share the pins but, while you have defined the sensor, you do not appear to have defined the RTC. It should look something like

#define DS3231_ADDRESS 0x68

I assume the code compiles, so it may be in the library but, if you are having problems with time, maybe that is why.

Hello,

I have defines the timer with its address too. But still it does not show any values.

I have checked the timer and the pressure sensor separately. They work fine.

I have even connected pull ups of 4.7k .

Is there something wrong with the code?

Perhaps you are too eager to make it work. If it doesn't work, print more data to the serial monitor. Test each part seperately (you have already done that), and make seperate sections in the sketch.
For example a section that prints the time, before the sensor is used. And print the time after the sensor is used.

To be sure that the I2C data is valid, check the return value of Wire.endTransmission() and Wire.requestFrom().
With everything connected, does the i2c_scanner see both the RTC and the sensor ?

Your sketch might get three times as big. But you have to know what is going on.

When you say: "But I dont get any values when I upload this code", then we have not enough information.
There is nothing on the serial monitor ? You could start by printing something from the setup() :

  Serial.begin(9600);
  Serial.println("Sketch has started");

This line is not needed:

while(Wire.available() == 0);

You can remove it, it doesn't do anything useful, it can only do harm.

The "Time" library is in the Library Manager in the Arduino IDE. It is maintained by Paul Stoffregen : Time Library, Timekeeping and Time/Date Manipulation on Teensy
I think you have a 5 year old version of that library.

Hello,

I tried printing Serial.println("Sketch has started");

The serial monitor does not show anything . It is a blank.

I tried to print time before the sensor.

It still does not print anything. the monitor is blank.
However, when I test the sensor and timer separately, they work perfectly well.

I am using RTC library downloaded from adafruit. (Arduino Usage | Adafruit DS3231 Precision RTC Breakout | Adafruit Learning System)

Is there anything wrong withthe I2C connections? How do I check them ?

Get the serial monitor working. The I2C and the RTC time and sensor data is for later.

Make a new sketch that does only do Serial.begin and print a message. Does it work ? Is the baudrate set to 9600 in the serial monitor ?

Add the Wire.begin, and try to add more.

I don't know the RTClib from Adafruit. Did you install it as a library ?
Try the "ds3231" example. It is in the menu of the Arduino IDE, when the library is correctly installed.

Replace this:

#include "RTClib.h"

#include "Wire.h"
#include <Arduino.h>

With this:

#include <Wire.h>
#include <RTClib.h>

Follow the examples.
Use this:

DateTime now = rtc.now();

inside the loop, just like the Adafruit example.

Once everything is working, you can change things and make you own code.

Hello,

I tried the ds3231 example code from the adafruit library. It works fine and even shows the correct time.

The serial monitor prints the values of the sensor when used separately.
It also prints when I use the timer separately.

DateTime now = rtc.now();

I put this in the loop and it shows values as below.

I reset the time using

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))

It starts running the time from the adjusted time.

How do I get the current date and time after I unplug and plug it in again? It shows the previously set date and time.

Is the only way to get the current time is using the above line of code and adjusting it every time I upload the code?

IMG_4600 (1).JPG

dpoornima:
Hello,

I tried printing Serial.println("Sketch has started");

The serial monitor does not show anything . It is a blank.

That bit of code is about the best advice you will ever get. If the code compiles and screen is blank at that point, my guess is that you have power problem and there is no point in looking at the rest of the code, which may well be kosher anyway.

If you are running on battery, now is the time to change to wall wart. You might also check the wiring and particularly the power to your peripherals.

The time should be set into the DS3231 chip just once. I don't know why it doesn't keep the time.

The way this is going, that is how to develop software. Make small steps; don't assume it will work; test everything; don't rush ahead.
1 ) The serial monitor is working.
2 ) And you are now testing the DS3231.

The Adafruit RTClib "ds3231" example check for "rtc.lostPower()" and only sets the compile time when the power was lost. Do you do that as well ?

Is the battery in the DS3231 module okay ?
Perhaps there are simple test sketches for the DS3231.

dpoornima:
I put this in the loop and it shows values as below.

I reset the time using

rtc.adjust(DateTime(F(__DATE__), F(__TIME__)))

It starts running the time from the adjusted time.

How do I get the current date and time after I unplug and plug it in again? It shows the previously set date and time.

Is the only way to get the current time is using the above line of code and adjusting it every time I upload the code?

I believe you may have at least one of two problems.

  1. Power (again)
    Is your ds3231 running on an LIR2032 battery? Is the battery OK? I don't believe the DS3231 module gives the battery problems its predecessor did.

  2. Software
    I suspect you are using the wrong clock software. The software I use is from
    http://bildr.org/2011/03/ds1307-arduino/
    Note that there are two separate programmes - clock reader, and clock setter -which simplifies things, and would be what you would expect. I imagine most are rather like this.

The Adafruit RTClib "ds3231" example check for "rtc.lostPower()" and only sets the compile time when the power was lost. Do you do that as well ?

Is the battery in the DS3231 module okay ?

It does not print "The sketch has started"

It also does not print the sentence, "RTC lost power,set the time" even though I unplug it and plug it again.
but the serial monitor starts showing the pressure values along with the time when it stopped before unplugging it.
What might be the problem?

Is your ds3231 running on an LIR2032 battery? Is the battery OK? I don't believe the DS3231 module gives the battery problems its predecessor did.

I am using this battery

CR1220 12mm Diameter - 3V Lithium Coin Cell Battery [CR1220] : ID 380 : $0.95 : Adafruit Industries, Unique & fun DIY electronics and kits and it is a new one.

The library I have is RTClib/examples/ds3231/ds3231.ino at master · adafruit/RTClib · GitHub

Does the DS3231 set automatically to the current time after it is unplugged and plugged back in again?
After I unplug it and plug it again, the timer starts from the moment where it was before unplugging it.
Is there any way to get it read the current time ?

The Serial functions are not working anymore ?
Everything else (I2C, RTC, the time, the battery) is now not important. There is a serious problem.

What happens if you use the Serial.begin and Serial.println as the first two functions ?

Replace this:

#include <RTClib.h>

#include <Wire.h>
#include <Arduino.h>

with this:

#include <Wire.h>
#include <RTClib.h>

I think you have a problem with the Arduino, the computer, the power... and maybe a lot more.
Perhaps you are using a mix of two Arduino versions, or something is wrong with the computer, or the Arduino board is a fake and doesn't work very well.

You can not go on like this.

Use another computer, the newest Arduino IDE, another Arduino board, and start all over again. Do you know someone who can help ? I mean not online, but someone who can check the computer and wiring, and maybe can bring his own working Arduino board.

You have commented out this line:

RTC_DS3231 rtc;

That is why the compiler does not know 'rtc'.

You don't have to include the <Arduino.h>.

Hello,

Sorry, My bad.

When I run the code, it prints "Sketch has started".

When I remove the battery and unplug the RTC and plug it again, It should be printing "rtc lost power, adjust the time" but it does not print anything like that.
It show the time when I first set up ans starts running.

Did you buy the DS3231 module from Adafruit ?
There are modules on Ebay that have a diode to charge the battery. With 5V, the battery gets bad very quick.

I found my DS3231 module, it was not in a project and it was still running. After a year, only 7 seconds too slow :o
The battery was 2.84V.

The RTClib from Adafruit (installed via the Library Manager) works.
I used the "ds3231" example from the RTClib. I disconnected the Arduino, removed the battery from the DS3231 module, waited one minute, put battery back, connected everything, and I got the message that the time was lost, and it was set correctly.
It is easy to miss that message. Because, when the serial monitor is opened after uploading, the time might have been set already, since the Arduino starts running after uploading.

I use Arduino IDE 1.6.8.

Did you unpower the Arduino as well ? Which module do you have ? Do you use the Arduino IDE 1.6.8 ? Did you use the Library Manager to install the library ? Can you measure the battery voltage ? What is the voltage of the Arduino 5V pin (perhaps you have a bad usb cable) ?

Hello,

I unpowered everything,replaced the battery, checked the wiring, installed everything all over again ,installed the library using library manager and then checked. It now works fine. :slight_smile:
Thank you.
I

I am getting an error saying "SDcard-1:7: error: expected unqualified-id before numeric constant
SDcard-1.ino:14:1: note: in expansion of macro 'HSCDDRD005PD3A3_I2C' "

Could you please explain the error.

Thank you.