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

Thread: Teensy 3.2, SSD1306 + Hardware SPI

  1. #1

    Teensy 3.2, SSD1306 + Hardware SPI

    Hi all,

    i've been trying to get hardware SPI to work for my SSD1306 (Adafruit Monochrome 1.3" 128x64 OLED graphic display - STEMMA QT / Qwiic) display on a Teensy 3.2 but with no luck. I've been trawling the posts on this forum and looking for alternative pinouts for the 3.2 but with no luck so far. It doesn't help that pins can have different names. Here is the latest code i used.

    Code:
    #include <SPI.h>
    #include <Wire.h>
    #include <Adafruit_GFX.h>
    #include <Adafruit_SSD1306.h>
    #include <elapsedMillis.h>
    #include <EEPROM.h>
    
    #define SCREEN_WIDTH 128 // OLED display width, in pixels
    #define SCREEN_HEIGHT 64 // OLED display height, in pixels
    
    // Comment out above, uncomment this block to use hardware SPI
    #define OLED_DC     9
    #define OLED_CS     10
    #define OLED_RESET  14
    Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &SPI, OLED_DC, OLED_RESET, OLED_CS);
    
    void setup() {
      // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
      if(!display.begin(SSD1306_SWITCHCAPVCC)) {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
      }
    
      // the code then goes on to use the display the same as when i was using it with I2C
    }
    At the moment i'm just trying to get the hardware SPI working so i can cut down on the time it takes to update a screen. Currently it's 35,000 microseconds (uS) for each update, want to cut that down as much as possible while keeping everything working fine.

    I have issues with the screen giving odd looking results sometimes as i have a clock running (using elapsedMillis) which updates the screen on each step (currently 4 times a second) but when i try updating the screen based on external input as well (buttons, encoders), the two updates interfere with each other even though they are both updated in the same function. No matter how i deal with this issue, i still want hardware SPI working with the screen to see the differences, and i have spare pins available to do it.

    Any advice on the correct pinout or problems i have with my code would be appreciated.

    Cheers

    NM

  2. #2
    p.s. i've just realised that i might need to hook up more than 3 pins from the Teensy 3.2 and the SSD1306. I still have enough free pins on the Teensy 3.2 in my project but am even more confused which other pins i should be using (MOSI, MISO etc)

    Cheers

    NM

  3. #3
    Senior Member
    Join Date
    Feb 2020
    Posts
    139
    Quote Originally Posted by NuttyMonk View Post
    p.s. i've just realised that i might need to hook up more than 3 pins from the Teensy 3.2 and the SSD1306. I still have enough free pins on the Teensy 3.2 in my project but am even more confused which other pins i should be using (MOSI, MISO etc)

    Cheers

    NM
    You need 5 or 4 pins connected
    MOSI
    SCK
    CS
    D/C
    Reset (can be optional, depends on library)

    The SPI hardware will dictate MOSI & SCK. You pick the rest of the pins from your available GPIO and define them in your code.

  4. #4
    Quote Originally Posted by TeensyWolf View Post
    You need 5 or 4 pins connected
    MOSI
    SCK
    CS
    D/C
    Reset (can be optional, depends on library)

    The SPI hardware will dictate MOSI & SCK. You pick the rest of the pins from your available GPIO and define them in your code.
    Am i correct in saying that these are how the pins are labelled on the Teensy 3.2?

    MOSI = DOUT (Pin 11)
    SCK = SCK (Pin 13)

    Can i pick another SCK pin such as pin 14? If so would i have to change my MOSI pin to pin 7? I'd rather not have the LED on pin 13 being used all of the time if possible.

    Are there any pins i should/shouldn't use for the other 3? Are there any conflicts which i should be aware of when picking them?

    Thanks for your help TeenWolf, this issue has been bugging me all day.

    NM

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,183
    Quote Originally Posted by NuttyMonk View Post
    Am i correct in saying that these are how the pins are labelled on the Teensy 3.2?

    MOSI = DOUT (Pin 11)
    SCK = SCK (Pin 13)

    Can i pick another SCK pin such as pin 14? If so would i have to change my MOSI pin to pin 7? I'd rather not have the LED on pin 13 being used all of the time if possible.

    Are there any pins i should/shouldn't use for the other 3? Are there any conflicts which i should be aware of when picking them?

    Thanks for your help TeenWolf, this issue has been bugging me all day.

    NM
    Yes, that is right for pins 11 and 13.
    If there is a way to specify the alternate pin 14 for SCK in the software, then that can be used.
    If you chnage one alternate pin that is independant of other optional ALT pins, so DOUT/MOSI can stay on pin 11.

  6. #6
    Thanks defragster.

    It's good to get confirmation.

    Much appreciated

    NM

  7. #7
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    16,183
    Quick look seems the Adafruit lib doesn't include pin changes.

    In the Teensy SPI library is this:
    Code:
    	// Teensy 3.x can use alternate pins for these 3 SPI signals.
    	void setMOSI(uint8_t pin);
    Do a forum search this went by recently indicating when the pin change should be made for setMOSI() BEFORE the SPI interface begins.

    SPI.H already included so: SPI.setMOSI(14);
    would be first line to add. Not sure if an overt SPI.begin() is needed AFTER that as in the other posts, or if that is part of the SSD1306 library.

  8. #8
    Very interesting.

    Thanks, will check it out.

    NM

  9. #9
    Those pins worked a treat. The display updates in 3.4 milliseconds now. It was 35 milliseconds when i was using I2C, Massive difference.

    Being able to pick the SPI pins will also help when creating the PCB.

    Cheers

    NM

Posting Permissions

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