Using your sensor station – make a temperature logger

Now that you know the trick of reading the ADC value from MCP3008 through an SPI, line we are ready to interface one of the sensors with MCP3008's channel 0. You can now use your sensor station to interface with up to eight sensors in parallel. To simplify the experiment, we are going to interface the temperature sensor with the sensor station board. Imagine that the RasPi has been connected at a remote place to log temperature data. Depending on our application, once in a month or after a certain time, we need the data to be recorded manually. To log this, we will make use of Python to store data in a text file, and read these values later on for analysis.

Know the LM36 temperature sensor

LM35 and LM36 (also known as TMP36) provide the linear response of temperature changes reflected on voltage. The change in output voltage of the sensor is directly proportional to the temperature change experienced by the temperature sensor. LM35 and LM36 are centigrade temperature sensors. Because of its wide availability, these sensors can be bought online or from any retail shop near you. We will build our project with the LM36 sensor; if you have got the LM35 sensor, don't panic! The difference between these two sensors is as follows: to measure subzero temperatures using LM35, we have to provide negative voltage at the output pin of the sensor, which needs special power supply requirements. If you provide only positive voltage supply to the LM35 temperature sensor, it will sense only the positive temperature range. This is not the case with the LM36. When provided with positive voltage, it gives us the full range from -50 ˚C to +150 ˚C. It can be said that LM36 is an improved version of the LM35 temperature sensor. Let's take a look at the pin out functions and the package of the sensor. The following figure is a representation of the LM36 temperature sensor from the bottom:

Know the LM36 temperature sensor

There are three pins protruding from this sensor. Consider the semicircular shape to be on top. Now the left-most pin is the GND pin, which needs the common ground. The pin in the center is the analog output pin, which should be connected to the ADC module input channel. To the rightmost pin, we can supply +2.7V to +20V. For our application, we will be sharing the +3.3V output of the RasPi expansion header (P3) to the GPCB.

Take three female-to-female jumper wires and connect them with the sensor station. Take 3.3V from header P3 and connect it to the supply pin of LM35 or TMP36. Connect the output pin to the channel 0 pin of header P2. Connect the ground wire pin to the ground pin header P4. Other sensors can be interfaced in the same manner. Longer wires can be used to put the temperature sensor in an application area to keep the sensor station away from the application.

Write the application

Now we are ready to write the code for the interfaced temperature sensor on our sensor station. We will use the same function to read the channels of MCP3008 one by one. If you have interfaced many sensors at a time, you can use the same code in your application. Along with that, we will log the data into the text file using another function, which will be introduced while explaining the code. While staying in the same folder, mcpgeneric, open nano editor using the sudo nano sensorstation.py command and start typing the following code:

import spidev
import time
import os
import csv
#start the SPI bus by opening the spi port
spi = spidev.SpiDev()
spi.open(0,0)
#SPI port 0 opened and Device Chip Select set to 0
#function to read the channels of MCP3008 
def readadc(channel):
     value = spi.xfer2([1,(8+channel)<<4,0])
     read = ((value[1]&3) << 8) + value[2]
     return read
#writer is an object or file reference to .csv file
writer = csv.writer(file('Datalog.csv','ab+'))

while True:
     #creating the list for the different values of each channels
     datalist = []
     for i in range(0,8):
          #read channel one by one using range of 0 to 8 
          data = readadc(i)
          #append data into the datalist created
          datalist.append(data)
          #convert temperature value from data received
          temperature = ((data * 330)/float(1023))-50
          print temperature
          time.sleep(3)
          print (datalist)
          #write data into the file 'Datalog.csv'
          writer.writerow(datalist)

If you still haven't connected the sensor station with the RasPi, connect the sensor station board using the ribbon cable. Here is how the code is structured: at first, we have to import the useful libraries and APIs to call when the program runs. The spare imported libraries are spidev, which provides SPI functionalities to transfer the data, and CSV (comma separated values), which provides functionalities to import and export the data into databases and spreadsheets. Values (0, 0) in the spi.open() function define the processor's SPI port number and chip enable value, which is set by default when the code begins to execute. After the calling of libraries, we used our generic function to get data from MCP3008 through the SPI port. Then we created the object reference to the file we are creating. The file will be created in the same folder where the program is kept and saved. The ab+ parameter is used to append the new data and is abbreviated as append in binary mode.

Inside the while loop, the datalist[] array is created to temporarily save the data into, and then the last line of the program is used to update this datalist[] array in the .csv file. By passing the values to the function in range of 0 to 7 (8 will be excluded when the loop will run) using the for loop, we will scan each channel of MCP3008. As stated earlier in this chapter in the Math behind ADC section, the formula of conversion for temperature sensor is created, and we have converted the data value into temperature values.

Tip

If an LM35 sensor is being used, remove -50 from the formula because LM35 cannot measure subzero temperatures unless provided with a negative voltage at the output voltage line.

Currently, it is unknown what types of sensors we are going to interface. Once the sensor is interfaced, the conversion formula should be created and imported to the same code. To append the converted data, simply use datalist.append(variable), and the value will be stored in the data file separated by a comma in a single row. Every sequence of the for loop will generate one row. You can play around with different values and conversions by interfacing some sensors on your sensor station. Have fun; it is so simple!

Tip

You can use crontab the same way as you used in Chapter 3, Measuring Distance Using Ultrasonic Sensors, to put this Python code in startup.

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

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