B4J Tutorial [RPI] DS18b20 Temp. Sensor HowTo

Hello,

today i will share my Experience with the DS 18B20 Temperature Sensor.
The first step is to prepare the Raspberry PI.
edit the file:
B4X:
sudo nano /boot/config.txt

insert the following line

B4X:
dtoverlay=w1-gpio,gpiopin=4,pullup=on

and Save it. For gpiopin=4 you can Use every GPIOI Port !

To autostart the 1wire edit the
B4X:
sudo nano /etc/modules
and insert the following lines
B4X:
w1-gpio pullup=1
w1-therm

Now is time to Shutdown your Raspberry and connect the DS18B20 as shown in the wiring diagram
DS18b20.png

You will see that i use Three Lines (+3.3 / GND / Signal) with 1Wire connection you need a Levershifter to get it stable to run.
Without a Levelshifter it is not possible to get a stable temperature if you use the 1wiring connection.

!!! Attention Don't connect the 3.3V Wire to the 5V this will Destroy your Raspberry !!!


With the connection shown in the wiring diagramm i have seven DS18b20 Sensors running, the longest cable is 12 m long an i have a stable signal.


Now is time to start your Raspberry and test if the 1wire is running.
After the restart you will find in /sys/bus/w1/devices for each sensor a sub directorie which looks like this 28-000007.....
Follow the next step to see if the sesor works

B4X:
cd 28-000007124*
cat w1_slave
0f 00 4b 46 ff ff 06 10 0c : crc=0c YES
0f 00 4b 46 ff ff 06 10 0c t=7375

on the first line you will receive the CRC and the information if the measured Themperatur is valid, in the second Line you will receive the Temperatur in thousandths of a degree Celsius.

Now you can use the following sample code to read the temp. data please replace 28-0000073c14fa with the ID's from your DS18b20 Sensors!

regards

Andy

B4X:
Sub Process_Globals

End Sub
'************************************************************************************************************************************************
'**                                                                                                                                            **
'***                                                 Temperaturfühler einlesen                                                                ***
'**                                                                                                                                            **
'************************************************************************************************************************************************

public Sub gettemp As String
    Private tempraw As String
    If File.Exists("/sys/bus/w1/devices/28-0000073c14fa/","w1_slave") Then
        tempraw= File.ReadString("/sys/bus/w1/devices/28-0000073c14fa/","w1_slave")
        'Log ("temperatur: "& tempraw)
        Return readtempCompleted (tempraw)
    Else
        'Log ("Keine Temperatur daten vorhanden")
        Return "-.--"
    End If
End Sub   

'************************************************************************************************************************************************
'**                                                                                                                                            **
'***                                              Temperatur auf gültigkeit prüfen                                                                ***
'**                                                                                                                                            **
'************************************************************************************************************************************************

Private Sub readtempCompleted (StdOut As String) As String
    Private BeckenTemperatur As String
    Private req() As String
    Private temperatur As Float
        req = Regex.Split("=",StdOut)
        If req(1).Contains("YES") Then
            temperatur=req(2)
            temperatur = temperatur/1000
            BeckenTemperatur=temperatur
            If BeckenTemperatur.IndexOf(".") <> -1 Then
                BeckenTemperatur=BeckenTemperatur.SubString2(0,BeckenTemperatur.IndexOf(".")+2)
            End If
        Else
        '    Log("temp. fehler "&DateTime.Date(DateTime.Now)&" "&DateTime.Time(DateTime.now))   
        End If
    Return (BeckenTemperatur)
End Sub
 

ta1dr

Member
Licensed User
Longtime User
thanks for share
I want to know ; if I connect two DS18B20 how can I read ?
exp:
sensor 1 =280000073C14FA
sensor 2 =280000073A15CA ( serial number not real only sample)
 

Siam

Active Member
Licensed User
Longtime User
Hello,

here is a sample for more sensors:
B4X:
Sub Process_Globals

End Sub
'************************************************************************************************************************************************
'**                                                                                                                                            **
'***                                                 Temperaturfühler einlesen                                                                ***
'**                                                                                                                                            **
'************************************************************************************************************************************************

public Sub gettemp (sensor as string) As String
    Private tempraw As String
    If File.Exists("/sys/bus/w1/devices/"&sensor&"/","w1_slave") Then
        tempraw= File.ReadString("/sys/bus/w1/devices/"&sensor&"/","w1_slave")
        'Log ("temperatur: "& tempraw)
        Return readtempCompleted (tempraw)
    Else
        'Log ("Keine Temperatur daten vorhanden")
        Return "-.--"
    End If
End Sub

Now you can call this funktion with:

B4X:
Temp = gettemp("280000073C14FA")
 

derez

Expert
Licensed User
Longtime User
In RPI 3 the pin to connect the data line is # 7, I guess its function is the GPCLK. The pin is the fourth on the left, like # 4 in the older versions !
 
Top