pin problems when using Teensy 4.0 + audio adaptor + rotary encoders + OLED

Status
Not open for further replies.

montoyamoraga

New member
hi!

i am using the Teensy 4.0 with the Audio adaptor :)
i have been able to make the display work, and i am adding 2 rotary encoders
the first one, called "navigation" on my code is working,
but the second one, called "volume" is making my audio crackle,
and my guess is that i am using pins that are conflicting with the audio library
right now i am using pins 5, 6 and 9 for the "navigation" encoder
and the pins 21, 22, 23 for the "volume" encoder

also, when i setup pin 23 as digitalRead(INPUT_PULLLUP)
the whole audio stops oops

any guidance about which pins are safe to use with the encoder,
for both the rotary value and the switch would be awesome thanks! :)

Code:
// TODO:
// there is a problem with the switch connection of the encoder volume
// when i setup the pin 23 for input pullup everything stops working, bad stuff

////////////
/// software
////////////

// Teensyduino 1.8.13
// flashed with these settings
// Board: Teensy 4.0
// USB Type: Serial
// CPU Speed: 600 MHz
// Optimize: Faster

///////////
// hardware
///////////

// hardware:
// 1x PJRC Teensy 4.0 https://www.pjrc.com/store/teensy40.html
// 1x PJRC Teensy Audio Adaptor for Teensy 4.x Rev D https://www.pjrc.com/store/teensy3_audio.html
// 1x Adafruit 128x32 OLED display https://www.adafruit.com/product/4440
// 2x Adafruit encoders https://www.adafruit.com/product/377
// 1x SD card, up to 32 GB

/////////////////////////////////////////////////////////
// pins used by Teensy Audio Adaptor for Teensy 4.x Rev D
/////////////////////////////////////////////////////////

// pin 07, 08, 20, 21, 23 for audio data
// pin 18, 19 for audio control
// pin 15 for volume pot
// pin 10, 11, 12, 13 for SD card
// pin 06, 11, 12, 13 for memory chip

/////////
// wiring
/////////

// power buses
// Teensy ground to GND bus
// Teensy 3.3V to V+ bus

// display
// Teensy digital pin 18 (SDA0) to display pin SDA
// Teensy digital pin 19 (SCL0) to display pin SCL
// Teensy digital pin 04 to display pin reset
// 1x ground pin to GND bus
// 1x VIN pin to V+ bus

// encoder navigation
// Teensy digital pin 06 to encoder navigation pin A
// Teensy digital pin 06 to encoder navigation pin B
// Teensy digital pin 09 to encoder navigation switch
// 2x ground pins to GND bus

// encoder volume
// Teensy digital pin 21 to encoder volume pin A
// Teensy digital pin 22 to encoder volume pin B
// Teensy digital pin 23 to encoder volume switch
// 2x ground pins to GND bus

////////////
// libraries
////////////

// library for Audio with Teensy
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>

// libraries for SD card
#include <SD.h>
#include <SerialFlash.h>

// libraries for display
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//
//// library for rotary encoders
//#include <RotaryEncoder.h>

//////////////////
// Teensy GUI tool
//////////////////

// playSdWav1 has two channels, 0 and 1
// its channel 0 goes to amplifier
// this amplifier goes to heapdhones

// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;
AudioOutputI2S           i2s1;
AudioConnection          patchCord1(playSdWav1, 0, i2s1, 0);
AudioConnection          patchCord2(playSdWav1, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;
// GUItool: end automatically generated code

/////////
// define
/////////

// Teensy Audio Shield
#define SDCARD_CS_PIN 10
#define SDCARD_MOSI_PIN 7
#define SDCARD_SCK_PIN 14

// display
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 32
#define SCREEN_ADDRESS 0x3C
#define OLED_RESET 4

//// encoder navigation
#define ENCODER_NAVIGATION_A 5
#define ENCODER_NAVIGATION_B 6
#define ENCODER_NAVIGATION_SWITCH 9
//
//// encoder volume
#define ENCODER_VOLUME_A 21
#define ENCODER_VOLUME_B 22
#define ENCODER_VOLUME_SWITCH 23

//////////////////////////////////
// variables, constants, instances
//////////////////////////////////


//// instances of RotaryEncoder for encoder navigation and encoder volume
//RotaryEncoder encoderNavigation(ENCODER_NAVIGATION_A, ENCODER_NAVIGATION_B,
//                                RotaryEncoder::LatchMode::TWO03);
//RotaryEncoder encoderVolume(ENCODER_VOLUME_A, ENCODER_VOLUME_B,
//                            RotaryEncoder::LatchMode::TWO03);
//

//// instance of display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// setup() runs once, at the beginning
void setup() {

  // begin serial communication
  Serial.begin(9600);

  // setup digital pins for reading switches of encoders
  pinMode(ENCODER_NAVIGATION_SWITCH, INPUT_PULLUP);
  //  pinMode(ENCODER_VOLUME_SWITCH, INPUT_PULLUP);

  // Audio connections require memory to work.  For more
  // detailed information, see the MemoryAndCpuUsage example
  AudioMemory(8);

  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  if (!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
    Serial.println(F("SSD1306 allocation failed"));
    for (;;)
      ; // Don't proceed, loop forever
  }

  display.clearDisplay();
  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(2000); // Pause for 2 seconds
}

// loop() runs after setup(), repeatedly
void loop() {

  playFile("PAG007.WAV");
  delay(500);
}

void playFile(const char *filename) {
  Serial.print("Playing file: ");
  Serial.println(filename);

  // Start playing the file.  This sketch continues to
  // run while the file plays.
  playSdWav1.play(filename);

  // A brief delay for the library read WAV info
  delay(5);

  // Simply wait for the file to finish playing.
  while (playSdWav1.isPlaying()) {

    //    /////////////////////
    //    // encoder navigation
    //    /////////////////////
    //
    //    encoderNavigation.tick();
    //
    //    int encoderNavigationValueNew = encoderNavigation.getPosition();
    //
    //    if (encoderNavigationValue != encoderNavigationValueNew) {
    //      int encoderNavigationDirection = (int)(encoderNavigation.getDirection());
    //      // -1 is clockwise for more, 1 is counterclockwise for less
    //      if (encoderNavigationDirection == -1) {
    //        // Serial.println("mas");
    //        fileCurrent = fileCurrent + 1;
    //      } else if (encoderNavigationDirection == 1) {
    //        // Serial.println("menos");
    //        fileCurrent = fileCurrent - 1;
    //      }
    //      if (fileCurrent < 0) {
    //        fileCurrent = 0;
    //      }
    //      if (fileCurrent > fileMax) {
    //        fileCurrent = fileMax;
    //      }
    //
    //      // update value
    //      encoderNavigationValue = encoderNavigationValueNew;
    //    }
    //
    //    encoderNavigationSwitch = digitalRead(ENCODER_NAVIGATION_SWITCH);
    //    Serial.println(encoderNavigationSwitch);
   
  }
}
 
hi thank you CorBee!
i just realized the difference between the pins for SD card and the ones for Memory Chip,
since i am not using the Memory Chip,
I moved my second encoder to pins 16, 17, 22 and now it's working great thanks :) !!
 
I annotated my T4.0 pin-out card with dots on each of the pins used by the AudioShield
so I don't have to keep pulling up the schematic of the shield. Might be a useful addition to
the cards in the future?
 
Status
Not open for further replies.
Back
Top