Krausmueller.de

Measure temperature with Cubietruck und DS18B20 sensor

Note: If you already configured your system and the sensor is not working all the time have a look at your cpu power saving settings.

In contrast to the Raspberry Pi unfortunately the Cubietruck is not as popular. Because of this there are much less instructions for how to use GPIO pins. Goal of this article is to measure the temperatur with a DS18B20 sensor. First of all you need the sensor (e.g. via Amazon or ebay). It has 3 wires: Ground (GND), 3,3 volt (VDD) and signal (DQ).

DS18B20

Between VDD and DQ you have to put a 4.7 K Ohm resistor (also see data sheet of the sensor). For testing purposes you can build the curcuit with the help of a breadboard, but I soldered the resistor directly to the sensor.

DS18B20 mit angelöteten Widerstand

The kernel needs to load the modules gpio_sunxi, w1_therm, w1_sunxi and w1_gpio. I use armbian as operating system. The Legacy Jessie image includes these modules. To load them automatically you can add them to /etc/modules.

hostname:~# cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

brcmfmac
rfcomm
hidp
bonding

gpio_sunxi
w1_therm
w1_sunxi
w1_gpio
hostname:~#

You have to select the GPIO pins you want to use to connect the sensor. I have chosen PB18 for DQ. The signals GND and VDD have to be connected to appropriate pins (e.g. pin 1 and 2 of CN8). The pin you choose for the data signal (DQ) has to be activated. This is done via a bin file. You can find more detailed instructions here. For armbian the file which needs to be edited is /boot/script.bin which is a symlink to /boot/bin/cubietruck.bin. Copy the file to your home directory and convert it to a fex file.

hostname:~# cp /boot/bin/cubietruck.bin ~
hostname:~# bin2fex cubietruck.bin cubietruck.fex
hostname:~# cp cubietruck.fex cubietruck_own.fex

The file cubietruck_own.fex can be edited. You need to adjust the section [gpio_para] and add the section [w1_para]. Im my case this looks like this:

[gpio_para]
gpio_used = 1
gpio_num = 4
gpio_pin_1 = port:PH20<1><1>
gpio_pin_2 = port:PH10<0><0>
gpio_pin_3 = port:PG08<1><1>
gpio_pin_4 = port:PB18<0><0>

[w1_para]
gpio = 4

4 pins are used (gpio_num=4). GPIO pin 4 is assigned to pin PB18 (gpio_pin_4 = …) and 1-wire is configured for pin 4 (gpio=4 in section [w1_para]). After saving the file you can convert it back to the bin format and replace the old file with your new one.

hostname:~# fex2bin cubietruck_own.fex cubietruck.bin
hostname:~# cp cubietruck.bin /boot/bin/cubietruck.bin

Your change is active after a reboot. If the modules are loaded, the pins are configured correctly and the sensor is connected it appears in /sys/bus/w1/devices/. Every sensor recognized (you could also connect more than one sensor) there is a directory with the pattern 28-XXXXXXXXXXXX. On my system the sensor was available after booting but disappeared after some time. With armbian the cpu frequency is reduced if not all cpu power is needed. But when the frequency falls below a certain value the sensor is not working anymore. I changed the minimum cpu frequency to 700 MHz.

hostname:~# sudo cpufreq-set -d 700000

When the sensor is available in /sys/bus/w1/devices/ you just have to read a file to get the temperature.

hostname:~# cat /sys/bus/w1/devices/28-000003ca3e36/w1_slave
e7 00 4b 46 7f ff 09 10 94 : crc=94 YES
e7 00 4b 46 7f ff 09 10 94 t=14437

YES in the first line tells you the checksum is correct and the value for the temperature is valid. The temperature of 14437 means 14,437 °C. You just have to divide the value by 1000. A quite simple bash script (based on code from the Wiki of the “linux-sunxi community”) or Python script (based on my-pi-scripts) helps to get the value well formated.

You can also find an german instruction on how to configure Cubietruck with a DS18B20 sensor at www.cubieforums.com.