Using a DS18B20 temperature sensor on a BeagleBone isn’t difficult, but requires a bit of coding. Let’s get started with an introduction on the different types of sensors.
Choose A Sensor
The actual DS18B20 sensor is around the size of a pea. You can order it pre-wired in a few different configurations, or purchase just the sensor itself. For my application, I am using the already waterproof sensor shown here. It is enclosed in a stainless thermowell and wrapped with shrinkwrap.
Wire It Up
Each manufacturer seems to use a different color for the data wire, so yours may differ. Here is how mine was setup:
- Red – 3V
- White – Data
- Black – GND
You will need to connect a
4.7k resistor between the data wire and 3V wire. This diagram shows 5V, but we can still have it work using 3V. You only need one if you are using multiple sensors. More on this below.
Connect Sensor to the BeagleBone

Next, you will need to choose a GPIO pin to plug your DS18B20 into. I used this image to help visualize where each pinout was located . For this example, I used the P8 header, pin number 11.
Notice that the number of the pin does not correspond to the GPIO number! Also of importance, you cannot simply choose any pin. Some of the pins are reserved- for example, most of the higher numbered pins on the P9 header are taken up by the HDMI interface unless you have disabled it.
Which pins are free are detailed in the next step.
Preparing to Build the Device Tree
If you’re familiar with raspian (the optimized debian distro running on Raspberry Pi’s), you’ll recall the setup is essentially plug and play thanks to raspian’s kernel. On the Beaglebone, it’s consistent with the newer linux kernel so you will need to compile a device tree. At it’s most basic level, a device tree simply informs the kernel about the location and type of a specific piece of hardware.
Now that we are wired up, boot your Beaglebone and login with SSH. We now need to compile a device tree for the one-wire interface. This is where noting the GPIO pin becomes important. Open the P8 or P9 PDF below and find the head pin you used. You may need to click ‘Raw’ if you are taken to Github.

P8 Header

P9 Header
Now, let’s get into some coding. Login via SSH. Using your favorite text editor, copy and paste the below code (I usually use nano
). If you are using a different GPIO pin, you will need to make some changes. See below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
/* * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. * * Modified by Russell Senior from the weather cape's DTS file. * Minor formatting by C W Rose. */ /dts-v1/; /plugin/; / { compatible = "ti,beaglebone", "ti,beaglebone-black"; part-number = "BB-W1"; version = "00A0"; exclusive-use = "P8.11"; fragment@0 { target = <&am33xx_pinmux>; __overlay__ { bb_w1_pins: pinmux_bb_w1_pins { pinctrl-single,pins = <0x34 0x37 /* gpmc_ad13.gpio1_13, OMAP_PIN_INPUT_PULLUP | OMAP_MUX_MODE7 - w1-gpio */ >; }; }; }; fragment@1 { target = <&ocp>; __overlay__ { onewire@0 { status = "okay"; compatible = "w1-gpio"; pinctrl-names = "default"; pinctrl-0 = <&bb_w1_pins> gpios = <&gpio2 13 0>; }; }; }; }; |
If you are using a pin other than P8_11, you will need to make a few changes. See the PDF header tables linked above for GPIO information.
- On line 19, change
exclusive-use = "P8.11";
to the corresponding header and header pin you used.
- On line 25, change
0x34
to your corresponding GPIO offset. The offsets are found in the PDF’s above. For example, if I used P8_12, the code should be 0x30
. Leave 0x37
where it is.
Save your edit and return to the shell.
Build the Device Tree
Now we can build the file using dtc:
|
dtc -O dtb -o w1-00A0.dtbo -b 0 -@ w1.dts |
Missing [email protected] flag error:
If you come across this error, you probably have a dated version of dtc. You can download a newer version that is compatible at:
|
wget -c https://raw.githubusercontent.com/RobertCNelson/tools/master/pkgs/dtc.sh |
Copy it to /lib/firmware and load:
|
cp w1-00A0.dtbo /lib/firmware echo w1 > /sys/devices/bone_capemgr.9/slots |
You should be set now. To ensure everything has loaded correctly, run the following command. If everything is correctly configured, you should see Override Board Name
in your list.
|
$ cat /sys/devices/bone_capemgr.9/slots 0: 54:PF--- 1: 55:PF--- 2: 56:PF--- 3: 57:PF--- 4: ff:P-O-L Bone-LT-eMMC-2G,00A0,Texas Instrument,BB-BONE-EMMC-2G 5: ff:P-O-L Bone-Black-HDMI,00A0,Texas Instrument,BB-BONELT-HDMI 7: ff:P-O-L Override Board Name,00A0,Override Manuf,w1 |
You will need to run the echo w1 > /sys/devices/bone_capemgr.9/slots
command on every boot. To do this automatically, add the line to your /etc/rc.local file somewhere after the comments section.
Taking a Temperature Reading
Your temperature sensors are located at /sys/devices/w1_bus_master1/
. They are of the format 28-00000xxxxxxx
and correspond to your sensor’s unique serial number.
To view the current value just cat
the file. You will have two lines of output. The temperature (in celsius) is at the very end.
|
$ cat /sys/devices/w1_bus_master1/28-0000045d9d8a/w1_slave 07 01 4b 46 7f ff 09 10 da : crc=da YES 07 01 4b 46 7f ff 09 10 da t=16437 |
The current reading is 16.4 degrees celsius.
Using Multiple DS18B20 Sensors
This is where the one wire interface really shines. You can connect up to about ten temperature probes and still use the same GPIO pin. This is incredibly useful and makes using multiple sensors clean and mess-free. You still only need to use one 4.7k resistor. The sensors are connected as shown in this diagram:
