📖 Scenario: You have a temperature sensor connected to your Arduino. You want to read the temperature using a sensor library and display it on the Serial Monitor.
🎯 Goal: Learn how to include a sensor library, initialize the sensor, read temperature data, and print it to the Serial Monitor.
📋 What You'll Learn
Include the sensor library DHT.h
Create a DHT sensor object with pin 2 and type DHT11
Initialize the sensor in setup()
Read temperature in Celsius using sensor.readTemperature()
Print the temperature value to the Serial Monitor
💡 Why This Matters
🌍 Real World
Reading sensor data is common in home automation, weather stations, and robotics.
💼 Career
Understanding how to use sensor libraries is important for embedded systems and IoT developer roles.
Progress0 / 4 steps
1
Include the sensor library and create sensor object
Write #include <DHT.h> at the top. Then create a DHT object called sensor using pin 2 and type DHT11.
Arduino
Hint
Use #include <DHT.h> to add the library. Then create the sensor object with DHT sensor(2, DHT11);.
2
Initialize Serial and sensor in setup
Write void setup() function. Inside it, start Serial communication at 9600 baud with Serial.begin(9600);. Then initialize the sensor with sensor.begin();.
Arduino
Hint
Use void setup() { Serial.begin(9600); sensor.begin(); } to prepare the sensor and Serial Monitor.
3
Read temperature in loop
Write void loop() function. Inside it, read temperature in Celsius using float temp = sensor.readTemperature();. Then add a delay of 2000 milliseconds with delay(2000);.
Arduino
Hint
Use float temp = sensor.readTemperature(); to get temperature and delay(2000); to wait 2 seconds.
4
Print temperature to Serial Monitor
Inside loop(), after reading temperature, print it using Serial.print("Temperature: "); and Serial.println(temp);.
Arduino
Hint
Use Serial.print("Temperature: "); and Serial.println(temp); to show the temperature value.
Practice
(1/5)
1. What is the first step when using a sensor library in an Arduino sketch?
easy
A. Include the sensor library at the top of the sketch
B. Read sensor values in the loop() function
C. Create a sensor object inside the loop()
D. Start the sensor in the loop() function
Solution
Step 1: Understand the library usage
Before using a sensor, you must include its library so Arduino knows how to communicate with it.
Step 2: Identify correct placement
Including the library is done at the top of the sketch, before setup() and loop().
Final Answer:
Include the sensor library at the top of the sketch -> Option A
Quick Check:
Include library first = A [OK]
Hint: Always include libraries before setup() [OK]
Common Mistakes:
Trying to create sensor object before including library
Starting sensor in loop() instead of setup()
Reading sensor before initializing it
2. Which of the following is the correct way to create a sensor object for a DHT sensor using its library?
easy
A. DHT sensor = DHT(2);
B. DHT sensor = new DHT(2, DHT11);
C. DHT sensor(2, DHT11);
D. DHT sensor = DHT11(2);
Solution
Step 1: Recall object creation syntax in Arduino C++
Objects are created by calling the constructor directly, like ClassName objectName(parameters); without '=' or 'new' keyword.
Step 2: Match correct syntax for DHT library
The DHT library uses: DHT sensor(pin, type); so DHT sensor(2, DHT11); matches this exactly.
Final Answer:
DHT sensor(2, DHT11); -> Option C
Quick Check:
Object creation = ClassName object(params); [OK]
Hint: Create objects without '=' or 'new' in Arduino [OK]
Common Mistakes:
Using '=' sign when creating objects
Using 'new' keyword which is not typical in Arduino sketches
Confusing sensor type and pin order
3. Given the code below, what will be printed if the sensor reading fails?
B. dht.begin() should be called in setup(), not loop()
C. Missing #define for sensor pin
D. Serial.begin() should be called in loop()
Solution
Step 1: Locate sensor initialization
dht.begin() initializes the sensor and should be called once in setup() to avoid repeated initialization.
Step 2: Identify why calling dht.begin() in loop() is wrong
Calling dht.begin() repeatedly in loop() can cause sensor communication issues and slow down the program.
Final Answer:
dht.begin() should be called in setup(), not loop() -> Option B
Quick Check:
Initialize sensor once in setup() [OK]
Hint: Initialize sensors once in setup() [OK]
Common Mistakes:
Calling sensor start functions repeatedly in loop()
Confusing Serial.begin() placement
Assuming readTemperature() returns int
5. You want to read temperature and humidity from a DHT11 sensor and turn on an LED if humidity is above 70%. Which code snippet correctly uses the sensor library and controls the LED?