Skip to content

Instantly share code, notes, and snippets.

@ecerulm
Created August 2, 2010 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecerulm/505342 to your computer and use it in GitHub Desktop.
Save ecerulm/505342 to your computer and use it in GitHub Desktop.
/*
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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment