A guide how to connect and read the temperature from DS18b20 temperature sensor to Banana Pi.
Prepare:
- Banana Pi M1 installed with Armbian 5.25 v3.4.113.
- You can download Armbian 5.25 v3.4.113 with torrent (magnet link).
- uname -a # Linux bananapi 3.4.113-sun7i #17 SMP PREEMPT Thu Feb 23 19:43:34 CET 2017 armv7l GNU/Linux.
- 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:
- 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.