Conflict problem between servo.h and LiquidCrystal_I2C.h

Hello.
I created a program in Nano that includes the servo.h and LiquidCrystal_I2C.h (LCD 20X4).
With deactivation. of command //servo.attach(10); the program and the screen work normally.
but.. by enabling the command servo.attach(10); the digital outputs stop working.
As I understand it, the problem is a conflict between libraries.
My question
Are there any other libraries to use LCD IC2 or servo, to stop the collision problem?
Note the servo works with sweep -millis so I can't use servoTimer2.h
Thanks for your response

Please don't post your question in code tags. Makes it harder to read. Just put code or error messages in code tags.

I have used servos and that library together before with no issue.

Please post the code that caused the problem.

Post a wiring diagram, also.

Thank you for your reply.
Unfortunately I can't post the code. Contains personal routines for my own use.
You say "I have used servos and that library together before with no issue..." I would be happy (if you want) to tell me the addresses of the libraries you used. So i can checking with my arduino code. Maybe i use a version library with problem
best regards

Then post a minimal reproducible code that demonstrates the problem.

Include a circuit diagram showing your wiring.

Which Arduino board do you use ?
The libraries work together without problem.

If you use a Arduino Uno and have a SD card, or many text strings without the F() macro or a graphical OLED display or too many String objects or large arrays, then you don't have enough SRAM memory.

I wrote in my first post what I use. I repeat the Arduino nano and
LCD IC2 (20X4).
I only use simple characters and two special characters. I don't think that's the problem.
I just stopped it (D9 exit) when the servo.attack() command fires.
No power supply problem. The servo is connected independently to the lm7805 and two caps

We really do not know enough to be able to give any real help. Best I can do is guess and I do not have time for that.

Post code that demonstrates the problem, in code tags and properly formatted.

Post a schematic or circuit diagram. Written descriptions are always more ambiguous than a drawing. Hand drawn, photographed and posted is fine. Include all pin names/numbers, components, their part numbers and/or values and power supplies.

Post a link to any libraries that did not come with the IDE.

Here is a program using the 20x4 I2C LCD, servo, potentiometer and some custom characters. This works fine on my Uno. I have no idea which of the several LiquidCrystal_I2C libraries that you use. I use the hd44780 library which is just superior to those old libraries anyway. The hd44780 library is available via the IDE library manager.

Turn the pot to change the servo position.

// hd44780 simple "Hello World" by c gounding AKA groundFungus
// hd44780 library see https://github.com/duinoWitchery/hd44780
// thehd44780 library is available through the IDE library manager

#include <Wire.h>
#include <hd44780.h>                       // main hd44780 header
#include <hd44780ioClass/hd44780_I2Cexp.h> // i2c expander i/o class header
#include <Servo.h>

uint8_t bell[8]  = {0x04, 0x0e, 0x0e, 0x0e, 0x1f, 0x00, 0x04, 0x00};
uint8_t note[8]  = {0x02, 0x03, 0x02, 0x0e, 0x1e, 0x0c, 0x00, 0x00};
uint8_t clockface[8] = {0x00, 0x0e, 0x15, 0x17, 0x11, 0x0e, 0x00, 0x00};

const byte potPin = A0;

hd44780_I2Cexp lcd; // declare lcd object: auto locate & auto config expander chip
Servo myservo;

// LCD geometry
const int LCD_COLS = 20;
const int LCD_ROWS = 4;

int val = 90;

void setup()
{
   Serial.begin(115200);
   myservo.attach(10 );
   lcd.begin(LCD_COLS, LCD_ROWS);

   lcd.createChar(0, bell);
   lcd.createChar(1, note);
   lcd.createChar(2, clockface);

   lcd.clear();
   lcd.print("Hello World");
   lcd.setCursor(0, 1);
   lcd.print("Millis ");
   lcd.setCursor(0, 3);
   lcd.print("servo pos  ");
}

void loop()
{
   readPotUpdateServo();
   updateLCD();
}

void readPotUpdateServo()
{
   val = analogRead(potPin);            // reads the value of the potentiometer (value between 0 and 1023)
   val = map(val, 0, 1023, 10, 170);     // scale it to use it with the servo (value between 0 and 180)
   myservo.write(val);
}
void updateLCD()
{
   static unsigned long lcdTimer = 0;
   unsigned long lcdInterval = 500;  // update 2 times per second
   if (millis() - lcdTimer >= lcdInterval)
   {
      lcdTimer = millis();
      lcd.setCursor(8, 1);
      lcd.print("       "); // overwrite old data
      lcd.setCursor(8, 1);  // reset the cursor
      lcd.print(millis());

      lcd.setCursor(10, 3);
      lcd.print("       "); // overwrite old data
      lcd.setCursor(10, 3);  // reset the cursor
      lcd.print(val);

      lcd.setCursor(5, 2);
      lcd.write(0);
      lcd.setCursor(10, 2);
      lcd.write(1);
      lcd.setCursor(15, 2);
      lcd.write(2);      
   }
}

Hi groundFungus
saw what you used and thank you.
I use D10 for my servo. I will do a test. Maybe that's the solution.
Thank you

Here a sample my code
"----------------------------------------------------------------------------------"

#include <PWM.h>
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,20,4);  // -- creating LCD instance

uint8_t bell[8]  = {0x04, 0x0e, 0x0e, 0x0e, 0x1f, 0x00, 0x04, 0x00};
int32_t frequency; 
float factor=10;  
// etc parameters and variables

void setup()
{
  pinMode(buttonPin, INPUT); //push button D3
  pinMode(sensorPin, INPUT); // A0
  pinMode(ledPin,OUTPUT); //led1 D4
  pinMode(5,OUTPUT);  //led2 D5
  pinMode(10,OUTPUT); //servo D10
// D9 output
//if
// servo.attach(10);   servo.attach(10) D3,D4,D5,D9,D10 working perfect
// if 
servo.attach(10); dont working D3,D4,D5,D9,D10
etc
etc
  InitTimersSafe();    
}

void loop()
{
pwmWrite(9, 127);     
  frequency = 562;                                                                                  
  SetPinFrequencySafe(9, frequency*factor);
 ....etc
.... etc
}

"----------------------------------------------------------------------------------"
I did tests with
1.
#include <PWM.h>
#include <Servo.h>
program run fine
2.
#include <PWM.h>
#include <LiquidCrystal_I2C.h>
program run fine
3.
#include <LiquidCrystal_I2C.h>
#include <Servo.h>
program dont working.
So i am sure the problem is a conflict between LiquidCrystal-I2.h and Servo.h
best regards``

Hi groundFungus
I used the libraries you attached in your example.
Problem solved!!!!
Thank you for your help
Best regards
Stathis

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.