Using multiple libraries together in Arduino - Time & Space Complexity
When using multiple libraries in Arduino, it's important to understand how the program's running time changes as more libraries are added or used together.
We want to know how the total work grows when combining different library functions.
Analyze the time complexity of the following code snippet.
#include <Wire.h>
#include <SPI.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Wire.begin();
SPI.begin();
lcd.init();
lcd.backlight();
}
void loop() {
lcd.setCursor(0, 0);
lcd.print("Hello");
delay(1000);
}
This code uses three libraries to control hardware: Wire for I2C, SPI for SPI communication, and LiquidCrystal_I2C for the LCD display.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The loop() function runs repeatedly, calling lcd.print() each time.
- How many times: The loop runs forever, repeating the print operation every second.
Here, the input size is not a variable like an array length, but the number of library calls and their internal operations.
| Input Size (number of library calls) | Approx. Operations |
|---|---|
| 1 | Small number of operations for one library call |
| 3 | Sum of operations from all three libraries |
| 10 | Sum grows roughly linearly with number of calls |
Pattern observation: The total work grows roughly in a straight line as more library calls are added.
Time Complexity: O(n)
This means the total running time grows linearly with the number of library calls made in the program.
[X] Wrong: "Adding more libraries does not affect the program speed because they run independently."
[OK] Correct: Each library call adds work that the microcontroller must do, so more libraries or calls increase total execution time.
Understanding how multiple libraries affect program speed helps you write efficient Arduino code and shows you can think about how different parts of a program work together.
"What if we replaced the lcd.print() call with a function that updates multiple sensors? How would the time complexity change?"
Practice
Solution
Step 1: Understand library inclusion syntax
Arduino uses#includeto add libraries at the top of the sketch.Step 2: Confirm multiple libraries usage
You can add multiple#includelines, one for each library needed.Final Answer:
Use multiple #include statements at the top of the sketch. -> Option AQuick Check:
Multiple #include lines = correct way [OK]
- Trying to include libraries inside functions
- Using #import instead of #include
- Combining all libraries into one include
Wire and LiquidCrystal libraries in an Arduino sketch?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 BQuick Check:
#include with angle brackets and .h = correct [OK]
- Using #import instead of #include
- Omitting the .h extension
- Using double quotes instead of angle brackets
#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?
Solution
Step 1: Analyze library initialization
Wire library is started withWire.begin()in setup; LiquidCrystal is initialized and prints "Hi".Step 2: Check output on LCD
Sincelcd.print("Hi")runs in setup, "Hi" appears on the LCD screen.Final Answer:
The text "Hi" on the LCD screen. -> Option DQuick Check:
lcd.print("Hi") = "Hi" shown [OK]
- Assuming Wire must be used in loop to avoid errors
- Thinking missing loop code means no output
- Confusing compile errors with unused library warnings
#include <Wire.h>
#include <Adafruit_Sensor.h>
void setup() {
Wire.begin();
sensor.begin();
}
void loop() {
sensor.read();
}What is the main problem here?
Solution
Step 1: Check object declaration
The code callssensor.begin()andsensor.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 CQuick Check:
Objects must be declared before calling methods [OK]
- Thinking Wire.begin() must be in loop
- Assuming header file alone creates objects
- Ignoring missing object declarations
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?Solution
Step 1: Properly include and declare libraries
Both libraries must be included at the top with#includeand objects created for Servo and SoftwareSerial.Step 2: Initialize and use in setup() and loop()
Initialize both insetup()and call their functions as needed inloop()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 AQuick Check:
Include, declare, initialize, then use both libraries [OK]
- Trying to include libraries inside functions
- Assuming one library can call another's functions
- Delaying includes until inside setup or loop
Ready to prove it?
Pass the test and this pattern moves unknown
