简体   繁体   中英

Changing Wire library's Hardware I2C pins for custom arduino

I am trying to change the wires i2c pins to 11,12 (SDA, SCL). I built my own PCB but do not have the i2c lines in the same location. I used adafruits feather52 bootloader.

I found online that changing the variant.h file changes the i2c lines, but I don't have that file on my computer (PC), although my mac does.

I have tried the Wire.begin(SDA, SCL) though that's for the esps

I also tried to edit the wire.h files in both the default wire under C/programs/Arduino and the one under user/AppData/Arduino/adafruit though I haven't found where the pins are set.

You can't change the hardware I2C pins. They are hardware . That means that they are physically connected inside the chip to the part that drives the I2C. You can use a software I2C to "bit-bang" your communication. But that won't use the regular wire library.

You have a solution though. I was facing the same problem two years ago, and what I ended up doing was using software I2C. It "Bit Bangs" the I2C protocol on any digital pins of Arduino. There are couple of libraries for that. For example, check this one: https://github.com/Testato/SoftwareWire

Start reading about I2C protocol and try to understand what the library is doing. Then it is possible to simulate the I2C protocol on the GPIO pins you have hardwired

The hardware packages are istalled into the Arduino15 folder. The location of the folder on Windows is in users home directory in C:\\Users\\\\AppData\\Local\\Arduino15\\packages.

The variant file is in variants/feather_nrf52832/variant.h

The nRF52 MCU can use any pair of pins for I2C.

So try to change in variant.h

#define PIN_WIRE_SDA         (25u)
#define PIN_WIRE_SCL         (26u)

to pins you need.

If it works, consider to define your own hardware definition instead of patching the variant.h.

You don't need to change NOTHING to use the Adafruit_SSD1306.h library.

If you're using a generic chinese OLED display, your problem is its address.

By default, the address of the 128x64 is 0x3D and if you have a 128x32 one the address is 0x3C, but chinese displays uses 0x3C on both sizes.

So look up for the line

#define SCREEN_ADDRESS 0x3D ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

and change it for this:

#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32

Then, on your setup function, add Wire.begin(SDA_PIN,SCL_PIN); line replacing SDA_PIN and SCL_PIN with the pins you want to use, adding D before the number, like this:

void setup(){
  Serial.begin(115200);
  Wire.begin(D3,D4); //To use D3 as SDA and D4 as SCL pins, for example.

those are the only changes you should do. Tested on chinese generic display and NODEMCU made by AMICA.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM