Problem with pin 13

Hello guys,

I own an Arduino Duemilanove and I'd like to control a few things with it. For example, I want to get and display temperature onto a LCD screen using DS18B20 temp sensors.

When I hook up the sensor to pin 12, it works fine and displays temperature but when I connect it on pin 13, it displays "0". I know the temp sensor is working so I guess there's a problem with pin 13.

Does anyone know what I could have done wrong ?

Thanks in advance for your help ;)

Does anyone know what I could have done wrong ?

You could have posted your sketch.

You realise that there is a on board LED and series resistor already connected to pin 13? This may be effecting your sensor.

Lefty

Lefty : this is one option, I’m going to try to hook it up to an other pin, but what’s pin 13 used for then ?

Here’s my code :

//REEF CONTROLLER

//Librairies**************

#define DS1307_I2C_ADDRESS 0x68
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>

#include <stdio.h>
#define TEMP_PIN 13

void getCurrentTemp(char *temp);

int ledPin = 10;

//**************

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;

int fan = 3;
int fan_on_temp = 2300; //Turn on fan at this temp
int fan_off_temp = 2200; //turn fan off once below this temp
int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

//Date functions : set, get ************************

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

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

void setDateDs1307(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)
{
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.send(decToBcd(second)); // 0 to bit 7 starts the clock
Wire.send(decToBcd(minute));
Wire.send(decToBcd(hour)); // If you want 12 hour am/pm you need to set
// bit 6 (also need to change readDateDs1307)
Wire.send(decToBcd(dayOfWeek));
Wire.send(decToBcd(dayOfMonth));
Wire.send(decToBcd(month));
Wire.send(decToBcd(year));
Wire.send(0x10);
Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_I2C_ADDRESS);
Wire.send(0);
Wire.endTransmission();

Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

// A few of these need masks because certain bits are control bits
second = bcdToDec(Wire.receive() & 0x7f);
minute = bcdToDec(Wire.receive());
hour = bcdToDec(Wire.receive() & 0x3f); // Need to change this if 12 hour am/pm
dayOfWeek = bcdToDec(Wire.receive());
dayOfMonth = bcdToDec(Wire.receive());
month = bcdToDec(Wire.receive());
year = bcdToDec(Wire.receive());
}

//Temperature functions : set, get***********

void OneWireReset(int Pin) // reset. Should improve to act as a presence pulse
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT); // bring low for 500 us
delayMicroseconds(500);
pinMode(Pin, INPUT);
delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
byte n;

for(n=8; n!=0; n–)
{
if ((d & 0x01) == 1) // test least sig bit
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(60);
}
else
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(60);
pinMode(Pin, INPUT);
}

d=d>>1; // now the next bit is in the least sig bit position.
}

}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
byte d, n, b;

for (n=0; n<8; n++)
{
digitalWrite(Pin, LOW);
pinMode(Pin, OUTPUT);
delayMicroseconds(5);
pinMode(Pin, INPUT);
delayMicroseconds(5);
b = digitalRead(Pin);
delayMicroseconds(50);
d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
}
return(d);
}

void getCurrentTemp(char *temp)
{
//int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

OneWireReset(TEMP_PIN);
OneWireOutByte(TEMP_PIN, 0xcc);
OneWireOutByte(TEMP_PIN, 0xbe);

LowByte = OneWireInByte(TEMP_PIN);
HighByte = OneWireInByte(TEMP_PIN);
TReading = (HighByte << 8) + LowByte;
sign = TReading & 0x8000; // test most sig bit
if (sign) // negative
{
TReading = (TReading ^ 0xffff) + 1; // 2’s comp
}
Tc_100 = (6 * TReading) + TReading / 4; // multiply by (100 * 0.0625) or 6.25

whole = Tc_100 / 100; // separate off the whole and fractional portions
fract = Tc_100 % 100;

sprintf(temp, “%c%3d%c%2d”, (sign==0)?’+’:’-’, whole, ‘.’, fract);

}

//Set up******************

void setup()
{
pinMode(ledPin, OUTPUT);

pinMode(fan, OUTPUT);

//pinMode(13, OUTPUT);

// initialize DS18B20 datapin
digitalWrite(TEMP_PIN, LOW);
pinMode(TEMP_PIN, INPUT); // sets the digital pin as input (logic 1)

lcd.begin(16, 2);
lcd.clear();
lcd.println("Reef Controller ");
delay(5000);
lcd.clear();
delay(1000);

Wire.begin();
Serial.begin(9600);

/second = 00;
minute = 22;
hour = 21;
dayOfWeek = 2;
dayOfMonth = 5;
month = 1;
year = 10;
setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
/
}

//Loop********************

void loop()
{
char temp_string[10];

getDateDs1307();

lcd.setCursor(0, 0);
if (dayOfMonth < 10) lcd.print(“0”);
lcd.print(dayOfMonth);
lcd.print("/");
if (month < 10) lcd.print(0);
lcd.print(month);
lcd.print("/");
if (year < 10) lcd.print(“0”);
lcd.print(year);

lcd.setCursor(0, 1);
if (hour < 10) lcd.print(“0”);
lcd.print(hour);
lcd.print(":");
if (minute < 10) lcd.print(“0”);
lcd.print(minute);
lcd.print(":");
if (second < 10) lcd.print(“0”);
lcd.print(second);

getCurrentTemp(temp_string);
lcd.setCursor(10, 1);
lcd.print(temp_string);

delay(1000);
//if(Tc_100 > fan_on_temp){ // turn fan on if temp is above fan_on_temp
// digitalWrite(fan, HIGH);
//}
//if(Tc_100 < fan_off_temp){ //turn fan off if temp is below fan_off_temp
// digitalWrite(fan, LOW);
//}

//for loop to go through low to high the arduino pwm cycle
/for(int i=0; i<=255; i++){
analogWrite(ledPin, i);
delay(200);
}
for(int j=255; j>=0; j–){ //for loop to go high to low pwm cycle
analogWrite(ledPin, j);
delay(200);
}
/

//delay(1000);
}

but what's pin 13 used for then

It is used to light an on board LED for testing things. Some have the resistor in series with the pin you see but others have the pin brought right out to the connector but have the LED and resistor across this. This makes the characteristics of that pin a bit different to the others but it is still will still work. By default this is defined as an output and all the other pins as inputs (except pins 0 & 1 which are used for serial)

Can you please modify that last post by putting a {code} in front of the code and a {/code} at the back. Only put square braces instead of the curly ones.

Alrite, here’s the code (hope it’ll work) :

//REEF CONTROLLER

//Librairies**************

#define DS1307_I2C_ADDRESS 0x68
#include <WProgram.h>
#include <Wire.h>
#include <DS1307.h>
#include <LiquidCrystal.h>

#include <stdio.h>
#define TEMP_PIN 13

void getCurrentTemp(char *temp);

int ledPin = 10;

//**************

LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
int second, minute, hour, dayOfWeek, dayOfMonth, month, year;

int fan = 3;
int fan_on_temp = 2300;  //Turn on fan at this temp
int fan_off_temp = 2200; //turn fan off once below this temp
int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;


//Date functions : set, get ************************

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

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

void setDateDs1307(int second, int minute, int hour, int dayOfWeek, int dayOfMonth, int month, int year)          
{
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  Wire.send(0);
  Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  Wire.send(decToBcd(minute));
  Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
                                  // bit 6 (also need to change readDateDs1307)
  Wire.send(decToBcd(dayOfWeek));
  Wire.send(decToBcd(dayOfMonth));
  Wire.send(decToBcd(month));
  Wire.send(decToBcd(year));
  Wire.send(0x10);
  Wire.endTransmission();
}

// Gets the date and time from the ds1307
void getDateDs1307()
{
 // Reset the register pointer
 Wire.beginTransmission(DS1307_I2C_ADDRESS);
 Wire.send(0);
 Wire.endTransmission();

 Wire.requestFrom(DS1307_I2C_ADDRESS, 7);

 // A few of these need masks because certain bits are control bits
 second     = bcdToDec(Wire.receive() & 0x7f);
 minute     = bcdToDec(Wire.receive());
 hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
 dayOfWeek  = bcdToDec(Wire.receive());
 dayOfMonth = bcdToDec(Wire.receive());
 month      = bcdToDec(Wire.receive());
 year       = bcdToDec(Wire.receive());
}

//Temperature functions : set, get***********

void OneWireReset(int Pin) // reset.  Should improve to act as a presence pulse
{
    digitalWrite(Pin, LOW);
    pinMode(Pin, OUTPUT); // bring low for 500 us
    delayMicroseconds(500);
    pinMode(Pin, INPUT);
    delayMicroseconds(500);
}

void OneWireOutByte(int Pin, byte d) // output byte d (least sig bit first).
{
  byte n;

  for(n=8; n!=0; n--)
  {
     if ((d & 0x01) == 1)  // test least sig bit
     {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(5);
        pinMode(Pin, INPUT);
        delayMicroseconds(60);
     }
     else
     {
        digitalWrite(Pin, LOW);
        pinMode(Pin, OUTPUT);
        delayMicroseconds(60);
        pinMode(Pin, INPUT);
     }

     d=d>>1; // now the next bit is in the least sig bit position.
  }
  
}

byte OneWireInByte(int Pin) // read byte, least sig byte first
{
   byte d, n, b;

   for (n=0; n<8; n++)
   {
       digitalWrite(Pin, LOW);
       pinMode(Pin, OUTPUT);
       delayMicroseconds(5);
       pinMode(Pin, INPUT);
       delayMicroseconds(5);
       b = digitalRead(Pin);
       delayMicroseconds(50);
       d = (d >> 1) | (b<<7); // shift d to right and insert b in most sig bit position
   }
   return(d);
}


void getCurrentTemp(char *temp)
{  
 //int HighByte, LowByte, TReading, Tc_100, sign, whole, fract;

 OneWireReset(TEMP_PIN);
 OneWireOutByte(TEMP_PIN, 0xcc);
 OneWireOutByte(TEMP_PIN, 0x44); // perform temperature conversion, strong pullup for one sec

 OneWireReset(TEMP_PIN);
 OneWireOutByte(TEMP_PIN, 0xcc);
 OneWireOutByte(TEMP_PIN, 0xbe);

 LowByte = OneWireInByte(TEMP_PIN);
 HighByte = OneWireInByte(TEMP_PIN);
 TReading = (HighByte << 8) + LowByte;
 sign = TReading & 0x8000;  // test most sig bit
 if (sign) // negative
 {
   TReading = (TReading ^ 0xffff) + 1; // 2's comp
 }
 Tc_100 = (6 * TReading) + TReading / 4;    // multiply by (100 * 0.0625) or 6.25

 whole = Tc_100 / 100;  // separate off the whole and fractional portions
 fract = Tc_100 % 100;

 sprintf(temp, "%c%3d%c%2d", (sign==0)?'+':'-', whole, '.', fract);

 
     
}      


//Set up******************

void setup()
{
 pinMode(ledPin, OUTPUT);
 
 pinMode(fan, OUTPUT);
 
 //pinMode(13, OUTPUT);
 
 // initialize DS18B20 datapin
 digitalWrite(TEMP_PIN, LOW);
 pinMode(TEMP_PIN, INPUT);      // sets the digital pin as input (logic 1)
 
 lcd.begin(16, 2);
 lcd.clear();
 lcd.println("Reef Controller ");
 delay(5000);
 lcd.clear();
 delay(1000);
   
 Wire.begin();
 Serial.begin(9600);

 /*second = 00;
 minute = 22;
 hour = 21;
 dayOfWeek = 2;
 dayOfMonth = 5;
 month = 1;
 year = 10;
 setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);*/
}

//Loop********************

void loop()
{
 char temp_string[10];
 
 getDateDs1307();
 
 lcd.setCursor(0, 0);
 if (dayOfMonth < 10) lcd.print("0");
 lcd.print(dayOfMonth);
 lcd.print("/");
 if (month < 10) lcd.print(0);
 lcd.print(month);
 lcd.print("/");
 if (year < 10) lcd.print("0");
 lcd.print(year);
 
 lcd.setCursor(0, 1);
 if (hour < 10) lcd.print("0");
 lcd.print(hour);
 lcd.print(":");
 if (minute < 10) lcd.print("0");
 lcd.print(minute);
 lcd.print(":");
 if (second < 10) lcd.print("0");
 lcd.print(second);
 
 getCurrentTemp(temp_string);
 lcd.setCursor(10, 1);
 lcd.print(temp_string);

 delay(1000);
 //if(Tc_100 > fan_on_temp){     // turn fan on if temp is above fan_on_temp
  // digitalWrite(fan, HIGH);
 //}  
 //if(Tc_100 < fan_off_temp){    //turn fan off if temp is below fan_off_temp
 // digitalWrite(fan, LOW);
 //}    

//for loop to go through low to high the arduino pwm cycle
 /*for(int i=0; i<=255; i++){  
   analogWrite(ledPin, i);  
   delay(200);
 }
 for(int j=255; j>=0; j--){  //for loop to go high to low pwm cycle
   analogWrite(ledPin, j);
   delay(200);
 }*/


 //delay(1000);
}

I just don’t think the one wire protocol will work on pin 13. The LED and series resistor is going to act as a pull-down when the protocol changes the pin to a input, etc. However I’ve never played with a one wire sensors so I will let others guide you on this attempt to use pin 13.

Lefty

I just don't think the one wire protocol will work on pin 13.

Yes I think you are right again. ;)

What it will be is the pull up resistor on the 1 wire bus will be too big to actually pull up that line sufficiently to make it look like a logic 1. The 1 wire bus works by devices pulling the bus low and when noting pulls low the bus is pulled high through a resistor. Also that high can power a device, if it takes it's power paralytically and again the pull down effect of resistor and diode will prevent the device from even powering up.

Alrite, considering what you have told me, I've switched the pins around so temp sensor is now hooked up to pin 3 and fan (which was connected to pin 3) to pin 13. I've tested it out and everything is working great !

Thank you guys for your help ! :)

Just a quick question:

I am also going to putting together a reef controller.

Is the temp sensor submerged directly in the water or is encased in something first?

I see a lot of programming and projects but no pictures or discussion of how the water temp is obtained.

Thanks