Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 25 of 25

Thread: Teensy 3.5 I2C tolerance

  1. #1

    Teensy 3.5 I2C tolerance

    Hi All,

    I did a search for my question but could not find a definitive answer despite posts regarding it.

    Is the 3.5 5v tolerant on the I2C bus? If I put the Teensy on the I2C with other 5v I2C devices will it damage the Teensy. There will not be 5v to any other pins on the Teensy pins, only pins 18 and 19.

    Regards
    Steve

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,674
    Quote Originally Posted by Steevo25 View Post
    Hi All,

    I did a search for my question but could not find a definitive answer despite posts regarding it.

    Is the 3.5 5v tolerant on the I2C bus? If I put the Teensy on the I2C with other 5v I2C devices will it damage the Teensy. There will not be 5v to any other pins on the Teensy pins, only pins 18 and 19.

    Regards
    Steve
    The Teensy 3.5 is tolerant on 5v on the digital pins. It is not 5v tolerant on the analog only pins (A10, A11, A21, A22), nor on the program or reset pins. On analog inputs, anything over 3.3v will register as 3.3v.

    So the narrow question of will attaching 5v devices to i2c and using 5v power for them, will not harm the Teensy. However, the i2c bus may not work properly with a mixture of voltages. Some of the 5v devices might not notice 3.3v signals from the Teensy. Generally, it is better to segment the i2c bus into 3.3v sections, and 5v sections, and use a bi-directional voltage converter to change from 3.3v to 5v. Adafruit, Sparkfun, and Pololu have all made versions of these level converters:


    In the past, I've used both the Pololu and Adafruit versions, and I may have used earlier versions of the Sparkfun. I don't use I2C that much these days, and where I use it, I'm using devices that work with 3.3v voltage, so I haven't bothered using a logical level converter.

    At present Adafruit is closed due to the virus, and I didn't see the Adafruit board at digikey.com. Sparkfun and Pololu seem to be shipping, but with notes that it may take longer than before.

    These converters are somewhat slow in terms of digital speeds. They are fast enough for normal I2C and UART speeds. They are not fast enough for things like WS2812B (neopixel) LEDs that have strict timing windows. I don't know if they are fast enough for the highest I2C and UART speeds.

    If you haven't used I2C on Teensys before, you will need pull-up resistors on each of the SDA and SCL pins (this is a resistor in parallel to the data between the pin and 3.3v). Older systems like the Arduino Uno don't need pull-up resistors, but systems using the ARM microprocessors do. Many devices have pull-up resistors, and if you are using such a device, you don't need extra resistors.

    If none of your I2C devices have pull-up resistors (or you sometimes operate without devices on the I2C bus), you will need to add pull-up resistors to the Teensy. Typically 2.2K is a good value for 3.3v i2c buses (and 4.7K is typically used for 5v I2C buses). The simple way to check is to run the Wire -> Scanner example, and if it hangs, you need pull-up resistors. I vaguely recall that when I was doing mixed voltage I2C setups, I didn't need the pull-up resistors because the voltage level shifter provided the necessary pull-ups.
    Last edited by MichaelMeissner; 03-22-2020 at 04:53 PM.

  3. #3
    The devices I will be using are the Adafruit ADS1015 and the Adafruit MCP4725. The issue is that I need to run those boards at 5v because the DAC has to output up to 5V and the ADC inputs will go up to 5V on the Adafruit boards.

    I have run these in the past at 5V and removed the pull-up resistors on the breakout boards and connected them to a 3.6 (running the Teensy 3.6 at 3.3v and the Adafruit boards at 5v). This worked fine. I am just trying to keep things simple and not have to start adding logic converters, resistor dividers and such like.

    The Adafruit boards seem to run fine at 3.3v logic on the I2C even when powered from 5v. I know the 3.5 is 5v tolerant on the digital pins but wasn’t sure that if 5v appeared at pins 18 and 19 it would damage then Teensy. The Adafruit breakout boards seem to see anything over 2v as a logic high and anything below that as a logic low even when powered at 5v. Last time I damaged one of the Adafruit boards trying to remove the pull-up resistors so I wanted to avoid removing them again. I like the Adafruit stuff but I really wish they would use dip switches or such like for the pull-up resistors so you can select what you want the pull-ups at easily.

  4. #4
    Junior Member
    Join Date
    Mar 2020
    Posts
    5

    Question Why level converters and not just pullups to 5V?

    Quote Originally Posted by MichaelMeissner View Post
    ...
    So the narrow question of will attaching 5v devices to i2c and using 5v power for them, will not harm the Teensy. However, the i2c bus may not work properly with a mixture of voltages. Some of the 5v devices might not notice 3.3v signals from the Teensy. Generally, it is better to segment the i2c bus into 3.3v sections, and 5v sections, and use a bi-directional voltage converter to change from 3.3v to 5v.
    ...
    My understanding: if you put pullups to 5V (not 3.3V) on SCL and SDA, you will get 0V (low) and 5V (high, because the I2C bus works with open collector drivers) in both directions (from and to the Teensy) and it should be
    a) interpreted correctly by the 5V devices AND by the Teensy (5V>3.3V, also high)
    b) not damage the Teensy as those pins are 5V tolerant.

    So why would one need level converters in this case?

    Regards
    Patrick

  5. #5
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,674
    Quote Originally Posted by paf View Post
    My understanding: if you put pullups to 5V (not 3.3V) on SCL and SDA, you will get 0V (low) and 5V (high, because the I2C bus works with open collector drivers) in both directions (from and to the Teensy) and it should be
    a) interpreted correctly by the 5V devices AND by the Teensy (5V>3.3V, also high)
    b) not damage the Teensy as those pins are 5V tolerant.

    So why would one need level converters in this case?

    Regards
    Patrick
    I'll let somebody else with more electronics knowledge answer this.

  6. #6
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,815
    Yes, Teensy 3.5 is 5V tolerant on those pins. So you can use pullup resistors to 5V. You can use 5V I2C chips without a level converter.

  7. #7
    Junior Member
    Join Date
    Mar 2020
    Posts
    5

    Thank you for the information!
    Regards, Patrick

  8. #8
    Junior Member
    Join Date
    Apr 2020
    Posts
    11

    Two I2C 5v devices on a Teensy 3.5

    Hi,

    I'm new on the forum and in the Teensy Community. Let me apologize in advance for my poor english, I'm French.
    I am trying to make an Arduino code work on a Teensy 3.5 and I've got troubles making I2C work. I think it's because my devices are 5V, so I'm very surprised to see you telling 5V devices can be connected to pin 18 (A4) and 19 (SCL) without level converter. I use two devices on the I2C network :
    - A display : 20 columns x 4 lines with I2C converter included
    - An analog to digital converter (used as voltmeter) : Adafruit ADS1115

    My wiring is :
    - On teensy 3.5 SDA0 : 4,7K resistor + SDA from screen + SDA from ADC converter
    - On teensy 3.5 SCL0 : 4,7K resistor + SCL from screen + SCL from ADC converter
    - Ground of every devices to ground
    - +5v on : teensy Vin + screen vcc + ADC converter vdd + ADC chanel 0

    When I try to use them both with this test program, there is no problem, everything works good :
    Code:
    #include <Adafruit_ADS1015.h>
    #include <LiquidCrystal_I2C.h>
    
    #define SERIAL_SPEED 115200
    
    #define SCREEN_ADDRESS 0x27
    #define NB_SCREEN_LINES 4
    #define NB_SCREEN_COLUMNS 20
    
    float VOLTMETER_RANGE = 6.144;
    
    LiquidCrystal_I2C lcd(SCREEN_ADDRESS, NB_SCREEN_COLUMNS, NB_SCREEN_LINES);
    Adafruit_ADS1115 voltmeter;
    
    void setup() {
      Serial.begin(SERIAL_SPEED);
    
      Serial.println("Setting ADC...");
      voltmeter.setGain(GAIN_TWOTHIRDS);
      voltmeter.begin();
    
      Serial.println("Setting screen...");
      lcd.init();
      lcd.backlight();
      lcd.clear();
      lcd.home();
      lcd.print(F("Screen OK"));
      delay(500);
    }
    
    byte line = 0;
    void loop() {
      line = (line == 0) ? 1 : 0;
      lcd.clear();
      lcd.setCursor(0, line);
      lcd.print("Screen test");
      int16_t voltmeter_U_B = voltmeter.readADC_SingleEnded(0); // Reading ADC chanel 0
      float U_B = float(VOLTMETER_RANGE * voltmeter_U_B / pow(2, 15));
      Serial.println(U_B);
      delay(200);
    }
    So far, so good. But when I try it on a much bigger program without changing my wiring, it works like 20 seconds, and then there are unknown characters written on the screen, and then my teensy freezes.
    What is very odd and surprising is that : when I take the screen out of the I2C network, the program is working well ! This make my think that it's a I2C problem…

    If anybody could have a look on the code attached, it may probably help me a lot !
    Note that this code was working well on an Arduino Mega, I just did some modifications to reach Teensy 3.5 requirements.
    I tested my code on a Teensy++ 2.0 (also with some modifications to reach Teensy++ 2.0 requirements, especially with timers problems) and I've got the same problem of freezing.

    cycle_battery.zip

    I'm still trying to see a bug in my code, given that my test code seems not to have any issues.
    Is it possible that it is not a problem with I2C but with anything else ?

    I hope somebody can help me and I hope I am in the right section of the forum.

  9. #9
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    When you say "take the screen out of the I2C network" do you mean that you only disconnected the screen or that you also removed the screen code from the program?
    Either way, I suspect there's more likely to be a bug in your screen code, especially when things work with the simple test code. Getting junk characters on the screen suggests that your larger code is overrunning a buffer/array and is writing junk from past the end of the array.

    I'll look at your code later today.

    Pete
    P.S. this should have been posted as a new topic.

  10. #10
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Yes, when I say that, it's just disconnected the screen, not removing the code.
    In fact, yes it may be a bug. But the teensy executes the same program without considering if the screen is connected or not. Or I missunderstand its way of working ?

    Thank you for having a look to my code. If you need, I can try to translate my comments, would it be easier for you ?

  11. #11
    Junior Member
    Join Date
    Jan 2018
    Posts
    5
    Quote Originally Posted by tilasolfa View Post
    My wiring is :
    - On teensy 3.5 SDA0 : 4,7K resistor + SDA from screen + SDA from ADC converter
    - On teensy 3.5 SCL0 : 4,7K resistor + SCL from screen + SCL from ADC converter
    - Ground of every devices to ground
    - +5v on : teensy Vin + screen vcc + ADC converter vdd + ADC chanel 0
    Are the 4.7K resistors pull-ups? What voltage are they connected to?

  12. #12
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Are the 4.7K resistors pull-ups? What voltage are they connected to?
    Indeed, the 4,7K resistors are pull-up resistors. I tried both +3V3 and +5V and I've got the same behaviour.

  13. #13
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    I have an other information about the bug. The bug occurs only when display is connected but there is more : when I disconnect it, the teensy stops freezing and the program continues to work fine. I’m lost...

  14. #14
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    What has been printed on the serial monitor up to the point that it crashes? Does it always crash at that same place?
    Are the same garbage characters always shown on the screen when it crashes?
    Did it show those same garbage characters on the screen when you tried this on the Teensy++2?


    FYI The Teensy 3.5 is a 3V3 device but all the digital pins are 5V tolerant.
    The Adafruit ADS1115 already has 10k pullups on SDA and SCL. If you use 4.7k pullups, that should be sufficient.

    Pete
    P.S. No need to translate your comments. Your English is very good.
    I asked about the screen connection because I also find it strange that your simple code works even if the display is removed. I would expect that one of the lcd functions would hang if the display wasn't there - but apparently it keeps going anyway.

  15. #15
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    On the serial monitor, when there are ADC + screen, what is written is okay, but it takes too much time for the ADC to calibrate. It takes maybe 5 seconds instead of 1, and it is not accurate.
    I had garbage characters on the screen when my pull-ups resistors where on 3V3. Since they are on 5V, the behavior is better, but not as expected.

    Let me explain :
    The serial don't always shows the whole announcements messages before it freezes. But when I disconnect the screen, the serial restarts from where it has been freezing and work normally.
    Sometimes the serial writes between 4 and 7 lines then it crashes. And again when I disconnect the screen, the serial restarts from where it has been freezing and work normally.
    If the serial has had time for writing some lines before it crashes, there is almost one voltage measure from the ADC which is wrong : 2.5 or 0V instead of 5V.

    I know that only digital pins are 5V tolerant, but I understood from @PaulStoffregen (and I have great respect for him too) that pin 18 and 19 (which are standard pins for SDA and SCL, and are analog pins) are "5V tolerant" if used with 4.7K pull-up resistors for I2C network. And that's what I do since I read him.
    What I did before reading his message was a Wire.setSDA(8) and Wire.setSCL(7) which are digital pins and which I'm sure are 5V tolerant.
    Anyway, I never used anything else that 4.7K resistors on SDA and SCL, regardless of which SDA/SCL port I used and if I used 3V3 or 5V to connect them.

  16. #16
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    Can you take a few close-up photos of how the devices are connected and post them here?

    Pete

  17. #17
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Sure ! Here they are !
    Click image for larger version. 

Name:	IMG_6249.jpg 
Views:	5 
Size:	210.3 KB 
ID:	20049
    Click image for larger version. 

Name:	IMG_6251.jpg 
Views:	4 
Size:	134.0 KB 
ID:	20050
    Click image for larger version. 

Name:	IMG_6253.jpg 
Views:	3 
Size:	130.4 KB 
ID:	20051
    Click image for larger version. 

Name:	IMG_6254.jpg 
Views:	4 
Size:	215.2 KB 
ID:	20053

    Tell me if it is sufficient for you or if you want more. If needed I can do a full schematic view with components I did not connect because of my screen problem.
    If case you need it :
    - the +12V line is supplied by an external power supply
    - the +5V line is supplied by the LM340T5 voltage regulator (itself supplied by the +12V)
    - the 2 mosfets are both IRLD120
    - in the middle, similar to a transistor, it is a 18B20 temperature sensor

    Thank you
    Sylvain
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	IMG_6254.jpg 
Views:	1 
Size:	214.0 KB 
ID:	20052  

  18. #18
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    Yes, post the full circuit diagram.

    One thing to try is to disconnect the wires to pins 3 and 4 of the T3.5 and then run the code again. If I'm reading the code and understand the photos correctly, these two pins are all that drives the 12V side of the circuit. Let's see if the code will run longer than 20 seconds when it can't switch anything on the 12V side of the circuit. Sometimes when mixing 12V and 5V circuits, the switching transients on the 12V side can knock out the 5V side, especially in your case when the 5 volts is being supplied from a regulator which is fed from the 12V.

    Pete

  19. #19
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Ok, I'll provide a circuit diagram as soon as I can. I think you misunderstand it because it is on a breadbord.
    The goal of this project is to make charge an discharge of very big cells of battery. These are 3.3V battery, 90Ah (yes Ah, not mAh).
    Thus, the MOSFETs are here to provide current to big relays (I don't know the correct word for "contacteur" in english) that requires 120mA on their coil. Pin 3 and 4 are here to drive the MOSFETs like would do with a simple 2N2222, so I don't understand how it would change anything to the screen behavior.

    I promise I'll do my best to send a circuit diagram as soon as possible.

    Best regards,
    Sylvain

  20. #20
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    Suddenly switching the relays on or off could cause large transients in the 12V supply which would in turn feed into the 5V supply and that in turn can make the display or even the Teensy3.5 as a whole go crazy. I would just like to make sure that it doesn't happen. If the code runs a lot longer than 20 seconds when pins 3 and 4 are disconnected, it will mean that you need to beef up your 12V supply.

    Pete

  21. #21
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Ok, I didn’t thought about that.
    So I tried it and it is the same.

    Note that it put a freewheeling diode on the coil of my 2 contractors.

    Sylvain

  22. #22
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Hi, here is the schematic you asked for. It took me some time to do it assuming it's the first one I do myself. Please tell me if it lacks of clarity.

    Here are the "real" specific components I use (I didn't find them all in EasyEDA) :
    - +12V power supply : https://tinyurl.com/y8qo74rx
    - Laboratory power supply (Voltcraft PPS-13610) : https://tinyurl.com/yd369a7g
    - ADC module (Adafruit ADS1115) : https://tinyurl.com/js7vaua
    - Current sensor module (ACS712 30A) : https://tinyurl.com/ycvjw6ee (datasheet of the component) and https://tinyurl.com/y9csdnzp (the module I use)
    - 2 contactors (TDK HVC200A-12, it's an overkill I know, but I have not anything better) : https://tinyurl.com/yc5sju3v
    - LiFePO4 battery : https://tinyurl.com/ybhxwn58
    - Discharge 0.150Ω 100W resistor (in fact, 3x 0.05Ω resistor in series) : https://www.vishay.com/docs/50051/lto100.pdf
    - PWM fan (be quiet! Pure wings 2 120mm) : https://www.bequiet.com/fr/casefans/617
    - Freeweeling diode : 1N4007 (1A)
    - Temperature sensor (DS18B20) : https://datasheets.maximintegrated.c...ds/DS18B20.pdf
    - I2C adapter for 20x4 lcd screen (integrated on my screen) : http://ww1.microchip.com/downloads/e.../20001952c.pdf (the IC) and (LCD screen) https://tinyurl.com/yc85ubm8
    - MOSFETs (IRLD120) : https://www.vishay.com/docs/91310/sihld120.pdf
    - Voltage regulator : https://tinyurl.com/yauav9y5
    - Buzzer : no reference but who cares ?

    Every other resistor are classic 0.25W.

    I hope it will help to find what's wrong with my screen.

    PS : Saturday I'll receive a level shifter, I'll try to put the screen on a channel and the ADC on another, who knows…

    Schematic_cyclage_batteries_2020-05-11_20-51-38.pdf

  23. #23
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,311
    One thing that I find strange about your code is that you turn off interrupts for an extraordinarily long time. You turn off interrupts during the entire MesurePuissance() function which does Serial I/O and even writes to the SD card which means that interrupts are off for potentially hundreds of milliseconds. MesurePuissance() is an interrupt routine which means that interrupts will be off anyway but the problem is that this function is doing way too much. An interrupt routine should do as little as possible so that it doesn't block interrupts for any other processes which occur.

    I'd suggest you make these changes to your code:

    > Remove noInterrupts() and interrupts() from the MesurePuissance() function.

    > Before setup() add this code.
    volatile uint8_t MPFlag = 0;
    void setMesurePuissanceFlag(void)
    {
    MPFlag = 1;
    }

    > In setup() change this line to call a new function
    interrupt.begin(setMesurePuissanceFlag, gItPeriodMs * 1000);

    > At the beginning of the loop() function add this code:

    if(MPFlag) {
    MPFlag = 0;
    MesurePuissance();
    }


    The interrupt will then just set a flag byte to indicate that the loop function should execute MesurePuissance.

    Pete

  24. #24
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    Thank you for your ideas, I'll try it this afternoon and I keep you posted.
    I'm not familiar with interrupts, it's the first time I really use them, so I don't really know what are the constraints of using them.
    I'll read about it.

  25. #25
    Junior Member
    Join Date
    Apr 2020
    Posts
    11
    el_supremo, many thanks to you, you solved my problem !
    I've done what you advises me to do and everything is running well.
    I am very grateful to you !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •