Skip to content

Instantly share code, notes, and snippets.

@jcere
Last active September 21, 2015 23:48
Show Gist options
  • Save jcere/a3f545f2bc334dc122c0 to your computer and use it in GitHub Desktop.
Save jcere/a3f545f2bc334dc122c0 to your computer and use it in GitHub Desktop.
Python script for taking timed temperature samples using an MCP3008 ADC
import spidev
import time
import os
import json
# Open SPI bus
spi = spidev.SpiDev()
spi.open(0,0)
# Function to read SPI data from MCP3008 chip
# Channel must be an integer 0-7
def ReadChannel(channel):
adc = spi.xfer2([1,(8+channel)<<4,0])
data = ((adc[1]&3) << 8) + adc[2]
return data
# Function to convert data to voltage level,
# rounded to specified number of decimal places.
def ConvertVolts(data,places):
volts = (data * 3.3) / float(1023)
volts = round(volts,places)
return volts
# Function to calculate temperature from
# TMP36 data, rounded to specified
# number of decimal places.
def ConvertTemp(data,places):
# ADC Value
# (approx) Temp Volts
# 0 -50 0.00
# 78 -25 0.25
# 155 0 0.50
# 233 25 0.75
# 310 50 1.00
# 465 100 1.50
# 775 200 2.50
# 1023 280 3.30
temp = ((data * 330)/float(1023))-50
temp = round(temp,places)
return temp
# Define sensor channels
pot_channel = 0
# Define delay between readings
delay = 5
# Open file for output
fileOp = 'a'
fileName = '/home/pi/projects/sensor_output/temperatureData.json'
outputFile = open(fileName, fileOp)
# Limit the loop
for x in range(0, 3):
# Get a time stamp
gmTime = time.gmtime()
strTime = time.asctime(gmTime)
timeStamp = time.time()
# Read the input voltage
level = ReadChannel(pot_channel)
volts = ConvertVolts(level,2)
temp = ConvertTemp(level,2)
# Print out results
print("Temp: {} ({}C) {} {}".format(level,temp,strTime,timeStamp))
#dataArr = [level,volts,temp,currentTime]
data = { 'level':level, 'volts':volts, 'temp':temp, 'dateTime':strTime, 'timeStamp':timeStamp }
# Print results to file
json.dump(data, outputFile)
# Wait before repeating loop
time.sleep(delay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment