📖 Scenario: You are building a simple Arduino project that reads temperature from a sensor and displays it on an LCD screen. To do this, you will use two libraries: one for the temperature sensor and one for the LCD display.
🎯 Goal: Create an Arduino sketch that uses the DHT library to read temperature from a DHT11 sensor and the LiquidCrystal library to show the temperature on an LCD screen.
📋 What You'll Learn
Include the DHT library for the temperature sensor
Include the LiquidCrystal library for the LCD display
Initialize both libraries correctly
Read temperature from the sensor
Display the temperature on the LCD
💡 Why This Matters
🌍 Real World
Many Arduino projects need to use more than one sensor or device at the same time. Knowing how to include and manage multiple libraries is essential for building real-world gadgets.
💼 Career
Embedded systems developers and IoT engineers often combine multiple hardware components and libraries to create functional devices. This skill is foundational for such roles.
Progress0 / 4 steps
1
Include libraries and create objects
Write the code to include the DHT.h and LiquidCrystal.h libraries. Then create a DHT object called dht with pin 2 and type DHT11. Also create a LiquidCrystal object called lcd with pins 12, 11, 5, 4, 3, 2.
Arduino
Hint
Use #include to add libraries. Create objects with the exact names dht and lcd using the given pins.
2
Initialize sensor and LCD in setup()
Write the setup() function. Inside it, call dht.begin() to start the sensor. Also call lcd.begin(16, 2) to set the LCD size to 16 columns and 2 rows.
Arduino
Hint
Remember to start both the sensor and the LCD inside setup().
3
Read temperature and display on LCD
Write the loop() function. Inside it, read the temperature using dht.readTemperature() and store it in a variable called temp. Then clear the LCD with lcd.clear(). Use lcd.print() to show the text "Temp: " followed by the temperature value and " C".
Arduino
Hint
Use dht.readTemperature() to get the temperature. Clear the LCD before printing new text.
4
Display temperature continuously
Add a delay of 2000 milliseconds at the end of loop() to update the temperature every 2 seconds. Then print the temperature on the LCD as before.
Arduino
Hint
Use delay(2000); to pause the program for 2 seconds.
Practice
(1/5)
1. Which of the following is the correct way to include multiple libraries in an Arduino sketch?
easy
A. Use multiple #include statements at the top of the sketch.
B. Write all library code inside the loop() function.
C. Include only one library and call others inside it.
D. Use #import instead of #include for multiple libraries.
Solution
Step 1: Understand library inclusion syntax
Arduino uses #include to add libraries at the top of the sketch.
Step 2: Confirm multiple libraries usage
You can add multiple #include lines, one for each library needed.
Final Answer:
Use multiple #include statements at the top of the sketch. -> Option A
Quick Check:
Multiple #include lines = correct way [OK]
Hint: Always put each library in its own #include line at the top [OK]
Common Mistakes:
Trying to include libraries inside functions
Using #import instead of #include
Combining all libraries into one include
2. Which of these is the correct syntax to include the Wire and LiquidCrystal libraries in an Arduino sketch?
easy
A. #include "Wire"
#include "LiquidCrystal"
B. #include
#include
C. #import
#import
D. include
include
Solution
Step 1: Check correct include syntax
The correct way is #include <LibraryName.h> with angle brackets and .h extension.
Step 2: Verify options
#include
#include uses correct syntax; others use wrong keywords or missing .h or wrong quotes.
Final Answer:
#include <Wire.h>\n#include <LiquidCrystal.h> -> Option B
Quick Check:
#include with angle brackets and .h = correct [OK]
Hint: Use #include with angle brackets and .h [OK]
Common Mistakes:
Using #import instead of #include
Omitting the .h extension
Using double quotes instead of angle brackets
3. Given this Arduino code snippet using two libraries:
B. Wire.begin() must be called inside loop, not setup.
C. The sensor object is not declared before use.
D. Missing #include for the sensor's specific library.
Solution
Step 1: Check object declaration
The code calls sensor.begin() and sensor.read() but never declares or creates a sensor object.
Step 2: Verify library usage
Without declaring sensor, the code will not compile or run correctly.
Final Answer:
The sensor object is not declared before use. -> Option C
Quick Check:
Objects must be declared before calling methods [OK]
Hint: Always declare objects before calling their methods [OK]
Common Mistakes:
Thinking Wire.begin() must be in loop
Assuming header file alone creates objects
Ignoring missing object declarations
5. You want to use both the Servo and SoftwareSerial libraries in your Arduino project to control a servo motor and communicate via serial. Which of the following is the best approach to combine them correctly?
hard
A. Include both libraries at the top, create objects for each, initialize in setup(), and use their functions in loop().
B. Include only the Servo library and call SoftwareSerial functions inside Servo methods.
C. Use #include for Servo in setup() and SoftwareSerial in loop().
D. Combine both libraries into one custom library before including.
Solution
Step 1: Properly include and declare libraries
Both libraries must be included at the top with #include and objects created for Servo and SoftwareSerial.
Step 2: Initialize and use in setup() and loop()
Initialize both in setup() and call their functions as needed in loop() to run both tasks simultaneously.
Final Answer:
Include both libraries at the top, create objects for each, initialize in setup(), and use their functions in loop(). -> Option A
Quick Check:
Include, declare, initialize, then use both libraries [OK]
Hint: Include and initialize all libraries before using their functions [OK]
Common Mistakes:
Trying to include libraries inside functions
Assuming one library can call another's functions
Delaying includes until inside setup or loop
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.