Go Down

Topic: Temp Sensor and RTC issue Help Please (Read 617 times) previous topic - next topic

Dvir812

Hi
First thank for your help :)

I'm Using Arduino mega and i'm try to connect RTC & Temp Sensor to my board

I have 3 temp sensors and 2 RTC .
1. I dont know why but the RTC dose not show the real time , each time i diconnect it from the power and connect it again it goes on from the last time ...instaed of getting the real time .

2. for some reason i can't read the temp

3.What is the DS pin ??

Attached my code :)
Code: [Select]

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


#define ONE_WIRE_BUS 2// Data wire is plugged into pin 2 on the Arduino
// Setup a oneWire instance to communicate with any OneWire devices
// (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);


OneWire  ds(53);  // on pin 10...
int Pump1 = 30;
int Pump2 = 32;
int Pump3 = 34;
int Pump4 = 44;// use the pin of pump 8 cause of relay issue !!
int Pump5 = 38;
int Pump6 = 40;
int Pump7 = 42;
//int Pump8 = 44;
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

const int ledPin = 20;
//const int floatSwitch = 9;
const int pump8 = 21;
//int floatStatecounter = 0;
//int buttonState = 0;
//long lastButtonState = 0;
//long failSafe = 5000;

int Emergency_switch = 11; //Will close the pump in case of over load
int TopSwitch = 22;//The max water level
int Button_Switch = 23;//The min water level

void setup(void)  {

  Wire.begin(); //initialize the I2C bus
lcd.begin(16, 2);// set up the LCD's number of columns and rows:
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Reef Controller"); //Print a message to the LCD.
delay(3000);

 
  pinMode(ledPin, OUTPUT);
  pinMode(Emergency_switch, INPUT);
  pinMode(TopSwitch, INPUT);
  pinMode(Button_Switch, INPUT);
  pinMode(pump8, OUTPUT);
 
  pinMode(Pump1, OUTPUT);
  pinMode(Pump2, OUTPUT);
  pinMode(Pump3, OUTPUT);
  pinMode(Pump4, OUTPUT);
  pinMode(Pump5, OUTPUT);
  pinMode(Pump6, OUTPUT);
  pinMode(Pump7, OUTPUT);
// pinMode(Pump8, OUTPUT);
   Serial.begin(9600);
   
    setTime(compileTime());    //set the system time to the sketch compile time
    RTC.set(now());            //set the RTC from the system time
   
  setSyncProvider(RTC.get);   // the function to get the time from the RTC
    if (timeStatus()!= timeSet)
        Serial.print("Unable to sync with the RTC");
    else
       Serial.println("RTC has set the system time");
  //  Serial.println("Dallas Temperature IC Control Library Demo");
  // Start up the library
   sensors.begin();


void loop(){
Pumps();
Temp();
ATO();
printTime(now());
}
//*********************************DOISING SYSTEM*******************************************
void Pumps(){
if(weekday()==1&&hour()==6&&minute()==55&&second()<3){
        digitalWrite(Pump1,HIGH);//Day1 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Pump1 is working");
        Serial.print("Puamp1 is working");
       // delay(40000);
        delay(7000);
        digitalWrite(Pump1, LOW);
        delay(500);
}
  else if(weekday()==2&&hour()==6&&minute()==55&&second()<3){
        digitalWrite(Pump2,HIGH);//Day2 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.println("Puamp2 is working");
        Serial.print("Puamp2 is working");
        delay(7000);
        digitalWrite(Pump2, LOW);
        delay(500);

}
  else if(weekday()==3&&hour()==6&&minute()==55&&second()<3){
        digitalWrite(Pump3,HIGH);//Day3 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Puamp3 is working");
        Serial.print("Puamp3 is working");
        delay(7000);
        digitalWrite(Pump3, LOW);
        delay(500);
}
  else if(weekday()==4&&hour()==6&&minute()==55&&second()<3){
        digitalWrite(Pump4,HIGH);//Day4 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Puamp4 is working");
        Serial.print("Puamp4 is working");
        delay(7000);
        digitalWrite(Pump4, LOW);
        delay(500);

}
  else if(weekday()==5&&hour()==6&&minute()==55&&second()<3){
        digitalWrite(Pump5,HIGH);//Day5 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Puamp5 is working");
        Serial.print("Puamp5 is working");
        delay(7000);
        digitalWrite(Pump5, LOW);
        delay(500);
}
  else if(weekday()==6&&hour()==7&&minute()==30&&second()<3){
        digitalWrite(Pump6,HIGH);//Day6 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Puamp6 is working");
        Serial.print("Puamp6 is working");
        delay(7000);
        digitalWrite(Pump6, LOW);
        delay(500);

}
  else if(weekday()==7&&hour()==17&&minute()==23&&second()<3){
        digitalWrite(Pump7,HIGH);//Day7 8:00
        lcd.clear();
        lcd.setCursor(0,1);
        lcd.print("Puamp7 is working");
        Serial.print("Puamp7 is working");
        delay(15000);
        digitalWrite(Pump7, LOW);
        delay(500);

}
}
//**********************************READ TEMPRATURE*****************************************
// call sensors.requestTemperatures() to issue a global temperature
  // request to all devices on the bus
void Temp(){
  Serial.print(" Requesting temperatures...");
  sensors.requestTemperatures(); // Send the command to get temperatures
  Serial.println("DONE");
  Serial.print("Temperature for Device 1 is: ");
  lcd.setCursor(6,0);
  lcd.print("T=");
  delay(1000);
  lcd.setCursor(8,0);
  lcd.print(sensors.getTempCByIndex(0));
  Serial.print(sensors.getTempCByIndex(0));
delay(1000);
// Why "byIndex"?
    // You can have more than one IC on the same bus.
    // 0 refers to the first IC on the wire
}
//**********************************AUTO TOP OFF********************************************
void ATO()
{
  if(Button_Switch==LOW && TopSwitch==LOW && pump8==LOW||Button_Switch==HIGH && TopSwitch==LOW && pump8==LOW)
  {
    digitalWrite(pump8, HIGH);
    lcd.print("ATO is on");
  }
  else if (TopSwitch==HIGH||Emergency_switch==HIGH)
  {
        digitalWrite(pump8, LOW);
        lcd.print("ATO is off");
 
  }
}

time_t compileTime(void)
{
#define FUDGE 15        //fudge factor to allow for compile time (seconds, YMMV)

    char *compDate = __DATE__, *compTime = __TIME__, *months = "JanFebMarAprMayJunJulAugSepOctNovDec";
    char chMon[3], *m;
    int d, y;
    tmElements_t tm;
    time_t t;

    strncpy(chMon, compDate, 3);
    chMon[3] = '\0';
    m = strstr(months, chMon);
    tm.Month = ((m - months) / 3 + 1);

    tm.Day = atoi(compDate + 4);
    tm.Year = atoi(compDate + 7) - 1970;
    tm.Hour = atoi(compTime);
    tm.Minute = atoi(compTime + 3);
    tm.Second = atoi(compTime + 6);
    t = makeTime(tm);
    return t + FUDGE;        //add fudge factor to allow for compile time
}

void printTime(time_t t){
printI00(hour(t), ':');
lcd.clear();
lcd.setCursor(0,0);
lcd.print(hour());
lcd.setCursor(2,0);
lcd.print(":");
lcd.setCursor(3,0);
lcd.print(minute());
delay(1000);
printI00(minute(t), ':');
printI00(second(t), ' ');
Serial.print(dayShortStr(weekday(t)));
  Serial.print(' ');
  printI00(day(t), ' ');
  Serial.print(monthShortStr(month(t)));
  Serial.print(' ');
  Serial.println(year(t));
}

//Print an integer in "00" format (with leading zero),
//followed by a delimiter.
//Input value assumed to be between 0 and 99.
void printI00(int val, char delim)
{
    if (val < 10) Serial.print('0');
    Serial.print(val);
    Serial.print(delim);
    return;
}

Nick_Pyner



1. I dont know why but the RTC dose not show the real time , each time i diconnect it from the power and connect it again it goes on from the last time ...instaed of getting the real time .

Code: [Select]

    setTime(compileTime());    //set the system time to the sketch compile time
    RTC.set(now());            //set the RTC from the system time
 



Possibly the above. Try this

http://bildr.org/2011/03/ds1307-arduino/

Dvir812

10x for your help

I'll try this code and let you know what happened  :D


Any idea for the DS18B20 sensor ?  :~


Nick_Pyner

#3
Jul 28, 2014, 04:49 pm Last Edit: Jul 28, 2014, 06:25 pm by Nick_Pyner Reason: 1
I use the weatherproof package from eBay,  complete with plug. They are available with cable length up to 5m. These plug into a home-brewed sensor shield.


Dvir812

Hi

little update , the Temp Sensor works great

but the RTC does not work at all , someone help please  =(

Nick_Pyner

You need to be more forthcoming about what the problem is. Note that, in the clock project I alluded to, there are two programmes - one to set the time, the other to read the time ( the clock)!

michinyon

The RTC needs a battery, so it doesn't forget the time every time you disconnect the arduino.


michinyon

why do you have two rtc ?

I would set that ridiculous and overcomplicated sketch aside for the time being,     and write another sketch which just tests the rtc.    It will then be easier to test,   and easier to compare with the many other examples of sketches which use rtc.    When you have got it to work and completely understand it,    then you can incorporate the functionality from it,  into your temperature process control sketch.


michinyon

Quote
What is the DS pin ??


You only need on instance of the OneWire object to communicate with your  one-wire devices.

You seem to be creating two instances at the top of your sketch.    This is both unneccesary,   and probably won't work.   When you create the OneWire class object,   you need to tell it which pin of your arduino the data line of the one-wire devices is connected you.    In your case,  pin 2,  or pin 53,   or maybe pin 10.    Make up your mind and stick with it.

Dvir812

Quote
why do you have two rtc ?


I thought the first one is broken so I bought another one

Quote
Quote
What is the DS pin ??

I choose pin 52 as my DS :)

Quote
The RTC needs a battery, so it doesn't forget the time every time you disconnect the arduino.


I know , and the battery is connected as well

Quote
You need to be more forthcoming about what the problem is. Note that, in the clock project I alluded to, there are two programs - one to set the time, the other to read the time ( the clock)!


I try to set the time with the second program , then i open the serial monitor and the time was as before (165:165...)

Connection :
SCl - SCL
SDA - SDA
+ - +
- - -
maybe the SCl & SDA on board is damaged ? how can i test it ?
thank you all :)


Nick_Pyner


Quote
why do you have two rtc ?


I thought the first one is broken so I bought another one


Haha! I had wondered about that.

Quote

I try to set the time with the second program , then i open the serial monitor and the time was as before (165:165...)

Connection :
SCl - SCL
SDA - SDA

maybe the SCl & SDA on board is damaged ? how can i test it ?


SDA/SCL are the I2C bus and not exclusive to the clock. You could test it with another I2C device like an LCDS but, if you don't have one, don't rush to do this as there is probably a simpler solution. I still think it is power or slack-arsed wiring.

This is a set and run variation that might help you see what is going on.

Code: [Select]
//Arduino 1.0+ Only
//Arduino 1.0+ Only
// pre-set the time in the void then use reset button to set it!

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

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

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

void loop(){
      lcd.clear();
  printDate();
 
  delay(1000);
}

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);


  lcd.print(monthDay);
  lcd.print("/");
  lcd.print(month);
  lcd.print("/");
  lcd.print(year);
 
  lcd.setCursor(0,1);
  lcd.print(hour);
  lcd.print(":");
  lcd.print(minute);
  lcd.print(":");
    if(second <10)
  {
    lcd.print("0");
  }
  lcd.print(second);
}

Dvir812

Hi
I did some test with the code you gave me ( with some minor lcd pins change )
The LCD display 165/165/165
                                 45:165:165

What does it mean ?


Nick_Pyner

You are right to change the numbers and the fact that you can see something clearly indicates the LCD is OK and properly wired. The numbers you see imply inadequate power. If you are using power from USB cable, now is the time to change to a proper 9v wall wart. You might be able to prove this by disconnecting the LCD and sending the data to the serial monitor instead. You might even get a result by disconnecting just the backlight to the LCD.

If you are using a Tiny RTC DS1307, there is a faint possibility that the battery is flat and causing confusion, but I suspect the supply.

Go Up