Display of SPI sensor data and I2C display won't work.

For Adafruit customers who seek help with microcontrollers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
HerrFistus
 
Posts: 2
Joined: Mon Oct 25, 2021 12:23 pm

Display of SPI sensor data and I2C display won't work.

Post by HerrFistus »

Hello. For a DIY project, I want to read out temperatures of an electric furnace and display it on a small OLED display. I use a S-type thermocouple with a MAX31856 SPI Module. As Display I bought a SH1106 I²C display. The microcontroller is a Seeeduino Xiao. I tested both components before, both work fine. I put the contents of both example codes together as follows:

Code: Select all

#include <Adafruit_MAX31856.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>

#define i2c_Address 0x3c //initialize with the I2C addr 0x3C Typically eBay OLED's
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1   //   QT-PY / XIAO

Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(3);

double temp=0;

void setup() {
  Serial.begin(115200);
  while (!Serial) delay(10);
  Serial.println("MAX31856 thermocouple test");

  if (!maxthermo.begin()) {
    Serial.println("Could not initialize thermocouple.");
    while (1) delay(10);
  }
  display.setTextSize(2);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0); 

  maxthermo.setThermocoupleType(MAX31856_TCTYPE_S);
  maxthermo.setConversionMode(MAX31856_ONESHOT_NOWAIT);
}

void loop() {
  maxthermo.triggerOneShot();
  delay(200);


  if (maxthermo.conversionComplete()) {
    temp=maxthermo.readThermocoupleTemperature();
  } else {
    Serial.println("Conversion not complete!");
  }    
  display.println(temp);
}
But the program seems to stop working just at the point where I want to print the reading on the display. Changing

Code: Select all

  display.println(temp);
at the end to a serial printin command works again. How can this be possible? Is this maybe a SRAM (32KB) problem?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Display of SPI sensor data and I2C display won't work.

Post by adafruit_support_bill »

You are missing a display.begin() in your setup.

User avatar
HerrFistus
 
Posts: 2
Joined: Mon Oct 25, 2021 12:23 pm

Re: Display of SPI sensor data and I2C display won't work.

Post by HerrFistus »

Thank you, I could make the code run now:

Code: Select all

#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SH110X.h>
#include <Adafruit_MAX31856.h>

#define i2c_Address 0x3c
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
#define LOGO16_GLCD_HEIGHT 16
#define LOGO16_GLCD_WIDTH  16

Adafruit_SH1106G display = Adafruit_SH1106G(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
Adafruit_MAX31856 maxthermo = Adafruit_MAX31856(3);

double temp;

void setup() { 
  Serial.begin(115200);
  if (!maxthermo.begin()) {
    Serial.println("Could not initialize thermocouple.");
    while (1) delay(10);
  }
  display.begin(i2c_Address, true);

  display.clearDisplay();

  display.setTextSize(1);
  display.setTextColor(SH110X_WHITE);
  display.setCursor(0, 0);
  display.println("Temp:");
  display.display();
  display.clearDisplay();
  delay(2000);  
  
  maxthermo.setThermocoupleType(MAX31856_TCTYPE_S);
  maxthermo.setConversionMode(MAX31856_ONESHOT_NOWAIT);
}

void loop() {
  maxthermo.triggerOneShot();
  delay(50);

  if (maxthermo.conversionComplete()) {
    temp=maxthermo.readThermocoupleTemperature();
    display.setCursor(0, 0);
    display.println(temp);
    display.display();
    delay(1000);
    display.clearDisplay();  
    } else {
    //Serial.println("Conversion not complete!");
  }
}
It works fine now. Nevertheless, when I copy this part

Code: Select all

    display.setCursor(0, 0);
    display.println(temp);
    display.display();
    delay(1000);
    display.clearDisplay();  
out of the if/else expression, the displayed value returns zero instead of the reading. Do you know how this can happen?

User avatar
adafruit_support_bill
 
Posts: 88087
Joined: Sat Feb 07, 2009 10:11 am

Re: Display of SPI sensor data and I2C display won't work.

Post by adafruit_support_bill »

If that is all that changed, I'd expect it to display zero until the first conversion was complete. After that it should display the previous value until updated by the next conversion.

Locked
Please be positive and constructive with your questions and comments.

Return to “Microcontrollers”