Set digital pins hd44780

welcome everybody
I am a beginner with Arduino
I am using a screen and I want to set its pins from the digital pins, for example D2, D3


#include <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina219;
float shuntvoltage = 0;
float busvoltage = 0;
float current_mA = 0;
float loadvoltage = 0;
float power_mW = 0;

#include <SoftwareWire.h> 
const int sda=2, scl=3; 
SoftwareWire Wire(sda,scl); 

#include <hd44780.h>
#include <hd44780ioClass/hd44780_I2Cexp.h> 
hd44780_I2Cexp lcd; 

void setup()
{

  int istatus = lcd.begin(16,4);
  if(istatus)
  {
    lcd.fatalError(istatus); // blinks error on built in LED
  }
  lcd.setCursor(0, 0);
  lcd.print("111111111111111z");
  lcd.setCursor(0, 1);
  lcd.print("222222222222222z");
  lcd.setCursor(0, 2);
  lcd.print("333333333333333z");
  lcd.setCursor(0, 3);
  lcd.print("444444444444444z");

     while(!Serial){delay(1);}
    uint32_t currentFrequency;
    if(!ina219.begin()){
      Set_Err_INA219();
      Check_Coct_INA219 = false;
     // while(1){delay(10);}
    }
}

void loop() { }
SoftwareWire Wire(sda,scl);

Why are you trying to use the SoftwareWire library ?

I don't know, I found the code like this in one of the threads

What is the correct way to set pins?

Set pins to do what ?

Change the default pins to run I2C
I want to connect the monitor to pins D2, D3

This may help ESP32 I2C Communication: Set Pins, Multiple Bus Interfaces and Peripherals | Random Nerd Tutorials

EDIT : apologies for thinking that an ESP32 was being used

1 Like

I'm a beginner and it's a bit ambiguous
Can you help me modify my code

The Adafruit library does not support using SoftwareWire. It is hardcoded to use the Wire library.

When using the hd44780 library and SoftwareWire, you won't ever include <Wire.h> if you are creating a Wire Object - as you have done.
To use SoftWire this way, you include SoftwareWire and create the Wire object before you include the hd44780 header files.

While there is a way to use Wire and SoftwareWire at the same tine,
I2c is a bus, and you can have multiple slaves on it.
I would just remove the SoftwareWire stuff and control both devices on the same i2c bus controlled by the Wire library.

--- bill

1 Like

Yes, that is the problem
I'm a newbie and I don't know how to use (Wire) to set pins
Can you help me modify my code and based on your modification I will try to understand things

@ali_alfoly
please explain:
Which microcontroller are you using?
What makes you believe that you want to use D2 and D3 (?) with SoftWire?

1 Like

It's just a code I got from a thread
All I want is to set screen pins
Use his library (hd44780)
I want to change the default pins with special pins like D2 and D3

Question 1: Why?
Question 2: Which microcontroller are you using?

1 - I need all the analog pins
2 - I2C Arduino Serial Module For LCD

"microcontroller" refers to a board like the Arduino Uno, Nano, Mega, ESP8266, ESP32.
a "I2C Arduino Serial Module for LCD" ... might be a I2C port expander only - that's not a microcontroller.

Again: Which microcontroller are you using?

either
a) chose another microcontroller like an Arduno Mega or an Arduino NANO with more analog pins and use, the desginated pins for I2C
b) use an I2C portexpander/ADC, to expand the analog inputs and use the desginated pins for I2C
c) rewrite the Adafruit Library to make it work with Soft-Wire.

I would go for option A. Even an Arduino Nano might be sufficent.

1 Like

An electronic circuit based on (Arduino Nano China) is designed and D2,D3 is customized for I2C Arduino Serial Module for LCD and it is difficult to modify the hardware
We just want to modify the software to fit the scheme
I found his library LiquidCrystal_Software_I2C I set pins the pins

#include <LiquidCrystal_Software_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4, 2, 3);

Unfortunately, it has a problem in dealing with the screen size 16X4
I decided to replace it with hd44780 library
That's it

and that other LCD library would work for you togehter with the Adafruit INA219 Library at the same time (beside some faults in the last two lines I guess)?

An electronic circuit based on (Arduino Nano China) is designed and D2,D3 is customized for I2C Arduino Serial Module for LCD and it is difficult to modify the hardware
We just want to modify the software to fit the scheme
I found his library LiquidCrystal_Software_I2C I set pins the pins

#include <LiquidCrystal_Software_I2C.h>
LiquidCrystal_I2C lcd(0x27, 16, 4, 2, 3);

Unfortunately, it has a problem in dealing with the screen size 16X4
I decided to replace it with hd44780 library
That's it

There are some technical issues:

  • Adafruit_INA219 is hardcoded to use the Wire library.
  • The Arduino.cc Wire library for AVR based boards that comes bundled with the IDE does not support changing the pins used.

You can run both the INA219 and the lcd on the same i2c bus using the Wire library, but depending on how the pins are mapped, it may not allow you use all the analog pins.

I'm not sure which board you have and how the i2c pins are mapped.
One thing that may help is if you run the I2CexpDiag sketch that is included with the hd44780 library.
It will print out information about your board and the i2c pins used by the Wire library.

--- bill

1 Like

I think you have to change the library.

Here is a project where I changed the library files: https://wokwi.com/arduino/projects/307841659182252608.
I took the "LiquidCrystal_I2C.cpp" and "LiquidCrystal_I2C.cpp" and copied them into my project. I changed the names of the files.
Then I added a pointer to a I2C object to the class of the display.
I changed the code of the display, the pointer to the I2C object is copied in the constructor function and the code uses that pointer for the I2C functions.

class LiquidCrystal_I2C : public Print {
public:
  LiquidCrystal_I2C(SoftwareWire * softwire, uint8_t lcd_Addr,uint8_t lcd_cols,uint8_t lcd_rows);

private:
  SoftwareWire * _swire;
};
	_swire->beginTransmission(_Addr);
	_swire->write((int)(_data) | _backlightval);
	_swire->endTransmission();   

I could not find a LCD library that made it easier.
Perhaps there is a LCD library that already uses its own software implementation for the I2C. If you find such a library, please give a link to it, so I can have a look at it.