Change LCD Pins

2.1k Views Asked by At

I have an Arduino Mega and would like to change the SDA&SCL pins from A4&A5 to A14&A15.

So that I can control an I2C LCD from there, I have the library but I don't see where the pins are set; however, I would imaging they have to be set somewhere...

I am new to c++ and libraries so my eyes may just be skipping over it

3

There are 3 best solutions below

4
On

I2C pins are set in hardware in AVRs; there is no way to change them other than to use a completely different I2C bus, assuming the MCU even has any others in the first place.

If you want to use pins other than those available in hardware then you'll need to find a library that bit-bangs I2C over normal GPIOs, and then modify the LCD library to use that library instead of hardware I2C.

0
On

The standard i2c library uses dedicated hardware, which is tied to certain pins. To send a byte out in this way, your program writes a byte to a certain register (this will take just a few clock cycles), and the hardware takes care of shifting the bits out one by one on the SDA pin and toggling the SCL pin automatically.

What you probably looking for is software i2c, which implements the same protocol in software and should allow you to use arbitrary pins. It is likely that this library is considerably slower and uses more resources than the standard one: When your program wants to send a byte, the library has to extract a single bit, lookup which data pin you had defined, write the bit-value to that pin pin, lookup which pin you had defined for the clock, toggle that pin, wait a bit, toggle the clock again and so on, all in software. This will take a lot of time, but maybe you don't care in your application.

0
On

This question is quite old, but I would like to suggest this LiquidCrystal Software I2C library, because I didn't found many other working resources online and this library implements I2C protocol in software, so you can use any input/output pin of your Arduino.

Just need to specify SDA and SCL pins as 4th and 5th arguments when creating the LiquidCrystal_I2C object and then has the same functions of the standard LiquidCrystal_I2C library.

So, for example, you if you want to use pin 3 and 4, as SDA and SCL respective, your hello word will be:

// https://github.com/francesco-scar/LiquidCrystal_Software_I2C
// Based on https://github.com/johnrickman/LiquidCrystal_I2C project

#include <LiquidCrystal_Software_I2C.h>     // Include library

LiquidCrystal_I2C lcd(0x3f, 16, 2, 3, 4);   // Set the LCD address to 0x27 for a 16 chars and 2 line display

void setup() {
  lcd.init();                               // LCD initialization
  lcd.backlight();                          // Turn on backlight
  lcd.print("Hello, world!");               // Print Hello, world!
}

void loop() {
}