Datalogging with the Arduino

30 seconds of temperature data recorded using the Arduino (over the serial port).
30 seconds of temperature data recorded using the Arduino (over the serial port).

I wired a temperature sensor to my Arduino as part of the third project in the Arduino Projects Book. The project has the Arduino send the data to the serial port where the Arduino program (IDE) can show it. This data would be most useful to me, however, if it could be logged in a plain text file for data analysis. However, it’s a little annoying that there isn’t an easy way to save the data output by the Arduino.

So, I needed to use the terminal program “cat” to look at what was coming across the serial port. First I had to look up which port the Arduino was connected to in the Tools menu of the Arduino IDE: since I’m on a Mac, running OSX, this turned out to be something like /dev/cu.usbmodem411. (I could have looked this up by listing the contents of the /dev/ directory and then looking for anything connected to the usbmodem). To see the data I used:

> cat /dev/cu.usbmodem411 

To actually save the data I directed the output of the cat command to the file data-log.txt:

> cat /dev/cu.usbmodem411 > data-log.txt

The contents of the file looked like this:

T25.68
6003,25.68
7504,25.68
Time (milliseconds), Temperature (deg. C) 
0,25.20
1501,25.68
3002,25.68
4502,25.68
6003,26.66
7504,28.12
9006,30.08
10335,32.03
11663,33.98
12992,35.45
14321,36.91
15650,38.38
16979,39.84
18309,41.31
19559,42.77
20810,43.75
22062,42.77
23313,41.31
24563,40.82
25815,39.36
27144,38.87
28473,37.89
29802,37.40
31131,36.91
32459,35.94
33788,35.45
35118,35.45
36447,34.96
37776,34.47
39105,33.98
40434,33.98
41763,33.50
43091,33.01
44421,33.01
45750,33.01
47079,32.52
48408,32.52
49737,32.52
51066,32.03
52396,31.54
53724,31.54

I’m not sure what’s the deal with the first three lines, but if you ignore them you see the column headers (Time and Temperature) and then the time (in milliseconds) and temperature data) in a nice comma delimited format.

The Arduino program (sketch) that produced this output was:

const int T_sensor_Pin = A0;
const float baselineTemp = 20.0;
const int outPin = 4;

void setup(){
  
  Serial.begin(9600);
  pinMode(outPin, OUTPUT);
  
  Serial.println("Time (milliseconds), Temperature (deg. C) ");

}

void loop(){
  int T_sensor_Val = analogRead(T_sensor_Pin);
  
  float T_sensor_Voltage = (T_sensor_Val * 5.0)/1024.0;
  
  float T = (T_sensor_Voltage - 0.5) * 100;
  float T_F = (T * 1.8) +32.0;
  
  Serial.print(millis());
  Serial.print(",");
  Serial.println(T); 

  delay(1500);
  
}

Alternative Methods of Saving Data

Python

Based on the same Arduino sketch, you can write a Python program to read the data. This method enables you to use the data directly with VPython for visualization.

First you need to install the pySerial library (pySerial). Then you can read the serial data using:

data-log.py

import serial

ser = serial.Serial('/dev/cu.usbmodem411', 9600)

while True:
    print ser.readline()

CoolTerm

You can also use CoolTerm to save the serial data (see directions on how).

An SD Card

Another alternative, is to get a shield that can hold an SD card (SparkFun and Adafruit have ones for less than $20) and write to that.

Sumac Berry Juice

Sumac berries.
A bunch of sumac berries on the branch.

Ripe, bright-purple sumac berries are quite astringent. Steep a ripe bunch in a quart or two of hot water for a few hours (or cold water for a day) and the result is a tart tea. Add a third of a cup of sugar to make a delicious juice. (Note: Poison sumac is not found in Missouri, but it has been identified in adjacent, eastern states, so be careful.)

3d Printing at School

The  RepRap 3D printer.
The RepRap 3D printer.

One of the key ideas behind the design of the RepRap 3D printer we just built is that you should be able to print as many of the components as possible. So you can use your 3D printer to build other 3D printers. As a consequence, the printer does not come as a nice little box. It looks a bit jury-rigged. Multicolored coils of wire snake everywhere; circuit boards and integrated chips are exposed; nuts, bolts and stainless steel rods are accessible for easy adjustment; and the plastic–printed–components are still rough from the printer. It is all function, no aesthetics. All of which make it a wonderful teaching tool.

The three students who built it got a crash course in robotic assembly. They learnt how to wire a power source, strip and solder wires, and construct the motor-controlled bed and extruder. They also learned how to use constructive solid geometry (using OpenSCAD) to create 3d shapes–I required them to design and print their own models before I would let them download object files from the internet.

On the down side, though they did have to plug a RAMPS motor shield, stepper-driver chips, and connecting wires into the Arduino microcontroller, we did not have much time to go into the detail of what it all was about. Also, we only edited an existing configuration file when we tried to calibrate the machine, so they did not learn how the programming works. Having to use the Arduino did inspire me to get one, and I was quite impressed with their starter kit, so I’m working on a “Microcontrollers for Beginners” type class or elective that I can offer over the next school year.

Arduino for Beginners

Arduino UNO connected to a breadboard from the starter kit.
Arduino UNO connected to a breadboard from the starter kit.

I’ve been avoiding working with the Arduino microcontrollers because I’d prefer to be able to program in Python with the Raspberry Pi (for example). However, since the 3d printer we just built this summer uses an Arduino for a brain, I broke down and picked up the Arduino Starter Kit (via Adafruit).

The Arduino Projects Book is an excellent resource for the beginner.
The Arduino Projects Book is an excellent resource for the beginner.

What I liked most about the Starter Kit most is the Arduino Projects Book that comes with it. It’s a wonderful introduction to circuits, electronics, circuit diagrams, and microcontrollers at the beginners level. If I offer an Arduino elective, I’ll use it as a textbook. Indeed, I’ll probably use bits of it as a reference when I teach circuits in middle school and Advanced Physics.

As for the programming, the basics, at least, are pretty straightforward. I got a blinking LED controlled by a switch input up an running pretty quickly. The code requires two loops, one to set up the inputs and the output, and a loop for the program to follow. The code below has a blinking light that’s controlled via pin 4, but changes to a solid light when the switch is pressed (the input for the switch is pin 2). The wiring for the circuit is shown in the picture at the top of the page.

blink_circuit

int switchOn = 0;

void setup(){
  pinMode(2, INPUT);
  pinMode(4, OUTPUT);
}

void loop(){
  switchOn = digitalRead(2);
  
  if (switchOn == HIGH) {
    digitalWrite(4, HIGH);
  } else {
    digitalWrite(4, LOW);
    delay(500);
    digitalWrite(4, HIGH);
    delay(200); 
  }
  
}

OpenSCAD: Creating 3d Objects for Printing

To create a rectangular prism and then take a circle out of the center requires just a few lines of code.
To create a rectangular prism and then take a circle out of the center requires just a few lines of code.

OpenSCAD bills itself as “The Programmers Solid 3D CAD Modeller”. It does this job pretty well, which is probably why I like it so much. Like POV-RAY, which I’ve used before, you create primitive objects–spheres, boxes, cylinders, etc.–and add or subtract them from one another to create the three-dimensional shapes you want.

Unlike the more graphical 3d modeling programs, like SketchUp (which I’ve played with in the past), in OpenSCAD you have to specify in the script the exact dimensions of your primitives, and how to rotate them and translate them to get them where you want them to be in space. This makes it a great language to use in geometry class, or anywhere else you want students to learn about co-ordinate systems.

The script to create the box with a circle cut out of it (see figure above) is:

difference() {
	cube([40,40,50], center=true);
	sphere(25);
}

Vpython requires students of make similar geometric movements of their objects and renders them nicely in 3d, but given the incentive that they can print up a tangible result of their work, I’d be willing to bet that students, especially younger ones, would be quite motivated to work with OpenSCAD. Vpython does retain the advantage that it is able to do animations, while you can only print static objects. In addition, OpenSCAD is more of a scripting language than a programming language like Python (see some of my Vpython programs here).

Student models a "fez" he modelled in OpenSCAD. This was the first object, other than the test cube, that was printed on our 3d printer.
8th grader models a “fez” he modelled in OpenSCAD. This was the first object, other than the test cube, that was printed on our 3d printer.

The OpenSCAD documentation is quite good. I also found it easy to find instructions on how to create a 3d object from a black and white image, simply by extruding it in the third dimension (by iamwil on the Cubehero Blog and by I Heart Robotics).

One note: I’m using OSX 10.6.8 at the moment, and the current version of OpenSCAD does not work on it. Since I’m loathe to upgrade, I had to use the prior release of OpenSCAD-2013.06.dmg.

Building a 3d Printer

Building the printer.
Building the printer.

I took three students to a workshop were we’re building a 3d printer. It’s run out of the Whitfield School. We spend today putting together the electronics to run the five motors we need to get the thing to work, and starting to put the frame together.

The frame is based on the RepRap Prusa i3 (via DIY Tech Shop), and the plastic parts that hold the rods, electronics and other metal bits together are 3d printed themselves.

Students learn to solder.
Students learn to solder.

The motors is run by an Arduino, which is great because I’ve been thinking about using one to operate the doors to the chicken coops.

Notes on hardware

Microcontroller

  • Arduino Mega 2560 R3
  • RAMPS Shield 1.4
  • RepRap StepStick Pololu A4988 (I think) stepper driver (plus heat sinks)

Filament: 1.75 mm PLA

The Final Product

My electric guitar.
My electric guitar.

So I made the guitar. The guitarbuilding group make it hard to make a bad guitar, with the beautiful materials they provide, and their expert instruction, however, I’m inordinately proud of myself as well.

Indeed, as more and more of the elements fell into place over the course of the week, it really brought home the affective power of a) building something with your own hands, and b) the iconography of the electric guitar.

Now I have to figure out the logistics of doing this at Fulton. But as the workshop instructors pointed out, even if you don’t have students build one, just bringing the electric guitar into the classroom and saying, “Today we’re going to study sound,” really catches the attention.