Getting to Know Arduino Part 2: Collecting Data

Send to Kindle

By Paul Hubbard
Photos by the author.

In case you missed Part 1 of this three-part series, you can find it here.

In part two we tackle the circuitry, add an Arduino library and get the code running on the device. At the end of this, you’ll have JSON-encoded data streaming to your computer.

Sensors
Let’s look at the sensors to start with. I chose these, but feel free to substitute whatever you have on hand.

Temperature
The DS18B20. A bit pricy at almost five dollars each, but very precise and accurate, in a compact TO-92 enclosure. It’s important to have accurate temperature readings, as both of the other sensors must be corrected for temperature. If temp is off, the other readings will be too; a very important thing to remember. The sensor is designed for 1-wire busses, where you have a bunch of them connected together and address them by the serial number encoded into each. Think HVAC for large buildings, for example. You can find example code included with Arduino under File / Examples / OneWire / DS18x20_Temperature

Compile and run this example, then open the Serial Monitor and cut and save the serial number it finds…

The 18B20 is connected to power with a 4.7K pull-up on the data line, and also to power and ground. Output (middle) pin goes to digital pin 8 on the Arduino. Here’s a circuit from the datasheet:

Light
A weather station doesn’t need to measure light levels really, but the TEMT6000 is a perfect example sensor for us. No extra parts needed, simple analog output, fast responding and simple. Connect Vcc to +5 from the Arduino board, ground to Arduino ground, and the sensor output to the Arduino analog pin zero.

Humidity
Relative humidity
is actually not all that simple, there are sensors for condensing or non-condensing environments, and most have to be calibrated for ambient temperature as well. I’ve chosen the $15 Ohmic SC-600, which I have had good luck with in the past. There are simpler sensors out there, so feel free to substitute another if you like. I tried the combined temp and humidity sensor from Seeed but was unimpressed with the low-res data.

The SC-600 has a 400Hz excitation signal that you have to filter out, otherwise your data will be junk. Ommic suggest an RC lowpass filter in their datasheet:

Connect the middle pin to analog input pin one on the Arduino.

Circuitry: Wiring It Up
One of the lovely things about the Arduino is that it provides 5 and 3.3 volts, perfect for powering sensors and small LEDs. Here’s my circuit, on the small protoboard:

I connected +5 to the top row, ground just below it. You can see analog 0 and 1 connected to the light sensor and humidity filter, and please disregard the fact that the temperature sensor was connected to digital pin 10 and not 8 – outdated picture, so connect yours to pin 8.

Another angle:

Arduino code

Installing Arduino Libraries
Unfortunately, the OneWire library is not included with the Arduino library, but installing it is both simple and a useful skill to have, as there are other useful libraries out there.

OneWire, available here, is a zip file. Download it, and then open a terminal window:

cd /Applications/Arduino/Contents/Resources/Java/libraries
unzip ~/Downloads/OneWire.zip

(Of course, if you’re on Linux or Windows the path will differ)

That’s it! Simply placing the files into the libraries directory makes them available, along with examples too. Restart the Arduino app, and if you’ve done it right there will be a new category under File / Examples / OneWire.

DS18B20 serial number
Here is where it starts to get fun! The first thing we need to do is setup the temperature chip. As I said earlier, the OneWire chips are designed to run many off of a single bus, so each one has a unique serial number in it. We need to get that serial number to add to our code.

Open the Arduino IDE, go to File / Examples /  OneWire / DS18x20_Temperature

Our circuit has the chip connected to pin 8, so change the line

OneWire ds(10);

to

OneWire ds(8);

Click the ‘go’ button to upload and run it:

Once it’s compiled, uploaded and running, open the Tools / Serial Monitor and look for the serial number. Here’s mine:

The bit you need is the “ROM = “. Save a copy.

Arduino source code
As I’m doing with most projects these days, the source code is all hosted on Github. If you don’t want to install Git, you can manually download the file by clicking on the ‘Raw’ button on each file, for example https://raw.github.com/phubbard/hws/master/csl/csl.ino

Download that, and open it in the Arduino IDE. Next, we’ll need to enter your temperature sensors’ address as hex into line 31 of the Arduino code:

byte addr[8] = {0x28, 0x2a, 0x34, 0xb7, 0x03, 0x00, 0x00, 0x0F};

Run it, and open the Tools / Serial Monitor:

Boom! As you can see, the data uses a simple string-based, single-line encoding. This JSON makes it easy to parse while remaining efficient and human-readable.

This entry was posted in Computer Science, Electronics, Tools, Uncategorized. Bookmark the permalink.

2 Responses to Getting to Know Arduino Part 2: Collecting Data

  1. Jim Hannon says:

    Looks like the last picture is missing. Screen Shot 2012-02-02 at 11.38.15 AM.png ¬

Add Comment Register



Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>