Advertisement
pleasedontcode

**Sensor Readings** rev_01

Dec 12th, 2024
59
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 Readings**
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-12-12 23:50:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The system shall initialize all connected */
  21.     /* components during the setup phase and continuously */
  22.     /* monitor their status in the loop phase to ensure */
  23.     /* optimal performance and responsiveness. */
  24. /****** SYSTEM REQUIREMENT 2 *****/
  25.     /* During the setup phase, the system shall configure */
  26.     /* all connected components, including sensors and */
  27.     /* actuators, and in the loop phase, it shall check */
  28.     /* their operational status to ensure reliability and */
  29.     /* timely responses. */
  30. /****** END SYSTEM REQUIREMENTS *****/
  31.  
  32. /* START CODE */
  33.  
  34. /****** DEFINITION OF LIBRARIES *****/
  35. #include <Wire.h>
  36. #include <Adafruit_Sensor.h>
  37. #include <Adafruit_BME280.h>
  38.  
  39. /****** FUNCTION PROTOTYPES *****/
  40. void setup(void);
  41. void loop(void);
  42.  
  43. // Instantiate the BME280 sensor object
  44. Adafruit_BME280 bme; // 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(0x77)) { // Check if the sensor is connected
  53.         Serial.println("Could not find a valid BME280 sensor, check wiring!");
  54.         while (1); // Stop if the sensor is not found
  55.     }
  56.    
  57.     Serial.println("BME280 sensor initialized successfully.");
  58. }
  59.  
  60. void loop(void)
  61. {
  62.     // Read temperature, pressure, and humidity from the BME280 sensor
  63.     float temperature = bme.readTemperature();
  64.     float pressure = bme.readPressure() / 100.0F; // Convert to hPa
  65.     float humidity = bme.readHumidity();
  66.  
  67.     // Print the sensor readings to the Serial Monitor
  68.     Serial.print("Temperature: ");
  69.     Serial.print(temperature);
  70.     Serial.println(" °C");
  71.  
  72.     Serial.print("Pressure: ");
  73.     Serial.print(pressure);
  74.     Serial.println(" hPa");
  75.  
  76.     Serial.print("Humidity: ");
  77.     Serial.print(humidity);
  78.     Serial.println(" %");
  79.  
  80.     // Wait for a second before the next reading
  81.     delay(1000);
  82. }
  83.  
  84. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement