Devices & Components
Arduino Nano RP2040 Connect
10 jumper wires 150mm male
Waveshare 0.96 inch OLED Display
DHT11 Temperature and Humidity Sensor Module Breakout
Software & Tools
Arduino IDE
Project description
Code
Code
cpp
Code for both OLED and DHT sensor
1#include <SPI.h> 2#include <Adafruit_GFX.h> 3#include <Adafruit_SSD1306.h> 4#include <dht11.h> 5 6// OLED display width and height 7#define SCREEN_WIDTH 128 8#define SCREEN_HEIGHT 64 9 10// Define SPI pin connections 11#define OLED_CLK 13 // Clock 12#define OLED_MOSI 11 // Data input (DIN) 13#define OLED_CS 10 // Chip select 14#define OLED_DC 9 // Data/command 15#define OLED_RESET 8 // Reset 16 17#define DHT11PIN 4 18 19// Create an SSD1306 SPI instance 20Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS); 21 22dht11 DHT11; 23 24void setup() { 25 Serial.begin(9600); 26 Serial.println("Initializing OLED..."); 27 28 // Initialize the display 29 if (!display.begin(SSD1306_SWITCHCAPVCC)) { 30 Serial.println(F("SSD1306 initialization failed!")); 31 for (;;); // Infinite loop on failure 32 } 33 34 Serial.println("OLED initialized successfully!"); 35 36 // Clear the display 37 display.clearDisplay() 38} 39 40void loop() { 41 42 int chk = DHT11.read(DHT11PIN); //Read the DHT 11 43 44 45 46 display.clearDisplay(); 47 48 49 display.setTextSize(2); // Size 2 50 display.setTextColor(SSD1306_WHITE); // White text 51 display.setCursor(0, 0); // Start at top-left corner 52 display.println("Temp:"); 53 display.println((float)DHT11.temperature, 2); //Display the Temperature 54 display.println("Humidity:"); 55 display.println((float)DHT11.humidity, 2); //Display the Humidity 56 display.setCursor(60,16); 57 display.print("C"); 58 display.setCursor(60,48); 59 display.print("%"); 60 display.display(); 61 delay(200); 62}
Comments
Only logged in users can leave comments