Temperature Monitor issues when combined with Serial communication

I am trying to build a 4 channel temperature monitor using the MAX6675 module with an I2C interface to an LCD screen. I am using an arduino UNO to read the temperature values from multiple MAX6675 modules and flusing that information the LCD after averaging over multiple readout cycles.

For similar examples, see:

I would like to add a serial interface to this to be able to query the temperature values when needed. However, when I connect UNO to a PC I can only see temperature values for 2 channels with rest of the channels showing NaN. This not a problem when I power it using an USB power adapter or usb charging connectors.

How can I have both a I2C interface to see the values flushed to the screen as well as query the temperature values from a PC?

Make a hand-drawn wiring diagram and post a picture of it. Clearly label non-obvious parts. Post all your code auto-formatted in code tags. Post any relevant serial output. Now we have a clue.

1 Like

So how do you have the PC connected if you have a USB power adaptor plugged into the USB port?

1 Like

No, I either power it up by connecting the USB-A to B connection to a PC or a USB power adapter. I can see the temperature reading on all the 6-8 channels on my LCD screen when it is connected to the power adapter but not when connected to the PC’s USB port.

I’ve attached the code I’m using for my temeperature monitor here.

Physically, the connections are as per the code. All the MAX6675 sensors have common clock (CLK) and serial out (SO) lines, with unique chip select (CS) lines to the microcontroller. The MCU does a serial read out from these slave devices in a loop.

temperature_mointor.ino (2.8 KB)

Please post it here in a reply to this topic, using code tags when you do

In my experience the easiest way to tidy up the code and add the code tags is as follows

Start by tidying up your code by using Tools/Auto Format in the IDE to make it easier to read. Then use Edit/Copy for Forum and paste what was copied in a new reply. Code tags will have been added to the code to make it easy to read in the forum thus making it easier to provide help.

Posting it here avoids the need to download it

#include <LiquidCrystal_I2C.h>
#include "max6675.h"


const int clkPin = 10;  // Serial Clock (SCK) pin
const int soPin = 11;   // Serial out (SO) pin
// array of chip select (CS) pins
const int csPins[8] = { 2, 3, 4, 5, 6, 7, 8, 9 };
const int cursor_x[8] = { 0, 0, 0, 0, 11, 11, 11, 11 };
const int cursor_y[8] = { 0, 1, 2, 3, 0, 1, 2, 3 };
// array to store temperatures
double temp[8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
char channels[8] = "ABCDEFGH";
char temp_buffer[4];  // buffer to store formatted temperature string

// array to store counts of valid temperatures in each averaging cycle.
int counts[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
// array of MAX6675 thermocouple instances
MAX6675 thermocouple[8] = {
  MAX6675(clkPin, csPins[0], soPin),
  MAX6675(clkPin, csPins[1], soPin),
  MAX6675(clkPin, csPins[2], soPin),
  MAX6675(clkPin, csPins[3], soPin),
  MAX6675(clkPin, csPins[4], soPin),
  MAX6675(clkPin, csPins[5], soPin),
  MAX6675(clkPin, csPins[6], soPin),
  MAX6675(clkPin, csPins[7], soPin),

};

// A function to read and average temperature.
void read_temp() {
  double t = 0.0;
  for (int j = 0; j < 100; j++) {
    for (int i = 0; i < 8; i++) {
      // Note: Each thermocouple read-out takes 16 clock cycles for MAX6675 as
      //
      t = thermocouple[i].readCelsius();
      if (t == NAN) {
        continue;
      } else {
        temp[i] += t;
        counts[i] += 1;
      }
    };
    delay(10);
  };
  for (int i = 0; i < 8; i++) {
    temp[i] = temp[i] / counts[i];
  };
};

void reset_temp() {
  for (int i = 0; i < 8; i++) {
    temp[i] = 0.0;
    counts[i] = 0;
  };
};


// set LCD address, number of columns and rows
// run an I2C scanner sketch to find the address, if you don't know the.
LiquidCrystal_I2C lcd(0x27, 20, 4);  // 16 character, 2 row display
void setup() {
  // initialize LCD
  lcd.init();
  // turn on LCD backlight
  lcd.backlight();
  // wait for the LCD to turn on
  delay(500);
}

void flush_output_to_LCD() {
  // clear the LCD screen before flush any new output
  lcd.clear();
  for (int i = 0; i < 8; i++) {
    lcd.setCursor(cursor_x[i], cursor_y[i]);
    lcd.print(channels[i]);
    lcd.print(":");
    // print the sign of the temperature
    if (temp[i] > 0) {
      lcd.print("+");
    } 
    else {
      lcd.print("+");
    };
    //format the temperature to 4 decimal places
    dtostrf(abs(temp[i]), 4, 0, temp_buffer);
    // Serial.begin(9600);
    // Serial.print(thermocouple[i].readCelsius());
    // Serial.print("\n");
    // Serial.flush();
    lcd.print(temp_buffer);
    // lcd.print(temp[i],0)
    lcd.print((char)223);
    lcd.print("C");
  }
}


void loop() {
  // Read and average temperature
  read_temp();
  // Flush the output to the LCD screen
  flush_output_to_LCD();
  // Reset the temperatures back to zero
  reset_temp();
  delay(500);
}

Thank you

Serial.begin(9600) goes in setup()

Why are you reading the thermocouple again rather that printing temp[i]?

Serial.begin belongs in setup(), not in loop(). It resets buffers and that is more than likely the cause of your problem.

Oh, that’s a stupid mistake. But, even with this commented out. I can only see readout on 4 channels on the LCD screen. After the moving Serial.begin to setup(), I have the same issue on my serial monitor. I can only see temperatures of 4 channels and the rest are just 0s or NAN as my code writes out 0s.

Are you printing temp[i]?

Are you using the Adafruit library?

Yes, I am using the Adafruit library and moved to printing temp[i].

I’ve added the following snippet before I reset the temperatures.

  if (Serial.available() >0){
      String command = Serial.readStringUntil("\n");
      command.trim();
      if (command =="GET_TEMP"){
        for (int i = 0; i < 8; i++) {
          Serial.print(channels[i]);
          Serial.print(abs(temp[i]));
          Serial.print("\n");
          Serial.flush();
        }
      }
      else {
      Serial.println("Unkown command.");
      }

    }

Adding more code doesn't help in debugging the problem.
First you need to get all 8 MAX6675 working with the LCD when you have usb connected.

Delete any code that has to do with Serial. Are you still only reading 4 thermocouples?

Yes, I can only read 4 thermocouples. I am beginning to wonder if this has something to do with the CH340 interface the arduino UNO board has.

No. That is just a USB to serial bridge and could not interfere with the thermocouples or LCD.

If you get NaN, that means a thermocouple is disconneced, so I think you may have a hardware problem.

Are you using the Adafruit boards?

but your code read 8
Do you actually have 8 connected?

Yes, I have 8 channels connected.

Then you have some hardware issue somewhere.
As I said, that library reports NaN if a thermocouple is disconnected.

Do they all say NaN?

I get this error when I compile your code:

C:\Temp\arduino_modified_sketch_241483\sketch_sep01a.ino:13:20: warning: initializer-string for array of chars is too long [-fpermissive]
char channels[8] = "ABCDEFGH";
^~~~~~~~~~

channels size should be 9 not 8