Go Down

Topic: Custom library including LiquidCrystal and Wire libraries (Read 1 time) previous topic - next topic


Dear community,

I'm new to the arduino board and have no experience in c++. I'm currently trying to learn how to write a custom library. However, I'm running into some problems when including libraries in my own custom library. I think I'm doing something wrong when calling the LiquidCrystal library. The main question is: How to I call my libraries properly?

What I want to archive:
I have a LCD screen and a I2C device. I want to write a custom library which includes the LiquidCrystal and Wire libraries. Also, these libraries have to be available for use in the main program.

Main program:
Code: [Select]

#include <LiquidCrystal.h>
#include <Wire.h>
#include "customLib.h"

customLib customlib;

void setup()

void loop()
  lcd.setCursor(0, 1);
  lcd.print("I am main program");

customLib header:
Code: [Select]

#ifndef customLib_h
#define customLib_h

#include "Arduino.h"
class Wire;
class LiquidCrystal;

class customLib
    void identify();


customLib cpp file:
Code: [Select]

#include "Arduino.h"
#include "customLib.h"
#include <LiquidCrystal.h>
#include <Wire.h>

LiquidCrystal lcd(4, 5, 6, 7, 8, 9, 10);


void customLib::identify()
  lcd.print("I am customLib");

Note: all this code is stripped down to the bare minimum I needed to describe my problem

When I compile this, it gives the error 'lcd was not declared in this scope'. This makes sense, as I've declared lcd in customLib.cpp. However, when I declare lcd in the main program, it will give the same error. This also makes sense, as I've now declared lcd in the main program, and not in the customLib.cpp. This brings us to the main question, How to I call my libraries properly?

Using the wire library gives no problems. I think this is because it doesn't need to be declared (e.g. no 'Wire wire;') in the code.

Thanks for your time, and I hope you can help me in understanding the Arduino code.

Used websites:



If I understand correctly, I can define lcd in my main program and declare it in customLib? I'm not sure how I would do that. I'm not sure what the data type of lcd is.


Okay, I got it. The data type of lcd is of course LiquidCrystal. So I would define lcd in my main program with
Code: [Select]
LiquidCrystal lcd(4, 5, 6, 7, 8, 9, 10);

and then declare lcd in customLib with
Code: [Select]
extern LiquidCrystal lcd

Thanks for helping me understand a bit more of c++!


Take a look at this LCD library. It does what you seem to be wanting to do
using C++ inheritance and virtual functions.
It separates things into layers and allows a common LCD layer to sit on top
of lower interface layers so you can swap in i2c, shift registers, or run the original 4 bit parallel mode
as well as any other interface.
The interface layer code is quite minimal since the bulk of the code that handles the LCD and API
is up above.


--- bill

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131