0
0
Arduinoprogramming~10 mins

Using multiple libraries together in Arduino - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Using multiple libraries together
Start Program
Include Library A
Include Library B
Initialize Objects from A and B
Setup: Initialize both libraries
Loop: Use functions from both libraries
Repeat Loop
The program starts by including multiple libraries, then initializes their objects, sets them up, and uses their functions together in the main loop.
Execution Sample
Arduino
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);

void setup() {
  Wire.begin();
  lcd.init();
  lcd.backlight();
}

void loop() {
  lcd.setCursor(0,0);
  lcd.print("Hello");
}
This code uses Wire and LiquidCrystal_I2C libraries together to display 'Hello' on an LCD screen.
Execution Table
StepActionLibrary/FunctionResult/State
1Include Wire libraryWireWire functions available
2Include LiquidCrystal_I2C libraryLiquidCrystal_I2CLCD functions available
3Create lcd objectLiquidCrystal_I2C lcdlcd ready to control LCD
4Call Wire.begin()Wire.begin()I2C bus initialized
5Call lcd.init()lcd.init()LCD initialized
6Call lcd.backlight()lcd.backlight()LCD backlight turned on
7Set cursor to (0,0)lcd.setCursor(0,0)Cursor at first row, first column
8Print 'Hello' on LCDlcd.print("Hello")'Hello' displayed on LCD
9Loop repeatsloop()Display stays on, ready for updates
💡 Loop runs continuously; program does not exit.
Variable Tracker
VariableStartAfter setupAfter loop 1After loop 2
lcdNot createdCreated and initializedCursor at (0,0), 'Hello' displayedCursor at (0,0), 'Hello' displayed
Key Moments - 3 Insights
Why do we need to call Wire.begin() before using the LCD?
Wire.begin() initializes the I2C communication bus that the LCD uses; without it, the LCD library cannot communicate with the hardware. See execution_table step 4.
Can we use functions from both libraries in the same loop?
Yes, after initialization, you can call functions from both libraries anytime, as shown in execution_table steps 7 and 8.
What happens if we forget to call lcd.init()?
The LCD will not initialize properly and may not display anything. This is shown in execution_table step 5 where lcd.init() prepares the LCD.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state after calling Wire.begin()?
ALCD backlight turned on
BI2C bus initialized
CCursor set to (0,0)
DLCD initialized
💡 Hint
Check execution_table step 4 for Wire.begin() result.
At which step is the LCD backlight turned on?
AStep 6
BStep 5
CStep 7
DStep 8
💡 Hint
Look at execution_table for lcd.backlight() call.
If we remove Wire.begin(), what will likely happen?
ALCD will display 'Hello' normally
BProgram will not compile
CLCD will not initialize or display correctly
DBacklight will turn on but no text
💡 Hint
Refer to key_moments about Wire.begin() importance.
Concept Snapshot
Using multiple libraries together:
- Include each library with #include
- Create objects for each library as needed
- Initialize each library in setup()
- Use functions from all libraries in loop()
- Ensure dependencies (like Wire.begin()) are called first
Full Transcript
This example shows how to use two Arduino libraries together: Wire and LiquidCrystal_I2C. First, both libraries are included. Then, an lcd object is created from LiquidCrystal_I2C. In setup(), Wire.begin() initializes the I2C bus, which is required for the LCD to communicate. Then lcd.init() and lcd.backlight() prepare the LCD screen. In the loop(), the cursor is set and the text 'Hello' is printed on the LCD. The loop repeats continuously, keeping the display on. Key points include calling Wire.begin() before using the LCD and initializing each library properly. This allows functions from both libraries to work together smoothly.