DS18B20 temperature sensor with Banana Pi M1 (Armbian 5.25 3.4.113)

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 v3.4.113.
  2. You can download Armbian 5.25 v3.4.113 with torrent (magnet link).
  3. uname -a # Linux bananapi 3.4.113-sun7i #17 SMP PREEMPT Thu Feb 23 19:43:34 CET 2017 armv7l GNU/Linux.
  4. Check in directory /boot/ if it has link script.bin (/boot/bin/bananapi.bin) or not. If Yes, continue.

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 install some essential apps:

cd ~
apt-get install build-essential pkg-config gcc git make
apt-get install libusb-1.0 libusb-dev

Clone the sunxi-tools and install it:

git clone https://github.com/linux-sunxi/sunxi-tools
cd sunxi-tools
make

Decompile script.bin file to editable format and make some changes:

bin2fex /boot/script.bin /boot/bpi.fex
nano /boot/bpi.fex

Find in the [gpio_para] the port PH20 is assigned to which gpio_pin:

[gpio_para]
gpio_used = 1
gpio_num = 88
gpio_pin_1 = port:PB20<1><default><default><default>
...
gpio_pin_23 = port:PH20<1><default><default><default>
...
gpio_pin_26 = port:PI16<1><default><default><default>

The physical pin that we connect DS18b20 is pin16, which is named PH20, so gpio_pin_23 are our target, add this line at the end of file:

[w1_para]
gpio = 23

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

Save file (Ctrl-X -> Y -> Enter). Compile it back to script.bin, edit the /etc/modules to automatically load the w1 modules:

fex2bin /boot/bpi.fex /boot/script.bin
nano /etc/modules
# Add these into the end of file:
w1-sunxi
w1-gpio
w1-therm
# Save and exit.

Reboot the system and check when it booted:

reboot
dmesg
# Looking for:
#[    7.256275] Driver for 1-wire Dallas network protocol.
#[    7.303197] w1_master_driver w1_bus_master1: Family 28 for 28.xxxxxxxxxxxx.18 is not registered.
# 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.

Troubleshooting:

  1. While I do this, on the first times, I couldn’t get it to work, after some experiment, I found out that one of my tweak in /etc/rc.local has caused the trouble. After comment out those lines, the DS18b20 has been recognised. I have no idea why, but at least, I get it to work after a week. Hoo-rah.
# These line below has caused 1-wire not working
#sysctl -w net/core/rmem_max=8738000 #131071
#sysctl -w net/core/wmem_max=6553600 #131071
#sysctl -w net/ipv4/tcp_rmem="8192 873800 8738000" #4096    87380   6177280
#sysctl -w net/ipv4/tcp_wmem="4096 655360 6553600" #4096    16384   4194304
#sysctl -w vm/min_free_kbytes=65536 #3514

 

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.

5 thoughts on “DS18B20 temperature sensor with Banana Pi M1 (Armbian 5.25 3.4.113)”

  1. Hi, nice article! I’d like to deploy my own BananaPi with Armbian 5.25 3.4.113 — do U still have an image of it? The newest Armbian is broken (USB does not work, bugged). Please, reply.
    Arthur

    1. Here is the magnet link for it, I’ll also put it in the post so everyone can easily find and download it:

      magnet:?xt=urn:btih:BBCF55476F2F82D7CF9AA5C084A83821B186C368&tr=http%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Fp4p.arenabg.com%3A1337%2Fannounce&tr=udp%3A%2F%2Fpeerfect.org%3A6969%2Fannounce

      How to use magnet link? – You can download μTorrent® (uTorrent) or Deluge client, install it and paste the above link in, it will download. It’s slow I know, but at least it stays on for a long time. And you can pause and resume as many times as you need.

      Why I use magnet link aka torrent? – As I intend to keep files for a long time, also I cannot choose any good file hosting out there that keeps my files without deleting it after a while.

  2. Hello,
    i can’t get it to work. Most likely is, tat i have not found the right gpio pin.
    My system is:
    [email protected]:~# cat /etc/armbian-release
    # PLEASE DO NOT EDIT THIS FILE
    BOARD=bananapim2zero
    BOARD_NAME=”Banana Pi M2 Zero”
    VERSION=5.34
    LINUXFAMILY=sun8i
    BRANCH=default
    ARCH=arm
    IMAGE_TYPE=user-built
    BOARD_TYPE=conf
    INITRD_ARCH=arm
    KERNEL_IMAGE_TYPE=zImage
    [email protected]:~#

    I have loaded the three modules w1_therm, w1_gpio and w1_sunxi. The DS18B20 is connected to Pin1:VCC, Pin7:Signal, Pin9:GND.
    I found Pin7 to be gpio4. So i have in /boot/script.bin (converted from /boot/bananapi.fex)
    [w1_para]
    w1_used = 0
    gpio = 4

    dmesg looks, as if everything is loaded correctly:
    [ 5.105841] W1_SUNXI: Added w1-gpio on GPIO-4
    [ 5.109902] Driver for 1-wire Dallas network protocol.
    [ 5.120284] systemd[1]: Mounted Debug File System.
    [ 5.140205] systemd[1]: Mounted POSIX Message Queue File System.

    But there is no device. What can i do? Might GPIO-4 be a different port?

    TIA
    R.S.

    1. Hi, I did try to get the exact gpio for your board, but it seems like I couldn’t find it. Still looking for it, when I have the right answer, I’ll let you know. In case you have found out the solution, welcome to post it here so others who have the same problem can solve it like you did 😉

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.