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