© Paul Bradt and David Bradt 2020
P. Bradt, D. BradtScience and Engineering Projects Using the Arduino and Raspberry Pihttps://doi.org/10.1007/978-1-4842-5811-8_2

2. Data Logging Basics

Paul Bradt1  and David Bradt2
(1)
Houston, TX, USA
(2)
Houston, USA
 

In Star Trek, the characters used tricorders to capture data. The technology we have today is not as advanced as that in Star Trek , but still very amazing. The next step in this book is to start using this technology to obtain scientific information. This chapter covers information that is very important for someone just starting with either the Arduino or Raspberry Pi and provides a few easy methods to save data using them. This is called data logging and is useful to save the information to be utilized at a later date. The STEM principles in this chapter include

Science

Measuring temperature of the environment.

Technology/Engineering

Using the Arduino, Raspberry Pi, and two types of temperature sensors. Gain an understanding on the difference between analog and digital sensors.

Mathematics

Programming temperature conversions and graphing the data.

While the reader may want to skip this chapter and go on to other more advanced projects, it is recommended to look over these projects and try them if the reader is unfamiliar with the Arduino or Raspberry Pi.

Data Logging with the Arduino

The easiest method shown in this book to log and capture data is to set up an Arduino to send data to the computer. Then use the IDE serial monitor to observe and save the data. Once the correct data is obtained, it is easy to copy using “Ctrl+C” after it is highlighted and then paste it into a spreadsheet or another document.

The parts needed are relatively straightforward to use the Arduino Uno and the serial monitor to gather data from a simple temperature sensor. The sensor used in this project is the MCP9700. It is a relatively inexpensive device and in this case is configured just like a standard transistor. This device is an analog sensor, which means the output is directly related to temperature and it can vary infinitely over the range. There is a flat side, and the leads must be connected as shown in Figures 2-1 and 2-2.

The parts needed are
  • Arduino Uno

  • MCP9700 temp sensor (output is 10 mV per oC with a 500 mV offset)

  • Miscellaneous wires to connect to the sensor
    ../images/488244_1_En_2_Chapter/488244_1_En_2_Fig1_HTML.jpg
    Figure 2-1

    Arduino and Basic Temperature Sensor

    ../images/488244_1_En_2_Chapter/488244_1_En_2_Fig2_HTML.jpg
    Figure 2-2

    Arduino and Temperature Sensor Schematic

Listing 2-1 provides the basic code that the user will need to upload via the IDE. This code outputs the data in degree Celsius along with the raw data from the sensor.
//SN102_Temperature
//Original code modified from Arduino Projects to
//Save the World
// Published by Apress
//Part 1 set up of parameters
int ADC0;
int MCPoffset = 500;
//Part 2 Sets serial port communication
void setup(){
  Serial.begin(9600);
  Serial.print("SN 102 Output Temp in C 5 sec: ");
  Serial.println(" ");
}
//Part 3 Gets data, converts to Temperature
//Sends the information to the serial port
void loop() {
  getADC();
  float temp0 = calcTemp(ADC0, MCPoffset);
  float Mvolt = ADC0;
  Serial.print("Temp in C ");
  Serial.print(temp0,0);
    Serial.print(" Raw Input ");
  Serial.print(Mvolt,0);
  Serial.println(" ");
  delay(5000);
}
void getADC() {
  ADC0 = analogRead(A0);
}
float calcTemp (int val,int offset) {
  return((val*4.8828)-offset)/10;
}
Listing 2-1

Arduino SN102 MCP9700 Temperature Sensor Code

The preceding code has three main parts. They are labeled as Parts 1, 2, and 3. The first part sets up parameter which is MCPoffest for this type of temperature sensor. The second part starts up the serial port. The third part of this code starts the loop that gets the data from the sensor, converts the data from the sensor to Celsius temperature, and then posts it to the serial monitor.

The best way for the reader to learn the symbols and the code language is to try several simple pieces of code, Blink being one which is located in the Arduino examples folder in basics. This simple code does not require any wiring; it blinks an LED that is located on the Arduino. Gaining experience and looking at these simple pieces of code gets the reader on the path of acquiring the ability to read code.

Also note in the preceding code the symbol // is used to comment out a line so it does not run. This is helpful to explain the code and document aspects.

Once the Arduino is running, it is easy to access the data from its serial USB connection to the computer. Simply click the Serial Monitor button on the Arduino IDE tool as seen in Figure 2-3.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig3_HTML.jpg
Figure 2-3

Serial Monitor in the Arduino IDE

An example of serial data from Arduino is shown in Figure 2-4. Note it is highlighted which allows it to be copied using “Ctrl+C” and then pasted into another file using “Ctrl+V.” The investigator may need to unplug the Arduino and stop the data coming across the serial port to capture it as it will continue to update as long as the Arduino has power. Figure 2-4 exemplifies the type of data and information sent over the serial port to the computer using Arduino as the bridge. The investigator can tailor the information inside the Arduino program so that the important data can be selected, formatted, and sent over the serial line to the computer.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig4_HTML.jpg
Figure 2-4

Highlighted Data in the Serial Monitor

A new feature in the Arduino IDE serial monitor is the ability to have it apply a time stamp each time data is taken. This does not require any code and makes it very easy to capture this information.

After copying the data and inserting it into a spreadsheet, the user needs to select how to limit (increment) the data. This means how the data is separated so the spreadsheet determines how to place it in individual cells.

Once copied and pasted into a spreadsheet, it can be displayed as a graph. Figure 2-5 shows what a common graph looks like in Excel or any other spreadsheet program.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig5_HTML.jpg
Figure 2-5

Excel Graph of Temperature Data

Another method of data logging will be discussed in a later chapter using a data logging shield. This board allows the user to set up the Arduino so it will collect data and save it in a file on an SD card on the shield without being connected to the computer. The SD card can be removed later from the Arduino shield to download the data to the computer.

Data Logging with the Raspberry Pi

The Raspberry Pi is a very low-cost technological breakthrough in a real personal computer that provides a lot of options for capturing and managing data. In this example, the authors will demonstrate how to capture and log data in a very similar way as was used on the Arduino in the previous section. One advantage of the Raspberry Pi is it has a built-in spreadsheet so that the data can stay on the device.

This project uses a different temperature sensor that is a bit more complex.

The parts needed are
  • Raspberry Pi 3

  • MCM 40-pin GPIO breakout board and cable for Raspberry Pi (or equivalent)

  • DS18B20 waterproof temperature sensor (one-wire temperature sensor)

  • 4.7K ohm resistor

  • Miscellaneous wires, proto-board, and terminal strips

The company MCM makes a 40-pin GPIO breakout board that has the power channels set up nicely for use (Figure 2-6 shows the 5 V connection on the left and 3.3 V connection on the right). There are other GPIO breakout boards available that are similar, but this one has the 5 V and 3.3 V connections directly to the proto-board which is helpful.

This project uses a digital sensor. It has additional circuitry inside the device and outputs a digital value given the temperature. It increments in whole steps over the range. Since it is digital, the Raspberry Pi can readily accept the digital signal. The setup of the Raspberry Pi and the MCM breakout board connected to the temperature sensor is shown in Figure 2-6.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig6_HTML.jpg
Figure 2-6

Raspberry Pi Connected to DS18B20 Temperature Sensor

Figure 2-7 shows the numbering of the IO ports. The 3.3 V and ground can be connected to any of the ports that provide those items. Port 1 is 3.3 V and port 6 is a ground.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig7_HTML.jpg
Figure 2-7

Schematic of Temperature Sensor and Raspberry Pi

The following link at Adafruit has a great example to connect a one-wire temperature sensor to a Raspberry Pi:

https://learn.adafruit.com/adafruits-raspberry-pi-lesson-11-ds18b20-temperature-sensing

One thing that is required is to set the Raspberry Pi configuration to one-wire. To do that, you need to open the terminal window and then type in the command sudo raspi-config to open the configuration tool described in Chapter 1. Then scroll down and select Interfacing Options which is 5. Another window will open and then scroll down to select one-wire. Select yes to enable and press Enter. In an older Raspberry Pi, you may need to use the Nano text editor to modify the config.txt file directly.

Listing 2-2 is the code used to scan the one-wire bus and capture the digital data sent to the Raspberry Pi.
#PI_SN001_Basic_1wire
#Basic Pi one wire Temp sensor code
#Part 1 imports code modules
import glob
import time
#Part 2 Configures and opens the device
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines
#Part 3 reads the device data on the bus
def read_temp():
    lines = read_temp_raw()
    while lines[0].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) / 1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        return temp_c, temp_f
#Part 4 prints the data in the monitor pane
while True:
    print(read_temp())
    time.sleep(1)
Listing 2-2

Raspberry Pi Code PI_SN001 Basic 1 Wire Code

The preceding code consists of four parts that are labeled Parts 1, 2, 3, and 4. Part 1 imports the code modules that will be used. Part 2 configures the devices so the Raspberry Pi knows what is connected to the one-wire bus. Part 3 reads the device data, and then Part 4 records the value and sends it to the monitor pane running on the Raspberry Pi. In Part 3, the code takes the raw value and converts it to both degree Centigrade and Fahrenheit. The reader may notice the “#” symbol used in the code; this is a comment line and the computer ignores it. This is helpful to explain the code and document aspects.

To get it to run, go to Programming, open Python 3 or Thonny, find the file you saved it under, and open it.

Once in the program, select Run module, and then the data will start flowing.

Figure 2-8 shows how the code and data will look.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig8_HTML.jpg
Figure 2-8

Code Running and Capturing Data in Python 3 Development Tool

The data on the left side can be highlighted and then copied over to a spreadsheet.

The authors also used the Thonny development tool (see Figure 2-9) which is both a compiler and debugger. A code debugger is a useful feature which lets the reader step through what the program has in memory for variables and really helps find flaws in the logic.
../images/488244_1_En_2_Chapter/488244_1_En_2_Fig9_HTML.jpg
Figure 2-9

Code Running and Capturing Data in Thonny Development Tool

For readers who want to explore more about the Raspberry Pi and how it works with the one-wire devices and the I2C bus, this site has some good explanations and additional examples:

https://learn.sparkfun.com/tutorials/raspberry-pi-spi-and-i2c-tutorial/all

If the reader wants to create a data storage location, Listing 2-3 takes the data, creates a file if it does not exist, and then saves the data to the file. If there is a file, it just adds the data to it. This would provide a permanent record of the data if the reader is capturing a lot or if the Raspberry Pi is running unattended. This code is structured in a very similar fashion as Listing 2-2 and has very similar parts.

Note

Make sure The following bolded lines of code are on one line:

#Pi_SN001B
#Pi code to save data to file
#Code developed by Paul Bradt
import os
import glob
import time
from datetime import datetime
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file= device_folder + '/w1_slave'
sensor = 0
#To add a sensor need to change the number
def read_temp_raw():
    f = open(device_file, 'r')
    lines = f.readlines()
    f.close()
    return lines
def read_temp(sensor):
    lines = read_temp_raw()
    while lines[sensor].strip()[-3:] != 'YES':
        time.sleep(0.2)
        lines = read_temp_raw()
    equals_pos = lines[1].find('t=')
    if equals_pos != -1:
        temp_string = lines[1][equals_pos+2:]
        temp_c = float(temp_string) /  1000.0
        temp_f = temp_c * 9.0 / 5.0 + 32.0
        dt = datetime.now()
        sensor_Num = str(equals_pos) + " Sensor"
        return sensor_Num, dt.isoformat(), temp_c, temp_f
def main():
    while True:
        f= open("temp_sensor_readings_RevA.txt","a+")
        #a+ parameter tells open to append every time
        # the program runs.
        #Otherwise create a new file
        #Start a new for loop for each senosr here?
        lst_mixed = read_temp(sensor)
        #return as a tuple
        toWrite = ', '.join(str(x) for x in lst_mixed)
        #Add all the data from tuple into a single
        #string
        f.write(toWrite + " ")
        print(toWrite)
        #f.write for file writing and print for
        #terminal.
        time.sleep(.3)
        f.close()
if __name__ == "__main__":
    main()
    #This is necessary to get the program to run
    #otherwise it will just execute
    #no code.run
Listing 2-3

Raspberry Pi Code PI_SN001B Advanced 1 Wire Code/Data File

The Arduino and the Raspberry Pi devices are approaching the Star Trek tricorder for gathering and saving data.

For those who really want to take this to the next level, this site describes a device built on a Raspberry Pi that resembles the Star Trek tricorder. It does not capture any data but instead plays videos of the original TV show; however, it looks incredible, and the Raspberry Pi fits inside the case:

www.raspberrypi.org/blog/raspberry-pi-tricorder-prop/

Summary

These projects point out some interesting aspects regarding the MCP9700 and DS18B20 sensors. First, the MCP9700 output is a linear relationship but requires an offset. The DS18B20 sensor is directly proportional to temperature. Another item observed is the comparison of the response time of the MCP9700 temp sensor and the DS18B20 waterproof temperature sensor. The DS18B20 is encased in a plastic cover and as such responds to temperature changes slower. This phenomenon is known as heat transfer and will be explored in later chapters. Finally, the MCP9700 is an analog sensor which means the millivolt reading is not modified or converted; it provides an analog output of the actual temperature. The DS18B20 is termed an I2C (Inter-Integrated Circuit) device where the raw voltage is converted over to a digital reading. This data can then be sent over a digital bus to the computer along with several other devices.

The Raspberry Pi is not set up to receive analog measurements, but with the addition of an analog to digital converter, it can accept this type of data. This is described in a later chapter. As the reader can see, there are advantages and disadvantages to both types of sensors. In a later chapter, there will be a third temperature sensor utilized. It is the LM35, and it will be in a different package that is sealed, designated TO. Electronically, it behaves very similar to the MCP9700.

While it is not necessary to complete these projects to proceed in this book, they lay the groundwork for using the Arduino and the Raspberry Pi tools to capture data. In particular, Listings 2-1 and 2-2 demonstrate the key sections of code that most of the code in this book follows. The reader should consider studying these examples to start to learn the language. When the Arduino IDE is downloaded, there is a folder created titled examples. In this folder, there are many pieces of code that provide a lot of useful examples. Studying these and other examples allows a researcher to start gathering data and then analyzing it or using it later. This groundwork will aid the reader when they develop their own or any of the other remaining projects.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.222.67.251