Advertisement
pleasedontcode

**Sensor Readings** rev_01

Dec 6th, 2024
48
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: Arduino Uno
  14.     - Source Code created on: 2024-12-06 10:05:26
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* read periodically temp and humidity. */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /* START CODE */
  24.  
  25. /****** DEFINITION OF LIBRARIES *****/
  26. #include <Wire.h>
  27. #include <GY21.h>   //https://github.com/JonasGMorsch/GY-21
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32. void readSensorData(void);
  33.  
  34. /***** DEFINITION OF I2C PINS *****/
  35. const uint8_t sensor_GY21_I2C_PIN_SDA_A4        = A4;
  36. const uint8_t sensor_GY21_I2C_PIN_SCL_A5        = A5;
  37. const uint8_t sensor_GY21_I2C_SLAVE_ADDRESS     = 64;
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. GY21 gy21(sensor_GY21_I2C_SLAVE_ADDRESS); // Create an instance of the GY21 class
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize I2C communication
  45.     Wire.begin(sensor_GY21_I2C_PIN_SDA_A4, sensor_GY21_I2C_PIN_SCL_A5);
  46.    
  47.     // Initialize the GY21 sensor
  48.     gy21.begin();
  49. }
  50.  
  51. void loop(void)
  52. {
  53.     readSensorData(); // Read sensor data periodically
  54.     delay(2000); // Wait for 2 seconds before the next reading
  55. }
  56.  
  57. void readSensorData(void)
  58. {
  59.     float temperature = gy21.readTemperature(); // Read temperature
  60.     float humidity = gy21.readHumidity(); // Read humidity
  61.    
  62.     // Print the temperature and humidity values to the Serial Monitor
  63.     Serial.print("Temperature: ");
  64.     Serial.print(temperature);
  65.     Serial.println(" °C");
  66.    
  67.     Serial.print("Humidity: ");
  68.     Serial.print(humidity);
  69.     Serial.println(" %");
  70. }
  71.  
  72. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement