Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino Mega 2560 Temperature Logging System with MLX90614 and Micro SD Card Module

View and Edit Project In Cirkit DesignerImage of Arduino Mega 2560 Temperature Logging System with MLX90614 and Micro SD Card Module

Circuit Documentation

Summary

This circuit involves an Arduino Mega 2560 microcontroller interfacing with an MLX90614 infrared temperature sensor and a Micro SD Card Module. The Arduino reads temperature data from the MLX90614 sensor via I2C and logs the data to the Micro SD Card Module via SPI. The circuit is designed to read and log temperature data in real-time.

Component List

  1. Arduino Mega 2560

    • Description: A microcontroller board based on the ATmega2560.
    • Pins: IOREF, RESET, 3V3, 5V, GND, VIN, A0-A15, D0-D53, AREF, SDA, SCL.
  2. Micro SD Card Module

    • Description: A module for interfacing with micro SD cards.
    • Pins: cs, sck, mosi, miso, vcc, gnd.
  3. MLX90614

    • Description: An infrared temperature sensor.
    • Pins: SDA, SCL, GND, VIN.

Wiring Details

Arduino Mega 2560

Micro SD Card Module

MLX90614

Code Documentation

Arduino Mega 2560 Code

/*
 * Arduino Mega 2560 controlling MLX90614 and ADXL326 sensors.
 * - MLX90614: Infrared temperature sensor connected via I2C (SDA, SCL).
 * - ADXL326: 3-axis accelerometer connected to analog pins (A0, A1, A2).
 * This code reads data from both sensors in parallel and prints the values
 * to the Serial Monitor and Serial Plotter.
 */

#include <Wire.h>

// MLX90614 I2C address
#define MLX90614_ADDR 0x5A

// Function prototypes
float readTemperature();
void readAccelerometer(int &x, int &y, int &z);

void setup() {
  Serial.begin(9600);
  Wire.begin();
}

void loop() {
  // Read temperature from MLX90614
  float temperature = readTemperature();
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");

  // Read accelerometer data from ADXL326
  int x, y, z;
  readAccelerometer(x, y, z);
  Serial.print("X: ");
  Serial.print(x);
  Serial.print(" Y: ");
  Serial.print(y);
  Serial.print(" Z: ");
  Serial.println(z);

  // Print data for Serial Plotter
  Serial.print(temperature);
  Serial.print(",");
  Serial.print(x);
  Serial.print(",");
  Serial.print(y);
  Serial.print(",");
  Serial.println(z);

  delay(1000); // Delay for 1 second
}

float readTemperature() {
  Wire.beginTransmission(MLX90614_ADDR);
  Wire.write(0x07); // Command to read object temperature
  Wire.endTransmission(false);
  Wire.requestFrom(MLX90614_ADDR, 3);

  if (Wire.available() == 3) {
    uint16_t temp = Wire.read();
    temp |= Wire.read() << 8;
    Wire.read(); // PEC byte, not used
    return temp * 0.02 - 273.15; // Convert to Celsius
  }
  return NAN; // Return NaN if read failed
}

void readAccelerometer(int &x, int &y, int &z) {
  x = analogRead(A0);
  y = analogRead(A1);
  z = analogRead(A2);
}

Micro SD Card Module Code

/*
 * Arduino Mega 2560 interfacing with MLX90614 and Micro SD Card Module.
 * - MLX90614: Infrared temperature sensor connected via I2C (SDA, SCL).
 * - Micro SD Card Module: Connected via SPI (CS, MOSI, MISO, SCK).
 * This code reads temperature data from the MLX90614 sensor and logs it
 * to a file named 'TempSen.txt' on the Micro SD card in a tabular format.
 */

#include <Wire.h>
#include <SPI.h>
#include <SD.h>

// MLX90614 I2C address
#define MLX90614_ADDR 0x5A

// SD card chip select pin
const int chipSelect = 53;

// Function prototypes
float readTemperature();
void logData(float temperature);

void setup() {
  Serial.begin(9600);
  Wire.begin();
  if (!SD.begin(chipSelect)) {
    Serial.println("SD card initialization failed!");
    return;
  }
  Serial.println("SD card initialized.");
  // Create or open file and write header if it doesn't exist
  File dataFile = SD.open("TempSen.txt", FILE_WRITE);
  if (dataFile) {
    if (dataFile.size() == 0) {
      dataFile.println("Time (s)\tTemperature (C)");
    }
    dataFile.close();
  }
}

void loop() {
  static unsigned long startTime = millis();
  unsigned long currentTime = (millis() - startTime) / 1000;
  // Read temperature from MLX90614
  float temperature = readTemperature();
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" C");
  // Log data to SD card
  logData(currentTime, temperature);
  delay(1000); // Delay for 1 second
}

float readTemperature() {
  Wire.beginTransmission(MLX90614_ADDR);
  Wire.write(0x07); // Command to read object temperature
  Wire.endTransmission(false);
  Wire.requestFrom(MLX90614_ADDR, 3);
  if (Wire.available() == 3) {
    uint16_t temp = Wire.read();
    temp |= Wire.read() << 8;
    Wire.read(); // PEC byte, not used
    return temp * 0.02 - 273.15; // Convert to Celsius
  }
  return NAN; // Return NaN if read failed
}

void logData(unsigned long time, float temperature) {
  File dataFile = SD.open("TempSen.txt", FILE_WRITE);
  if (dataFile) {
    dataFile.print(time);
    dataFile.print("\t");
    dataFile.println(temperature);
    dataFile.close();
    Serial.println("Data written to TempSen.txt");
  } else {
    Serial.println("Error opening TempSen.txt");
  }
}