Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Sensor Readings**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-12-06 10:05:26
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* read periodically temp and humidity. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <Wire.h>
- #include <GY21.h> //https://github.com/JonasGMorsch/GY-21
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- void readSensorData(void);
- /***** DEFINITION OF I2C PINS *****/
- const uint8_t sensor_GY21_I2C_PIN_SDA_A4 = A4;
- const uint8_t sensor_GY21_I2C_PIN_SCL_A5 = A5;
- const uint8_t sensor_GY21_I2C_SLAVE_ADDRESS = 64;
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- GY21 gy21(sensor_GY21_I2C_SLAVE_ADDRESS); // Create an instance of the GY21 class
- void setup(void)
- {
- // Initialize I2C communication
- Wire.begin(sensor_GY21_I2C_PIN_SDA_A4, sensor_GY21_I2C_PIN_SCL_A5);
- // Initialize the GY21 sensor
- gy21.begin();
- }
- void loop(void)
- {
- readSensorData(); // Read sensor data periodically
- delay(2000); // Wait for 2 seconds before the next reading
- }
- void readSensorData(void)
- {
- float temperature = gy21.readTemperature(); // Read temperature
- float humidity = gy21.readHumidity(); // Read humidity
- // Print the temperature and humidity values to the Serial Monitor
- Serial.print("Temperature: ");
- Serial.print(temperature);
- Serial.println(" °C");
- Serial.print("Humidity: ");
- Serial.print(humidity);
- Serial.println(" %");
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement