Arduino and 1-wire

Has anyone tried connecting to a dallas 1-wire temperature sensor yet?

looking for tips on it

DS18B20 is the sensor

I have found a site with 1-wire support for the atmega16, with source

http://gandalf.arubi.uni-kl.de/avr_projects/tempsensor/index.html

Does anyone know how I might integrate this with the arduino IDE

or am I barking up the wrong tree,

I am pretty good with the hardware but I am fairly poor with the software stuff

It certainly look like a possibility. You could try putting onewire.c and onewire.h into the ARDUINO/lib/targets/arduino directory and adding:

include "onewire.h"

to the top of your sketch. It's probably not that easy, but that's how I'd start.

On this site you can find a nice and fully documented project using 16 1 wire temp sensors with a bunch of Atmegas:

Introduction: http://66.249.93.104/translate_c?&langpair=de%7Cen&u=http://s-huehn.de/elektronik/tempmess/tempmess.htm

How to connect several 1 wire buses to an Atmega8: http://66.249.93.104/translate_c?&langpair=de%7Cen&u=http://s-huehn.de/elektronik/tempmess/tempmess-sm.htm

Edit: Google translator is acting up, maybe these links will work? http://translate.google.com/translate?u=http%3A%2F%2Fs-huehn.de%2Fe+lektronik%2Ftempmess%2Ftempmess.htm+&langpair=de%7Cen& http://translate.google.com/translate?u=http%3A%2F%2Fs-huehn.de%2Fe+lektronik%2Ftempmess%2Ftempmess.htm+&langpair=de%7Cen&

Not alot of help on that one, the hardware connects are correct but there is not much in software. I have found WinAVR had built in 1-wire support, but I dont yet know if its possible to load a WinAVR hex file using the arduino bootloader. Im gonna have to do a little more research.

I've been playing around with those 1-wire sensors as well a few months ago. They're very cool!

Interesting idea to try and hook them up to an Arduino. :)

(BUMP)

I'm new to this whole Arduino thing but it is a great design and tool, and I'm very enthusiastic about what I may be able to do with it.

Recently I received my own Arduino USB development board to play around with, and I also happened to order one of Dallas semiconductor's 1-wire temperature sensors. Painfully going through the datasheet for the DS12S20, I hacked together code to try to interface with this unit. I am not working in parasite power mode.

After getting the timing is right and the subroutines in place in order to read and write to the one wire interface, I was able to come up with a working software. The current version simply sends a request to do a conversion, waits until it is done, and then requests the scratchpad from the unit.

I thought that this would take much longer than a few hours, however at this point I can communicate with the temperature sensor and read back data. At this point, I simply have the nine bytes set back over in their numeric value, via the serial port (USB interface in my case).

Interestingly enough, so far I am able to receive proper data. What is sent back is on par with what the data sheet says should be sent back, including reserved registers and CRC codes, and the data that I received corresponds to what this should be by default. Also, I was able to make the Arduino write to the temperature sensor's storage registers and read the data back properly, so everything is working in that department.

The problem is, when I send a request to do a conversion, that is fine and well, except that the data that is returned for bytes one and two (which should be the temperature reading), always are returned as the default power on value for the DS12S20. For some reason, I cannot get it to convert and return temperature readings.

So, at this point it is only a marginal success in that I can get these sensors to hold two bytes for me, however I cannot get it to do what it was designed to do, and convert temperatures.

If anyone at this point is still trying to interface these or other chips that use the one wire interface, to Arduinos, it is possible with not too much code, however I am stumped at this point as to why the sensor will not initiate a conversion and return sensible data. I've tried hooking up an oscilloscope to the one wire data pin, and the transaction seems to be taking place properly, yet the DS12S20 will not write the finished conversion value to the scratchpad. Perhaps I have a bad sensor, however at this point I only have one so I cannot confirm this to be the problem.

The other thing is that in the data sheet for this temperature sensor, all of their examples heavily rely on the "parasite power mode" and I do not see specific examples of using it with external power. Any suggestions?

I tried to get a DS18S20 working but didn't get any further than the reset. That seemed to work but I never got any data from the sensor. I played around with the timing values but that didn't help me. The delayMicrosecond function seemed to work ok as far as I could measure it with my scope. So I guess filling in the delay's as mentioned in the datasheets should work.

Do you want to share your code? Or at least a read function?

Please let us know if you get it working! Here, or in my comments section.

Edit: My scope doesn’t seem to trigger well & sharp unless I do one request at a time in the loop. It appears that the data line goes to around 3v when the conversion starts. I did use the required 4.7k pull-up resistor on the data line. And, I accidentally hooked it up backwards polarity once, oops!

/* DS18S20 Temperature chip i/o
 * ---------------
 *
 * See http://pdfserv.maxim-ic.com/en/ds/DS1820-DS1820S.pdf for the datasheet.
 *
 * (copyleft) 2006 by Derek Yerger - Free to distribute freely.
 *
 *
 */
// variable declaration
int onepinio = 7; // i/o
int pres; // just a temporary variable to store return data  

void ds_reset(void) {
  pinMode(onepinio, OUTPUT);  // Pull low for 500mcs
  digitalWrite(onepinio,0);
  delayMicroseconds(500);
  pinMode(onepinio, INPUT);   // Release for at least 500mcs
  pres=0;
  delayMicroseconds(500);
  while (pres = 0) {
    pres=digitalRead(onepinio);
  }
}
void ds_write1(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(5);        // A "1" is when the line is pulled
  pinMode(onepinio, INPUT);    //     1mcs < t < 15mcs  
  delayMicroseconds(45);       // so the write slots are even
}

void ds_write0(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(20);        // A "0" is when t > 15mcs (30 typical)
  pinMode(onepinio, INPUT);
  delayMicroseconds(30);        // evening up again...
}

int ds_readbit(void) {
  pinMode(onepinio, OUTPUT);
  digitalWrite(onepinio,0);
  delayMicroseconds(1);          // A "read slot" is when 1mcs > t > 2mcs
  pinMode(onepinio, INPUT);
  delayMicroseconds(2);          // Wait just a bit
  return(digitalRead(onepinio)); // return what we see on the line
}

void ds_writebyte(byte dsbyte) {
  byte mask;                            // How heavy is this bit?
  int cwt;
  cwt=0;
  for (mask = 0x01; mask; mask <<= 1) {  // Thanks to mellis - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1147888882
    if (mask & dsbyte) {                 // Or whoever they got it from...
      ds_write1();
    } 
    else {
      ds_write0();
    }
  }
}

int ds_readbyte(void) {
  byte mask;
  int cwt;
  cwt=0;
  for (mask = 0x01; mask; mask <<= 1) {  // Thanks again.
    cwt = cwt + mask * ds_readbit();
  }
  return(cwt);
}

void setup(void) {
  // initialize inputs/outputs
  // start serial port
  pinMode(onepinio,INPUT);
  digitalWrite(onepinio,1);
  Serial.begin(9600);
}

void loop(void) {
  ds_writebyte(0xCC);         // Skip ROM
  ds_writebyte(0x44);         // Supposed to work, but doesn't for me.
  pres=0;
  //pinMode(onepinio, OUTPUT);  // Maybe I was going to try parasite power?
  //digitalWrite(onepinio, 1);
  //delay(100);
  //digitalWrite(onepinio, 0);  // In thst case, be sure to turn it back to 0
  //pinMode(onepinio, INPUT);   // to turn off the internal pull-up resistor.
  while (pres = 0) {          //   The chip's supposed to return a 0 until the
    pres=ds_readbit();        // conversion's done (can't do in parasite mode)
  }
  ds_reset();
  ds_writebyte(0xCC);         // Skip ROM
  ds_writebyte(0xBE);         // Read Scratchpad
  int cby;                    // Call me lazy, or creative: "current byte"
  cby=0;
  while (cby <=8) {           // we need 9 bytes
    Serial.print(ds_readbyte(), HEX);
    Serial.print(" ");
    cby=cby+1;
  }
  Serial.println();
  delay(100);                 // Lets not flood.
}

Well, unfortunately it doesn't work for me either. But, at least I understand why my code didn't work.

I Keep getting the same line: "AA 0 4B 46 FF FF C 10 87" The only thing I changed in the code was adding a ds_reset() at the start of the loop(void). But it didn't make any difference. I tried a "read rom", (33h) command. Then the 64bit ROM code is read from the scratchpad. So, that works. As you stated in the comment after the CONVERT (44h) command this probably doesn't work.

Well, unfortunately it doesn't work for me either. But, at least I understand why my code didn't work.

I Keep getting the same line: "AA 0 4B 46 FF FF C 10 87" The only thing I changed in the code was adding a ds_reset() at the start of the loop(void). But it didn't make any difference. I tried a "read rom", (33h) command. Then the 64bit ROM code is read from the scratchpad. So, that works. As you stated in the comment after the CONVERT (44h) command this probably doesn't work.

Yes, that's all I get too :-/ All the digital operations work, but convert doesn't. I haven't had time to, but I'd like to read my ROM code and address the chip specifically when issuing the convert command. The datasheet isn't very clear and straightforward in describing when "Skip ROM" can be used.

Happy Holidays!

The datasheet isn't very clear and straightforward in describing when "Skip ROM" can be used.

No, they mention it is possible with only one slave on the bus. See page 9 "SKIP ROM", in the DS1820 datasheet. (you can follow your own link tot the datasheet)

I searched around on avrfreaks.net to see if there are similar problems. I couldn't find anything useful. I thought I read something about a similar problem.

Would the problem be on timing? How long will the temperature be in the scratchpad? Or would it take more time to finish writing it to the scratchpad than you wait with the while(pres=0) loop?

The datasheet says the chip will return "0" if it is requested, until the conversion is done. I think the temperature is stored in the scratchpad and shouldn't have to be read x amount of time after converting. Your guess is as good as mine :-?

You almost certainly have a DS18S20P on your hands. This is a variant that only uses parasitic power, the power lead is not connected in the package. You can verify this by using another pin to trigger a scope just before you write the 0x44, you will then see the 8 bits followed by a tragically sagging voltage on the data pin.

I ran out to the FET shop and got a VN2406L (the guy said "everyone buys these", good enough for this) and wired it to another pin like the data sheet shows and everything works now.

The only sad part about this is that now you have to blindly wait 750ms for the temperature to read instead of waiting just long enough.

The atmega8 io pins have a maximum rating of 40mA per pin... But I'm not seeing maximum power consumption for the converter while converting in parasite mode. I did see the voltage drop about half after the start conversion command, so it must be trying to do parasite power.

Enabling pull-up on the data line by the microcontroller didn't seem to fix the problem. Also, I didn't spend too much time on the oscilliscope trying to perfect timings in accordance with the microcontroller.

So the fet is required to supply the device, and in that case, I assume a second digital out could be used to control that pull-up. If it could be done with the USB's 500mA, what would be suitable for this application? I need to order a few, there are no FET shops around these parts.

With a 750ms delay on conversions, if you had numerous devices on a line, it wouldn't be a very quick operation, to say the least. When I saw one when ordering my arduino, I thought it might be an interesting thing to use, but thermistors are just so much cheaper! :)

I’m missing something here, looking at the atmel source current spec (~20mA) and the ds18s20p current (~1.5mA) draw during a conversion it should be able to power a dozen temperature sensors on a single pin without a problem. Very odd. It is almost as if the Atmel is using its internal pull up resistor to go high instead of the buffer, but I don’t see a combination of settings that would cause that. I’ll try a bunch of resistors and make a current/voltage plot when I get back to m analog bench and see what those ports are really doing.

Ok, the world has not gone crazy. The Atmels really do provide >20ma on the pins and the DS18S20P does take about 1ma. The problem was in the timing. As soon as the DS18S20 receives the 0x44 it will start taking current for the conversion. Unfortunately, the write_byte function is sitting around for many microseconds with the pin in a high impedance state in order to provide timing margins. The DS18S20P crashes before the power gets turned on.

The solution is to not let the external pull-up bring the pin high after the low period of the final bit of the 0x44 is written, force it high with the output driver. Then after the 750ms conversion period passes flip the pin back to an input and let the external pull-up take over.

My little sensor is now happily converting away on my desk.

I'll clean up my changes to the one-wire code and put them up here soon.

That’s good news jims! How do you connect the DS18S20? I have a resistor between +5V and the data pin. I found this somewhere in the avrfreaks forum.

I have the grounds connected, the DQ pin on the DS18S20P connected to the pin of the Arduino, and a 4.7kohm resistor from DQ to +5v as the pullup. It is good to connect the Vdd pin of the DS18S20P to ground also so if you get a non-parasite version (DS18S20) it will also work in parasite mode.

If you are going to use more than a few of these on the same pin you will need to use a pullup transistor and another pin, as shown in the DS18S20 datasheet, but for a few, say less than 10? of them the Arduino has enough current capacity.

The solution is to not let the external pull-up bring the pin high after the low period of the final bit of the 0x44 is written, force it high with the output driver. Then after the 750ms conversion period passes flip the pin back to an input and let the external pull-up take over.

Do you use the same pin for this? In yerg2k's code I bring the pin high immediately after the ds_writebyte(0x44) function. But it still doesn't work. I was wondering if it maybe takes too much time to finish the ds_writebyte() function and that you are doing someting different with an additional pin.

You almost certainly have a DS18S20P on your hands.

Hmm, overlooked this one but indeed I have the DS18S20*P*. So far I still couldn't find what I am doing wrong. I am using the 750 uS delay after the 44h write, then pulling the pin high. Why don't I see what's going wrong?