A guide how to connect and read the temperature from DS18b20 temperature sensor to Banana Pi.

Prepare:

  1. Banana Pi M1 installed with Armbian 5.25 v4.9.7.
  2. uname -a # Linux bananapi 4.9.7-sunxi #1 SMP Thu Feb 2 01:52:06 CET 2017 armv7l GNU/Linux.
  3. Check in directory /boot/ if it has dir /dtb/ or not, if yes then continue, if not then use other way.

Hardware:

Connect the DS18b20 with the Banana Pi as following:

If you have 1 sensor, just connect one, if you have more, the same applied.

Setup

(all the command here is running under root account, if you logged in with other account, please use sudo or similar way to run these commands)

Go to your home folder and make a new dir named “dtb”:

cd ~
mkdir dtb
cd dtb

Copy the .dtb file from /boot/dtb to ~/dtb folder:

cp /boot/dtb/sun7i-a20-bananapi.dtb . # Remember the last dot.

Backup the original .dtb file then decompile the .dtb file:

mv sun7i-a20-bananapi.dtb sun7i-a20-bananapi.dtb.orig
dtc -I dtb -O dts sun7i-a20-bananapi.dtb > sun7i-a20-bananapi.dts

Edit the sun7i-a20-bananapi.dts file:

nano sun7i-a20-bananapi.dts

Find this line: pinctrl@01c20800, and add pio: before it like this:

pio: pinctrl@01c20800 {

Then go to end of file, before the last }; add this:

onewire@0 {
 compatible = "w1-gpio";
 gpios = <&pio 7 20 0>; /* PH20 */
 status = "okay";
};
# Note: PH20 is pin 16.

Hint 1: Why <&pio 7 20 0>; is PH20? The answer is the first number comes from H character (A-0 B-1 C-2 D-3 E-4 F-5 G-6 H-7 and so on…). 20 is from the number after PH.

Hint 2: Why do PH20 is physically pin 16 on the board? Please refer to this image:

Save the file with Ctrl+X -> Y -> Enter.
Compile .dts file back to .dtb, then copy .dtb back to /boot/dtb folder:

dtc -I dts -O dtb sun7i-a20-bananapi.dts > sun7i-a20-bananapi.dtb
cp sun7i-a20-bananapi.dtb /boot/dtb/sun7i-a20-bananapi.dtb
sync
shutdown -r now

After reboot, the Banana Pi should detect the DS18B20 sensor. Check and get the result:

ls /sys/bus/w1/devices
# Result
# ls /sys/bus/w1/devices
# 28-xxxxxxxxxxxx w1_bus_master1
# 28-xxxxxxxxxxxx is the ID of the sensor.
cat /sys/bus/w1/devices/28-xxxxxxxxxxxx/w1_slave
# Result:
# 70 01 4b x6 7f xx 0c 10 40 : crc=40 YES
# 70 01 4b x6 7f xx 0c 10 40 t=23000

Which means the DS18B20 temperature sensor read 23.000°C.

ooOoo

Disclaimer: The information in this page is collected and learned from my experience. If any info is wrong, please kindly let me know in the comment.

Leave a Reply

Your email address will not be published. Required fields are marked *