Arduino Data Logging Shield With Real Time Clock Timestamp ... and Telemetry, Sensor Network

by JD_K in Circuits > Arduino

6975 Views, 33 Favorites, 0 Comments

Arduino Data Logging Shield With Real Time Clock Timestamp ... and Telemetry, Sensor Network

Title Blue.jpg

A data logger is a device that captures sensor information and stores it for later use. It can use multiple sensors and collect data over time to record events, analyze for trends or help predict events. Using a microcontroller such as an Arduino is an excellent device to do this. In this Instructable, I show how to use an Arduino Uno for data logging complete with a DS1307 real time clock (RTC). Although this project is a DIY shield, I included Fritzing images for setting up on a breadboard if you do not want to solder. Multiple sensor readings can be saved to the microSD card along with a time (down to the second) and date from the RTC. The file type is a “comma separated values” (.csv) file that can later be opened in a spreadsheet program like Excel for analysis and graphing.

The shield was also purposefully designed with wireless data transmission in mind so that it can transmit or receive the data wirelessly to create a simple telemetry system. In fact, it is able to receive data from multiple transmitters and log them all on the microSD card in a single file. If you have tried to use a microSD card reader and an nRF24L01 together on the same project, you may have found that both often use the “Serial Peripheral Interface” (SPI), and are meant to be able to work together in the same circuit. However, many people have had trouble getting these modules working together. I will include a link to a video explaining a hack to the microSD card reader module that will fix this problem.

This Instructable covers the connections and sample code for a number of demonstrations on data logging with a timestamp. Simple sensor setups are used below to demonstrate how data can be recorded. The last two demonstrations involve wireless transmission. I will build upon a previous Instructable about using the nRF24L01 (link). You should have a look at that before trying Demo 3 and 4. 

It is assumed that the reader has some knowledge of using an Arduino already, including downloading and using libraries from the internet. If not, there are many tutorials on getting started with the Arduino. That being said, I have included many pictures of the connections, links to other resources for important concepts and video demonstrations explaining the code and illustrating the function of the data logger.

Supplies

Components for data logging with an RTC time stamp

  • Arduino Uno
  • MicroSD card module with SPI communication
  • MicroSD card
  • DS1307 RTC integrated circuit
  • 32.768 kHz crystal oscillator (for DS1307)
  • 2032 3Volt coin cell
  • 2032 coin cell holder
  • Resistors 4.7 kOhm, x2
  • Jumper Wires
  • Solderless breadboards for prototyping


Components for the Demonstrations, but you could use other “sensors”:

Demo 1: Digital Hall effect sensor (3144), 10 kOhm resistor, and a magnet

Demo 2: Thermistor, 10 kOhm resistor, LED and 220 Ohm resistor

Demo 3: A 2nd Arduino (Nano, or UNO), nRF24L01 (x2), and 10 uF electrolytic capacitor (x2)

Demo 4: A 3rd Arduino (Nano, or UNO), additional nRF24L01 & a 10 uF electrolytic capacitor, additional 10 kOhm resistor, a light dependent resistor, and a tactile push button


Additional Components I used for this Shield

  • Perforated circuit board (5x7 cm, double sided)
  • IC socket 8-pin dip (for DS1307)
  • Wires (I used 26AWG, various colours)
  • Male header pins
  • Jumper connector (connects two adjacent male header pins)
  • Set of stackable header pins for Arduino**
  • AVERY8160 address label (one sheet, for labeling the Arduino pins)


Tools for making the shield

  • Arduino IDE (free downloadable software to program the Arduino)
  • Soldering iron and solder
  • Wire for connections (e.g. single stranded 26 AWG)
  • Side cutters
  • Pliers 
  • Needle nose pliers
  • File
  • Precision screw drivers
  • Hobby knife
  • Digital multimeter (useful for checking connections, troubleshooting, etc.)


* In my original build, I have two slide switches and a number of through-hole capacitors (100 nF ceramic, 100uF electrolytic, 10uF electrolytic). I was trying to include an option of using an Arduino digital pin to power the microSD card reader, but I have not been able to get it reliable enough to suggest doing this. I tried different capacitors not knowing why it wasn’t working and ended up with this combination of capacitors through trial and error until it seemed to work, but not always. 

** Although I used stackable headers in this shield, I prefer NOT using them when I make stackable shields. I find the pins too flimsy and too easily bent out of alignment. Instead for making an Arduino UNO Shield, I prefer using female header pins on the top of the shield and connecting them to adjacent rows of male header pins underneath. See the section “Making the Shield” of a previous DIY shield (link)

Before putting all the components together, whether you plan to make the shield or just put components on a breadboard, you should make sure you can get each component working with the Arduino. So, in the next several steps I will discuss the RTC and the microSD card reader module separately.

The DS1307 RTC

DS1307 components.jpg
DS1307_only.jpg
DS1307_bb.jpg
uRTCLib_example.jpg

The RTC is used to include a timestamp with any data that is being recorded. The DS1307 is an older RTC chip, but it is still available and inexpensive. It requires a 32.768 kOhm crystal oscillator to keep accurate time, and this is sometimes sold together with the chip. You will want the coin cell battery as a back up. By using the backup power, the chip will keep time even if you remove the shield or if you unplug the Arduino. Typically, the battery will last for years. Then when you provide the 5V power from the Arduino, you can request the time and date from the RTC. This is done using I2C communication, so the SDA and SCL pins will need to be connected to Arduino’s A4 and A5 pins, respectively. It is a good idea to also connect the 4.7 kOhm pull up resistors to these pins (see connections below). There is a square wave pin (SQW) on the chip, which can provide a pulse at 1 Hz, which I haven’t ever needed but I brought this out to a single male header pin on the shield in case I ever did.

The Connections

This table lists the pins of the DS1307 with the pin’s respective name in parentheses, and their connections to other components:

DS1307               32.768kHz crystal                         Coin Cell            Arduino

1 (X1)                to one electrode of crystal

2 (X2)                  to other electrode of crystal

3 (Vbat)                                                                         to V+                                                 

4 (GND)                                                                         to V-                    and to Arduino GND                                   

5 (SDA)                                                                                                        to A4; and Vcc via 4.7kOhm resistor

6 (SCL)                                                                                                         to A5; and Vcc via 4.7kOhm resistor

7 (SQW) not used

8 (Vcc)                                                                                                         to Arduino 5V

The Code: uRTCLib Library

The code I used for setting and accessing the time on the RTC, is the uRTClib library. You download it from Github here (link). It works for what I was doing, and was smaller than some of the other libraries. Download the zip file, save it where you will be able to find it. In the Arduino IDE software, go to Sketch > Include Library > Add Zip Library… and navigate to the zip file. You may need to close the Arduino IDE and restart it. You should be able to find the example sketches (see picture) for it under:

File > Examples > uRTCLib > uRTCLib_example

Setting the Clock:

In this sketch you find a line similar to:

                    rtc.set(0, 42, 16, 6, 2, 5, 15);

This is the line that is used to set the clock. The order of the numbers is:

rtc.set(seconds, minutes, hour, day of the week, day of the month, month, year);

The values for day of the week are 1 to 7, corresponding to Sunday to Saturday.

Thus, to set the clock:

  1. In the example code mentioned above, set these numbers for a couple minutes into the future, using a clock known to be accurate to the second. Then upload the sketch to your Arduino.
  2. Press the reset button on your Arduino Uno board about two seconds before that time (in the case above 16:41 and 58 seconds), because it takes about two seconds for the Arduino to restart and go through its bootloader and reset the time on the RTC again. So, when that is done the actual time will have caught up with your “future time”
  3. Immediately comment out this line with the two slashes, and re-upload the sketch. The line will look like this: // rtc.set(0, 42, 16, 6, 2, 5, 15);
  4. Now you can open the serial monitor with this example sketch and see if the time is indeed accurate.

By doing this, you should have a pretty accurate time set in your DS1307. Because the updated programming in the Arduino no longer has the active rtc.set line of code after it is “commented out”. So, it won’t keep resetting the RTC to that time which is now in the past (16:42:00 on May 2nd) each time you reset, or each time you open the serial monitor. If you have the coin cell connected, (make sure all the ground lines between the DS1307, battery and Arduino are connected), you can unplug your Arduino to power off, wait a bit, plug it back in and open the serial monitor to see what time it is. It should still be running at the correct time because the backup battery keeps the RTC running in the background even when there isn’t the Arduino’s 5 volts supplying it.

Checking the Time on the RTC

In the sketch you will see a line of code:

 rtc.refresh(); 

This is the function that the Arduino uses to request the time from the RTC which is then stored at that point in the Arduino’s memory. Call this immediately after data is acquired so the time stamp will be as close to when the data was collected as possible. Then you can get each value from using any or all of the following, which gives each value as a byte variable:

  • rtc.year()
  • rtc.month()
  • rtc.day()
  • rtc.hour()
  • rtc.minute()
  • rtc.second()
  • rtc.dayOfWeek()

The last thing I will mention about the rtc.refresh(); function is to not call it continuously, multiple times per second, this can slow it down the RTC and make it inaccurate.

You now have the RTC working. We will be using it for our data logger, but this chip can be used to make all kinds of interesting Arduino clocks, alarms, timers, etc.

MicroSD Card Reader Module

microSDModule.jpg
microSD_bb.jpg
CardInfo in SD Library.jpg

These modules are very common, and use the same microSD cards as cell phones. They are a good option for storing simple serial data that you can look up later on a computer. The Arduino can also access data from them but that will not be used in this Instructable. The module I have uses the serial peripheral interface (SPI) communication which is different than the I2C used in the RTC. SPI requires a different set of pins including a chip select (CS) pin which should be connected to an Arduino digital pin (I used pin 9).

The Connections

Here are the basic connections you can make with the module on a breadboard:

Arduino              microSD

GND                    GND

5V                        Vcc*

D12                     MISO

D11                     MOSI

D13                     SCK

D9                        CS

*Note that the microSD card reader operates on 3.3V, but this module has a 3.3Volt regulator and a small chip that acts like a level shifter, so it is safe for the module to connect the Arduino’s 5V pin and the digital pins which on the Arduino are also 5V. For the shield I put an option to use a digital pin to power the module, more on that later.

Testing out the Module: Cardinfo example from SD Library

The easiest way to test the module, is to open the Arduino IDE, and upload an example from the SD library which is included with the Arduino IDE. I suggest uploading the CardInfo sketch in the Arduino IDE by going to (see picture):

File > Examples > SD > CardInfo

 

In that sketch, you will see a line:

              const int chipSelect = 4;

Change the ‘4’ to ‘9’ as pin 9 is the pin connected to the module’s CS pin in my setup. Insert a card into the reader, upload the sketch, then open the serial monitor. It should produce a bunch of lines that include various information about the microSD card. If you can see that, then I think it is safe to assume you have your module working with the Arduino.

Putting It All Together on a Breadboard or Shield

DIY datalogger Shield.jpg
Shield analogPin Sideview.jpg
Shield_sideview.jpg
SideView Bent pins.jpg
Underside.jpg
DS1307_miscroSD_bb.jpg
Shield_uSd_DS1307rtc_bb.jpg
Shield_uSd_DS1307rtc_schem.jpg
UnoPins2.png
UnoPins1.png

At this point you would want to put all the components together on a breadboard and see that they all work together as intended. You can try the two data logging demos in later sections to see that the RTC and the module work together. If you want to keep your project on a breadboard, it should work. However, before proceeding to those demos, I would like to discuss the shield, and troubleshooting including the card reader hack if you plan to use other SPI devices.

About the Making the Shield

Creating a shield is very convenient if you plan to use the data logging more than once. As you can see there are many connections so making them permanent will save you time in the future and free up your breadboard for other things. You can buy custom-shaped circuit boards to be used for Arduino Uno shields, but I used a generic perforated board. This board is a 5x7 cm perf board, trimmed to size for the Arduino. To trim the perf board, centre it width-wise across the Uno pins and mark the side rows you plan to cut off. If you plan to use stackable header pins, you should end up with 20 columns of holes, such that the 1st and 20th columns line up with the Arduino pins and you have an extra 2 columns of pins on either side. Place the perf board on a cutting board and with a ruler and knife, cut several times along the column of unwanted holes on either side of the perf board. After scoring the board like this you should be able to break off the sides off with a pair of pliers, and file the sides and corners.

Place the components on the perf board to figure out the spacing keeping in mind that any wires on the top side of the board will need holes so that the connections can be soldered underneath. Keeping wires straight with any bends at 90 decrees (which I didn’t do when making this shield) will keep the board looking neat and tidy. I put the microSD card reader extending beyond the front of the shield, but still within the footprint of the Arduino Uno. This helped to save space on the board, as well as making it easier to push the card in/out of the module than if it was resting on the perfboard.

As you probably know, the pin spacing of the digital pins is strange on the Arduino Uno. The pitch is generally 2.53mm (0.1 inch) for the holes which is standard, except between D7 and D8. Since I used the generic perf board with the standard spacing, I had to bend some of the pins. This is the trickiest part of a DIY stackable shield, bending all the pins of D8 to D13 and the 4 other pins after D13 (GND, AREF, SDA & SCL). In the sideview image of the shield, you can see the long pins were bent back, inserted into the board, soldered to the board, then bent forward. Use the Arduino to see that the pins align from the top of the board where the female end is, and bottom where the long male pins are.

I have included a Fritzing diagram of the connections, as well as pictures of the shield itself. Once you are satisfied with the layout, solder all the connections.

Also included is a couple of png images that I use to create stickers to denote the UNO pins of the shield. Place them in a Word document. You may have to adjust the size before printing so test print on a plain sheet of paper. I use an AVERY 8160 sheet of sticky labels and print them on a computer. Then with a sharp pair of scissors, cut the labels to side to fit the four sections of pins on the Arduino UNO or shield.

Troubleshooting microSD Card Reader Module

Devices that use the SPI interface should be able to be connected to the Arduino simultaneously with other SPI devices. A common problem with these microSD card modules is that the level shifter chip on the module prevents other SPI devices from working. In my case, the nRF24L01 transceiver would not work when trying to send data wirelessly for data logging (see later demos). I discovered a hack to get around his problem and demonstrated it in a video (video link). Basically, in that video it shows how to separate the MISO connection on the level shifter and then complete the connection directly between the header pin and the microSD card reader with a small wire. In this Instructable, you can see the yellow wire on my module where I did this. If you do not plan to use the nRF24L01 or any other SPI device, then this hack is not necessary.

The other issue I want to include here about the microSD card reader is what I believe is a problem supplying enough power. If you use USB from the computer to power the Arduino, the resultant voltage can be inadequate because the voltage can change over time. Initially, the card reader would not work, and I was troubleshooting with a multimeter, testing voltages were at different point on the shield. Then suddenly it worked without me changing anything. But sometimes, I would go to look at the saved data only to find out that sometimes it did not work. Thinking it was an issue with sudden spikes in power demand or noise on the Vcc voltage, I tried different capacitors and it seemed to work better with the three capacitors I have on the board. Here is some advice related to powering the shield:

  • When you want accurate sensor data, use a battery rather than computer USB power because of the variations in computer power delivery. Analog sensors are dependent on a stable reference voltage.
  • When you want to still use the Serial Monitor of your computer, then power the Arduino with a 9Volt battery or a power supply to the barrel jack on the Arduino. Then plug in the USB cable to the computer and open the Serial Monitor. That way the power will come from the battery, but you can see serial statements on the serial monitor.
  • Using an oscilloscope would help diagnose the power issue. If you don’t have one, then when testing this circuit out on the breadboard try it without the capacitors. If you have trouble with the module working properly, try adding the capacitors including one small ceramic capacitor (100nF) as close to the module as possible and an electrolytic (e.g., 10 uF). (Here is a very interesting video about decoupling capacitors to reduce noise in circuits)


Lastly about the two slides switches. The shield has two slide switches, that I use to provide an option for powering the board. The first switch allows you to change where the module gets power: either the 5V pin or one of the digital pins. The second slide switch allows me to disconnect the capacitors from the digital pin so it does not affect the function of the pin if it were to be used for something else.

The option of using the digital pin for power would allow the opportunity to shut power off to the microSD card reader when not in use by making that pin low (zero volts). This would help to save power if the Arduino is running on batteries. However, the Arduino itself uses a lot of power on its own (~25 mA if using a 9V battery). Therefore, it is better suited to being plugged in rather than battery-powered, in which case reducing power to the microSD card reader is not a priority. Another argument for not using batteries is that if the Arduino runs out of power when you are recording to the card, which has happened to me multiple times when the batteries died, the file gets corrupted and you lose all the data in the file. So, currently I would not recommend the digital pin as an option to power the microSD module directly. If I ever made another shield, I would not try to power the module from a digital pin at all, or use a digital pin to turn on a transistor to power the module.


With those troubleshooting suggestions in mind, it is time to try the data logger…

Demo 1 – Data Logging Events With a Time Stamp

Arduino Data Logger Shield with RTC Time Stamp || Demo 1 of 4 || Hall effect door sensor
Digital Hall Effect_breadboard.jpg
DS1307_miscroSD_Hall effect_bb.jpg

Here is a simple example of the data logger with time stamp. It uses a Hall effect sensor which senses a magnetic field, but you can use anything that gives a similar digital (high/low) signal. The Hall effect sensor in this example gives a low (0 Volt) signal when in a magnetic field, and it switches to a high (5 Volt) signal when the magnetic field is removed. It is polarized so if you try it and it does not work, try a different orientation of the magnet. In our example, let’s say you want to record each time a door is opened and closed. You would attach the magnet to the face of a door near the edge, and mount the sensor on the door frame right next to it. These are close together without touching. When the door is opened, the magnet is moved away with the motion of the door and the sensor switches to a high signal. The Arduino then records on the card that the door was opened and the date and time this event happened. When the door is closed, the sensor changes back to low and the Arduino logs that the door was closed as well as the time. The Arduino is always on and always active, but only writes to the microSD card when the signal is changed.

For this setup, you should have a DIGITAL Hall effect sensor, not a linear one, so that the output is on/off rather than a signal that varies with the strength of the magnetic field. In my case, the part number of the sensor is 3144. You will also need a 10 kOhm resistor.

The Connections

Here are the connections, which must be added to the RTC and card reader module:

Arduino:             Hall Effect Sensor:

5V                        Left pin

GND                    Centre pin

D2                        Right pin; and to the 10 kOhm resistor which is also connected to 5V


You can then download the code below and upload it to the Arduino. I have tried to include many comments so you can get a sense of what the code does. If you want to see a demonstration of it, including a review of the code, have a look at the YouTube video. Essentially though, the Arduino uses pin 2 which is setup as an external interrupt with the line of code:

attachInterrupt(digitalPinToInterrupt(HallPin), interruptFunction, CHANGE);

When an external interrupt is active, then regardless of where the Arduino is in the program, it will detect the signal change from the Hall effect sensor. This will trigger the Arduino to check the time and record it to the microSD card along with whether the door is opened or closed.

The dataString

When the interrupt triggers the data logging the function, rtc.refresh(), is called which updates all the date/time numbers in the Arduino. This data is then formed into a string called “dataString” using a line of code that looks like this:

String dataString = String(rtc.month()) + "," + String(rtc.day()) + "," + String(rtc.hour()) + "," + String(rtc.minute()) + "," + String(rtc.second()) + "," + doorState[doorOpenFlag];

This dataString is not a series of numbers, but a series of characters that represent the numbers. So, for the December, the number 12 which means the 12th month, is recorded as a “1” and a “2”. This is the followed by a comma which will separate these characters from the next character which represents the day, etc. The last part of code “doorState” is an array and adds the letters for “closed” or “opened” depending on what happens with the door. For an explanation of the code, please see the video of the demo 1.

Once this dataString is complete it has all the information of the date, time and the state of the door. So, the file on the microSD card is opened, and this string is added to the file. Each time an event is triggered, the dataString re-written with the new information and is added to the file as a new line of text.

Testing it Out

You can open the serial monitor of the Arduino and see the print statements as you move the magnet. Once you have collected some data, unplug the Arduino, remove the card and insert it into your computer. Some computers have a slot for a microSD card, otherwise you may need a card adapter. Once you have the microSD card inserted, you can open it just like any drive on your computer. There should be a file called DATALOG. This can be opened by a spreadsheet program like Excel, or a program that can open text files like Notepad. You should see lines of numbers which represent the date, time and if the door opened or closed. 


Congratulations, you now have a working data logger with a time stamp.

Demo 2 – Data Logging at Regular Intervals

Arduino Data Logger Shield with RTC Time Stamp || Demo 2 of 4 || Thermistor Temperature Monitor
Thermistor.jpg
Demo2_DS1307_miscroSD_Thermistor_bb.jpg

In this demo, the data logger is recording an analog value at regular intervals, and not using an external interrupt as in the previous demo. The sensor setup includes a simple thermistor and 10 kOhm resistor. A thermistor is an inexpensive component that changes resistance with changing temperature. So, if the thermistor and 10kOhm resistor are placed in series, this makes a voltage divider, and thus the voltage between them will change with changing temperature. The Arduino measures this voltage with an analog input pin and calculates the temperature. Then with the interval you have set up has passed, the Arduino will check the temperature and record the date and time with the temperature reading to the microSD card using a similar dataString to that in Demo 1.

The Connections

You can remove the Hall effect sensor setup and add these connections for the thermistor and 10 kOhm resistor to the RTC and card reader module:

Arduino  Thermistor            

5V                       to one lead of the thermistor

A0                       to other lead of the thermistor and to one lead of the 10 kOhm resistor

GND                   to the other lead of the resistor

Collecting and Viewing the Data

You can upload the code below to the Arduino and view the data on the serial monitor of the Arduino IDE. Again, there is an accompanying demo video if you want to see the review of the code and the demo. After you have collected data, you should be able to connect the microSD card to your computer and open the file in Excel. If you are familiar with Excel, you can sort the data, or graph, look for trends, etc.

If you have gotten to this point, you can experiment with the sketches of the two demos, try different sensors and change the dataString to customize the data logging to your needs. You can remove the date if it is not needed, or change the recording intervals, or you may have multiple sensors working at the same time, etc. Data logging with the Arduino has so many uses and is really useful for a lot of different applications!

Demo 3 – Telemetry: Receive Data Wirelessly and Data Logging

nRF24L01 Telemetry with Arduino Data Logger Shield with RTC Time Stamp || Demo 3 of 4
Inserting nRF24L01 into Shield.jpg
nRF2L01 Pinout_bb.jpg
Demo3 Server_RTC mciroSD nRF24L01_bb.jpg
Demo3 Client_nRF24L01_thermistor_bb.jpg

Telemetry involves transmitting data and not just storing them locally. For this demo, the data logger will only have the RTC and card reader with their supportive components, a wireless transceiver (allows 2-way signals: receive and transmit), but no sensors. You will need to add a second Arduino which will have a sensor (the thermistor in this example) and its own transceiver. It will send data from the sensor wirelessly at regular intervals to the data logger. These data will be recorded to the microSD card with the time stamp of when the data is received. The wireless transceivers used here are the nRF24L01. These are relatively common RF transceivers with a good range. Some people have gotten about 1km in open space with line-of-site between two modules. I have not tired this, but I have used them successfully transmitting to different floors of my house. They can be tricky to get working when you first start using them. I have previously published an Instructable of an adapter for the nRF24L01 to aid in convenience and reliability. Even if you don’t use an adapter, that Instructable discusses some of the common traps when connecting the nRF24L01 to the Arduino, so you may find it helpful. In any case, either add that adapter with a transceiver to the shield, or complete the SPI connections with jumper wires to the shield and power the transceiver with 3.3 volts (see the connections and Fritzing pictures in this step). DO NOT CONNECT the nRF24L01 Vcc pin to 5 volts. Lastly, you should include a capacitor across the transceivers 3.3V and ground (see note below).

Arduino 1 Connections: the Server (Data Logger)

Here are all the connections for the “Server” Arduino which receives data and logs it. It is the same as before but with the nRF24L01:

nRF24L01          Arduino              

GND                    GND

Vcc                      3.3V

MISO                   D12

MOSI                   D11

SCK                      D13

CS                        D8

                             5V to one lead of the thermistor

                             A0 to both the second lead of the thermistor and one lead of the 10 kOhm resistor

                             GND to the second lead of the resistor, and one side of the tactile pushbutton


microSD             Arduino

GND                    GND

Vcc*                    5V

MISO                   D12

MOSI                   D11

SCK                      D13

CS                        D9


DS1307              32.768kHz crystal                         Coin Cell            Arduino

1 (X1)                to one electrode of crystal

2 (X2)                  to other electrode of crystal

3 (Vbat)                                                                         to V+                                                 

4 (GND)                                                                         to V-                    and to Arduino GND                                   

5 (SDA)                                                                                                        to A4; and Vcc via 4.7kOhm resistor

6 (SCL)                                                                                                         to A5; and Vcc via 4.7kOhm resistor

7 (SQW) not used

8 (Vcc)                                                                                                         to Arduino 5V



Arduino 2 Connections: “Client” Sending Temperature Data

Here are all the connections for the “Client” Arduino which sends data:

nRF24L01          Arduino

GND                    GND

Vcc                      3.3V

MISO                   D12

MOSI                   D11

SCK                      D13

CS                        D8

                             5V to one lead of the thermistor

                             A0 to both the second lead of the thermistor and one lead of the 10 kOhm resistor

                             GND to the second lead of the resistor, and one side of the tactile pushbutton

Many of the connections to the microSD card reader and nRF24L01 are the same because they both use SPI communication, but the CS pin for the transceiver is 8, and the card reader is 9. As mentioned above the Vcc pin of the transceiver is connected to 3.3 volts, unless it is plugged into an adapter with a voltage regulator. In addition to the above connections, you should put a 10 uF capacitor (or similar value) across the power pins of the transceiver and as close as possible. Remember that electrolytic capacitors are polarized, so the long lead of the capacitor goes to the Vcc (3.3 V) side, and the short lead (with the strip on the capacitor) connects to the ground. Failure to do so could cause the capacitor to pop. The voltage rating of the capacitor is unlikely to be an issue since we are dealing with only 3.3 volts.

The Code: Adding the RadioHead Library – Reliable Datagram

For the background programming of the nRF24L01, I relied upon the RadioHead library (link), specifically the Reliable Datagram examples in the nRF24 section from the RadioHead library. In the Reliable Datagram example, one transceiver sends a message and waits for an acknowledgement message back to verify that the message was received. If you download this library, there are several smaller libraries included, such as, <RHReliableDatagram.h> and <RH_NRF24.h>, which are used in this demo.

In this demo, the “client” Arduino reads the analog signal from the thermistor and calculates a temperature. This float value must be converted to a string, so it can be sent wirelessly (for an awesome tutorial about converting variables to strings and parsing strings, see this video, especially “Lesson 7”) which is sent along with the “address” of the “server” who it is sent to and the address of client itself. The “server” Arduino receives the data, sends an acknowledgement to the address of the client, and saves the data along with the time stamp. If the client does not receive an acknowledgement from the server, it can resend the message a number of times which helps improve the reliability of the data transfer.

You can upload the code to each Arduino below. The code is a combination of the programming from demo 2 and the nRF24_reliable_datagram_server example. Using all these components and libraries gets to the limits of the memory storage of the Arduino. If you are tinkering with the sketch and run into a low memory issue, some ways to get around this a remove the Serial.print (and Serial.println) statements. They are useful for troubleshooting your project, but are not strictly necessary for the wireless data logging. The other option is to specify to the Arduino IDE to save the text for serial print statements to flash memory (which I did). This is done by changing statements from, for example, Serial.print(“Hello”); to Serial.print(F(“Hello));. Again, similar to the previous demos, for more information about the code, and the demonstration, see the embedded video for this section.

When you have set up both Arduinos, and power them both, you can view the serial statements from either Arduino to see what it is doing. If you want to view both at the same time, you will have to open both separately. If it isn’t immediately working, try resetting one or both Arduino. After you have collected some telemetry data, unplug the server being sure that you don’t do it when it is receiving/recording, and you can view the data on your computer.

Demo 4 – Telemetry: Multiple Wireless Transmitters, One Data Logger

Demo4 Client3_nRF24L01_LDR_button_bb.jpg
Arduino Sensor Network with nRF24L01 Telemetry and DIY Data Logger Shield || Demo 4 of 4

This last demo is an attempt at a sensor network. It is similar to the previous demonstration, but there are two clients, meaning two Arduinos, independently sending data wirelessly to the server for data logging. Each client, and the server, should have a separate address. In code you will see that, the server is still 1, and the Arduino with the thermistor is still 2, and the third Arduino is the second client and has an address of 3. The second client is set up with an nRF24L01, a light dependent resistor (instead of the thermistor), a 10 kOhm resistor, and a push button. This client will only send data when the button is pressed. It measures the analog voltage between the LDR and the 10 kOhm resistor and sends that value. The brighter the light on the LDR, the higher the analog reading.

About the Code

The benefit of the Reliable Datagram is that with the address of each unit, then only the intended target responds to the message. And if there is a collision where both clients are trying to send data simultaneously, one or both will not get an acknowledgement. So, they can resend the data to improve the reliability. The RadioHead library randomizes the delay before resending to minimize the chance of another collision.

Since this system is taking data from two different clients, the set up for the data string looks like:

Month,Day,Hour,Minutes,Seconds,Sender,Celcius,LDR

The date and time columns are the same, but at the end there is a column “Sender, to record which client sent the data, and it also has separate columns for temperature and LDR data. So, when the data arrives, the server records the address where the data came from, and saves the data in the correct column depending on the type (temperature or LDR). Therefore, when writing the code for the server, you must know what data type each client sends, so that the data string has the data in the appropriate column before saving to the microSD card.

Arduino 3 Connections: Second Client Sending LDR Data

Here are the connections for the second client:

nRF24L01          Arduino

GND                    GND

Vcc                      3.3V

MISO                   D12

MOSI                   D11

SCK                      D13

CS                        D8

                             5V to one lead of the LDR

                             A0 to both the second lead of the LDR and one lead of the 10 kOhm resistor

                             GND to the second lead of the resistor, and one side of the tactile pushbutton

                             D2 to the other side of the tactile push button

Download the two example sketches below. One is uploaded to the server (data logger) to replace the programming from Demo 3, and the other code is for the second client. Try placing the Arduinos in different rooms or floors and see if the data can transmit through the walls in your home. Once you have collected some data, you can view them on your computer.

List of Links

I referred to a number of links in this Instructable, here is a summary of links, plus one or two more:

Conclusion

A couple of suggestions to include that I did not think of back when I made the shield. If the card reader tries to open the file as the power turns off or you remove the card, the file gets corrupted and you will lose all your data. So, there should be a button that momentarily prevents the card reader from opening the file, so you can safely turn the data logger off. Also, if you plan to use batteries, a way of monitoring the battery and safely shutting off the device rather than letting the Arduino crash when the battery goes low. I already published an Instructable on a shield for that too… 😊


Congratulations if you made it to the end. This was a long Instructable. However, I hope it gives you some ideas of data logging or making your own sensor network. There are so many possibilities and uses with this basic setup as a model: GPS tracking, environmental monitoring, monitoring the habitat of your pet lizard/fish/dog, counting inventory, testing your other electric device, etc.  Please feel free to leave a comment or a question, or I would love to hear if you have an interesting idea of how this could be used. Thanks for reading!