MCP9600 not changing thermocouple type setting

Breakout boards, sensors, other Adafruit kits, etc.

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
BShmid
 
Posts: 13
Joined: Wed Nov 24, 2021 6:33 am

MCP9600 not changing thermocouple type setting

Post by BShmid »

Hello
I'm running 4 MCP9600s on ESP32 and experience a problem with setting the TK type.
Here is part of the code:

Code: Select all

for (i = 0; i < 4; i++){
    myMcps[i].setThermocoupleType(MCP9600_TYPE_K);
    Serial.print("Thermocouple ");
    Serial.print(i);
    Serial.print(" type set to ");
    switch (myMcps[i].getThermocoupleType()) {
      case MCP9600_TYPE_K:  Serial.print("K"); break;
      case MCP9600_TYPE_J:  Serial.print("J"); break;
      case MCP9600_TYPE_T:  Serial.print("T"); break;
      case MCP9600_TYPE_N:  Serial.print("N"); break;
      case MCP9600_TYPE_S:  Serial.print("S"); break;
      case MCP9600_TYPE_E:  Serial.print("E"); break;
      case MCP9600_TYPE_B:  Serial.print("B"); break;
      case MCP9600_TYPE_R:  Serial.print("R"); break;
    }
    Serial.println(" type");}
and this is the output:
12:40:21.184 -> Thermocouple 0 type set to K type
12:40:21.184 -> Thermocouple 1 type set to K type
12:40:21.184 -> Thermocouple 2 type set to S type
12:40:21.278 -> Thermocouple 4 type set to R type
S.O.S.
P.S. I also have no idea how did 'i' hit 4.

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP9600 not changing thermocouple type setting

Post by adafruit_support_carter »

Can you post full code.

User avatar
BShmid
 
Posts: 13
Joined: Wed Nov 24, 2021 6:33 am

Re: MCP9600 not changing thermocouple type setting

Post by BShmid »

Note: dealing with custom hardwere.
Here is the code:

Code: Select all

/**
   A BLE client example that is rich in capabilities.
   There is a lot new capabilities implemented.
   author unknown
   updated by chegewara
*/

#include "BLEDevice.h"
#include <Ticker.h>
#include <Wire.h>
#include <Adafruit_I2CDevice.h>
#include <Adafruit_I2CRegister.h>
#include "Adafruit_MCP9600.h"
#include <bitset>

const int ADDRESSES[] = {0x60, 0x61, 0x62, 0x63};
Adafruit_MCP9600 myMcps[4];
// The remote service we wish to connect to.
static BLEUUID serviceUUID("4fafc201-1fb5-459e-8fcc-c5c9c331914b");
// The characteristic of the remote service we are interested in.
static BLEUUID    charUUID("beb5483e-36e1-4688-b7f5-ea07361b26a8");

static boolean doConnect = false;
static boolean connected = false;
static boolean doScan = false;
static BLERemoteCharacteristic* pRemoteCharacteristic;
static BLEAdvertisedDevice* myDevice;
int TKReadings[4],i,j;
String newValue, package;
bool flag = 0;
Ticker sensor;
float updateRate = 0.02;  //seconds
void updateTemperature () {
  if (!flag) {   
    for (i = 0; i < 4; i++){
      TKReadings[i] = myMcps[i].readThermocouple();//this float gives 2 digits after the decimal point which get lost in String.toInt conversion
  } 
   flag = 1;}
}
static void notifyCallback(
  BLERemoteCharacteristic* pBLERemoteCharacteristic,
  uint8_t* pData,
  size_t length,
  bool isNotify) {
  Serial.print("Notify callback for characteristic ");
  Serial.print(pBLERemoteCharacteristic->getUUID().toString().c_str());
  Serial.print(" of data length ");
  Serial.println(length);
  Serial.print("data: ");
  Serial.println((char*)pData);
}

class MyClientCallback : public BLEClientCallbacks {
    void onConnect(BLEClient* pclient) {
    }

    void onDisconnect(BLEClient* pclient) {
      connected = false;
      Serial.println("onDisconnect");
    }
};

bool connectToServer() {
  Serial.print("Forming a connection to ");
  Serial.println(myDevice->getAddress().toString().c_str());

  BLEClient*  pClient  = BLEDevice::createClient();
  Serial.println(" - Created client");

  pClient->setClientCallbacks(new MyClientCallback());

  // Connect to the remove BLE Server.
  pClient->connect(myDevice);  // if you pass BLEAdvertisedDevice instead of address, it will be recognized type of peer device address (public or private)
  Serial.println(" - Connected to server");

  // Obtain a reference to the service we are after in the remote BLE server.
  BLERemoteService* pRemoteService = pClient->getService(serviceUUID);
  if (pRemoteService == nullptr) {
    Serial.print("Failed to find our service UUID: ");
    Serial.println(serviceUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - Found our service");


  // Obtain a reference to the characteristic in the service of the remote BLE server.
  pRemoteCharacteristic = pRemoteService->getCharacteristic(charUUID);
  if (pRemoteCharacteristic == nullptr) {
    Serial.print("Failed to find our characteristic UUID: ");
    Serial.println(charUUID.toString().c_str());
    pClient->disconnect();
    return false;
  }
  Serial.println(" - Found our characteristic");

  // Read the value of the characteristic.
  if (pRemoteCharacteristic->canRead()) {
    std::string value = pRemoteCharacteristic->readValue();
    Serial.print("The characteristic value was: ");
    Serial.println(value.c_str());
  }

  if (pRemoteCharacteristic->canNotify())
    pRemoteCharacteristic->registerForNotify(notifyCallback);

  connected = true;
  return true;
}
/**
   Scan for BLE servers and find the first one that advertises the service we are looking for.
*/
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    /**
        Called for each advertising BLE server.
    */
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.print("BLE Advertised Device found: ");
      Serial.println(advertisedDevice.toString().c_str());

      // We have found a device, let us now see if it contains the service we are looking for.
      if (advertisedDevice.haveServiceUUID() && advertisedDevice.isAdvertisingService(serviceUUID)) {

        BLEDevice::getScan()->stop();
        myDevice = new BLEAdvertisedDevice(advertisedDevice);
        doConnect = true;
        doScan = true;

      } // Found our server
    } // onResult
}; // MyAdvertisedDeviceCallbacks


void setup() {
  Serial.begin(115200);
  while (!Serial) {
    delay(10);
  }
  Serial.println("Starting Arduino BLE Client application...");
  BLEDevice::init("");

  // Retrieve a Scanner and set the callback we want to use to be informed when we
  // have detected a new device.  Specify that we want active scanning and start the
  // scan to run for 5 seconds.
  BLEScan* pBLEScan = BLEDevice::getScan();
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setInterval(1349);
  pBLEScan->setWindow(449);
  pBLEScan->setActiveScan(true);
  pBLEScan->start(5, false);
  sensor.attach(updateRate, updateTemperature);
  /* Initialise the driver with I2C_ADDRESS and the default I2C bus. */
  for (i = 0; i < 4; i++){
    if (! myMcps[i].begin(ADDRESSES[i])) {
      Serial.println("MCP No " + String(i+1) +" not found. Check wiring!");
      while (1);}
  }
  Serial.println("Found 4 MCP9600s!");

    for (i = 0; i < 4; i++)
      myMcps[i].setADCresolution(MCP9600_ADCRESOLUTION_14);
  
    for (i = 0; i < 4; i++){
    Serial.print("ADC resolution set to ");
    switch (myMcps[i].getADCresolution()) {
      case MCP9600_ADCRESOLUTION_18:   Serial.print("18"); break;
      case MCP9600_ADCRESOLUTION_16:   Serial.print("16"); break;
      case MCP9600_ADCRESOLUTION_14:   Serial.print("14"); break;
      case MCP9600_ADCRESOLUTION_12:   Serial.print("12"); break;
    }
    Serial.println(" bits");}
 
  for (i = 0; i < 4; i++){
    myMcps[i].setThermocoupleType(MCP9600_TYPE_K);
    Serial.println("Thermocouple " + String(i+1) + " type set to ");
    switch (myMcps[i].getThermocoupleType()) {
      case MCP9600_TYPE_K:  Serial.print("K"); break;
      case MCP9600_TYPE_J:  Serial.print("J"); break;
      case MCP9600_TYPE_T:  Serial.print("T"); break;
      case MCP9600_TYPE_N:  Serial.print("N"); break;
      case MCP9600_TYPE_S:  Serial.print("S"); break;
      case MCP9600_TYPE_E:  Serial.print("E"); break;
      case MCP9600_TYPE_B:  Serial.print("B"); break;
      case MCP9600_TYPE_R:  Serial.print("R"); break;
    }
    Serial.println(" type");}
  
  
      for (i = 0; i < 4; i++)
        myMcps[i].enable(true);  


  Serial.println(F("------------------------------"));
} // End of setup.


// This is the Arduino main loop function.
void loop() {
  // If the flag "doConnect" is true then we have scanned for and found the desired
  // BLE Server with which we wish to connect.  Now we connect to it.  Once we are
  // connected we set the connected flag to be true.

  if (doConnect == true) {
    if (connectToServer()) {
      Serial.println("We are now connected to the BLE Server.");
    } else {
      Serial.println("We have failed to connect to the server; there is nothin more we will do.");
    }
    doConnect = false;
  }

  // If we are connected to a peer BLE Server, update the characteristic each time we are reached
  // with the current time since boot.

  if (connected) {

    if (flag) {
      Serial.println("The readings *80 are:");
      package = "";
      for (i = 0; i < 4; i++){
        newValue = TKReadings[i]*80;  //in assignment conversion to String
        Serial.println(newValue);
        const int wordLength = (newValue.length());
        for (j = 0; j < 4-wordLength; j++)  //pad with zeros to 4 digits
          newValue = 0 + newValue;
       package = package + newValue;   //pack up all 4 readings
     }  
        pRemoteCharacteristic->writeValue(package.c_str(), package.length());

      flag = 0;
    }
  } else if (doScan) {
    BLEDevice::getScan()->start(0);  // this is just example to start scan after disconnect, most likely there is better way to do it in arduino
  }

} // End of loop

User avatar
adafruit_support_carter
 
Posts: 29457
Joined: Tue Nov 29, 2016 2:45 pm

Re: MCP9600 not changing thermocouple type setting

Post by adafruit_support_carter »

Your MCP9600 is custom hardware? You are not using one of these?
https://www.adafruit.com/product/4101

User avatar
BShmid
 
Posts: 13
Joined: Wed Nov 24, 2021 6:33 am

Re: MCP9600 not changing thermocouple type setting

Post by BShmid »

No I'm not.
Anyhow Issue fixed by changing I2C Clock frquency from 100 to 25 kHz. (In my case by adding

Code: Select all

Wire.setClock(25000); 
after

Code: Select all

 Wire.begin();
(In my case after

Code: Select all

mcp.begin();
).

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

Re: MCP9600 not changing thermocouple type setting

Post by adafruit_support_bill »

100KHz is the 'Standard' speed supported by virtually every i2c device. But bus problems can interfere with the production of clean signals on the bus. Slowing down the clock is a workaround for many such problems.

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

Return to “Other Products from Adafruit”