Pages: 1 [2]   Go Down
Author Topic: Datalogging with RTC  (Read 2569 times)
0 Members and 1 Guest are viewing this topic.
Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The arduino activates via relays either a fan or a heater depending of course on the temperature, so the amount of hours a fan or heater is on per day is variable, and will not occur at set times during the day

So it appears you are simply timestamping those actions. This is quite straightforward, where you read the clock on an event and record it. No special alarm system warranted.

Quote
This is the RTC module I ended up getting, Im not sure how it works exactly because there is a p1 and a p2. Are they independant of each other in terms of power and grounding etc? i.e will the square wave pin interfere with the arduino if its connected via the p2 side?

All you need is four connections Vcc, gnd, sda, scl. The points with the same name are cross-connected so either will do and you can put the module on a proto shield and  use it as a sort of junction box.

Quote
Also I believe the battery terminal is for an optional battery and isn't required to use the battery at the back of the module? What is it for?  

The battery terminal makes the on-board battery voltage available. I guess you could use it for an off-board battery but I won't ask why you would do this and not use the on-board battery, which is free and I understand is good for ten years.

Quote
it would be neat if I could just sit it in there because its nice and compact plus I can just wire it to the analog pin from there. Maybe if its sitting on top of some header pins?

The obvious thing to do is build it onto a proto shield.


* protos.JPG (118.94 KB, 614x817 - viewed 30 times.)
« Last Edit: July 02, 2013, 10:10:57 am by Nick_Pyner » Logged

Offline Offline
Jr. Member
**
Karma: 1
Posts: 95
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I think your project could benefit from Proportional Integral Derivitave (PID) type control.  There are PID libraries available.  Take a look at this implementation:
http://fermentationriot.com/arduinopid.php
Logged

Offline Offline
Edison Member
*
Karma: 8
Posts: 1341
If you're not living on the Edge, you're taking up too much space!
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The Uno millis() function can be made to be accurate to within 1ppm simply by multiplying by a constant.  There is another thread on here describing exactly how to do it.
Logged

If you fall... I'll be there for you!
-Floor

Skype Brighteyes3333
(262) 696-9619

Offline Offline
Full Member
***
Karma: 0
Posts: 164
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I got the DS1307 module wired up and am experiencing some of the problems I anticipated. At the moment I am trying to set the RTC time to the system time and getting the RTC to save the time when the arduino is unplugged.

After downloading the time library I tried the TimeRTCSet example and it seemed to set it to the system time albeit a number of seconds behind. Now I upload the TimeRTC example and it also outputs the correct time to Serial. But when I unplug the Arduino to see if it will use the battery on the RTC module, the date and time have changed when I again run TimeRTC. E.g. it says the time is 17:18:42 2 11 2035, when the time is 19:23:00 7 21 2013.

What is the DS pin used for? And am I supposed to do something with the BAT terminal? Though I understand that is for an optional external battery and should keep the time with the included battery somehow?
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You have probably got a timeset routine with a fixed example messing things up.

I submit you are better off having the clock set by a separate programme.

Like this

Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only
// PRE-set the time in the void then USE THE RESET BUTTON to set it!

#include "Wire.h"
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527

void setup(){
  Wire.begin();
  Serial.begin(9600);
     
  setDateTime(); //MUST CONFIGURE IN FUNCTION
printDate();
Serial.println("loopstart");
}

void loop(){

  printDate();
 
  delay(1000);
}
// THIS IS WHERE YOU ENTER THE TIME
void setDateTime(){

  byte second =      30; //0-59
  byte minute =      24; //0-59
  byte hour =        0; //0-23
  byte weekDay =     5; //1-7
  byte monthDay =    19; //1-31
  byte month =       4; //1-12
  byte year  =       13; //0-99

  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero); //stop Oscillator

  Wire.write(decToBcd(second));
  Wire.write(decToBcd(minute));
  Wire.write(decToBcd(hour));
  Wire.write(decToBcd(weekDay));
  Wire.write(decToBcd(monthDay));
  Wire.write(decToBcd(month));
  Wire.write(decToBcd(year));

  Wire.write(zero); //start

  Wire.endTransmission();

}

byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
  return ( (val/10*16) + (val%10) );
}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());

  //print the date EG   3/1/11 23:59:59
  Serial.print(month);
  Serial.print("/");
  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(year);
  Serial.print("     ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
}

The battery keeps time when power is off and should be good for ten years.

You only need four connections; +5v, gnd, sda, scl.

the date and time have changed when I again run TimeRTC. E.g. it says the time is 17:18:42 2 11 2035, when the time is 19:23:00 7 21 2013.

What is the DS pin used for? And am I supposed to do something with the BAT terminal? Though I understand that is for an optional external battery and should keep the time with the included battery somehow?

Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 164
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When I used your code to set the time it did set correctly (as did the TimeRTCSet example), then I upload the TimeRTC and again the time is correct. However again when I unplug the arduino from the serial port and plug it in again, the serial moniter then shows "17:18:21 2 11 2035"

So it is still not saving the time, could you perhaps give me some other code that only reads the RTC time to test it?

This is the TimeRTC from the Time library I am using and I don't think it has any timeset routines as it outputs the correct time until the arduino is unplugged, thanks:

Code:
/*
 * TimeRTC.pde
 * example code illustrating Time library with Real Time Clock.
 *
 */

#include <Time.h> 
#include <Wire.h> 
#include <DS1307RTC.h>  // a basic DS1307 library that returns time as a time_t

void setup()  {
  Serial.begin(9600);
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
  if(timeStatus()!= timeSet)
     Serial.println("Unable to sync with the RTC");
  else
     Serial.println("RTC has set the system time");     
}

void loop()
{
   digitalClockDisplay(); 
   delay(1000);
}

void digitalClockDisplay(){
  // digital clock display of the time
  Serial.print(hour());
  printDigits(minute());
  printDigits(second());
  Serial.print(" ");
  Serial.print(day());
  Serial.print(" ");
  Serial.print(month());
  Serial.print(" ");
  Serial.print(year());
  Serial.println();
}

void printDigits(int digits){
  // utility function for digital clock display: prints preceding colon and leading 0
  Serial.print(":");
  if(digits < 10)
    Serial.print('0');
  Serial.print(digits);
}
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe it's something in the library, one of them. Using two libraries for this simple job seems rather a lot.

Try this

Code:
//Arduino 1.0+ Only
//Arduino 1.0+ Only

#include <LiquidCrystal.h>
#include "Wire.h"
#define DS1307_ADDRESS 0x68

LiquidCrystal lcd(8,9,16,5,6,7);

void setup(){
  Wire.begin();
  Serial.begin(9600);
      lcd.begin(16, 2);
      lcd.clear();
  // Print a message to the LCD.
  lcd.print("It is");
}

void loop(){
  printDate();
  delay(1000);

}

byte bcdToDec(byte val)  {
// Convert binary coded decimal to normal decimal numbers
  return ( (val/16*10) + (val%16) );
}

void printDate(){

  // Reset the register pointer
  Wire.beginTransmission(DS1307_ADDRESS);

  byte zero = 0x00;
  Wire.write(zero);
  Wire.endTransmission();

  Wire.requestFrom(DS1307_ADDRESS, 7);

  int second = bcdToDec(Wire.read());
  int minute = bcdToDec(Wire.read());
  int hour = bcdToDec(Wire.read() & 0b111111); //24 hour time
  int weekDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
  int monthDay = bcdToDec(Wire.read());
  int month = bcdToDec(Wire.read());
  int year = bcdToDec(Wire.read());


 lcd.setCursor(2, 1);
    switch (weekDay)                      // Friendly printout the weekday
  {
    case 1:
      lcd.print("MON  ");
      Serial.print("MON  ");
      break;
    case 2:
      lcd.print("TUE  ");
      Serial.print("TUE  ");
      break;
    case 3:
      lcd.print("WED  ");
      Serial.print("WED  ");
      break;
    case 4:
      lcd.print("THU  ");
      Serial.print("THU  ");
      break;
    case 5:
      lcd.print("FRI  ");
      Serial.print("FRI  ");
      break;
    case 6:
      lcd.print("SAT  ");
      Serial.print("SAT  ");
      break;
    case 7:
      lcd.print("SUN  ");
       Serial.print("SUN  ");
      break;
  }

  Serial.print(monthDay);
  Serial.print("/");
  Serial.print(month);
  Serial.print("/");
  Serial.print(year);
  Serial.print(" ");
  Serial.print(hour);
  Serial.print(":");
  Serial.print(minute);
  Serial.print(":");
  Serial.println(second);
 
   lcd.setCursor(8,1);
 
  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);

  lcd.setCursor(8,0);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
  lcd.print(second);
  lcd.println("   ");
 
}
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 164
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

After trying your code and leaving it for a while, I've come to the conclusion that it takes a few minutes for the time to update to the correct time. I'm a bit confused though because I thought that it only got the time from the RTC in setup, so why would it update after its been looping for a while?

Possible explanations for this? I'm not using any pullup resistors, I just have the SDA and SCL plugged into the A4 and A5 analog ins respectively. However if this is normal how can I check when the time is updated to the correct time?

Also one more question, what happens if the RTC updates the time by say, a second, when the arduino is running a delay? Even if its only a 30 millisecond delay, if the RTC is updating the seconds during this time wont that mess up my logging?
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you are referring to me, I have posted two programmes, one to set the time and the other to read it. Neither will do both. The time set will display the time but that is only for confirmation.

Procedure:

1. Open windows clock
2. Enter the start time in the setup section
3. Load the programme about thirty seconds before that time
4. See that time on the display i.e. about fifteen seconds fast
5. Press reset button a few seconds early
6. Release the button at the appropriate time
7. Confirm clock is in sync with windows clock.

I've come to the conclusion that it takes a few minutes for the time to update to the correct time.

Definitely not. It happens more or less instantly on release of the reset button. If anything else is happening it is either mechanical, or user error.

I
Quote
I'm a bit confused though because I thought that it only got the time from the RTC in setup, so why would it update after its been looping for a while?

It sends the time to the RTC in setup.

Quote
I just have the SDA and SCL plugged into the A4 and A5 analog pins respectively.

That should be OK, as proven in that you actually see something. No pullups necessary.

Quote
However if this is normal how can I check when the time is updated to the correct time?

It isn't normal.

Quote
Also one more question, what happens if the RTC updates the time by say, a second, when the arduino is running a delay? Even if its only a 30 millisecond delay, if the RTC is updating the seconds during this time wont that mess up my logging?

You are missing the point of an RTC. The RTC runs independently of the Arduino, is not interested or affected by what the Arduino is doing, and this applies even if the Arduino is not running. It's job is to supply the Arduino with a time signal on demand.

The only way you can stuff things up with a delay, is by inserting a delay between calling for the time and actually displaying it. I guess this needs to be taken into account in a large programme but, even then, any error will be consistent and therefore probably irrelevant.
Logged

Offline Offline
Full Member
***
Karma: 0
Posts: 164
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the misunderstanding, I didn't have any problems setting or displaying the time and am aware of the seperate functions of the seperate pieces of code you posted.

After unplugging the RTC from my breadboard and connecting the power and ground directly to the arduino, I've determined that it must be a voltage/wiring issue, since it works perfectly and doesn't display a single line of incorrect time when connected directly.

I need some help however understanding where the new issue is coming from. Im using a 820 point breadboard and have one 5v relay, one DHT11 sensor, SD card, two connections to + for the LCD and now one connection for the RTC, the same for ground.

I was under the impression that if I used the power rails all components would be wired in parallel and thus woulden't be prone to voltage drops.

Thanks for taking the time to reply so concisely, and quickly. Further assistance would be equally appreciated, thanks.
Logged

Dee Why NSW
Offline Offline
Edison Member
*
Karma: 47
Posts: 2272
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


I was under the impression that if I used the power rails all components would be wired in parallel and thus woulden't be prone to voltage drops.


Partly true. The real problem is the available current. If you are short of that, the voltage will drop, no matter what else you do. You don't say where the power is coming from. If you are using the USB cable, you might fix all this simply by using a 9v wall wart. I understand you have already proven the code is kosher. Weird clock readings can be an indication of inadequate power.
Logged

Pages: 1 [2]   Go Up
Jump to: