Ruben Laguna's blog

Aug 31, 2008 - 3 minute read - arduino digital ds1620 electronics measure measurement sensor temp temperature

Arduino and DS1620 digital temperature sensor

I received a couple of Arduinos Diecimilla that I bought from Libelium.

My first project with Arduino has been interfacing with DS1620 digital temperature sensor.

Picture 17

It was really easy it just a matter of connecting pin 3, 4 and 5 on the Arduino to RST, CLK and DQ on the DS1620. The source code in this post will read the temperature from the DS1620 using the 3 wire interface and it will output the result to the serial interface in the Arduino.

IMG_0329

I mostly use the information of PC Parallel Port Interfacing with DS1620 Digital Thermometer / Thermostat page to learn how to interface to this thermometer and the information on the SerialPortExample page to learn how to output to the serial port.

The DS1620 measures temperatures from -55°C to +125°C in 0.5°C increments. As you can see in the code reading a DS1620 sensor is way more complicated than reading an analog LM35 temperature sensor (and the LM35 gives you more precision and also takes one pin instead of three) so probably is a LM35 is a better option if you want an easy way to get the temperature in your arduino project.

Source code ( gist):

/*
  DS160 example

  Reading temperature from DS1620 digital temperature sensor 
  and showing the result via serial interface.
  
  
  Arduino    DS1620
  pin 3  ->    RST
  pin 4  ->    CLK
  pin 5  ->    DQ
  
  by Ruben Laguna
  based on examples from Tom Tigoe <http://www.arduino.cc/en/Reference/SoftwareSerialExample>
  and phanderson <http://www.phanderson.com/printer/ds1620/ds1620.html>
  written: 30 Aug 2008
  

*/

// include the SoftwareSerial library so you can use its functions:
#include <SoftwareSerial.h>

#define rxPin 0
#define txPin 1
#define ledPin 13
#define buttonPin 2

#define rstPin 3
#define clkPin 4
#define dqPin 5




// set up a new serial port
SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);
byte pinState = 0;

void setup()  {
  // define pin modes for tx, rx, led pins:
  pinMode(rxPin, INPUT);
  pinMode(txPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);
  pinMode(rstPin, OUTPUT);
  pinMode(clkPin, OUTPUT);
  pinMode(dqPin, OUTPUT);
  
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
}


void loop() {
  int val = digitalRead(buttonPin);
        
    rst_low();
    
    clk_high();
    rst_high(); //all data transfer are initiated by driving RST high
    write_command(0x0c); // write config command
    write_command(0x02); // cpu mode
    rst_low();
    delay(200); //wait until the configuration register is written 
    
    clk_high();
    rst_high();
    write_command(0xEE); //start conversion
    rst_low();
    delay(200);
   
    clk_high();
    rst_high();
    write_command(0xAA);
    int raw_data = read_raw_data();
    rst_low();
    
    mySerial.print("temperature:");
    mySerial.print(raw_data/2);
    mySerial.println(" C");
    
    
    delay(100);
  
  
  // toggle an LED just so you see the thing's alive.  
  
  toggle(13);

}


void toggle(int pinNum) {
  // set the LED pin using the pinState variable:
  digitalWrite(pinNum, pinState); 
  // if pinState = 0, set it to 1, and vice versa:
  pinState = !pinState;
}

void write_command(int command)
/* sends 8 bit command on DQ output, least sig bit first */ 
{
  int n, bit;
  
  for(n=0;n<8;n++)
  {
    bit = ((command >> n) & (0x01));
    out_bit(bit);
  }
}

int read_raw_data(void)
{
  int bit,n;
  int raw_data=0;

  pinMode(dqPin,INPUT);
  
     /* jam the dq lead high to use as input */
  for(n=0;n<9;n++)
  {
     clk_low();
     bit=(digitalRead(dqPin));
     clk_high();
     raw_data = raw_data | (bit <<  n);
  }
  pinMode(dqPin, OUTPUT);
  return(raw_data);
}

void out_bit(int bit) 
{
  digitalWrite(dqPin, bit);  /* set up the data */
  clk_low();             /* and then provide a clock pulse */   
  clk_high();
}

void clk_high(void)
{
  digitalWrite(clkPin,HIGH);
}

void clk_low(void)
{
  digitalWrite(clkPin,LOW);
}

void rst_high(void)
{
   digitalWrite(rstPin,HIGH);
}

void rst_low(void)
{
   digitalWrite(rstPin,LOW);
}