Hi,
I'm a little confused as to how to set up my SPI code if using a TFT display (ILI9341 driver) and a MAX31855 (well, four actually) module.
I've been using the MAX31855 (one module) with an ESP32C3 with onboard OLED using the U8G2LIB library and all is working fine, but I want to use a TFT display with Bodmer's library as it's fantastic and have used it on other projects. The problem is though that I can't get my head around how to set up the SPI bit.
I'm not using a specific library to control the MAX31855, and for those interested, here's the code that I'm using that works great on an OLED and i2c:
#include <SPI.h>
#include <U8g2lib.h>
#include <Wire.h>
// Define the pins connected to the MAX31855
#define MAX31855_CS 3 // CS pin
#define MAX31855_MISO 8 // MISO pin
#define MAX31855_SCK 10 // SCK pin
#define SDA_PIN 5 // 5
#define SCL_PIN 6 // 6
char buf[10]; // Array for showing rpm on OLED display
float maxTemp = 0;
uint32_t prevMillis = 0;
uint32_t interval = 1000;
U8G2_SSD1306_72X40_ER_F_HW_I2C u8g2(U8G2_R2, /* reset=*/U8X8_PIN_NONE); // Frame Buffer EastRising 0.42" OLED
// Function to read temperature from MAX31855
double readTemp() {
// Select the MAX31855
digitalWrite(MAX31855_CS, LOW);
delayMicroseconds(1);
// Read the data
byte data[4];
SPI.transfer(data, sizeof(data));
// Deselect the MAX31855
digitalWrite(MAX31855_CS, HIGH);
// Check for thermocouple error
if (data[3] & 0x01) {
Serial.println("Thermocouple Error");
return NAN; // Not a Number
}
// Combine the data
int32_t tempRaw = ((data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]) >> 18;
// Convert to Celsius
return tempRaw * 0.25;
}
void setup() {
// Initialize SPI communication
SPI.begin(MAX31855_SCK, MAX31855_MISO, MAX31855_CS);
Wire.begin(SDA_PIN, SCL_PIN);
Wire.setClock(400000); // choose 400 kHz I2C rate
u8g2.begin();
u8g2.enableUTF8Print();
Serial.begin(115200);
delay(250);
// Set the CS pin as an output
pinMode(MAX31855_CS, OUTPUT);
prevMillis = millis();
}
void loop() {
// Read temperature from MAX31855
double temperature = readTemp();
if (temperature > maxTemp) {
maxTemp = temperature;
}
// Print temperature to serial monitor
// if (!isnan(temperature)) {
// Serial.print("Temperature: ");
// Serial.print(temperature);
// Serial.println(" °C");
// }
if (millis() - prevMillis >= interval) {
prevMillis = millis();
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_logisoso18_tf);
u8g2.setCursor(0, 22);
sprintf(buf, "%5.1f°C", temperature);
u8g2.print(buf);
u8g2.setFont(u8g2_font_helvR10_tf);
u8g2.setCursor(10, 40);
u8g2.printf("%5.1f°C", maxTemp);
u8g2.sendBuffer(); // transfer internal memory to the display
}
delay(100); // Wait for 100ms
}
Now I want to use an ESP32 S3 zero board.
I set some pins for the SPI in the User_Setup.h file and ran a couple of the TFT_eSPI examples on the S3 board and all works fine. It's just that I don't know how to set up the pins to also use the MAX31855 modules.
I understand that the chip select pins need to be all different for each device, but do I just use the MISO and SCK pins as defined in the User_Setup.h file for the MAX31855? I read a few things of people saying that they have issues and had to set a specific SCK pin(s). That was mostly specific to the MAX6675 module, but they work pretty much the same as the 31855 so I presume that what works for the 6675 will work for the 31855. However, I can't find any specific code for how to do this.
If anyone can assist I would be super grateful as I don't really want to have to stick with an OLED display purely due to my incompetence! ![]()
Fingers crossed that the answer to this puzzle is not too complicated.
If more info is required then please let me know.
Cheers,
Matt