الكود البرمجي
نقوم بتحميل مكتبة DallasTemperature بالبحث عن المكتبات عن طريق Tools > Manage Libraries لبرمجة DS18B20 مستشعر درجة الحرارة المقاوم للماء

و مكتبة OneWire Arduino library

ثم ستقوم برفع الكود البرمجي التالي :
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Wire.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3D (for the 128x64)
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
Serial.begin(9600);
sensors.begin();
}
void loop() {
// Send the command for all devices on the bus to perform a temperature conversion:
sensors.requestTemperatures();
// Fetch the temperature in degrees Celsius for device index:
float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
// Fetch the temperature in degrees Fahrenheit for device index:
float tempF = sensors.getTempFByIndex(0);
// Print the temperature in Celsius in the Serial Monitor:
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Temp: ");
display.print(tempC);
display.print((char)247); // degree symbol
display.print("C");
display.display();
delay(2000);
// routine for displaying text for temp/hum readout
}
شرح الكود البرمجي
استدعاء المكتبات التي ستساعدنا في برمجة شاشة OLED Display و حساس الحرارة
#include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <Wire.h> #include <OneWire.h> #include <DallasTemperature.h>
نقوم بتعريف المنفذ الذي سيتم توصيل and حساس الحرارة معه
#define ONE_WIRE_BUS 2
بعد ذلك ، نقوم بإنشاء كائن
من أجل التواصل مع مستشعر DS18B20 ، نحتاج إلى إنشاء كائن من مكتبة DallasTemperature
OneWire oneWire(ONE_WIRE_BUS); DallasTemperature sensors(&oneWire);
نكتب بروتوكول إعادة الضبط
#define OLED_RESET 4
Adafruit_SSD1306 display(OLED_RESET);
#if (SSD1306_LCDHEIGHT != 64)
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
#endif
في دالة void setup()نحتاج إلى تهيئة حساس الحرارة و تهيئة العرض ومسحه لشاشة OLED Display تأكد من كتابة عنوان I2Cلشاشة العرض
void setup() {
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.display(); // show splashscreen
delay(2000);
display.clearDisplay(); // clears the screen and buffer
Serial.begin(9600);
sensors.begin();
}
نعطي الأمر استشعار درجة الحرارة
void loop() {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0); // the index 0 refers to the first device
نعطي الأمر بطباعة درجة الحرارة بالدرجة المئوية على شاشة OLED Display
// Print the temperature in Celsius in the Serial Monitor:
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println("Temp: ");
display.print(tempC);
display.print((char)247); // degree symbol
display.print("C");
display.display();
delay(2000);
}

