Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Sensor Display**
- - Source Code NOT compiled for: ESP32 DevKit V1
- - Source Code created on: 2025-04-25 13:22:21
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Monitor temperature and humidity using a DHT22 */
- /* sensor, displaying the readings on an SSD1306 OLED */
- /* screen via I2C communication. Ensure accurate data */
- /* retrieval and display updates every few seconds. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <DHT.h> //https://github.com/adafruit/DHT-sensor-library
- #include <Adafruit_SSD1306.h> //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void updateOutputs();
- void displayData(float temperature, float humidity);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t DHT22_DHT22_DOUT_PIN_D4 = 4;
- /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
- const uint8_t myLED_LED_PIN_D13 = 13;
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t SSD1306_SSD1306OledDisplay_I2C_PIN_SDA_D21 = 21;
- const uint8_t SSD1306_SSD1306OledDisplay_I2C_PIN_SCL_D22 = 22;
- const uint8_t SSD1306_SSD1306OledDisplay_I2C_SLAVE_ADDRESS = 60;
- /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
- /***** used to store raw data *****/
- bool myLED_LED_PIN_D13_rawData = 0;
- /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
- /***** used to store data after characteristic curve transformation *****/
- float myLED_LED_PIN_D13_phyData = 0.0;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- DHT dht(DHT22_DHT22_DOUT_PIN_D4, DHT22); // Create DHT object
- Adafruit_SSD1306 display(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT, &Wire, -1); // Create display object
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(DHT22_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
- pinMode(myLED_LED_PIN_D13, OUTPUT);
- // Initialize DHT sensor
- dht.begin();
- // Initialize the OLED display
- display.begin(SSD1306_SWITCHCAPVCC, SSD1306_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
- display.clearDisplay();
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- float temperature = dht.readTemperature(); // Read temperature as Celsius
- float humidity = dht.readHumidity(); // Read humidity
- // Check if any reads failed and exit early (to try again).
- if (isnan(temperature) || isnan(humidity)) {
- displayData(0, 0); // Display error values
- return;
- }
- // Display the data on the OLED
- displayData(temperature, humidity);
- updateOutputs(); // Refresh output data
- delay(2000); // Wait a few seconds between measurements
- }
- void updateOutputs()
- {
- digitalWrite(myLED_LED_PIN_D13, myLED_LED_PIN_D13_rawData);
- }
- void displayData(float temperature, float humidity)
- {
- display.clearDisplay(); // Clear the display
- // Set text size and color
- display.setTextSize(1);
- display.setTextColor(SSD1306_WHITE);
- // Display temperature and humidity
- display.setCursor(0, 0);
- display.print("Temp: ");
- display.print(temperature);
- display.println(" C");
- display.print("Humidity: ");
- display.print(humidity);
- display.println(" %");
- display.display(); // Update the display with the new data
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement