Analog datalogger in SD card

January 18, 2019 by
Analog datalogger in SD card
Bernat Garcia

Introduction

In this post, we will see how to use an M-Duino 21+ as an analog datalogger. The analog data will be collected from a potentiometer and will be stored on the internal SD card of the M-Duino 21+. Apart from that, we also will use the Filter library to filter the analog value taking some samples.

Requirements

Ethernet PLC: Ethernet PLC 

Filter Library:  Filter Library  

Industrial Shields boards:  How to use the mapping pins of Industrial Shields boards

An SD card


Description and connections

First of all, we will insert the SD card to the M-Duino and we will make sure that is properly configured. Take a look at this post to make sure that you have well connected the SD card and the M-Duino switches properly placed:  How to connect a SD Card to an Industrial Shields Ethernet PLC

SD/Q2.0 switch must be placed in the OFF position and the file system architecture of the SD card shall be FAT32 or FAT16.

The connection is very simple. The potentiometer is connected to the I0.12 and is power supplied by 5V. So, the Arduino controller shall receive a value between 0-5V and will be interpreted by ADC as a value between 0 to 512. Remember that the full scale of M-Duino family units is between 0-10V or 0-1024 for the ADC.  


Software

This sketch allows us to perform analog readings of the potentiometer. The Filter.h library will take 5 samples and will make the arithmetic average every 10ms. These parameters can be configured in the initialization of the AnalogFilter.
Every 3 seconds the filtered value from the potentiometer will be shown on the serial monitor and will be stored on the SD card. 

Take a look at the video shown below to see the results.

/*

   Copyright (c) 2018 Boot&Work Corp., S.L. All rights reserved


This program is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this program.  If not, see <http://www.gnu.org/licenses/>.

 */


////////////////////////////////////////////////////////////////////////////////////////////////////

#include <Filter.h>

#include <SD.h>


Sd2Card card;

SdVolume volume;


AnalogFilter<5, 2> aFilter;


//In this example, the sample time is 2ms and the number of samples is 5.


void setup() {

    Serial.begin(9600L);

    Serial.println("microsd started");

   

    // since we're just testing if the card is working!

    if (!SD.begin(53)) {

        Serial.println("Card failed, or not present");

        // don't do anything more:

        while (1);

    }

    Serial.println("card initialized.");

}


/////////////////////////////////////////////////////////////////////////////////////////////////


void loop() {

    int input = analogRead(I0_12);

    int filtered = aFilter.update(input);

    static uint32_t lastPrint = millis();

    if (millis() - lastPrint > 3000) {

        Serial.println(filtered);

        logToSd(filtered);

        lastPrint = millis();

    }

}


////////////////////////////////////////////////////////////////////////////////////////////////


void logToSd(int value){

  // open the file. note that only one file can be open at a time,

  // so you have to close this one before opening another.

  File dataFile = SD.open("datalog.txt", FILE_WRITE);


  // if the file is available, write to it:

  if (dataFile) {

        dataFile.println(value);

        dataFile.close();

  }

  

  // if the file isn't open, pop up an error:

  else {

        Serial.println("error opening datalog.txt");

  }

}

​Search in our Blog

Analog datalogger in SD card
Bernat Garcia January 18, 2019

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>