Home > electronics > The 1-wire (o/w) Protocol – DS18S20

The 1-wire (o/w) Protocol – DS18S20

Hello again,

This post is actually going to become handy on the future posts I am planning for the DS18S20 thermometer. This is a well known and used thermometer as it simple to implement and quite accurate. I will do a brief description of the protocol it uses to communicate with our PIC. Through this protocol we can send commands to the DS18S20 and receive data and responses.
This is actually a brief review of the datasheet of the device which is already very simple to understand. This post is just to introduce the basics of the o/w protocol.

First you can download the latest pdf from here.

1. Hardware Connection

Now let’s see how a o/w device should be connected in the circuit:

ds18s20 hardware connection

ds18s20 hardware connection

As you can see you have to connect the DQ line, which is the only connection (duh! o/w protocol!) , with a 5kOhm pullup resistor.

2. Communication Overview

Now we have the thermometer connected how do we get the temperature? Well, we need to follow the protocol rules about temperature conversion.

The o/w protocol specifies the following communication sequence:

  1. Initialization
  2. ROM command – followed by the required data
  3. DS18S20 function command – again followed by the required data

The Initialization step, is a way to tell everyone connected on the o/w DQ line that we need to make a communication. Something you need to know is that the beauty of the o/w protocol is the fact that we can connect multiple devices on the same DQ line. So we only need to occupy one pin of our microcontroller. So, in order to initialize, the master (our PIC) need to reset the line by pulling it low (0 volts). The exact procedure will be explained later. So after this, every device in our o/w network will respond with a presence pulse. So now we know the number of the devices connected. But lets take the simplest example and asssume that we only have one DS18S20 connected.

After the DS18S20 responded, we can proceed to the next step which is the ROM command. This is a way to distinguish the slave devices. So for example if I have 10 different DS18S20 in my house, this is the step where I define that I want to talk to the temperature sensor in the kitchen. This is done using the 64-bit ROM codes that every o/w device has a unique one. In cases where we have only one device (our case!) this step is again necessary, but instead of passing a ROM code we just send a ‘SKIP ROM’ command. This means that the command that will follow should be executed by all slave devices.

The third and final step, is the DS18S20 function command. This is where you actually send a command to the o/w device. There are commands which are global for all the o/w devices. There are also other commands which correspond to particular devices like in the DS18S20. You can read the commands in the datasheet. For our example here we need 2 of those commands. The ‘CONVER T’ is used to tell the thermometer that we need a temperature reading and it should start making a conversion. DS18S20 takes about 1 second to convert the temperature to a digital value we can use. So after this second passes will need to issue a ‘READ SCRATCHPAD’ command to read the temperature. The scratchpad is the place where the DS18S20 will store the digital value after the conversion. It is a memory location on the device’s EEPROM.

Hmm, that’s it actually! In the next paragraph we will just see how this happens on a lower level.

3. The Actual o/w Protocol Signals

We have talked about sending and receiving data to the DS18S20 but how is that actually implemented? Well Maxim has an extremely useful and simple document which you should read before trying this out. You can get it here. Again, I will not get into details as this is not the purpose of this post. I will just briefly familiarize you with the protocol signals.

Everything discussed in Paragraph 2 is actually made of 3 types of signals:

  • The Initialization signal
  • The Write 0 and Write 1 signals
  • The Read 0 and Read 1 signals

As explained the master (our PIC) needs to issue the initialization signal before sending anything else. Therefore, it needs to pull the DQ line low for at least 480 micro secs and then poll the line to listen to the response of the DS18S20. The exact timings are shown in the datasheet. You need to follow them accurately but don’t worry. The application note I showed you above will make everything quite easy!

The Write 0/1 signals are used to send data to the slave (DS18S20). Again the exact timings are shown in the datasheet. Let’s take an example. Let’s say we initilized the line, the DS18S20 responded and we are ready to send the ‘SKIP ROM’ command as we described before. This command in HEX is ‘CC’. In binary that is ‘1100 1100’. So in order to send the command we need to:

  1. Write 0
  2. Write 0
  3. Write 1
  4. Write 1
  5. Write 0
  6. Write 0
  7. Write 1
  8. Write 1

Remember that the o/w protocol is least-significant-bit first. And this is the way to send commands to the DS18S20.

So, we initialized the DQ line, we send the ‘SKIP ROM’ command followed by the ‘CONVERT T’ command. We now have to wait 1 sec until the conversion ends. After that we need to issue the ‘READ SCRATCHPAD’ command. Find the value of the command and follow the procedure above to send the commnd. After that the DS18S20 is ready to transmit the data. So how do we receive it?

We use the READ signals to receive data from the thermometer. We start the READ signal by pulling the line low for about 1 micro second and then release it. Then the master will read the line status. If the line is low then the DS18S20 transmitted a ‘0’ bit else if it is high it transmitted a ‘1’ bit.

That is how protocol works. Go read the pdf’s and if you have any questions let me know!

Related Posts:

Incoming search terms:

  • DS18S20 (26)
  • ds18s20 pic (17)
  1. No comments yet.
  1. No trackbacks yet.