This circuit is designed to interface a Wemos D1 Mini microcontroller with a DHT11 Humidity and Temperature Sensor, a 0.96" OLED display, and an HW-040 Rotary Encoder. The purpose of the circuit is to read temperature and humidity data from the DHT11 sensor, display this information along with a menu on the OLED screen, and use the rotary encoder for navigating through the menu options.
/*
* This Arduino Sketch is designed for a Wemos D1 Mini microcontroller.
* It interfaces with a DHT11 sensor to read temperature and humidity,
* a 0.96" OLED display to show a menu with these readings and other
* options, and an HW-040 Rotary Encoder for menu navigation.
*/
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <DHT.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define DHTPIN D4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
#define CLK D5
#define DT D6
#define SW D7
int menuIndex = 0;
const int menuItems = 3;
void setup() {
pinMode(CLK, INPUT);
pinMode(DT, INPUT);
pinMode(SW, INPUT_PULLUP);
dht.begin();
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
display.display();
delay(2000);
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Initializing...");
display.display();
delay(1000);
}
void loop() {
static int lastClkState = HIGH;
int clkState = digitalRead(CLK);
if (clkState != lastClkState) {
if (digitalRead(DT) != clkState) {
menuIndex = (menuIndex + 1) % menuItems;
} else {
menuIndex = (menuIndex - 1 + menuItems) % menuItems;
}
lastClkState = clkState;
displayMenu();
}
if (digitalRead(SW) == LOW) {
executeMenuAction();
delay(200);
}
}
void displayMenu() {
display.clearDisplay();
display.setCursor(0,0);
switch(menuIndex) {
case 0:
display.print("1. Show Temp & Humidity");
break;
case 1:
display.print("2. Option 2");
break;
case 2:
display.print("3. Option 3");
break;
}
display.display();
}
void executeMenuAction() {
switch(menuIndex) {
case 0:
displayTempHumidity();
break;
case 1:
// Add action for Option 2
break;
case 2:
// Add action for Option 3
break;
}
}
void displayTempHumidity() {
float h = dht.readHumidity();
float t = dht.readTemperature();
display.clearDisplay();
display.setCursor(0,0);
display.print("Temp: ");
display.print(t);
display.print(" C");
display.setCursor(0,10);
display.print("Humidity: ");
display.print(h);
display.print(" %");
display.display();
delay(2000);
displayMenu();
}
This code is responsible for initializing the components, reading sensor data, handling the rotary encoder input, updating the OLED display, and executing actions based on the menu selection.