Advertisement
pleasedontcode

**Sensor Readings** rev_08

Dec 6th, 2024
56
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 compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-06 11:35:58
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read periodically all data from sensor. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <DFRobot_BME280.h> //https://github.com/DFRobot/DFRobot_BME280
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF I2C PINS *****/
  34. const uint8_t sensor_BME280_I2C_SLAVE_ADDRESS = 119;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. DFRobot_BME280_IIC bme280(&Wire, sensor_BME280_I2C_SLAVE_ADDRESS); // Create an instance of the DFRobot_BME280_IIC class
  38.  
  39. void setup(void)
  40. {
  41.     // Initialize I2C communication
  42.     Wire.begin(); // Initialize I2C without parameters
  43.    
  44.     // Initialize the BME280 sensor
  45.     if (bme280.begin() != bme280.eStatusOK) {
  46.         Serial.println("BME280 initialization failed!");
  47.         while (1); // Stop execution if sensor initialization fails
  48.     }
  49.    
  50.     Serial.begin(9600); // Start serial communication for debugging
  51. }
  52.  
  53. void loop(void)
  54. {
  55.     // Read sensor data periodically
  56.     float temperature = bme280.getTemperature();
  57.     uint32_t pressure = bme280.getPressure();
  58.     float humidity = bme280.getHumidity();
  59.  
  60.     // Print the sensor data to the Serial Monitor
  61.     Serial.print("Temperature: ");
  62.     Serial.print(temperature);
  63.     Serial.println(" °C");
  64.    
  65.     Serial.print("Pressure: ");
  66.     Serial.print(pressure);
  67.     Serial.println(" Pa");
  68.    
  69.     Serial.print("Humidity: ");
  70.     Serial.print(humidity);
  71.     Serial.println(" %");
  72.  
  73.     delay(2000); // Wait for 2 seconds before the next reading
  74. }
  75.  
  76. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement