Saturday, March 11, 2023

Teensy 4.1 with Small I2C OLED Display

Overview

The Teensy 4.1 can be easily used with small I2C OLED displays. The display requires use of the Adafruit SSD1306 and GFX libraries which can be installed via the Arduino IDE. 


Hardware



The following connections should be made between the Teensy 4.1 and the TMP117:
  • Teensy 4.1 3V to OLED VCC - orange in the above photo
  • Teensy 4.1 ground to OLED GND - black in the above photo
  • Teensy 4.1 pin 19 / A5 / SCL / PWM to OLED SCL - white in the above photo
  • Teensy 4.1 pin 18 / A4 / SDA / PWM to OLED SDA - yellow in the above photo

Software


Example 1 - Data value with graphical representation

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

void setup() {
Serial.begin(115200);

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

display.display();
delay(2000); // Pause for 2 seconds

// Clear the buffer
display.clearDisplay();
display.display();
delay(2000);
display.setTextColor(WHITE);
display.setCursor(0, 0);
display.setTextSize(1);

display.display();
}

void loop() {
for(int i = 0; i < 128; i ++) {
display.clearDisplay();
display.setCursor(0, 0);
display.print("Value: ");
display.print(i);

display.fillRect(0, 16, i, 10, WHITE);

display.display();
delay(100);
}
}


Example 2 - MIDI and Audio Device with FFT analysis of digital audio signal 

#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>

#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

unsigned int count;

Adafruit_SSD1306 display(128, 64, &Wire, -1);

// GUItool: begin automatically generated code
AudioInputAnalog adc1; //xy=365,264
AudioSynthSimpleDrum drum1; //xy=371,346
AudioAnalyzeFFT1024 fft1024_1; //xy=626,304
AudioOutputMQS mqs1; //xy=629,370
AudioConnection patchCord1(drum1, 0, mqs1, 0);
AudioConnection patchCord2(drum1, 0, mqs1, 1);
AudioConnection patchCord3(drum1, fft1024_1);
// GUItool: end automatically generated code



void setup() {
Serial.begin(115200);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;)
; // Don't proceed, loop forever
}
AudioMemory(64);

delay(2000); // Pause for 2 seconds
display.clearDisplay();
display.display();

drum1.frequency(100);
drum1.length(500);
drum1.secondMix(0.5);
drum1.pitchMod(0.7);
drum1.noteOn();

Serial.println("connected");

usbMIDI.setHandleNoteOn(MyNoteOn);
}

void loop() {
usbMIDI.read();

if (fft1024_1.available() == true) {
display.clearDisplay();
for (int i = 0; i < 128; i ++) {
int data = constrain(fft1024_1.read(i) * 256.0, 0, 63);
display.drawLine(i, 32 + (data / 2), i, 32 - (data / 2), WHITE);
}
display.display();
}
}

void MyNoteOn(byte channel, byte note, byte velocity) {
drum1.noteOn();
Serial.println("Note received");
}




0 comments: