BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

Question about the ds18b20 temperature sensor

Hello, I have a cold room gland for medicines. I want to use the ds18b20 temperature sensor. Is it recommended to use it to measure the temperature from 2 degrees to 8 degrees, and what is the error percentage? I did the experiment, but I felt happier with the result. The temperature in the room was 4 degrees, but the temperature sensor ds18b20 gives me 8 degrees

#include <OneWire.h> 
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 5
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);
int relay=7;
void setup() {Serial.begin(9600);
  // put your setup code here, to run once:
sensors.begin(); 
pinMode(relay,1);
digitalWrite(relay,1);
}
long tem;
void loop() {
  // put your main code here, to run repeatedly:
sensors.requestTemperatures(); 
 Serial.println("DONE"); 
 Serial.print("Temperature is: "); 
 Serial.print(sensors.getTempCByIndex(0));
  tem=sensors.getTempCByIndex(0) ;
 if(tem>69){digitalWrite(relay,0);}
 else{digitalWrite(relay,1);}
}

Are you using Arduino ?

I tried arduino and nodemcu

I dont see any Blynk stuff in your code!! Is this all your code ?

This is not blynk sketch.

I know it’s not a blynk drawing, but I use the same code with blynk


#define BLYNK_TEMPLATE_ID "TMPLx76oHv"
#define BLYNK_DEVICE_NAME "Temprator Freez 1"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiManager.h>
#include "uptime.h"
#include <TridentTD_LineNotify.h>
#include <OneWire.h> 
#include <DallasTemperature.h>

char auth[] = "qxOrvYVf-DRiZfSku02F8GcsZZXuxb";
char ssid[] = "ULTRA TECH1";
char pass[] = "qwerty123";

int lastConnectionAttempt = millis();
int connectionDelay = 5000; // اعادة اتصال كل 5 ثواني


#define LINE_TOKEN  "oMPewQHyJ1D2inWdkh9uKMHEhgO6jwnZT7xj75Ew"
#define ONE_WIRE_BUS D5
OneWire oneWire(ONE_WIRE_BUS); 
DallasTemperature sensors(&oneWire);


#define vPIN_UPTIME                 V0  
#define vPIN_Temp                   V1   // درجة الحرارة
#define vPIN_HighTemp               V2   // الحد الاعلى للحرارة
#define vPIN_LogTerminal            V3   // مسجل الاحداث
#define vPIN_LowTemp                V4   // الحد الادنى للحرارة
#define vPIN_State                  V5   // الوضع التلقائي
#define vPIN_LedState               V6   // ليد الوضع التلقائي
#define vPIN_LedAmper               V7   // ليد حالة الكهرباء

const int Relay = D7;         // رجل الريلي
const int Switsh1  = D0;      // رجل تفعيل الوضع التلقائي يدويا
const int Switsh2  = D6;      // رجل حالة الكهرباء
bool buttonStatus1 = 0;
bool buttonTemp1 = 0;
bool buttonStatus2 = 0;
bool buttonTemp2 = 0;
float HighTemp=0;             // متغير الحد الاعلى للحرارة
float LowTemp=0;              // متغير الحد الادنى للحرارة
int  NotifyAmper=0;
int  NotifyHighTemp=0;        // متغير ايقاف الاشعار عند ارتفاع الفولت
int  NotifyLowTemp=0;         // متغير ايقاف الاشعار عند انخفاض الفولت
int  CuntHigh=0;              // عداد قبل ارسال اشعار عند ارتفاع الحرارة
int  CuntLow=0;               // عداد قبل ارسال اشعار عند عودة الحرارة للحد الطبيعي
int  CuntUnplogedSensor=0;    //  عداد قبل ارسال اشعار عند توقف الحساس
int  StateSensor=0;           // لارسال الاشعار مرة واحدة عند توقف الحساس
bool IsFirstRun   = 1;
bool State   = 1;            // متغير للوضع التلقائي
float tem = 0;               // متغير تحويل قيمة الحرارة الى فلوت

BlynkTimer   UltraTimer;

WidgetLED LedState(vPIN_LedState);
WidgetLED LedAmper(vPIN_LedAmper);

BLYNK_CONNECTED() 
{
  if (IsFirstRun==1)
  {
    Blynk.syncVirtual(vPIN_HighTemp,vPIN_LowTemp,vPIN_State);
    LedAmper.off();
    IsFirstRun=0;
  }
}


BLYNK_WRITE(vPIN_HighTemp){
  HighTemp=param.asFloat();
  Blynk.virtualWrite(vPIN_LogTerminal,"تم تغيير الحد الأعلى لدرجة الحرارة"+String(HighTemp));
  //LINE.notify("تم تغيير الحد الأعلى لدرجة الحرارة"+String(HighTemp)); 
  
}


BLYNK_WRITE(vPIN_LowTemp){
  LowTemp=param.asFloat();
  Blynk.virtualWrite(vPIN_LogTerminal,"تم تغيير الحد الطبيعي لدرجة الحرارة"+String(LowTemp)); 
  //LINE.notify("تم تغيير الحد الطبيعي لدرجة الحرارة"+String(LowTemp));
  
}


BLYNK_WRITE(vPIN_State)
{
  if (param.asInt() == 1)
  {
    LedState.on();
    Serial.println("تم تفعيل وضع التنبيه");
    State=1;
    Serial.print("State : ");
    Serial.println(State);     
  }
  else
  {
    digitalWrite(Relay, HIGH);
    LedState.off();
    Serial.println("تم ايقاف وضع التنبيه");
    State=0;
    Serial.print("State : ");
    Serial.println(State);
   
  }
}


void checkStatusButton1()
{
  buttonStatus1 = digitalRead(Switsh1);

  if (buttonStatus1 != buttonTemp1)
  {
    if (buttonStatus1 == HIGH) {
      Blynk.virtualWrite(V5, 1);
      LedState.on();
      State=1;  
      Serial.print("State : ");
      Serial.println(State);
      Serial.println("تم تشغيل وضع التنبيه يدويا");    
      
    }
    else {
      digitalWrite(Relay, HIGH);
      Blynk.virtualWrite(V5, 0);
      LedState.off();
      State=0;
      Serial.print("State : ");
      Serial.println(State);
      Serial.println("تم ايقاف وضع التنبيه يدويا");
      
    }
    buttonTemp1 = buttonStatus1;
  }
}


void checkStatusButton2()
{
  buttonStatus2 = digitalRead(Switsh2);

  if (buttonStatus2 != buttonTemp2)
  {
    if (buttonStatus2 == HIGH) {
      digitalWrite(Relay, HIGH);
      LedAmper.on(); 
      Serial.println("الكهرباء متوفرة"); 
      LINE.notify("الكهرباء متوفرة");   
      
    }
    else {
      digitalWrite(Relay, LOW);
      LedAmper.off();
      Serial.println("الكهرباء غير متوفرة");
      LINE.notify("الكهرباء غير متوفرة");
      
    }
    buttonTemp2 = buttonStatus2;
  }
}


void UPTIME(){
 

   uptime::calculateUptime();
   Serial.println(" "+String(uptime::getDays())+" days "+String(uptime::getHours())+":"+String(uptime::getMinutes())+":"+String(uptime::getSeconds()));
   Serial.print("\n");
   Blynk.virtualWrite(vPIN_UPTIME," "+String(uptime::getDays())+" days "+String(uptime::getHours())+":"+String(uptime::getMinutes())+":"+String(uptime::getSeconds()));

  
  
}




void temprator(){

  sensors.requestTemperatures();  
  Serial.print("Temperature is: "); 
  Serial.println(sensors.getTempCByIndex(0));
  tem=sensors.getTempCByIndex(0) ;
  Blynk.virtualWrite(vPIN_Temp,tem);
  Serial.print("NotifyHighTemp:   ");
  Serial.println(NotifyHighTemp);
  Serial.print("NotifyLowTemp:   ");
  Serial.println(NotifyLowTemp);
  
  }

void highTemp(){

  if ((tem>HighTemp)and(NotifyHighTemp==0)and(State==1)and(CuntHigh<20)){
      CuntHigh++;
      delay(250);
  }
  
  if ((tem>HighTemp)and(NotifyHighTemp==0)and(State==1)and(CuntHigh==20)){

   // Blynk.logEvent("TempRator", "درجة الحرارة أعلى من الحد الطبيعي ");
    Blynk.virtualWrite(vPIN_LogTerminal,"درجة الحرارة أعلى من الحد الطبيعي");
    Serial.println("تم تشغيل الانذار");
    LINE.notify("درجة الحرارة أعلى من الحد الطبيعي "+String(tem));
    digitalWrite(Relay,LOW);
    NotifyHighTemp=1;
    NotifyLowTemp=0;
    CuntLow=0;
    StateSensor=1;
    
    
  }
    if ((tem<HighTemp)and(NotifyHighTemp==1)and(State==1)) 
    {
      digitalWrite(Relay,HIGH);
      NotifyHighTemp=0;
      CuntHigh=0;
      StateSensor=1;
    }
}


void lowTemp(){

  if ((tem<LowTemp)and(tem> 0)and(NotifyLowTemp==0)and(State==1)and(CuntLow<20)){
       CuntLow++;
       delay(250);
    
  }
  
  if ((tem<LowTemp)and(tem> 0)and(NotifyLowTemp==0)and(State==1)and(CuntLow==20)){

    //Blynk.logEvent("TempRator", "درجة الحرارة الآن ضمن الحد الطبيعي");
    Blynk.virtualWrite(vPIN_LogTerminal,"درجة الحرارة الآن ضمن الحد الطبيعي");
    Serial.println("درجة الحرارة الآن ضمن الحد الطبيعي");
    LINE.notify("درجة الحرارة الآن ضمن الحد الطبيعي"+String(tem));
    NotifyLowTemp=1; 
    StateSensor=1;   
    
  }   
}


void UnplogedSensor(){
    if ((tem== -127)and(CuntUnplogedSensor<5)and(StateSensor==0)){
       CuntUnplogedSensor++;
       delay(250);
    }
    if ((tem== -127)and(CuntUnplogedSensor==5)and(StateSensor==0)){
    Serial.print("يوجد عطل في حساس الحرارة");
    LINE.notify("يوجد عطل في حساس الحرارة");
    Serial.print(StateSensor);
    Serial.print("CuntUnplogedSensor");
    Serial.print(CuntUnplogedSensor);
    StateSensor=1;
}
   if ((tem>1)and(StateSensor==1)){
    StateSensor=0;
   }
}

void setup()
{
  Serial.begin(115200);
  pinMode(Relay,OUTPUT);
  pinMode(Switsh1, INPUT);
  pinMode(Switsh2, INPUT);
  digitalWrite(Relay,HIGH);
  Blynk.begin(auth, ssid, pass);
  LINE.setToken(LINE_TOKEN);
  sensors.begin(); 
  IsFirstRun = 1 ;
  UltraTimer.setInterval(1000L, UPTIME);
  UltraTimer.setInterval(1000L, highTemp);
  UltraTimer.setInterval(1000L, lowTemp);
  UltraTimer.setInterval(1000L, temprator); 
  UltraTimer.setInterval(50000L, UnplogedSensor);
  UltraTimer.setInterval(1000L, checkStatusButton1);
  UltraTimer.setInterval(1000L, checkStatusButton2);
  
}


void loop()
{
  
  Blynk.run();
  UltraTimer.run();

  if (WiFi.status() != WL_CONNECTED)
  {
    if (millis() - lastConnectionAttempt >= connectionDelay)
    {
      lastConnectionAttempt = millis();

      if (pass && strlen(pass))
      {
        WiFi.begin((char*)ssid, (char*)pass);
      }
      else
      {
        WiFi.begin((char*)ssid);
      }
    }
  }
  else
  {
    Blynk.run();
  }
}





Your sketch should look like this :

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SimpleTimer.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
char auth[] = "";
 
/* WiFi credentials */
char ssid[] = "";
char pass[] = "";
 
SimpleTimer timer;
 
 
 
#define ONE_WIRE_BUS 2 // DS18B20 on arduino pin2 corresponds to D4 on physical board "D4 pin on the ndoemcu Module"
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature DS18B20(&oneWire);
float temp;
float Fahrenheit=0;

void getdata()
{
  DS18B20.requestTemperatures(); 
  temp = DS18B20.getTempCByIndex(0); // Celcius
   Fahrenheit = DS18B20.toFahrenheit(temp); // Fahrenheit
  Serial.println(temp);
  Serial.println(Fahrenheit);
  Blynk.virtualWrite(V3, temp); //virtual pin V3
  Blynk.virtualWrite(V4, Fahrenheit); //virtual pin V4
}

void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  DS18B20.begin();
  timer.setInterval(1000L, getdata);
}
 
void loop() 
{
  timer.run(); // Initiates SimpleTimer
  Blynk.run();
}

This is my code it works fine but there is a difference of 4 degrees

By the way, there’s some mistakes in your sketch for example there’s 2 blynk.run in your void loop.

Thank you, I know, but it is not the cause of the temperature difference

Can you try the sketch I just sent you please ?

Well, I will give it a try

Thank you

thank you my friend

1 Like

How do you know this? What type of device are you using to measure the temperature, and how accurate is it, and when was it last calibrated?

Standard ds18b20 sensors have a ±0.5°C accuracy, but this can be 0.1°C for the clinical version of the sensor.
However, I think you’re confusing accuracy with calibrated accuracy.
If you have a reference thermometer which you know to be accurate then you can introduce a calibration factor into your sketch to ensure that the ds18b20 gives the same results as the reference thermometer. The ds18b20‘s calibrated readings should then be accurate to within half a degree of the reference thermometer.

Pete.

Thank you Pete, I knew the temperature difference compared to the air conditioner in the cold room

Do you mean that I reduce the excess difference by means of a variable? I did that and when the room temperature changed, the match was different. Unfortunately, I tried several ds18b20 sensors, but the same result

Is that a regular domestic AC unit?
If so then they are VERY inaccurate, and have a very wide hysteresis setting (the temperature at when the AC kicks in and out) so that they aren’t constantly switching the compressor on/off.

Yes

Probably because the temperature in the room is varying massively, due to the intentional insensitivity of the AC unit
You’re wasting your time unless you have a calibrated reference thermometer.

Pete.

It is a unit manufactured with international standards made in Turkey, not a local manufacture dedicated to preserving the corona vaccine