B4R Library rDS18B20

B4R Library rDS18B20


Brief
rDS18B20
is an open-source library for reading data from a DS18B20 temperature sensor.
The DS18B20 is a digital temperature sensor (range ~-55 to 125°C) using the 1-Wire interface with one data pin.


Purpose
This library exposes a minimal interface for B4R, allowing to:
  • read DS18B20 temperature data triggering state change event.
  • handle errors using the error event.


Development Info
This B4R library is:
  • using the external Arduino libraries OneWire (2.3.8) and DallasTemperature (4.0.6).
  • Written in C++ (Arduino IDE 2.3.8 and B4Rh2xml tool).
  • Tested with an Arduino UNO and ESP-WROOM-32.
  • Tested with B4R 4.00 (64-bit).


Files
The rDS18B20-NNN.zip archive contains the library and examples.
  • Basic - Read temperature from sensor DS18B20.


Install
Copy the rDS18B20 library folder from the ZIP into your B4R Additional Libraries folder, keeping the folder structure intact.
Ensure the external Arduino libraries OneWire and DallasTemperature are installed using the Arduino IDE libraries manager.


Wiring Example
B4X:
VCC(+) = 5V
DAT(out) = GPIO 4 > 4k7 Ω pull-up resistor between the 1-Wire data line and 5V power
GND(-) = GND


Functions

Initialize(Pin As Byte, Resolution As Byte, StateChangedSub As String, ErrorSub As String)
Initializes the device.
Pin - Pin number to connect the sensor data signal.
Resolution - Resolution 9 (0.5°C), 10 (0.25°C), 11 (0.125°C), or 12 (0.0625°C, default) bits.
StateChangedSub - Callback for the `StateChanged` event.
ErrorSub - Callback for the `Error` event.

IsInitialized As Boolean
Check if sensor is properly initialized.
Returns false if there was a failure.

GetTemperature As Float
Read temperature from DS18B20 (Celsius).
Returns nan if there was a failure.

setEventEnabled(state As Boolean)
getEventEnabled As Boolean

Set/Get enabled state change event.

SetDebug(Enabled As Boolean)
Toggles verbose Serial logging for the library.
Enabled - True to enable debug output.

Constants
ADC_RESOLUTION_9
ADC_RESOLUTION_10
ADC_RESOLUTION_11
ADC_RESOLUTION_12
ERR_INVALID_RESOLUTION
ERR_NO_SENSOR_FOUND


Example
B4X:
Sub Process_Globals
    Private VERSION As String = "rDS18B20 Basic v20260328"
    'Serial
    Public serialLine As Serial
    Private SERIALLINE_BAUDRATE As ULong = 115200
    'Sensor
    Private Sensor As DS18B20           
    Private PIN_NUMBER As Byte = 4
End Sub

Private Sub AppStart
    serialLine.Initialize(SERIALLINE_BAUDRATE)
    Log("[AppStart][I] ", VERSION)
    Sensor.Initialize(PIN_NUMBER, 12, "OnStateChanged", "OnError")
    If Not(Sensor.IsInitialized) Then Return
    Log("[AppStart][I] OK")
End Sub

Private Sub OnStateChanged(t As Float)
    Log("[OnStateChanged][I] t=", t)
End Sub

Private Sub OnError(code As Byte)
    Log("[OnError][E] code=", code)
End Sub

Logging Example
B4X:
[OnStateChanged][I] t=19.4375
[OnStateChanged][I] t=19.5000
[OnStateChanged][I] t=19.5625
[OnStateChanged][I] t=19.6250


License
MIT - see LICENSE file.


Credits
  • Developers & maintainers of the OneWire and DallasTemperature libraries (MIT License).


Disclaimer
  • All trademarks are property of their respective owners.
 

Attachments

  • rDS18B20-100.zip
    5.6 KB · Views: 4
Last edited:

peacemaker

Expert
Licensed User
Longtime User
Is pin number settable ? Better if any B4R-library or code is to be made with the pins variables during setup...
 

rwblinn

Well-Known Member
Licensed User
Longtime User
Is pin number settable ? Better if any B4R-library or code is to be made with the pins variables during setup...
YES in the function Initialize use the Pin parameter to connect the sensor data pin to the MCU.
See the example post #1.
B4X:
Initialize(Pin As Byte, Resolution As Byte, StateChangedSub As String, ErrorSub As String)
Have updated post #1 to make more clear. Thanks for the hint.
 
Last edited:
Top