Bird
Raised Fist0
Arduinoprogramming~20 mins

Using multiple libraries together in Arduino - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Multi-Library Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of combined sensor readings
What is the output of this Arduino sketch that uses two libraries to read temperature and humidity?
Arduino
#include <DHT.h>
#include <Wire.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %");
  Serial.print(", Temperature: ");
  Serial.print(t);
  Serial.println(" *C");
  delay(2000);
}
AHumidity: 45.0 % , Temperature: 23.0 *C
BHumidity: nan % , Temperature: nan *C
CHumidity: 100 % , Temperature: 0 *C
DCompilation error due to missing Wire.begin()
Attempts:
2 left
💡 Hint
Check if the sensor is properly initialized and if the readings are valid.
🧠 Conceptual
intermediate
1:30remaining
Reason for including multiple libraries
Why do Arduino sketches often include multiple libraries like and together?
ATo make the sketch run faster by default
BTo reduce the size of the compiled program
CTo avoid using any hardware pins
DTo handle different hardware communication protocols simultaneously
Attempts:
2 left
💡 Hint
Think about what each library controls in hardware communication.
🔧 Debug
advanced
2:00remaining
Identify the error when using two libraries
What error will this Arduino code produce when compiling?
Arduino
#include <Wire.h>
#include <SPI.h>

void setup() {
  Wire.begin();
  SPI.begin();
}

void loop() {
  Wire.beginTransmission(0x3C);
  SPI.transfer(0xFF);
  Wire.endTransmission();
  delay(1000);
}
AError: SPI.transfer() called outside SPI transaction
BError: Wire.beginTransmission() missing address
CNo error, code compiles and runs correctly
DError: Cannot use Wire and SPI in the same sketch
Attempts:
2 left
💡 Hint
Check the correct usage of SPI.transfer() in SPI communication.
📝 Syntax
advanced
1:00remaining
Correct syntax for using two libraries
Which option shows the correct way to initialize both Wire and SPI libraries in Arduino?
AWire.begin(); SPI.begin();
BWire.start(); SPI.start();
CWire.init(); SPI.init();
DWire.beginTransmission(); SPI.beginTransmission();
Attempts:
2 left
💡 Hint
Check the official Arduino library functions for starting communication.
🚀 Application
expert
3:00remaining
Combining sensor data with display using multiple libraries
You want to read temperature from a DHT sensor and display it on an OLED screen using Wire and Adafruit_SSD1306 libraries. Which code snippet correctly initializes and uses both libraries together?
A
#include &lt;Wire.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;DHT.h&gt;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;SPI);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  dht.begin();
}
void loop() {
  float temp = dht.readTemperature();
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temp);
  display.print(" C");
  display.display();
  delay(2000);
}
B
#include &lt;Wire.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;DHT.h&gt;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  dht.begin();
}
void loop() {
  float temp = dht.readTemperature();
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temp);
  display.print(" C");
  display.display();
  delay(2000);
}
C
#include &lt;Wire.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;DHT.h&gt;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT11
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &amp;Wire);
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  dht.begin();
}
void loop() {
  float temp = dht.readTemperature();
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temp);
  display.print(" C");
  display.display();
  delay(2000);
}
D
#include &lt;Wire.h&gt;
#include &lt;Adafruit_SSD1306.h&gt;
#include &lt;DHT.h&gt;
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define DHTPIN 2
#define DHTTYPE DHT11
Adafruit_SSD1306 display;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  dht.begin();
}
void loop() {
  float temp = dht.readTemperature();
  display.clearDisplay();
  display.setTextSize(2);
  display.setCursor(0,0);
  display.print("Temp: ");
  display.print(temp);
  display.print(" C");
  display.display();
  delay(2000);
}
Attempts:
2 left
💡 Hint
Check how the display object is constructed and which communication protocol it uses.

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

  1. Step 1: Understand library inclusion syntax

    Arduino uses #include to add libraries at the top of the sketch.
  2. Step 2: Confirm multiple libraries usage

    You can add multiple #include lines, one for each library needed.
  3. Final Answer:

    Use multiple #include statements at the top of the sketch. -> Option A
  4. 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

  1. Step 1: Check correct include syntax

    The correct way is #include <LibraryName.h> with angle brackets and .h extension.
  2. Step 2: Verify options

    #include #include uses correct syntax; others use wrong keywords or missing .h or wrong quotes.
  3. Final Answer:

    #include <Wire.h>\n#include <LiquidCrystal.h> -> Option B
  4. 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:
#include <Wire.h>
#include <LiquidCrystal.h>
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  Wire.begin();
  lcd.begin(16, 2);
  lcd.print("Hi");
}

void loop() {
  // nothing here
}

What will be displayed on the LCD when the Arduino runs?
medium
A. Nothing will display because Wire library is not used in loop.
B. The LCD will show random characters.
C. A compile error due to missing Wire usage.
D. The text "Hi" on the LCD screen.

Solution

  1. Step 1: Analyze library initialization

    Wire library is started with Wire.begin() in setup; LiquidCrystal is initialized and prints "Hi".
  2. Step 2: Check output on LCD

    Since lcd.print("Hi") runs in setup, "Hi" appears on the LCD screen.
  3. Final Answer:

    The text "Hi" on the LCD screen. -> Option D
  4. Quick Check:

    lcd.print("Hi") = "Hi" shown [OK]
Hint: Look for lcd.print() calls to find displayed text [OK]
Common Mistakes:
  • Assuming Wire must be used in loop to avoid errors
  • Thinking missing loop code means no output
  • Confusing compile errors with unused library warnings
4. This Arduino sketch uses two libraries but does not work as expected:
#include <Wire.h>
#include <Adafruit_Sensor.h>

void setup() {
  Wire.begin();
  sensor.begin();
}

void loop() {
  sensor.read();
}

What is the main problem here?
medium
A. Adafruit_Sensor.h does not exist as a library.
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

  1. Step 1: Check object declaration

    The code calls sensor.begin() and sensor.read() but never declares or creates a sensor object.
  2. Step 2: Verify library usage

    Without declaring sensor, the code will not compile or run correctly.
  3. Final Answer:

    The sensor object is not declared before use. -> Option C
  4. 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

  1. Step 1: Properly include and declare libraries

    Both libraries must be included at the top with #include and objects created for Servo and SoftwareSerial.
  2. 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.
  3. Final Answer:

    Include both libraries at the top, create objects for each, initialize in setup(), and use their functions in loop(). -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard