Advertisement
pleasedontcode

**Sensor Readings** rev_03

Dec 6th, 2024
63
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:23:08
  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.  
  33. /***** DEFINITION OF I2C PINS *****/
  34. const uint8_t sensor_GY21_I2C_PIN_SDA_A4        = A4;
  35. const uint8_t sensor_GY21_I2C_PIN_SCL_A5        = A5;
  36. const uint8_t sensor_GY21_I2C_SLAVE_ADDRESS     = 64;
  37.  
  38. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  39. GY21 sensor; // Create an instance of the GY21 class
  40.  
  41. void setup(void)
  42. {
  43.     // Initialize serial communication for debugging
  44.     Serial.begin(9600);
  45.     // Initialize I2C communication
  46.     Wire.begin(sensor_GY21_I2C_PIN_SDA_A4, sensor_GY21_I2C_PIN_SCL_A5);
  47. }
  48.  
  49. void loop(void)
  50. {
  51.     // Read temperature and humidity
  52.     float temperature = sensor.GY21_Temperature();
  53.     float humidity = sensor.GY21_Humidity();
  54.  
  55.     // Print the results to the serial monitor
  56.     Serial.print("Temperature: ");
  57.     Serial.print(temperature);
  58.     Serial.println(" °C");
  59.    
  60.     Serial.print("Humidity: ");
  61.     Serial.print(humidity);
  62.     Serial.println(" %");
  63.  
  64.     // Wait for 2 seconds before the next reading
  65.     delay(2000);
  66. }
  67.  
  68. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement