Wire library question for Teensy 4.1

Gaztech

Member
Hi,
Quick question (hopefully) regarding the Wire library, I2C and Wire.begin() using a Teensy 4.1.

I'm want to use pins 24 and 25 (which can also function as SCL2 / SDA2) as specific in and out functions in a new sketch. I'm planning to use them as an input and output. But... I want to add a I2C display to pins 19 / 18 (SCL / SDA).

The question is... If I envoke Wire.begin() will it map ALL the SCL / SDA pins (19/18, 16/17, 24,25) for I2C use (as Wire0, Wire1, Wire2)? I don't want it to map pins 24 and 25.

All the documentation I have been looking through seems to suggest this happens. I only wish to set up pins 19 / 18 for I2C - not the others. Or do I simply envoke Wire.begin() then re-map the specific pins? (But I'm not sure how to do that).

Or, can I not use Wire at all and map pins 19 / 18 specifically like you do with switches and LEDs? There doesn't appear to be a way to do that either as far as I can see. I thought that you have to envoke Wire to make I2C work... Am I completely wrong?

I hope this isn't a stupid question but it's not very clear what happens in this instance. I haven't written the sketch yet and am only in the planning stage but I want to clarify this before proceeding blindly into it.

I'm using a lot of inputs so it's not just a case of moving my in / outs to different pins. I need to use pins 24/25 if I can.

Any information would be useful. Thanks.
 
I would have thought that you can (should) use pinMode(??,??); after using Wire.begin().
 
I would have thought that you can (should) use pinMode(??,??); after using Wire.begin().

Yes. I would have thought so. I just wanted someone to clarify this as it really isn't clear... However, how do you specify pinMode for the SCL/SDA pins?
 
Yes. I would have thought so. I just wanted someone to clarify this as it really isn't clear... However, how do you specify pinMode for the SCL/SDA pins?

I2C is only enable when you call Wire, Wire1 or Wire2. So in your case since you want to use pins18/19 as I2c you would call:
Code:
Wire.begin()
which enables I2C on those pins. Then for pins 24/25 you would use pinMode to set up your IO as @BriComp said. That's it.
 
Only the primary 'BOLD' pins will be affected. It takes effort to select the 'ALT' pins and then they would be used instead.
 
This page has the Teensy Wire library documentation.

https://www.pjrc.com/teensy/td_libs_Wire.html

Scroll down to "Pin Configuration"

Don't use pinMode() after Wire.begin(), as it will reconfigure the pin for use with digitalRead() and digitalWrite(), which takes away control from the I2C hardware.

@GazTech was enquiring about using digitalRead() and digitalWrite() on I2C ports other than the one he wished to use. I.e. using the pins of Wire2 for digital IO when using Wire for I2C comms.
 
This page has the Teensy Wire library documentation.

https://www.pjrc.com/teensy/td_libs_Wire.html

Scroll down to "Pin Configuration"

Don't use pinMode() after Wire.begin(), as it will reconfigure the pin for use with digitalRead() and digitalWrite(), which takes away control from the I2C hardware.

So, specifically for my original question I need to call Wire.begin(), followed by pinMode(Pin_24, INPUT_PULLUP), then pinMode(Pin_25, OUTPUT) ??

If I'm right about this I call Wire.begin() to set up pins 18,19 16,17 and 24,25 for I2C and the PinMode commands re-configure pins 24 and 25 for digital output. Yes?

Hopefully inserting pinMode on pins 24 and 25 doesn't affect the other I2C pins which should stay set as I2C.

If this is correct, it's not obvious at all and this should be documented somewhere. The confusion arises as it's implied that Wire.begin() sets up ALL the Teensy's I2C pins (on the 4.1 there are 3 pairs) - even if you don't want to use them all.

I'd appreciate if someone could confirm that it's ok to do this.
 
So, specifically for my original question I need to call Wire.begin(), followed by pinMode(Pin_24, INPUT_PULLUP), then pinMode(Pin_25, OUTPUT) ??

No. For Teensy 4.1, all you need is Wire2.begin(), because pins 24 & 25 are the default pins for Wire2.

After begin, you must use "Wire2", not "Wire", for all communication.

If using a library which does the communication, you either need to tell the library to use Wire2 (many modern libs have a constructor or begin function which tell the library which port to use) or if using an old library with "Wire" hard-coded, you'll need to edit the library source code to change all places with "Wire" to "Wire2".
 
On Teensy 4.1
Wire.begin() .... Configures pins 18 & 19 as I2C (I2C port0)
Wire1.begin() .. Configures pins 16 & 17 as I2C (I2C port1)
Wire2.begin() .. Configures pins 24 & 25 as I2C (I2C port2)

So, to answer your original question, if you are using Wire.begin() (pins 18 & 19) yes you can use as input and output.
 
On Teensy 4.1
Wire.begin() .... Configures pins 18 & 19 as I2C (I2C port0)
Wire1.begin() .. Configures pins 16 & 17 as I2C (I2C port1)
Wire2.begin() .. Configures pins 24 & 25 as I2C (I2C port2)

So, to answer your original question, if you are using Wire.begin() (pins 18 & 19) yes you can use as input and output.

Ah - A light bulb suddenly turned on! We're getting there but we still don't have anything specific - not quite anyway...

To recap: I want to use pins 24/25 for In/Outs and pins 18/19 as I2C. Pins 16/17 - Don't care.

Based on what we're saying here, the code should be:

Wire.begin(); // Configures pins 18 & 19 as I2C (I2C port0)
pinMode(Pin_24, INPUT_PULLUP); // Configures pin 24 as Input with Pullup Resistor
pinMode(Pin_25, OUTPUT); // Configures pin 25 as Output

The thing that wasn't clear is what you put at the start... This is the missing piece.
Wire.begin() .... Configures pins 18 & 19 as I2C (I2C port0)
Wire1.begin() .. Configures pins 16 & 17 as I2C (I2C port1)
Wire2.begin() .. Configures pins 24 & 25 as I2C (I2C port2)

I thought that Wire.begin() configured ALL of the I2C ports. That's what the documentation seems to imply. I guess when that was written the Teensy only had just one I2C port!

If what I have written above is correct, NOW I understand how it works.

Many thanks for all the contributors here. I simply had to get this clarified.
 
No problem.

Sometimes we can get too involved in the nitty gritty without stepping back and looking for simplicity.
 
Quick followup - since this conversation the Wire Library page was updated with a new table showing all the ports and their pins on every Teensy model. The "Pin Configuration" info was updated, and a "Use of Other I2C Ports" section was added.

Hopefully these changes make the documentation easier to understand for use of the other ports and pins.
 
On my screen white lines are extremely faint. See picture below:
z.png
 
Back
Top