Aokiji_Master
Forum Veteran
Arduino code to display humidity sensor status at 0.9 oled display 128x64 i2c
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 sensor pin
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Humidity Sensor");
// Initialize DHT sensor
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
if (isnan(humidity)) {
display.println("Error reading sensor");
} else {
display.print("Humidity: ");
display.print(humidity);
display.print("%");
}
display.display();
}
setup()
function initializes the OLED display and the DHT sensor. The loop()
function reads the humidity value from the sensor, and if it's a valid value, it displays it on the OLED display. If there's an error reading the sensor, it displays an error message on the OLED display. The display is updated every 2 seconds.code to display humidity and sensorHere's an example Arduino code to display the humidity sensor status on a 0.9 OLED display (128x64, I2C):
Code:#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define DHTPIN 2 // DHT11 sensor pin #define DHTTYPE DHT11 // DHT11 sensor type DHT dht(DHTPIN, DHTTYPE); #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // Initialize OLED display display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.setTextSize(1); display.println("Humidity Sensor"); // Initialize DHT sensor dht.begin(); } void loop() { delay(2000); float humidity = dht.readHumidity(); display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); if (isnan(humidity)) { display.println("Error reading sensor"); } else { display.print("Humidity: "); display.print(humidity); display.print("%"); } display.display(); }
This code uses the DHT11 humidity sensor connected to pin 2 and the Adafruit_SSD1306 library to control the OLED display. Thesetup()
function initializes the OLED display and the DHT sensor. Theloop()
function reads the humidity value from the sensor, and if it's a valid value, it displays it on the OLED display. If there's an error reading the sensor, it displays an error message on the OLED display. The display is updated every 2 seconds.
Here's an example Arduino code to display the humidity and temperature sensor status on a 0.96 inch OLED display (128x64, I2C):code to display humidity and sensor
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 sensor pin
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(1);
display.println("Humidity Sensor");
// Initialize DHT sensor
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(1);
if (isnan(humidity) || isnan(temperature)) {
display.println("Error reading sensor");
} else {
display.print("Humidity: ");
display.print(humidity);
display.print("%");
display.setCursor(0, 20);
display.print("Temperature: ");
display.print(temperature);
display.print("C");
}
display.display();
}
setup()
function initializes the OLED display and the DHT sensor. The loop()
function reads the humidity and temperature values from the sensor, and if they are valid values, it displays them on the OLED display. If there's an error reading the sensors, it displays an error message on the OLED display. The display is updated every 2 seconds.code maximazie the display of humidity and temperature status on oled 0.9Here's an example Arduino code to display the humidity and temperature sensor status on a 0.96 inch OLED display (128x64, I2C):
Code:#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define DHTPIN 2 // DHT11 sensor pin #define DHTTYPE DHT11 // DHT11 sensor type DHT dht(DHTPIN, DHTTYPE); #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // Initialize OLED display display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.setTextSize(1); display.println("Humidity Sensor"); // Initialize DHT sensor dht.begin(); } void loop() { delay(2000); float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(1); if (isnan(humidity) || isnan(temperature)) { display.println("Error reading sensor"); } else { display.print("Humidity: "); display.print(humidity); display.print("%"); display.setCursor(0, 20); display.print("Temperature: "); display.print(temperature); display.print("C"); } display.display(); }
This code uses the DHT11 humidity and temperature sensor connected to pin 2 and the Adafruit_SSD1306 library to control the OLED display. Thesetup()
function initializes the OLED display and the DHT sensor. Theloop()
function reads the humidity and temperature values from the sensor, and if they are valid values, it displays them on the OLED display. If there's an error reading the sensors, it displays an error message on the OLED display. The display is updated every 2 seconds.
Here is an example code that maximizes the display of humidity and temperature sensor status on a 0.96 inch OLED display (128x64, I2C):code maximazie the display of humidity and temperature status on oled 0.9
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 sensor pin
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(2);
display.println("Humidity Sensor");
// Initialize DHT sensor
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
display.clearDisplay();
display.setCursor(0, 0);
display.setTextSize(2);
if (isnan(humidity) || isnan(temperature)) {
display.println("Error reading sensor");
} else {
display.print("Humidity: ");
display.print(humidity);
display.print("%");
display.setCursor(0, 30);
display.print("Temp: ");
display.print(temperature);
display.print("C");
}
display.display();
}
display.setTextSize(2);
.display.setCursor(0, 30);
to allow more space for the temperature display.display.print("Temp: ");
to save space on the OLED display.code to display humidity and temperature first line humidity second line percent of humidity third line temperature fourth line temperature statusHere is an example code that maximizes the display of humidity and temperature sensor status on a 0.96 inch OLED display (128x64, I2C):
This code is similar to the previous example, but with the following changes:Code:#include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #include <DHT.h> #define DHTPIN 2 // DHT11 sensor pin #define DHTTYPE DHT11 // DHT11 sensor type DHT dht(DHTPIN, DHTTYPE); #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); // Initialize OLED display display.begin(SSD1306_SWITCHCAPVCC, 0x3C); display.clearDisplay(); display.setTextColor(SSD1306_WHITE); display.setCursor(0, 0); display.setTextSize(2); display.println("Humidity Sensor"); // Initialize DHT sensor dht.begin(); } void loop() { delay(2000); float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); display.clearDisplay(); display.setCursor(0, 0); display.setTextSize(2); if (isnan(humidity) || isnan(temperature)) { display.println("Error reading sensor"); } else { display.print("Humidity: "); display.print(humidity); display.print("%"); display.setCursor(0, 30); display.print("Temp: "); display.print(temperature); display.print("C"); } display.display(); }
- The text size has been increased to 2 with the line
display.setTextSize(2);
.- The cursor has been moved down to line 30 with the line
display.setCursor(0, 30);
to allow more space for the temperature display.- The text for the temperature has been changed to "Temp" with the line
display.print("Temp: ");
to save space on the OLED display.
By making these changes, the humidity and temperature values are displayed in a larger font and take up more space on the OLED display for easy viewing.
Here's an example code that displays humidity and temperature on a 0.96 inch OLED display (128x64, I2C) with the following format:code to display humidity and temperature first line humidity second line percent of humidity third line temperature fourth line temperature status
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define DHTPIN 2 // DHT11 sensor pin
#define DHTTYPE DHT11 // DHT11 sensor type
DHT dht(DHTPIN, DHTTYPE);
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
void setup() {
Serial.begin(9600);
// Initialize OLED display
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.setTextSize(1);
// Initialize DHT sensor
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
display.clearDisplay();
display.setCursor(0, 0);
// Display humidity
display.print("Humidity: ");
display.print(humidity);
display.println("%");
// Display temperature
display.print("Temperature: ");
display.print(temperature);
display.println("C");
// Display temperature status message
if (temperature < 18) {
display.println("Too Cold");
} else if (temperature > 28) {
display.println("Too Hot");
} else {
display.println("Normal");
}
display.display();
}
setup()
function initializes the OLED display and the DHT sensor. The loop()
function reads the humidity and temperature values from the sensor and displays them on the OLED display with the appropriate text and format. The temperature status message is based on a temperature range of 18-28 degrees Celsius, but this can be adjusted as needed. The display is updated every 2 seconds.