Tutorial 9 – Using Sensors with Arduino
Welcome back to the CraftedTech Engineering Arduino tutorial series! In this tutorial, we’ll explore how to interface sensors with your Arduino. Sensors are essential for gathering data from the environment and making your projects interactive. In this case, we’ll work with a temperature sensor to read the ambient temperature and display it on the Serial Monitor.
🔌 What You’ll Need
- 1 Arduino Board (e.g., Arduino UNO)
- 1 DHT11 or DHT22 Temperature and Humidity Sensor
- 1 10kΩ Resistor (for DHT22 sensor)
- Breadboard
- Jumper wires
- USB cable for Arduino
You can order the required components here:
🧠 What Is a Temperature Sensor?
A temperature sensor is a device that measures temperature. The DHT11 and DHT22 are popular temperature and humidity sensors used with Arduino. They provide digital output and are easy to interface, making them ideal for many home automation and environmental monitoring projects.
In this tutorial, we’ll be reading temperature data from a DHT11 sensor and displaying it on the Serial Monitor.
🧾 Step-by-Step Instructions
🔧 Step 1: Wiring the DHT11 Sensor
- Connect the VCC pin of the DHT11 to the 5V pin on Arduino.
- Connect the GND pin of the DHT11 to the GND pin on Arduino.
- Connect the DATA pin of the DHT11 to pin 7 on the Arduino (or any other digital pin).
- Place a 10kΩ resistor between the VCC and DATA pins (for DHT22).
💻 Step 2: Arduino Code to Read Temperature
#include <DHT.h>
#define DHTPIN 7 // Pin connected to the DHT11 sensor
#define DHTTYPE DHT11 // Define the sensor type
DHT dht(DHTPIN, DHTTYPE); // Create DHT object
void setup() {
Serial.begin(9600);
dht.begin(); // Initialize the DHT sensor
}
void loop() {
// Wait a few seconds between readings
delay(2000);
// Read the temperature as Celsius
float tempC = dht.readTemperature();
// Check if reading failed
if (isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the temperature to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" °C");
}
🔍 Code Breakdown
- DHT.h: A library to easily interface with DHT sensors.
- dht.readTemperature(): Reads the temperature in Celsius from the DHT sensor.
- Serial.print() and Serial.println(): These commands send data to the Serial Monitor.
- isnan(): Checks if the sensor data is valid (to prevent errors).
🧪 Try This!
- Experiment with different sensors such as DHT22 for more accurate temperature readings.
- Add an LCD to display the temperature on-screen instead of using the Serial Monitor.
🛠️ Troubleshooting Tips
- Double-check the wiring and ensure the sensor is connected to the correct pins.
- If the readings are incorrect, check that the sensor is properly powered (5V and GND).
- Ensure that you’ve installed the DHT sensor library in the Arduino IDE: Sketch > Include Library > Manage Libraries > Search for "DHT".
🧭 What You Learned in This Tutorial
✔️ How to interface a temperature sensor with Arduino
✔️ How to read and display data from a sensor using the Serial Monitor
✔️ How to use the DHT library to make reading data easier
✔️ Troubleshooting common sensor issues
📌 Next Up: Tutorial 10 – Using an LCD with Arduino
In our next tutorial, we will show you how to display data from your sensors on an LCD screen. This will give your projects a more interactive and user-friendly interface.
📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!
🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering