Program to print temperature

We learned about analog theory and BeagleBone's special analog input pins in Chapter 5, Reading from Analog Sensors. In that chapter, we read the temperature from the TMP36 sensor. Let's write a Python program to do the same.

Connect the TMP36 sensor to P9_40 as shown in the diagram in Chapter 5. Type the following program in Cloud9, save it as TMP36.py and run. It will print the current temperature in degrees Celsius. If you touch the sensor, you will see an increase in temperature.

This is the code for TMP36.py:

#!/usr/bin/python
import Adafruit_BBIO.ADC as ADC
from time import sleep

tmp36 = "P9_40"

ADC.setup()

while True:
    volts = ADC.read(tmp36)* 1.8
    ##Equation created after reading TMP36 datasheet
    temperature = (volts*100) - 50
    print "Current Temperature is " + str(round(temperature,2))
    sleep(1)

Explanation

This time we took the ADC namespace from the BBIO library. This namespace has variables and functions related to analog input. Function ADC.setup() initializes and tests analog entries in sysfs. Function ADC.read() reads analog voltage measured at the corresponding pin and normalizes it in a range of 0 to 1. We multiply it by 1.8 to get the actual voltage at the pin. Please refer to Chapter 5, Reading from Analog Sensors for details about the TMP36 sensor. The TMP36 datasheet gives us an equation to convert measured voltage to temperature in degrees Celsius. We used that equation to calculate the current temperature and printed it after rounding off using the function round(). You can use the LM35 temperature sensor instead. In that case you just need to change the equation line to temperature = (volts*100).

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

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