Difference between revisions of "EBC Exercise 31 Dallas 1-Wire"

From eLinux.org
Jump to: navigation, search
m (Software Setup: Updated for 4.19 kernel)
m (Maxim MAX31820 on a PocketBeagle Grove Kit)
(10 intermediate revisions by the same user not shown)
Line 5: Line 5:
 
The data communication requires only one wire! (However you still need wires for ground and 3.3V.)
 
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.   
 
You can wire it to any GPIO port.   
 +
 +
Here we show you how to wire a DS18B20 to a Beagle Bone Black and a MAX31820 to a PocketBeagle.
 +
 +
== DS18B20 on a Black ==
  
 
[https://www.sparkfun.com/products/11050 SparkFun] sells a DS18B20 that's in a waterproof probe. You'll need it and maybe a 4.7kΩ pull up resistor.
 
[https://www.sparkfun.com/products/11050 SparkFun] sells a DS18B20 that's in a waterproof probe. You'll need it and maybe a 4.7kΩ pull up resistor.
Line 31: Line 35:
 
loaded.
 
loaded.
 
  bone$ '''cd /opt/source/bb.org-overlays'''
 
  bone$ '''cd /opt/source/bb.org-overlays'''
 +
bone$ '''git pull'''
 
  bone$ '''ls'''
 
  bone$ '''ls'''
 
  COPYING  dtc-overlay.sh  include    jenkins_build.sh  Makefile          readme.md  src
 
  COPYING  dtc-overlay.sh  include    jenkins_build.sh  Makefile          readme.md  src
Line 59: Line 64:
 
  };
 
  };
  
gpio3, pin 28 is P9_12.
+
gpio0, pin 28 is P9_12.
 +
 
 +
==== Installing ====
 +
Now edit <code>/boot/uEnv.txt</code> and find the line:
 +
#uboot_overlay_addr4=/lib/firmware/<file0>.dtbo
 +
and change it to
 +
uboot_overlay_addr4=/lib/firmware/BB-W1-P9.12-00A0.dtbo
 +
Note the <code>#</code> is missing from the beginning of the line.
 +
 
 +
Reboot
 +
bone$ '''sudo reboot'''
  
 
=== Reading the DS18B20 ===
 
=== Reading the DS18B20 ===
  bone$ '''cd /sys/bus/w1/devices'''
+
  bone$ '''cd /sys/class/hwmon/'''
 +
hwmon0  hwmon1
 +
Oh, we have two devices here.  Let's see which is which
 +
bone$ '''cat */name'''
 +
w1_slave_temp
 +
tmp101
 +
So one is our one-wire temp sensor and the other is a tmp101 sensor.  Let's read ours.
 +
bone$ '''cd hwmon0'''
 
  bone$ '''ls'''
 
  bone$ '''ls'''
  28-00000829ed85  w1_bus_master1
+
  device  name  power  subsystem temp1_input uevent
You should see two directories, the first will have a different number than mine.
+
  bone$ '''cat temp1_input'''
The number is the serial number of your DS18B20, which is unique to each device.
+
  20812
bone$ '''cd 28-00000829ed85'''
+
 
bone$ '''ls'''
+
The '''20812''' is the temperature in C times 1000, that is, divide this number by 1000 to get the temp in C.
driver  id name  power  subsystem  uevent w1_slave
 
  bone$ '''cat w1_slave'''  
 
  87 01 4b 46 7f ff 09 10 48 : crc=48 YES
 
87 01 4b 46 7f ff 09 10 48 t=24437
 
The '''t=24437''' 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.
 
Warm up the probe and see what happens to the temp.
Line 83: Line 100:
 
  bone$ '''cp BB-W1-P9.12-00A0.dts BB-W1-''P9.14''-00A0.dts'''
 
  bone$ '''cp BB-W1-P9.12-00A0.dts BB-W1-''P9.14''-00A0.dts'''
 
Substitute your pin number for '''P9.14'''.   
 
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.  
+
Then edit your newly created file and switch all the occurrences of P9_12 and P9.12 to the new pin number. Also look for the lines near the end:
 +
compatible = "w1-gpio";
 +
gpios = <&gpio1 28 GPIO_ACTIVE_HIGH>;
 +
Change the gpio port number and pin number to match your new pin.  Then compile and install.
 
  bone$ '''cd /opt/source/bb.org-overlays'''
 
  bone$ '''cd /opt/source/bb.org-overlays'''
 
  bone$ '''make install'''
 
  bone$ '''make install'''
bone$ '''echo BB-W1-P9.14 > $SLOTS'''
+
Now edit the line in <code>/boot/uEnv.txt</code> to point to your new device.
 +
 
 
Wire your DS18B20 to the new pin and test it.
 
Wire your DS18B20 to the new pin and test it.
 +
 +
== Maxim MAX31820 on a PocketBeagle Grove Kit ==
 +
 +
The MAX31820 uses the same interface as the 18D20.  The only difference is editing the device tree file for the PocketBeagle.  The device is wired as:
 +
 +
[[File:MAX31820.jpg|frameless|MAX31820 Bottom View]]
 +
 +
I attached the data line (DQ) to the UART4-TX line on the Grove cape. The table in <code>/var/lib/cloud9/PocketBeagle/Grove/README.md</code>
 +
shows that UART4-TX is attached to <code>P2_07</code> on the Pocket.
 +
 +
The
 +
[https://docs.google.com/spreadsheets/d/1FRGvYOyW1RiNSEVprvstfJAVeapnASgDXHtxeDOjgqw/edit#gid=0&range=48:48 PocketBeagle Header Table]
 +
shows that <code>P2_07</code> is <code>GPIO 0_31</code> and also appears at <code>P9_13</code> on the Black. 
 +
With this information you can now copy and edit the device tree.
 +
 +
bone$ '''cd /opt/source/bb.org-overlays/src/arm/'''
 +
bone$ '''cp BB-W1-P9.12-00A0.dts BB-W1-P2.07-00A0.dts'''
 +
 +
Edit <code>BB-W1-P2.07-00A0.dts</code> and change the three appearances of <code>P9_12</code> to <code>P2_07</code>.
 +
{| class="wikitable"
 +
|-
 +
! Line !! Code
 +
|-
 +
| 28 || <code>BB-W1-P2.07-00A0 = __TIMESTAMP__;</code>
 +
|-
 +
| 40 || <code>P2_07_pinmux { status = "disabled"; };</code>
 +
|-
 +
| 50 || <code>BONE_P9_13 0x37</code>
 +
|-
 +
| 66 || <code>gpios = <&gpio0 31 GPIO_ACTIVE_HIGH>;</code>
 +
|}
 +
 +
Note: On line 50 BONE_2_07 isn't defined so we use the BeagleBone Black pin name instead.
 +
 +
Finally note, line 66 uses GPIO 0_31.
 +
 +
Do like before to make and install
 +
 +
bone$ '''cd /opt/source/bb.org-overlays'''
 +
bone$ '''make && sudo make install'''
 +
 +
Then edit <code>/boot/uEnv.txt</code> to include the new device tree.  Reboot and test.
 +
 
{{YoderFoot}}
 
{{YoderFoot}}

Revision as of 10:45, 27 July 2021

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.

Here we show you how to wire a DS18B20 to a Beagle Bone Black and a MAX31820 to a PocketBeagle.

DS18B20 on a Black

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*
/lib/firmware/BB-W1-P9.12-00A0.dtbo

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$ git pull
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*
BB-W1-P9.12-00A0.dts
bone$ less *W1*

Page down a ways to see

       fragment@3 {
               target-path="/";
               __overlay__ {

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

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

gpio0, pin 28 is P9_12.

Installing

Now edit /boot/uEnv.txt and find the line:

#uboot_overlay_addr4=/lib/firmware/<file0>.dtbo

and change it to

uboot_overlay_addr4=/lib/firmware/BB-W1-P9.12-00A0.dtbo

Note the # is missing from the beginning of the line.

Reboot

bone$ sudo reboot

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
w1_slave_temp
tmp101

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
20812

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. Also look for the lines near the end:

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

Change the gpio port number and pin number to match your new pin. Then compile and install.

bone$ cd /opt/source/bb.org-overlays
bone$ make install

Now edit the line in /boot/uEnv.txt to point to your new device.

Wire your DS18B20 to the new pin and test it.

Maxim MAX31820 on a PocketBeagle Grove Kit

The MAX31820 uses the same interface as the 18D20. The only difference is editing the device tree file for the PocketBeagle. The device is wired as:

MAX31820 Bottom View

I attached the data line (DQ) to the UART4-TX line on the Grove cape. The table in /var/lib/cloud9/PocketBeagle/Grove/README.md shows that UART4-TX is attached to P2_07 on the Pocket.

The PocketBeagle Header Table shows that P2_07 is GPIO 0_31 and also appears at P9_13 on the Black. With this information you can now copy and edit the device tree.

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

Edit BB-W1-P2.07-00A0.dts and change the three appearances of P9_12 to P2_07.

Line Code
28 BB-W1-P2.07-00A0 = __TIMESTAMP__;
40 P2_07_pinmux { status = "disabled"; };
50 BONE_P9_13 0x37
66 gpios = <&gpio0 31 GPIO_ACTIVE_HIGH>;

Note: On line 50 BONE_2_07 isn't defined so we use the BeagleBone Black pin name instead.

Finally note, line 66 uses GPIO 0_31.

Do like before to make and install

bone$ cd /opt/source/bb.org-overlays
bone$ make && sudo make install

Then edit /boot/uEnv.txt to include the new device tree. Reboot and test.




thumb‎ Embedded Linux Class by Mark A. Yoder