EBC Exercise 31 Dallas 1-Wire

From eLinux.org
Revision as of 11:55, 24 October 2019 by Yoder (talk | contribs) (Reading the DS18B20)
Jump to: navigation, search

thumb‎ Embedded Linux Class by Mark A. Yoder

The DS18B20 is an interesting temperature sensor that uses Dallas Semiconductor's 1-wire based interface. The data communication requires only one wire! (However you still need wires for ground and 3.3V.) You can wire it to any GPIO port.

SparkFun sells a DS18B20 that's in a waterproof probe. You'll need it and maybe a 4.7kΩ pull up resistor.

Attach the leads a follows.

DS18B20 Lead Attach to
Red 3.3V
Black ground
White P9_12

You may also need to attach the 4.7kΩ resistor between P9_12 and 3.3V.

Software Setup

The DS18B20 can be attached to any GPIO pin, but there's a device tree already created to attach it to P9_12.

Finding the device tree

/lib/firmware contains many device trees. Let's see which ones work with one-wire interface

bone$ ls /lib/firmware/*W1*

Looks like there is one setup for P9_12. Let's check the source code. The Bone should already have the source files loaded.

bone$ cd /opt/source/bb.org-overlays
bone$ ls
COPYING  dtc-overlay.sh  include     jenkins_build.sh  Makefile          readme.md  src
debian   examples        install.sh  Jenkinsfile       readme-legacy.md  scripts    tools

If the cd fails you will have to clone the repository.

bone$ git clone https://github.com/beagleboard/bb.org-overlays
bone$ cd bb.org-overlays

Either way

bone$ cd src/arm
bone$ ls *W1*
bone$ less *W1*

Page down a ways to see

       fragment@3 {
               __overlay__ {

                       onewire {
                               status = "okay";
                               pinctrl-names = "default";
                               pinctrl-0 = <&dallas_w1_pins>;

                               compatible = "w1-gpio";
                               gpios = <&gpio1 28 GPIO_ACTIVE_HIGH>;

gpio3, pin 28 is P9_12.

Reading the DS18B20

bone$ cd /sys/class/hwmon/
hwmon0  hwmon1

Oh, we have two devices here. Let's see which is which

bone$ cat */name

So one is our one-wire temp sensor and the other is a tmp101 sensor. Let's read ours.

bone$ cd hwmon0
bone$ ls
device  name  power  subsystem  temp1_input  uevent
bone$ cat temp1_input

The 20812 is the temperature in C times 1000, that is, divide this number by 1000 to get the temp in C.

Warm up the probe and see what happens to the temp.

Using a Different GPIO Pin

You can use pins other than the P9_12. Follow the unconfiguring instructions for the GPIO pin of your choice. Then

bone$ cd /opt/source/bb.org-overlays/sr/arm
bone$ cp BB-W1-P9.12-00A0.dts BB-W1-P9.14-00A0.dts

Substitute your pin number for P9.14. Then edit your newly created file and switch all the occurrences of P9_12 and P9.12 to the new pin number.

bone$ cd /opt/source/bb.org-overlays
bone$ make install
bone$ echo BB-W1-P9.14 > $SLOTS

Wire your DS18B20 to the new pin and test it.

thumb‎ Embedded Linux Class by Mark A. Yoder