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

Thread: Oled i2c

  1. #1
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13

    Oled i2c

    Hello,
    I have Teensy 4.0 and I want to connect oled I2C, 4-pin, VCC, GND, SCK (pin 13?), SDA (pin 18?). I use library SSD1306. But no success. Could you help me please?
    Thanks a lot.

  2. #2
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    Hi Frohr,
    The forum encourages you to "post the complete source code & details to reproduce any issue!" [see top of this page]
    This normally helps a lot.
    With reference to your question: if you're using I2C, pin 19 is the I2C CLK [SCL0]. Pin 18 is the I2C DATA [SDA0].
    Pin 13 is the SPI clock.

    Paul

  3. #3
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    8,195
    Those are not valid I2C pins.

    Click image for larger version. 

Name:	T4-Cardlike.jpg 
Views:	26 
Size:	165.8 KB 
ID:	22300

    The main Wire Pins on T4 are: SCL 19(A5) and SDA 18(A4)

    There is a second Wire Bus Wire1 SCL 16 and SDA 17, but that would require you to use the Wire1 object instead of Wire. Not sure which library you are using to know if they already added support for a
    different wire object or not, or if you need to do something like make copy of library and change all Wire to Wire1

  4. #4
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    I tried this code - https://www.pjrc.com/teensy/td_libs_SSD1306.html
    On oled display is SCK, not SCL...

    Main problem for me is:
    How define Pins - or are there any difficulties with this type of Oled?

  5. #5
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    SCK == SCL , SClocK or SCLock.
    But what OLED display are you using exactly? We need details to help you...

    Paul

  6. #6
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    Its this display (sorry it is in Czech language but datasheet and code is clear I hope)

    https://arduino-shop.cz/docs/produkt...1487765029.pdf

    https://navody.arduino-shop.cz/navod...j-ssd1306.html

    I tried 2IC on Arduino Mega and it works well.
    I tried SPI Oled (7 pins) on Teensy with U8glib.h library and it works.
    I tried 2IC on Teensy with U8glib.h (SSD1306) and it is problem.

  7. #7
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    Datasheet is clear. You need to connect SCK [a.k.a. SCL] of the display to pin 19 and SDA to pin 18.
    Just to check: what do you mean by "it is problem"? No image? Or corrupted image or...? Do you see any errors/warning during compilation of the code?
    By the way, can you post your exact code that is the problem?

    Paul

  8. #8
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    I can see just black display.

    I will try use pins 18 and 19 and if there will be still problem I will post my code.
    Thanks a lot for now, I let you know evening.

  9. #9
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,929
    Quote Originally Posted by frohr View Post
    Its this display (sorry it is in Czech language but datasheet and code is clear I hope)

    https://arduino-shop.cz/docs/produkt...1487765029.pdf

    https://navody.arduino-shop.cz/navod...j-ssd1306.html

    I tried 2IC on Arduino Mega and it works well.
    I tried SPI Oled (7 pins) on Teensy with U8glib.h library and it works.
    I tried 2IC on Teensy with U8glib.h (SSD1306) and it is problem.
    Assuming you have the correct pinout for I2C (pin 18 for SDA, pin 19 for SCL), ground, and VCC/3.3v, there are two common issues you might be running into:

    The first issue is I2C buses on the Teensy need 2 pull-up resistors (one between SDA and 3.3v and the other between SCL and 3.3v). Older AVR processors like the Mega did not need pull-up resistors, but ARM processors like the Teensy or the newer Arduino platforms need pull-up resistors. Many newer devices include pull-up resistors in the device, but not all devices do, especially the older devices. Note, if you have multiple devices on an I2C, each with a pull-up resistor, the combined resistance may mean you can't use the fastest I2C modes (but you should be able to use the slower standard mode).

    A pull-up resistor is a resistor that is wired in parallel to the data. I.e. one end of the resistor is connected to the data pin (18 or 19 in this case) and the other to normal voltage (3.3v). The data connection is also connected to the pin. For 3.3v systems like Teensy, a common value for the resistor is 2.2K ohms. For 5.0v systems, 4.7K is a common resistor value. Teensys will work with 4.7K resistors.

    The way to see if you need pull-up resistors is to run the Examples -> Wire -> I2C scanner with the device connected. If you see the OLED monitor in the list of devices connected, than it means you don't need pull-up resistors. If the scanner hangs or has errors, you will may need to add pull-up resistors (or check your wiring).

    The other issue is voltage. Some displays need 5v to run, some are happy with 3.3v or 5v. If you are mixing voltages (5v for VCC, 3.3v signals for SCL/SDA), you may or may not run into issues. Most new devices will automatically convert the SDA/SCL output to 3.3v so it is safe to use on 3.3v devices. Older devices might use the 5v power on the I2C bus. With the exception of the Teensy 3.1/3.2 and 3.5, all of the ARM Teensys might be destroyed if they receive 5v input signals. If you have a mixed voltage I2C bus setup,you may need to use bi-directional voltage level shifters.

    So the simplest pinout is to try 3.3v connected to VDD, instead of VIN. That way the I2C bus is all 3.3v.

    I have run into older displays as I moved from an Arduino Uno to Teensy where the display needed 5v to be able to display anything. If you used 3.3v and hooked up the scanner sketch, it would show the device being on the I2C bus, but you wouldn't be able to see anything until the display was fed 5v.

    Depending on the device, where you are located, your budget, etc. sometimes it is simpler to get a new device that works with 3.3v directly and has pull-up resistors then to do the trouble-shooting.

    One other thing about voltage -- the 3.3v pins can deliver roughly 250mA of power. The Teensy 4.x generally uses 100mA of power. In theory, you should have enough power for 1 display, but if you hook up multiple displays, you might find the displays consume too much power.

    Good luck.

  10. #10
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    There could be a problem with the U8glib library on Teensy 4.0 - I get warnings while compiling.
    With the code from this page:
    Code:
    // OLED displej přes I2C
    // řadič SSD1306
    
    // připojení knihovny U8glib
    #include "U8glib.h"
    
    // inicializace OLED displeje z knihovny U8glib
    U8GLIB_SSD1306_128X64 mujOled(U8G_I2C_OPT_NONE);
    
    // proměnná pro uchování času poslední obnovy displeje
    long int prepis = 0;
    
    void setup(void) {
      // pro otočení displeje o 180 stupňů
      // stačí odkomentovat řádek níže
      // mujOled.setRot180();
    }
    
    void loop(void) {
      // porovnání uloženého a aktuálního času
      // při rozdílu větším než 100 ms se provede
      // obnovení displeje, čas můžeme nastavit dle potřeby
      if (millis()-prepis > 100) {
        // následující skupina příkazů
        // obnoví obsah OLED displeje
        mujOled.firstPage();
        do {
          // funkce vykresli vykreslí žádaný obsah
          vykresli();
        } while( mujOled.nextPage() );
        // uložení posledního času obnovení
        prepis = millis();
      }
      
      // zde je místo pro další příkazy pro Arduino
      
      // volitelná pauza 10 ms pro demonstraci
      // vykonání dalších příkazů
      delay(10);
    }
    // funkce vykresli pro nastavení výpisu informací na OLED
    void vykresli(void) {
      // nastavení písma, toto písmo umožní vypsat
      // přibližně 15x4 znaků
      mujOled.setFont(u8g_font_unifont);
      // nastavení pozice výpisu v pixelech
      // souřadnice jsou ve tvaru x, y
      // souřadnice 0, 0 je v levém horní rohu
      // OLED displeje, maximum je 128, 64
      mujOled.setPrintPos(0, 10);
      // výpis textu na zadanou souřadnici
      mujOled.print("Arduino navody");
      mujOled.setPrintPos(0, 25);
      mujOled.print("arduino-shop.cz");
      mujOled.setPrintPos(0, 40);
      mujOled.print("Cas od zapnuti:");
      mujOled.setPrintPos(40, 55);
      mujOled.print(millis()/1000);
      mujOled.print(" vterin");
    }
    I get these warnings during compilation

    In file included from C:\Users\Paul\Documents\Arduino\libraries\U8glib\s rc/U8glib.h:42:0,
    from C:\Users\Paul\Documents\Arduino\sketch_nov03a\sket ch_nov03a.ino:5:
    C:\Users\Paul\Documents\Arduino\libraries\U8glib\s rc/clib/u8g.h:142:0: warning: "PROGMEM" redefined
    # define PROGMEM
    ^
    In file included from C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/WProgram.h:41:0,
    from C:\Users\Paul\AppData\Local\Temp\arduino_build_996 591\pch\Arduino.h:6:
    C:\Program Files (x86)\Arduino\hardware\teensy\avr\cores\teensy4/avr/pgmspace.h:30:0: note: this is the location of the previous definition
    #define PROGMEM __attribute__((section(".progmem")))
    ^
    and the display stays blank after uploading.
    [I hooked up a similar 128x64 I2C OLED display to a Teensy 4.0, using pin 18 & 19].

    This code however does run on the Teensy 4 and my display:

    Code:
    #include <Wire.h>               // SCL pin 19, SDA pin 18
    #include <Adafruit_GFX.h>      
    #include <Adafruit_SSD1306.h>   
    
    Adafruit_SSD1306 display(128, 64, &Wire, -1, 1000000);  // 1MHz I2C clock
    
    void setup()
    {
      display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
      delay(100);
      display.clearDisplay();
      display.display();
    }
    
    void loop()
    {
      display.clearDisplay(); // clear buffer
      for (int r = 0; r < 9; r++)
      {
        for (int y = 0; y < 8; y++) // fill buffer completely withs chars
        {
          display.setTextSize(1);
          display.setTextColor(1);
          display.setCursor(0, (y * 8));
          display.print("012345678901234567890");
        }
        display.setTextSize(1); // erase one line at the time
        display.setTextColor(0);
        display.setCursor(0, (r * 8));
        display.print("012345678901234567890");
    
        display.display();
        delay(200);
      }
    }
    Click image for larger version. 

Name:	T4.0_OLED.jpg 
Views:	17 
Size:	112.4 KB 
ID:	22301

    Paul

  11. #11
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    Quote Originally Posted by frohr View Post
    I tried this code - https://www.pjrc.com/teensy/td_libs_SSD1306.html
    On oled display is SCK, not SCL...
    By the way, the example code on that page is for an SPI OLED not for an I2C OLED...

    Paul

  12. #12
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    Hey ALL,
    thanks a lot for you great help. Now it works :-)
    I use PIN 18 and 19 and I do not need pull-up resistors.
    PaulS code works great.
    Thanks a lot again and have a great evening.

    Michal Fröhr


    Click image for larger version. 

Name:	IMG_20201103_190927.jpg 
Views:	26 
Size:	239.4 KB 
ID:	22303

  13. #13
    Senior Member
    Join Date
    Jul 2020
    Posts
    557
    Quote Originally Posted by MichaelMeissner View Post
    Older AVR processors like the Mega did not need pull-up resistors, but ARM processors like the Teensy or the newer Arduino platforms need pull-up resistors.
    AVR microcontroller internal pullups are too weak for I2C (50k worst case), you definitely need pull up resistors for them too.

  14. #14
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    3,929
    Quote Originally Posted by MarkT View Post
    AVR microcontroller internal pullups are too weak for I2C (50k worst case), you definitely need pull up resistors for them too.
    Back in the day, when I used Arduino Uno's, I did not need external pull-up resistors on the I2C bus. When I moved over to Teensy 3.0, I needed them. Likewise when I used some of the Adafruit SAMD processors without having a device with pull-up resistors attached, I generally needed to add them also.

    Whether it was due to resistors on the boards, or some thing the library set, I don't know. Perhaps it works with the 328P (Uno) but possibly not with the 32u4 (Leonardo) or whatever the Mega uses. Since I haven't powered on my Unos in several years, it is an academic exercise at this point about what the Arduino AVR boards did or did not do.

  15. #15
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    I wouldn't be surprised if Michal's OLED came with pullup resistors mounted. Can't tell from the datasheet though.
    My OLED has 10K onboard pullups on the SDA & SCL lines.

    Paul

  16. #16
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    This is my OLED:

    Click image for larger version. 

Name:	oled.jpg 
Views:	27 
Size:	182.8 KB 
ID:	22304

  17. #17
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    Looks like 10K pullup resistors are present [R1 & R2].

    Paul

  18. #18
    Junior Member
    Join Date
    Nov 2020
    Location
    Czech Republic
    Posts
    13
    Am I right? (without pull-up)

    Click image for larger version. 

Name:	OLED-withOUT-pullup.jpg 
Views:	18 
Size:	93.1 KB 
ID:	22305

    Click image for larger version. 

Name:	OLED-with-pullup.jpg 
Views:	12 
Size:	85.9 KB 
ID:	22306

  19. #19
    Senior Member PaulS's Avatar
    Join Date
    Apr 2015
    Location
    Netherlands
    Posts
    411
    You are right!

    Paul

Posting Permissions

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