Monitoring Temperature and Humidity with Arduino Code




Arduino Code for Temperature and Humidity Sensor


Arduino Code for Temperature and Humidity Sensor

Article Summary

  1. Introduction
  2. Requirements
  3. Setup
  4. Arduino Code
  5. Conclusion

Introduction

The Arduino platform is widely used in various electronics projects due to its simplicity and versatility. One common use case is to interface temperature and humidity sensors with Arduino boards to monitor environmental conditions. This article provides an overview of how to write Arduino code to read data from a temperature and humidity sensor.

Requirements

Before starting, ensure you have the following components ready:

  • Arduino board (e.g., Arduino Uno)
  • Temperature and humidity sensor (e.g., DHT11 or DHT22)
  • Jumper wires
  • Breadboard (optional)

Setup

To set up the circuit, follow these steps:

  1. Connect the sensor to the Arduino board using jumper wires. The sensor has three pins: VCC, GND, and DATA. Connect VCC to 5V on the Arduino, GND to GND, and DATA to any digital pin (e.g., pin 2).
  2. If using a breadboard, connect the VCC and GND of the sensor to the power rails on the breadboard for easier wiring.
  3. Ensure the Arduino board is properly connected to your computer via USB cable.

Arduino Code

Here’s an example Arduino code snippet to read temperature and humidity data from the sensor:


#include <DHT.h>
 
#define DHTPIN 2     // Digital pin connected to the sensor
 
// Uncomment the type of sensor you're using
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT22   // DHT 22  (AM2302)
//#define DHTTYPE DHT21   // DHT 21 (AM2301)
 
DHT dht(DHTPIN, DHTTYPE);
 
void setup() {
  Serial.begin(9600);
  dht.begin();
}
 
void loop() {
  float temperature = dht.readTemperature();      // Read temperature value
  float humidity = dht.readHumidity();            // Read humidity value
  // Print temperature and humidity values to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.print("°Ct");
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println("%");
  delay(2000);  // Delay for 2 seconds before next reading
}

Conclusion

In this article, we discussed how to write Arduino code to read temperature and humidity data from a sensor. By following the provided guidelines and using the example code, you can easily interface a temperature and humidity sensor with an Arduino board. This opens up a wide range of possibilities for environmental monitoring and automation projects.


Related Post

Shopping Cart
Scroll to Top
Scroll to Top