| 
  • If you are citizen of an European Union member nation, you may not use this service unless you are at least 16 years old.

  • You already know Dokkio is an AI-powered assistant to organize & manage your digital files & messages. Very soon, Dokkio will support Outlook as well as One Drive. Check it out today!

View
 

Launchable Data-logger

This version was saved 9 years, 5 months ago View current version     Page history
Saved by James Dunn
on November 26, 2014 at 7:04:56 am
 

$15 Datalogger using Arduino Micro-controller

 

Enclosed: All the Arduino datalogger code

 

                                          

 

Parts Required:

  • Arduino UNO,
  • SD Memory Shield
    • Video Tutorial uses a more expensive version, the alternate selected has a different pin-out sequence
    • Datasheet
  • Micro-SD Flash,
  • 9v Battery & connectors,
  • USB programming cable,
  • Arduino Proto Shield,
  • & whatever sensors/actuators selected
  • Book:  "Make: Sensors: A Hands-On Primer for Monitoring the Real World with Arduino and Raspberry Pi

 

 

Note:  If your Arduino Uno datalogger works fine on USB power, but fails when on battery or external power, change the value of R12 from 1k to 10k.  A known feedback condition may be corrupting the initialization process.  Schematic

 

Also enclosed below are video tutorials that allow for understanding the code to create a datalogger.  Many micro-SD card shields exist for as little as about $1.50 (USD).  The tutorial uses a different version than the one used by us.  SD card shields (circuit boards) and SD memory cards.

As instructors, you can choose whether to teach coding, just assemble the datalogger, or some combination.

Recommendation if teaching coding: teach how to set up small amounts of code and check usefulness, save incremental versions, and add tested code in pieces.  Back up incremental accomplishments.  That way one problem is being solved at a time, and not working on 10 simultaneous problems; usually syntax errors.

Use // to comment out code that is working, to debug code

When starting from scratch, the four layers of coding:

 

  1. Details, objectives, correlations, description of sequences, anticipated restrictions, and risks all written in English;
  2. Flow Chart and/or State Diagram to reflect written English;
  3. Pseudo-code (ends up driving code comments); &
  4. Syntax Correct code

    followed by:
    entering code
    testing, and
    debugging


 

All tutorials are added for completeness.  But the main ones for the datalogger are: start with 11, then 1, 3, 6, 7, 8, & repeat 11

In one class period (or homework assignment) the students can read through everything needed to make a datalogger.  With 10% expected uptake.  Hands-on practiced experience is needed to correlate the conveyed information effectively.

 

An important aspect is to as much as practical, have the students learn "how" to solve their own problems.  The act of getting practices experience in problem solving better prepares them for actual life experiences.


The students can then at your option write and implement code based upon tutorials, or just build the datalogger and explore other class efforts.

 


For explanations:
SD.h library reference
http://arduino.cc/en/Reference/SD
Wire.h library reference
http://arduino.cc/en/Reference/wire

Tutorials at times use a software called "Processing".  The more advanced code editor for Arduino; free from

 

     https://www.processing.org/download/
     select "no donation" to download

 

"Processing" code (Arduino code is based on Processing code) uses .pde file extensions and contains the code in text format.

Each of the Video tutorials has a related download page for the related code, to minimize making typos.

"Arduino" automatically includes libraries, while "Processing" requires "Import" of every library used.
 

Arduino Free Software Downloads: www.Arduino.cc ; all parts can be purchased on eBay (~$15 total)

 

Tutorial 01 for Arduino: Getting Acquainted with Arduino

http://www.youtube.com/watch?v=fCxzA9_kg6s

 

Tutorial 02 for Arduino: Buttons, PWM, and Functions

http://www.youtube.com/watch?v=_LCCGFSMOr4

 

Tutorial 03 for Arduino: Electrical Engineering Basics

http://www.youtube.com/watch?v=abWCy_aOSwY

 

Tutorial 04 for Arduino: Analog Inputs

http://www.youtube.com/watch?v=js4TK0U848I

 

Tutorial 05 for Arduino: Motors and Transistors

http://www.youtube.com/watch?v=5bHPKU4ybHY

 

Tutorial 06 for Arduino: Serial Communication and Processing

http://www.youtube.com/watch?v=g0pSfyXOXj8

 

Tutorial 07 for Arduino: I2C Communication and Processing

http://www.youtube.com/watch?v=GJX0BRUagCg

 

Tutorial 08 for Arduino: SPI Interfaces

http://www.youtube.com/watch?v=1nO2SSExEnQ

 

Tutorial 09 for Arduino: Wireless Communication

http://www.youtube.com/watch?v=vKVNmA8C6m8

 

Tutorial 10 for Arduino: Interrupts and Hardware Debouncing

http://www.youtube.com/watch?v=CRJUdf5TTQQ

 

Tutorial 11 for Arduino: SD Cards and Datalogging

http://www.youtube.com/watch?v=5v5A3j7Rrco

 

Tutorial 12 for Arduino: RFID Card Reading

http://www.youtube.com/watch?v=gIlSLwcbeTU

 

Tutorial 13 for Arduino: Liquid Crystal Displays (LCDs)

http://www.youtube.com/watch?v=oIiDseJO4dM

 

Tutorial 14 for Arduino: Holiday Lights and Sounds Spectacular!

http://www.youtube.com/watch?v=CoG_Czyr7z0

 

Tutorial 15 for Arduino: GPS Tracking

http://www.youtube.com/watch?v=TtZEZYQG0xk

 

 

Actual Code to Use with Video Tutorial specified SD Card Adapter

 

datalogger_code.txt

//Program by Jeremy Blum

//www.jeremyblum.com

//SD Card Demonstration

//Some code from public domain work by Tom Igoe

 

#include <SD.h>  

//SD Card Library

 

#include <Wire.h> 

//I2C Library

 

//SPI SD Card Pins

  //MOSI = Pin 11

  //MISO = Pin 12

  //SCLK = PIN 13

 

  int CS_pin = 10;

 

  int pow_pin = 8;

 

 

//I2C Temperature Pins

//SDA = Analog Pin 4

//SCL = Analog Pin 5

 

//IR Distance Sensor Pins

  int IR1_pin = 2;

  int IR2_pin = 3;

 

//Light Sensor Pins

  int light_pin = 1;

 

float refresh_rate = 0.0; 

//Dataloger Refresh Rate

 

  int temp_address = 72;  

//Address of the I2C Temp Sensor

 

  long id = 1;               

//Use this to store the id # of our reading.

 

void setup()

{

  Wire.begin();

  Serial.begin(9600);

  Serial.println("Initializing Card");

 

//CS Pin is an output

  pinMode(CS_pin, OUTPUT);

 

//SD Card will Draw Power from Pin 8, so set it high

  pinMode(pow_pin, OUTPUT); 

 

  digitalWrite(pow_pin, HIGH);

 

//Initialize Card

  if (!SD.begin(CS_pin))

  {

      Serial.println("Card Failure");

      return;

  }

    Serial.println("Card Ready");

   

 

//Read the Configuration information (COMMANDS.txt)

  File commandFile = SD.open("COMMANDS.txt");

  if (commandFile)

  {

       Serial.println("Reading Command File");

   

       float decade = pow(10, (commandFile.available() - 1));

 

    while(commandFile.available())

    {

      float temp = (commandFile.read() - '0');

      refresh_rate = temp*decade+refresh_rate;

      decade = decade/10;

    }

 

 

    Serial.print("Refresh Rate = ");

    Serial.print(refresh_rate);

    Serial.println("ms");

    commandFile.close();

  }

  else

  {

    Serial.println("Could not read command file.");

    return;

   }

 

 

//Write Log File Header

  File logFile = SD.open("LOG.csv", FILE_WRITE);

  if (logFile)

  {

    logFile.println(", , , ,");

   //Just a leading blank line, in-case there was previous data

 

    String header = "ID, Light, Temp, IR1, IR2";

    logFile.println(header);

    logFile.close();

    Serial.println(header);

  }

  else

  {

    Serial.println("Couldn't open log file");

  }

 

 

}

 

 

 

void loop()

{

  //Check Light Level

  int light_level = analogRead(light_pin);

 

  //Read Temperature

  Wire.beginTransmission(temp_address);

 

  //Start talking

  Wire.send(0);

 

  //Ask for Register zero

  Wire.endTransmission();

 

  //Complete Transmission

  Wire.requestFrom(temp_address, 1);

 

  //Request 1 Byte

  while(Wire.available() == 0);

 

  //wait for response

  int temp_c = Wire.receive();

 

  // Get the temp

  int temp_f = round(temp_c*9.0/5.0 +32.0);

 

  //Convert to stupid American units

 

  //Read Distances

  int IR1_val = analogRead(IR1_pin);

  int IR2_val = analogRead(IR2_pin);

 

 

  //Create Data string for storing to SD card

 

  //We will use CSV Format

  String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val);

 

 

  //Open a file to write to

  //Only one file can be open at a time

  File logFile = SD.open("LOG.csv", FILE_WRITE);

 

  if (logFile)

  {

    logFile.println(dataString);

    logFile.close();

    Serial.println(dataString);

  }

  else

  {

    Serial.println("Couldn't open log file");

  }

 

 

  //Increment ID number

  id++;

 

  delay(refresh_rate);

 

}

 

 

 

/////////// Stripped of Comments ///////////////

 

datalogger_code.txt

 

#include <SD.h>  

#include <Wire.h>

  int CS_pin = 10;

  int pow_pin = 8;

  int IR1_pin = 2;

  int IR2_pin = 3;

  int light_pin = 1;

 

float refresh_rate = 0.0; 

 

  int temp_address = 72;  

  long id = 1;               

 

void setup()

{

  Wire.begin();

  Serial.begin(9600);

  Serial.println("Initializing Card");

 

  pinMode(CS_pin, OUTPUT);

  pinMode(pow_pin, OUTPUT); 

 

  digitalWrite(pow_pin, HIGH);

 

  if (!SD.begin(CS_pin))

  {

      Serial.println("Card Failure");

      return;

  }

 

    Serial.println("Card Ready");

    File commandFile = SD.open("COMMANDS.txt");

  

if (commandFile)

  {

       Serial.println("Reading Command File");

       float decade = pow(10, (commandFile.available() - 1));

 

    while(commandFile.available())

    {

      float temp = (commandFile.read() - '0');

      refresh_rate = temp*decade+refresh_rate;

      decade = decade/10;

    }

 

    Serial.print("Refresh Rate = ");

    Serial.print(refresh_rate);

    Serial.println("ms");

    commandFile.close();

  }

  else

  {

    Serial.println("Could not read command file.");

    return;

   }

 

 

  File logFile = SD.open("LOG.csv", FILE_WRITE);

  if (logFile)

  {

    logFile.println(", , , ,"); 

    String header = "ID, Light, Temp, IR1, IR2";

    logFile.println(header);

    logFile.close();

    Serial.println(header);

  }

  else

  {

    Serial.println("Couldn't open log file");

  } 

}

 

void loop()

{

  int light_level = analogRead(light_pin);

  Wire.beginTransmission(temp_address);

  Wire.send(0);

  Wire.endTransmission();

  Wire.requestFrom(temp_address, 1);

 

  while(Wire.available() == 0);

 

  int temp_c = Wire.receive();

  int temp_f = round(temp_c*9.0/5.0 +32.0);

  int IR1_val = analogRead(IR1_pin);

  int IR2_val = analogRead(IR2_pin);

 

  String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val); 

  File logFile = SD.open("LOG.csv", FILE_WRITE);

 

  if (logFile)

  {

    logFile.println(dataString);

    logFile.close();

    Serial.println(dataString);

  }

  else

  {

    Serial.println("Couldn't open log file");

  }

 

  id++;

  delay(refresh_rate);

 

}

 

 

 

Actual Code to Use with Alternately specified SD Card Adapter (see Datasheet)

in development

 

datalogger_code.txt

//Program by Jeremy Blum

//www.jeremyblum.com

//SD Card Demonstration

//Some code from public domain work by Tom Igoe

//Pinouts & related code modified by James Dunn to adapt a different model of SD Card Adapter

 

#include <SD.h>  

//SD Card Library

 

#include <Wire.h>

//I2C Library

 

//SPI SD Card Pins   modified for Datasheet

  //CS = Pin 9

  //SCLK = Pin 10

  //MOSI = Pin 11

  //MISO = Pin 12

  //VCC = Pin 13

  //GND = Pin 14

 

  int CS_pin = 9;

  int SCLK_pin = 10;

  int pow_pin = 13;

//CS and SCLK work in parallel in this configuration

 

 

//I2C Temperature Pins

//SDA = Analog Pin 4

//SCL = Analog Pin 5

 

//IR Distance Sensor Pins

  int IR1_pin = 2;

  int IR2_pin = 3;

 

//Light Sensor Pins

  int light_pin = 1;

 

float refresh_rate = 0.0; 

//Dataloger Refresh Rate

 

  int temp_address = 72;  

//Address of the I2C Temp Sensor

 

  long id = 1;               

//Use this to store the id # of our reading.

 

void setup()

{

  Wire.begin();

  Serial.begin(9600);

  Serial.println("Initializing Card");

 

//CS Pin is an output

  pinMode(CS_pin, OUTPUT);

 

//SCLK Pin is an output

  pinMode(SCLK_pin, OUTPUT);

 

//SD Card will Draw Power from Pin 13, so set it high

  pinMode(pow_pin, OUTPUT); 

 

  digitalWrite(pow_pin, HIGH);

 

//Assert SCLK to follow CS

  if (CS_pin)

  {

    digitalWrite(SCLK_pin, HIGH)

    return;

  }

    digitalWrite(SCLK_pin, LOW);

 

//Initialize Card

  if (!SD.begin(CS_pin))

  {

      Serial.println("Card Failure");

      return;

  }

    Serial.println("Card Ready");

   

 

//Read the Configuration information (COMMANDS.txt)

  File commandFile = SD.open("COMMANDS.txt");

  if (commandFile)

  {

       Serial.println("Reading Command File");

   

       float decade = pow(10, (commandFile.available() - 1));

 

    while(commandFile.available())

    {

      float temp = (commandFile.read() - '0');

      refresh_rate = temp*decade+refresh_rate;

      decade = decade/10;

    }

 

 

    Serial.print("Refresh Rate = ");

    Serial.print(refresh_rate);

    Serial.println("ms");

    commandFile.close();

  }

  else

  {

    Serial.println("Could not read command file.");

    return;

   }

 

 

//Write Log File Header

  File logFile = SD.open("LOG.csv", FILE_WRITE);

  if (logFile)

  {

    logFile.println(", , , ,");

   //Just a leading blank line, in-case there was previous data

 

    String header = "ID, Light, Temp, IR1, IR2";

    logFile.println(header);

    logFile.close();

    Serial.println(header);

  }

  else

  {

    Serial.println("Couldn't open log file");

  }

 

 

}

 

 

 

void loop()

{

  //Check Light Level

  int light_level = analogRead(light_pin);

 

  //Read Temperature

  Wire.beginTransmission(temp_address);

 

  //Start talking

  Wire.send(0);

 

  //Ask for Register zero

  Wire.endTransmission();

 

  //Complete Transmission

  Wire.requestFrom(temp_address, 1);

 

  //Request 1 Byte

  while(Wire.available() == 0);

 

  //wait for response

  int temp_c = Wire.receive();

 

  // Get the temp

  int temp_f = round(temp_c*9.0/5.0 +32.0);

 

  //Convert to stupid American units

 

  //Read Distances

  int IR1_val = analogRead(IR1_pin);

  int IR2_val = analogRead(IR2_pin);

 

 

  //Create Data string for storing to SD card

 

  //We will use CSV Format

  String dataString = String(id) + ", " + String(light_level) + ", " + String(temp_f) + ", " + String(IR1_val) + ", " + String(IR2_val);

 

 

  //Open a file to write to

  //Only one file can be open at a time

  File logFile = SD.open("LOG.csv", FILE_WRITE);

 

  if (logFile)

  {

    logFile.println(dataString);

    logFile.close();

    Serial.println(dataString);

  }

  else

  {

    Serial.println("Couldn't open log file");

  }

 

 

  //Increment ID number

  id++;

 

  delay(refresh_rate);

 

}

 

 

 

/////////// Stripped of Comments ///////////////

 

 

Comments (0)

You don't have permission to comment on this page.