Advertisement
pleasedontcode

**Sensor Display** rev_01

Apr 25th, 2025
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Sensor Display**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2025-04-25 13:22:21
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Monitor temperature and humidity using a DHT22 */
  21.     /* sensor, displaying the readings on an SSD1306 OLED */
  22.     /* screen via I2C communication. Ensure accurate data */
  23.     /* retrieval and display updates every few seconds. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /* START CODE */
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <Wire.h>
  30. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  31. #include <Adafruit_SSD1306.h>    //https://github.com/stblassitude/Adafruit_SSD1306_Wemos_OLED.git
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36. void updateOutputs();
  37. void displayData(float temperature, float humidity);
  38.  
  39. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  40. const uint8_t DHT22_DHT22_DOUT_PIN_D4        = 4;
  41.  
  42. /***** DEFINITION OF DIGITAL OUTPUT PINS *****/
  43. const uint8_t myLED_LED_PIN_D13        = 13;
  44.  
  45. /***** DEFINITION OF I2C PINS *****/
  46. const uint8_t SSD1306_SSD1306OledDisplay_I2C_PIN_SDA_D21        = 21;
  47. const uint8_t SSD1306_SSD1306OledDisplay_I2C_PIN_SCL_D22        = 22;
  48. const uint8_t SSD1306_SSD1306OledDisplay_I2C_SLAVE_ADDRESS        = 60;
  49.  
  50. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  51. /***** used to store raw data *****/
  52. bool    myLED_LED_PIN_D13_rawData        = 0;
  53.  
  54. /***** DEFINITION OF OUTPUT PHYSICAL VARIABLES *****/
  55. /***** used to store data after characteristic curve transformation *****/
  56. float   myLED_LED_PIN_D13_phyData        = 0.0;
  57.  
  58. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  59. DHT dht(DHT22_DHT22_DOUT_PIN_D4, DHT22); // Create DHT object
  60. Adafruit_SSD1306 display(SSD1306_LCDWIDTH, SSD1306_LCDHEIGHT, &Wire, -1); // Create display object
  61.  
  62. void setup(void)
  63. {
  64.     // put your setup code here, to run once:
  65.     pinMode(DHT22_DHT22_DOUT_PIN_D4, INPUT_PULLUP);
  66.     pinMode(myLED_LED_PIN_D13, OUTPUT);
  67.  
  68.     // Initialize DHT sensor
  69.     dht.begin();
  70.  
  71.     // Initialize the OLED display
  72.     display.begin(SSD1306_SWITCHCAPVCC, SSD1306_SSD1306OledDisplay_I2C_SLAVE_ADDRESS);
  73.     display.clearDisplay();
  74. }
  75.  
  76. void loop(void)
  77. {
  78.     // put your main code here, to run repeatedly:
  79.     float temperature = dht.readTemperature(); // Read temperature as Celsius
  80.     float humidity = dht.readHumidity(); // Read humidity
  81.  
  82.     // Check if any reads failed and exit early (to try again).
  83.     if (isnan(temperature) || isnan(humidity)) {
  84.         displayData(0, 0); // Display error values
  85.         return;
  86.     }
  87.  
  88.     // Display the data on the OLED
  89.     displayData(temperature, humidity);
  90.  
  91.     updateOutputs(); // Refresh output data
  92.     delay(2000); // Wait a few seconds between measurements
  93. }
  94.  
  95. void updateOutputs()
  96. {
  97.     digitalWrite(myLED_LED_PIN_D13, myLED_LED_PIN_D13_rawData);
  98. }
  99.  
  100. void displayData(float temperature, float humidity)
  101. {
  102.     display.clearDisplay(); // Clear the display
  103.  
  104.     // Set text size and color
  105.     display.setTextSize(1);
  106.     display.setTextColor(SSD1306_WHITE);
  107.  
  108.     // Display temperature and humidity
  109.     display.setCursor(0, 0);
  110.     display.print("Temp: ");
  111.     display.print(temperature);
  112.     display.println(" C");
  113.     display.print("Humidity: ");
  114.     display.print(humidity);
  115.     display.println(" %");
  116.  
  117.     display.display(); // Update the display with the new data
  118. }
  119.  
  120. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement