I am a novice programmer, and for a mock project I am trying to make a temperature control system but the code is not working. It uses an arduino uno r4 wifi, a sparkfun bme280 and an adafruit OLED display.
This is the code:
#include <Wire.h> // Includes I2C connections
#include <SparkFun BME280.h> // Includes the SparkFun BME280 library
#include <U8g2lib.h> // Includes the U8g2 library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define ALTITUDE 37 // The altitude
BME280 bme; // Creates an instance of the SparkFun BME280 sensor
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // Creates a U8g2 object for the OLED
int bme280Address = 0x77; // Sets the adress for the temperature sensor
const int HeaterControlPin = A3; // Sets a constant for the controlling the mosfet to analogue pin 3
const int CfanPin = 5; // sets the constant for controlling the cooling fan to digital pin 5
const int HfanPin = 6; // sets a constant for controlling the heating fan to digital pin 6
const int HeaterOn = 255; // sets a constant for turning the heater on
const int HeaterOff = 0; // sets a constant for turning the heater off
void setup() {
Serial.begin(9600); // Starts the communication to the terminal at 9600 bits/s
Wire.begin(); // Initialize I2C communication
u8g2.begin(); // Initialize the display using the u8g2 library
SparkFunBME280.begin(); // Initialises the Sparkfun sensor
pinMode(CfanPin, OUTPUT); // Set the cooling fan pin as an output
pinMode(HfanPin, OUTPUT); // Set the heating fan pin as an output
pinMode(HeaterControlPin, OUTPUT); // Set the A3 pin as an output for the heating element
}
void loop() {
float celsius = bme.readTempC(); //Reads the temperature
float humidity = bme.readFloatHumidity(); //Reads the humidity
Serial.print("Celsius: "); // Prints "celcius: " on the serial monitor
Serial.println(celsius); // Prints the value of temperature(in celcius) afterwards
Serial.print("Humidity: "); // Prints "Humidity: " on the serial monitor
Serial.println(humidity); // Prints the value of the humuidity afterwards
u8g2.clearBuffer(); // Clears the buffer in the library
u8g2.setFont(u8g2_font_ncenB08_tr); // Set the font from the library
u8g2.setCursor(0, 10); // Set the start cursor for temperature
u8g2.print("Temp: "); // Prints "Temp: " on the display at (0, 10)
u8g2.print(celsius) // Prints the temperature value afterwards
u8g2.print(" C"); // Prints the unit "C" for celcius afterwards
u8g2.setCursor(0, 50); // Set the start cursor for humidity
u8g2.print("Humidity: "); // Print "Humidity: " on the display at (0, 50)
u8g2.print(humidity); // Prints the humidity value afterwards
u8g2.print(" %"); // Prints the unit "%" afterwards
u8g2.sendBuffer(); // Updates the display for new content
if (celsius >= 32) { // If the temperature is greater than/equal to 32, then:
analogWrite(HeaterControlPin, HeaterOff); // Turns the heating element off
digitalWrite(CfanPin, HIGH); // Turns on the cooling fan
digitalWrite(HfanPin, LOW); // Turns off the heating fan
} else { // If it isnt(all other values below 32), then:
analogWrite(HeaterControlPin, HeaterOn); // Turns the heating element on
digitalWrite(CfanPin, LOW); // Turn off the cooling fan
digitalWrite(HfanPin, HIGH); // Turns on the heating fan
}
delay(30000); // Delays for 30 seconds
}
(the 3 sensors are supposed to represent the fans, and the solar panel the heating pad. a4 and a5 are going to scl and sda on the temperature sensor too)
Thanks for reading!
And srsly, even if you want this to only be functional twice a minute, reduce the delay severely for now - life is short. Try 777 milliseconds, you won't break anything.
Does the serial monitor window agree with the baud rate you are using?
The serial monitor doesnt print anything, but now there is an error on line 32 saying "Compilation error: 'SparkFunBME280' was not declared in this scope" that wasnt there before.
#include <Wire.h> // Includes I2C connections
#include <SparkFunBME280.h> // Includes the SparkFun BME280 library
#include <U8g2lib.h> // Includes the U8g2 library
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define ALTITUDE 37 // The altitude in Melbourne
BME280 bme; // Creates an instance of the SparkFun BME280 sensor
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // Creates a U8g2 object for the OLED
int bme280Address = 0x77; // Sets the adress for the temperature sensor
const int HeaterControlPin = A3; // Sets a constant for the controlling the mosfet to analogue pin 3
const int CfanPin = 5; // sets the constant for controlling the cooling fan to digital pin 5
const int HfanPin = 6; // sets a constant for controlling the heating fan to digital pin 6
const int HeaterOn = 255; // sets a constant for turning the heater on
const int HeaterOff = 0; // sets a constant for turning the heater off
void setup() {
Serial.begin(9600); // Starts the communication to the terminal at 9600 bits/s
Wire.begin(); // Initialize I2C communication
u8g2.begin(); // Initialize the display using the u8g2 library
SparkFunBME280.begin(); // Initialises the Sparkfun sensor
pinMode(CfanPin, OUTPUT); // Set the cooling fan pin as an output
pinMode(HfanPin, OUTPUT); // Set the heating fan pin as an output
pinMode(HeaterControlPin, OUTPUT); // Set the A3 pin as an output for the heating element
}
void loop() {
Serial.println("starting loop()");
float celsius = bme.readTempC(); //Reads the temperature
float humidity = bme.readFloatHumidity(); //Reads the humidity
Serial.print("Celsius: "); // Prints "celcius: " on the serial monitor
Serial.println(celsius); // Prints the value of temperature(in celcius) afterwards
Serial.print("Humidity: "); // Prints "Humidity: " on the serial monitor
Serial.println(humidity); // Prints the value of the humuidity afterwards
u8g2.clearBuffer(); // Clears the buffer in the library
u8g2.setFont(u8g2_font_ncenB08_tr); // Set the font from the library
u8g2.setCursor(0, 10); // Set the start cursor for temperature
u8g2.print("Temp: "); // Prints "Temp: " on the display at (0, 10)
u8g2.print(celsius); // Prints the temperature value afterwards
u8g2.print(" C"); // Prints the unit "C" for celcius afterwards
u8g2.setCursor(0, 50); // Set the start cursor for humidity
u8g2.print("Humidity: "); // Print "Humidity: " on the display at (0, 50)
u8g2.print(humidity); // Prints the humidity value afterwards
u8g2.print(" %"); // Prints the unit "%" afterwards
u8g2.sendBuffer(); // Updates the display for new content
if (celsius >= 32) { // If the temperature is greater than/equal to 32, then:
analogWrite(HeaterControlPin, HeaterOff); // Turns the heating element off
digitalWrite(CfanPin, HIGH); // Turns on the cooling fan
digitalWrite(HfanPin, LOW); // Turns off the heating fan
} else { // If it isnt(all other values below 32), then:
analogWrite(HeaterControlPin, HeaterOn); // Turns the heating element on
digitalWrite(CfanPin, LOW); // Turn off the cooling fan
digitalWrite(HfanPin, HIGH); // Turns on the heating fan
}
delay(777); // Delays for 10 seconds
}
I also got my numbers muddled up so it was on line 23, my bad.
This creates the an instance of BME280 called bme. You use the bme object / variable everywhere except in the below. You do not have an object / variable called SparkFunBME280, you have an object / variable called bme.
You have a sensor connected to pin 1 which is also used for the USB and will stop the serial monitor working .
That’s in your diagram , but not the code !
You have what looks like a potentiometer in series with Vin , that should stop things working too !
I would pull all the wires out !
Then see if you can write to the serial monitor z, then see if you can toggle the outputs - that way you can see any error immediately .
If you have sensors , try a library example first .
Then build your project up
في دالة setup()، تستخدم SparkFunBME280.begin() وهي غير صحيحة. يجب استدعاؤها على الكائن bme الذي أنشأته. استبدل السطر التالي:
cppCopy
SparkFunBME280.begin(); // تهيئة المستشعر
بهذا السطر:
cppCopy
bme.begin(); // تهيئة المستشعر باستخدام الكائن
عرض درجة الحرارة على شاشة OLED
تأكد من وجود فاصلة منقوطة (;) في نهاية سطر u8g2.print(celsius):
cppCopy
u8g2.print(celsius); // هناك حاجة لفاصلة منقوطة هنا
التحقق من عنوان المستشعر
تحقق من أن عنوان I2C 0x77 للمستشعر BME280 صحيح. إذا كان لديك أجهزة I2C متعددة، فمن الأفضل استخدام سكربت فحص I2C للتأكد من أنك تستخدم العنوان الصحيح.
منطق التحكم بالحرارة
تأكد من أن منطق التحكم في درجات الحرارة مناسب لجهاز التسخين والتبريد لديك. لقد قمت بتعيين الشرط لإيقاف التسخين عند درجة حرارة 32 °C. يمكنك تعديل هذا الحد وفقًا لاحتياجات تطبيقك.
إدارة الذاكرة (اختياري)
بما أنك تستخدم شاشة OLED ومستشعر، قد يكون من المفيد إعداد تحقق من الأخطاء لضمان أن القراءات صحيحة. بهذه الطريقة، لن تقوم بتشغيل المراوح والمكيفات بشكل غير ضروري بناءً على بيانات مستشعر غير صحيحة.
تعديل الكود
إليك مثال على نسخة معدلة من الكود مع التحسينات المقترحة:
cppCopy
#include <Wire.h> // تضمين اتصالات I2C
#include <SparkFun BME280.h> // تضمين مكتبة SparkFun BME280
#include <U8g2lib.h> // تضمين مكتبة U8g2
#define SCREEN_WIDTH 128 // عرض شاشة OLED بالبكسل
#define SCREEN_HEIGHT 64 // ارتفاع شاشة OLED بالبكسل
#define ALTITUDE 37 // الارتفاع
BME280 bme; // إنشاء كائن من مستشعر SparkFun BME280
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0); // إنشاء كائن U8g2 لشاشة OLED
const int HeaterControlPin = A3; // تعيين ثوابت للتحكم في MOSFET إلى الدبوس التناظري 3
const int CfanPin = 5; // تعيين ثوابت للتحكم في مروحة التبريد إلى الدبوس الرقمي 5
const int HfanPin = 6; // تعيين ثوابت للتحكم في مروحة التسخين إلى الدبوس الرقمي 6
const int HeaterOn = 255; // تعيين ثابت لتشغيل المدفأة
const int HeaterOff = 0; // تعيين ثابت لإيقاف تشغيل المدفأة
void setup() {
Serial.begin(9600); // بدء الاتصال بالمنفذ التسلسلي بسرعة 9600 بت/ثانية
Wire.begin(); // تهيئة اتصال I2C
u8g2.begin(); // تهيئة الشاشة باستخدام مكتبة u8g2
bme.begin(); // تهيئة مستشعر Sparkfun
pinMode(CfanPin, OUTPUT); // تعيين دبوس مروحة التبريد كمخرج
pinMode(HfanPin, OUTPUT); // تعيين دبوس مروحة التسخين كمخرج
pinMode(HeaterControlPin, OUTPUT); // تعيين دبوس A3 كمخرج للعنصر المدفئ
}
void loop() {
float celsius = bme.readTempC(); // قراءة درجة الحرارة
float humidity = bme.readFloatHumidity(); // قراءة الرطوبة
Serial.print("Celsius: "); // طباعة "Celsius: " في المنفذ التسلسلي
Serial.println(celsius); // طباعة قيمة درجة الحرارة (بالدرجات المئوية) بعدها
Serial.print("Humidity: "); // طباعة "Humidity: " في المنفذ التسلسلي
Serial.println(humidity); // طباعة قيمة الرطوبة بعدها
u8g2.clearBuffer(); // مسح الذاكرة المؤقتة في المكتبة
u8g2.setFont(u8g2_font_ncenB08_tr); // تعيين الخط من المكتبة
u8g2.setCursor(0, 10); // تعيين موضع المؤشر لدرجة الحرارة
u8g2.print("Temp: "); // طباعة "Temp: " على الشاشة
u8g2.print(celsius); // طباعة قيمة درجة الحرارة بعدها
u8g2.print(" C"); // طباعة الوحدة "C" للدرجة المئوية بعدها
u8g2.setCursor(0, 50); // تعيين موضع المؤشر للرطوبة
u8g2.print("Humidity: "); // طباعة "Humidity: " على الشاشة
u8g2.print(humidity); // طباعة قيمة الرطوبة بعدها
u8g2.print(" %"); // طباعة الوحدة "%" بعدها
u8g2.sendBuffer(); // تحديث الشاشة بالمحتوى الجديد
// منطق التحكم في التسخين والتبريد
if (celsius >= 32) { // إذا كانت درجة الحرارة أكبر من أو تساوي 32،
analogWrite(HeaterControlPin, HeaterOff); // إيقاف تشغيل العنصر المدفئ
digitalWrite(CfanPin, HIGH); // تشغيل مروحة التبريد
digitalWrite(HfanPin, LOW); // إيقاف تشغيل مروحة التسخين
} else { // إذا كانت أقل من 32،
analogWrite(HeaterControlPin, HeaterOn); // تشغيل العنصر المدفئ
digitalWrite(CfanPin, LOW); // إيقاف تشغيل مروحة التبريد
digitalWrite(HfanPin, HIGH); // تشغيل مروحة التسخين
}
delay(30000); // تأخير لمدة 30 ثانية
}
اختبار واستكشاف الأخطاء
استخدام المنفذ التسلسلي: تأكد من مراقبة مخرجات المنفذ التسلسلي لمعرفة ما إذا كانت قيم درجة الحرارة والرطوبة تتغير كما هو متوقع.
تحقق من التوصيلات: تأكد من أن لديك التوصيلات صحيحة للأجهزة.
مصدر الطاقة: تأكد من أن الأردوينو مزود بالطاقة الكافية، خصوصاً مع الأجهزة المتصلة.
استكشاف الأخطاء: إذا لم يتم تحديث شاشة OLED، تحقق مما إذا كانت مكتبة u8g2 تعمل بشكل صحيح.
إذا كنت تتبع التصحيحات والنقاط المذكورة أعلاه، يجب أن يعمل الكود بشكل أفضل. إذا واجهت أخطاء أو مشاكل محددة، يرجى إبلاغي بذلك!