I've been looking at this tutorial: http://www.cl.cam.ac.uk/projects/raspberrypi/tutorials/temperature/

And I have some questions:

  1. Must we connect the data pin to GPIO4? Why not some other GPIO?

  2. How does exactly the sensor work? I mean what is the signal that it sends through the DATA pin? Is it somehow unified for all temp sensors so the kernel can understand any of them?

  3. Why do we have to put a resistor between the POWER and DATA pins?


Must we connect the data pin to GPIO4? Why not some other GPIO?

The sensor uses the 1-wire communication protocol. Originally the Pi driver for the 1-wire protocol was hard wired to use GPIO4. Now you can use any accessible GPIO. The GPIO still defaults to 4 but you can specify another by


where x is the Broadcom numbered GPIO you want to use.

How does exactly the sensor work?

I have no idea. It communicates the result in digital fashion using the complicated Dallas 1-wire bus protocol. Only 1-wire sensors use this protocol. Other popular sensors such as the DHTxx series do not.

Why do we have to put a resistor between the POWER and DATA pins

You don't unless the sensor is powered from 3V3. You should have an external pull-up to 3V3 of about 4k7. The sensor signals 0 by pulling the DATA line to ground. It signals 1 by letting the signal float to an external voltage. This system simplifies signalling in both directions along one wire. It also allows the device to be powered by 5V but have its DATA line connected to a only 3V3 tolerant GPIO like the Pis.

| improve this answer | |
  1. See joan's answer for how to configure the w1-gpio driver in your raspberry Pi config file.

  2. You can see more information about how the sensor and 1-Wire protocol work by looking at the data sheet for that particular sensor. Additionally if you want some real code to look at, you can see the code for the Linux w1-therm driver which implements the protocol. The most important pieces are in the w1_slave_show function – the driver has to send over a bunch of commands in order to actually get the reading. Here's some annotated bits from that function:

    // select the appropriate device on the bus
    if (!w1_reset_select_slave(sl)) {
        // get the power supply mode
        w1_write_8(dev, W1_READ_PSUPPLY);
        external_power = w1_read_8(dev);
        // send a convert temperature command to get a reading
        w1_write_8(dev, W1_CONVERT_TEMP);
        // wait up to 750ms for the conversion
        sleep_rem = msleep_interruptible(tm);
        // now read the full scratchpad memory in from the sensor
        w1_write_8(dev, W1_READ_SCRATCHPAD);
        if ((count = w1_read_block(dev, rom, 9)) != 9) {
            /* snip */

    The most recent reading is stored in the first 2 bytes of the scratchpad memory, in 12.4 fixed point. Later on it reads out those bytes and converts them to an integer, which corresponds to temp * 1000:

    static inline int w1_DS18B20_convert_temp(u8 rom[9])
        s16 t = le16_to_cpup((__le16 *)rom);
        return t*1000/16;

    There is a bunch of stuff in there about using the parasite power mode, which uses only the data line and an internal capacitor to power the device. Assuming you're externally powering the device via the Raspberry Pi's 3.3V rails then you can ignore a lot of that. All of this information is elaborated on in the data sheet for the DS18B20 component in the tutorial. It would seem from that code that all of the supported devices share most of the same protocol.

  3. This is called a pull-up resistor and is used to ensure that the data line has a reference point for when it signals high. See this question for more information about why that particular resistor works.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.