SubName: Using inline C/C++ to read temperature module DS18B20.
Description: You can use this basic code to read the temperature readings from a DS18B20 temperature module, this can easily be done using inline C/C++. I'm reading °C but you can easily change it to °C by replacing 'sensors.getTempCByIndex(0)' with 'sensors.getTempFByIndex(0)'.
Yes there's already code and also a library on this forum to do this, but I personally use this solution.
Tags: Temperature, Sensor, DS18B20, Arduino, Resistor, inline, C, C++
Description: You can use this basic code to read the temperature readings from a DS18B20 temperature module, this can easily be done using inline C/C++. I'm reading °C but you can easily change it to °C by replacing 'sensors.getTempCByIndex(0)' with 'sensors.getTempFByIndex(0)'.
AppStart
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.3125°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Temperature is: 23.2500°C
Yes there's already code and also a library on this forum to do this, but I personally use this solution.
B4X:
Sub Process_Globals
'These global variables will be declared once when the application starts.
'Public variables can be accessed from all modules.
Public Serial1 As Serial
Public Temperature As Float
Public TempTimer As Timer
End Sub
Private Sub AppStart
Serial1.Initialize(115200)
Log("AppStart")
RunNative("loop", Null) 'NOT REALLY NEEDED, BUT I USE IT TO IGNORE THE VERY FIRST TEMP READING THAT IS SOMETIME INCORRECT
TempTimer.Initialize("TempUpdate_Tick", 2500)
TempTimer.Enabled = True
End Sub
Sub TempUpdate_Tick
RunNative("loop", Null)
Log("Temperature is: ", Temperature, "°C")
End Sub
#if C
// Include the libraries
#include <OneWire.h>
#include <DallasTemperature.h>
// DS18B20 temperature sensor data wire is plugged into pin 2
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass oneWire reference to Dallas Temperature.
DallasTemperature sensors(&oneWire);
void setup (B4R::Object* unused) {
// Start up the library.
sensors.begin(); //B4R USERS, YOU CAN TAKE THIS LINE OUT AS IT STILL WORKS WITHOUT IT
}
void loop (B4R::Object* unused) {
// Request temperature readings from ALL devices on the bus
sensors.requestTemperatures();
// You can have more than one DS18B20 on the same bus. 0 refers to the first IC on the wire. sensors.getTempCByIndex(0) = °C, sensors.getTempFByIndex(0) = °F
b4r_main::_temperature = sensors.getTempCByIndex(0);
}
#End if
Tags: Temperature, Sensor, DS18B20, Arduino, Resistor, inline, C, C++
Last edited: