B4R Code Snippet Temperature sensors comparison - DS18B20 vs DHT11

Peter Simpson

Expert
Licensed User
SubName: Comparing the DS18B20 and the DHT11 temperature sensors at the exact same time.
Description: This is just a quick comparison between the two above named temperature sensors, nothing special really. I was looking through my Arduino parts toolbox when I suddenly realised that I had two types of temperature sensors, I had not even opened the anti-static bag with the DHT11 inside it, so I decided to place them both on a mini breadboard with a 4 pin OLED display to compare and display their temperature readings.

AppStart
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 22°C
DHT11 = 20°C
DS18B20 = 21.8625°C
DHT11 = 20°C
DS18B20 = 21.8125°C
DHT11 = 20°C
DS18B20 = 21.8010°C
DHT11 = 20°C
DS18B20 = 21.7925°C
DHT11 = 20°C
DS18B20 = 21.7975°C

This has probably already been done on this forum, I've not checked.
B4X:
#Region Project Attributes
    #AutoFlushLogs: True
    #CheckArrayBounds: True
    #StackBufferSize: 300
#End Region
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 DHTPin As Pin

    Private TempDS18 As Double = 0
    Private TempDHT11 As dht
    Private TempTimer As Timer
    Private SSD As AdafruitSSD1306
End Sub

Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")

    SSD.InitializeI2C(4, 0x3c) '4 pin display
    SSD.ClearDisplay
    SSD.GFX.ConfigureText(1, SSD.WHITE, False)

    TempTimer.Initialize("TempUpdate_Tick", 2500)
    TempTimer.Enabled = True
End Sub

Sub TempUpdate_Tick
    Dim BC As ByteConverter

    RunNative("loop", Null)
    Log("DS18B20 = ", TempDS18, "°C")

    TempDHT11.Read11(DHTPin.A0)
    Log("DHT11 = ", TempDHT11.GetTemperature, "°C")

    SSD.ClearDisplay
    SSD.GFX.SetCursor(3, 0)
    SSD.GFX.DrawText("Temperature Readings:")

    SSD.GFX.DrawLine(0, 10, 128, 10, SSD.WHITE)

    SSD.GFX.SetCursor(0, 15)
    SSD.GFX.DrawText(JoinStrings(Array As String("DS18B20 = ", TempDS18, BC.StringFromBytes(Array As Byte(247)), "C")))

    SSD.GFX.SetCursor(0, 25)
    SSD.GFX.DrawText(JoinStrings(Array As String("DHT11 = ", TempDHT11.GetTemperature, BC.StringFromBytes(Array As Byte(247)), "C")))
    SSD.Display
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::_tempds18 = sensors.getTempCByIndex(0);
}
#End if
Tags: Temperature, Sensor, DS18B20, DHT11, OLED, Display, Arduino, Resistor, Inline, C, C++

Yeah yeah I know I know, the DHT11 is blue and NOT white. Fritzing does not have the DHT11 (blue), so I used the DHT22 (white) instead.
DS18B20 vs DHT11_bb.png


The project on a mini breadboard.
IMG_20170401_051444.jpg


Enjoy...
 
Last edited:
Top