Advertisement
pleasedontcode

**Sensor Display** rev_01

Nov 8th, 2024
72
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: 2024-11-08 23:11:23
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The project aims to automate sensor data */
  21.     /* collection and processing using Arduino, */
  22.     /* integrating libraries for efficient communication */
  23.     /* and control of connected components. Focus on */
  24.     /* modular design for easy updates and maintenance. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /* START CODE */
  28.  
  29. /****** DEFINITION OF LIBRARIES *****/
  30. #include <Wire.h>
  31. #include <SPI.h>
  32. #include <Adafruit_Sensor.h>
  33. #include <Adafruit_BME280.h>
  34. #include <Adafruit_SSD1306.h>
  35.  
  36. /****** FUNCTION PROTOTYPES *****/
  37. void setup(void);
  38. void loop(void);
  39.  
  40. /****** GLOBAL VARIABLES *****/
  41. // Create an instance of the BME280 sensor
  42. Adafruit_BME280 bme; // I2C
  43. // Create an instance of the OLED display
  44. Adafruit_SSD1306 display(128, 32, &Wire, -1); // Initialize with I2C
  45.  
  46. void setup(void)
  47. {
  48.     // Initialize serial communication for debugging
  49.     Serial.begin(9600);
  50.    
  51.     // Initialize the BME280 sensor
  52.     if (!bme.begin(0x76)) { // Check the I2C address
  53.         Serial.println("Could not find a valid BME280 sensor, check wiring!");
  54.         while (1);
  55.     }
  56.  
  57.     // Initialize the OLED display
  58.     display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // Initialize with I2C address
  59.     display.clearDisplay();
  60.     display.display();
  61. }
  62.  
  63. void loop(void)
  64. {
  65.     // Read temperature, humidity, and pressure from the BME280 sensor
  66.     float temperature = bme.readTemperature();
  67.     float humidity = bme.readHumidity();
  68.     float pressure = bme.readPressure() / 100.0F; // Convert to hPa
  69.  
  70.     // Display the sensor data on the OLED display
  71.     display.clearDisplay();
  72.     display.setTextSize(1);
  73.     display.setTextColor(SSD1306_WHITE);
  74.     display.setCursor(0, 0);
  75.     display.print("Temp: ");
  76.     display.print(temperature);
  77.     display.println(" C");
  78.     display.print("Humidity: ");
  79.     display.print(humidity);
  80.     display.println(" %");
  81.     display.print("Pressure: ");
  82.     display.print(pressure);
  83.     display.println(" hPa");
  84.     display.display();
  85.  
  86.     // Wait for 2 seconds before the next loop
  87.     delay(2000);
  88. }
  89.  
  90. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement