PlayingWithFusion.com Home


0 Items
View shopping cart itemsView Cart

Document: 1214
Posted: March 6, 2025
Modified: March 6, 2025

Home > Tech Article Categories > Temperature Measurement > Temperature Logging with R3aktor M0 Core

Temperature Logging with R3aktor M0 Core

Combine a R3aktor M0 Logger with an I2C thermocouple shield to create a simple battery powered temperature data logger.

Introduction

The onboard SD card, LiPo battery support, and QWIIC connector make the R3aktor M0 Logger ideal for quick and portable data acquisition projects.   In this example a SEN-30011 shield is used to monitor the temperature of four thermocouples and then log their values to a SD card.

This example represents a 'from scratch' solution, where you have complete control of the software running on the R3aktor.   For a more complete solution, check out R3aktor Core and Control Center.   R3aktor Core combines a temperature measurement shield and a M0 Logger into a single product and includes free firmware and PC software!

Hardware Setup

For this project, you will need:

Connect the 4 thermocouples to the SEN-30011 breakout board. Connect the SEN-30011 to the R3aktor board with the qwiic connector cable. Your setup should look like the image below.

Connect the R3aktor board to your computer with the USB-C cable.

Install Required Libraries

Open the Arduino IDE. For this project, you will need to install the PWFusion MCP906X library. Open the library manager by selecting the library icon on the left or by selecting Tools -> Manage Libraries

In the library manager panel, type pwfusion in the search box and locate the MCP906X library. Select install and wait for the installation process to complete.

You will also need to install the Arduino SD card interface library. Repeat the above process but instead, type sd in the search box, set the type filter to Arduino, and then locate and install the library called SD by Arduino, SparkFun.

Wait for the installation process to complete.

Write the Software

Copy and paste this code into a new Arduino IDE sketch:

  1. #include <SD.h>
  2. #include <Wire.h>
  3. #include <PWFusion_Mcp960x.h>
  4.  
  5. #define NUM_THERMOCOUPLES   4 // Define the number of thermocouples connected
  6. Mcp960x thermo[NUM_THERMOCOUPLES];
  7. const uint8_t mcp9601Addr[] = {0, 1, 4, 7};
  8. int logTime = 1000; // Time to log data (in seconds)
  9.  
  10. File sdFile;
  11.  
  12. int tempPin = 0;
  13.  
  14. void setup() {
  15.   Wire.begin();
  16.   Wire.setClock(100000L);
  17.   // Open serial communications and wait for port to open:
  18.   Serial.begin(9600);
  19.   while (!Serial); // wait for serial port to connect. Needed for native USB port only
  20.  
  21.   // Initialize SD card
  22.   Serial.print("Initializing SD card...");
  23.   if (!SD.begin(SDCARD_SS_PIN)) { // SDCARD_SS_PIN is the defined pin for the SD Card reader
  24.     Serial.println("Initialization failed.");
  25.     while (1);
  26.   }
  27.   Serial.println("Initialization done.");
  28.  
  29.   // Initialize MCP960X Thermocouple
  30.   for (int i=0; i<NUM_THERMOCOUPLES; i++) {
  31.     thermo[i].begin(mcp9601Addr[i]);
  32.     if (thermo[i].isConnected()) {
  33.       Serial.print(F("Found MCP9601 sensor at address "));
  34.       Serial.println(i);
  35.  
  36.       thermo[i].setThermocoupleType(TYPE_K);
  37.       thermo[i].setResolution(RES_18BIT, RES_0p0625);
  38.     }
  39.     else {
  40.       Serial.print(F("ERROR: Unable to connect to MCP9601 sensor at address "));
  41.       Serial.println(i);
  42.     }
  43.   }
  44.  
  45.   SD.remove("temp.csv"); // If the file exists, remove it so the data will be stored in a new file
  46.   sdFile = SD.open("temp.csv", FILE_WRITE);
  47.   if (sdFile) {
  48.       sdFile.println("Temperature");
  49.       sdFile.close();
  50.     } else {
  51.       Serial.println("error opening temp.csv");
  52.     }
  53.  
  54.   for(int i=0; i < logTime; i++) {
  55.     sdFile = SD.open("temp.csv", FILE_WRITE);
  56.     if (sdFile == true) {
  57.       sdFile.print(i);  Serial.print(i);
  58.       for (int j=0; j<NUM_THERMOCOUPLES; j++) { // Loop through each thermocouple
  59.         float value = readThermocouples(j);
  60.         sdFile.print(","); sdFile.print(value); Serial.print(","); Serial.print(value);
  61.       }
  62.       sdFile.println(); Serial.println();
  63.       sdFile.close(); // Each file must be closed before another can be opened
  64.     } else {
  65.       Serial.println("error opening PWF.txt"); // If the file didn't open, print an error
  66.     }
  67.     delay(1000); // Read the thermocuople temp every 1 second
  68.   }
  69.   Serial.println("File closed. Logging complete");
  70. }
  71.  
  72. float readThermocouples(int thermocoupleID) {
  73.   // Function to read the temp
  74.   switch (thermo[thermocoupleID].getStatus()) {
  75.     case READY:
  76.       return thermo[thermocoupleID].getThermocoupleTemp();
  77.     default:
  78.       return -1;
  79.   }
  80. }
  81.  
  82. void loop() {
  83.  
  84. }

Before uploading the code, insert the micro-SD card into the R3aktor board’s micro-SD card slot.

Make sure to select the correct board and COM port number and select upload.

Open the serial monitor by selecting the serial monitor icon in the upper right corner or by selecting Tools -> Serial Monitor

You will see a string of numbers separated by commas. This is exactly how the data from the thermocouples is being recorded in the .csv file.

Wait 1000 seconds for the logging to complete.

A message will appear in the serial monitor when the logging process is complete.

When this happens, remove the micro-SD card form the R3aktor board and insert it into your computer. Navigate to the micro-SD card folder location and open the file named temp.csv in a spreadsheet application like Microsoft Excel.

As you can see, the output is almost identical to the output in the serial monitor of the Arduino IDE.

There are many applications for logging data in a .csv format, such as making the utilization of the data much easier. Below is a graph of the data for visualization purposes.

 

Related Products