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
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
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");
 }
}
Analog datalogger in SD card