Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

refactor temperature monitoring into a classed structure. only suppor…

…ts one mcp3008 chip at once right now.
  • Loading branch information...
commit ba4c9170a206a4bdc4cd1f5a3b04f5ec29659a05 1 parent 41f564e
@eastein authored
Showing with 102 additions and 68 deletions.
  1. +92 −68 mcp3008.py
  2. +10 −0 temperature_example.py
View
160 mcp3008.py
@@ -11,48 +11,8 @@
To download, we suggest logging into your Pi with Internet accessibility and typing: git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
"""
-
-import time
import RPi.GPIO as GPIO
-
-GPIO.setmode(GPIO.BCM)
-
-# read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
-def readadc(adcnum, clockpin, mosipin, misopin, cspin):
- if ((adcnum > 7) or (adcnum < 0)):
- return -1
- GPIO.output(cspin, True)
-
- GPIO.output(clockpin, False) # start clock low
- GPIO.output(cspin, False) # bring CS low
-
- commandout = adcnum
- commandout |= 0x18 # start bit + single-ended bit
- commandout <<= 3 # we only need to send 5 bits here
- for i in range(5):
- if (commandout & 0x80):
- GPIO.output(mosipin, True)
- else:
- GPIO.output(mosipin, False)
- commandout <<= 1
- GPIO.output(clockpin, True)
- GPIO.output(clockpin, False)
-
- adcout = 0
- # read in one empty bit, one null bit and 10 ADC bits
- for i in range(12):
- GPIO.output(clockpin, True)
- GPIO.output(clockpin, False)
- adcout <<= 1
- if (GPIO.input(misopin)):
- adcout |= 0x1
-
- GPIO.output(cspin, True)
-
- adcout /= 2 # first bit is 'null' so drop it
- return adcout
-
# change these as desired - they're the pins connected from the
# SPI port on the ADC to the Cobbler
SPICLK = 18
@@ -60,31 +20,95 @@ def readadc(adcnum, clockpin, mosipin, misopin, cspin):
SPIMOSI = 24
SPICS = 25
-# set up the SPI interface pins
-GPIO.setup(SPIMOSI, GPIO.OUT)
-GPIO.setup(SPIMISO, GPIO.IN)
-GPIO.setup(SPICLK, GPIO.OUT)
-GPIO.setup(SPICS, GPIO.OUT)
-
-# temperature sensor connected channel 0 of mcp3008
-adcnum = 0
-
-while True:
- # read the analog pin (temperature sensor LM35)
- read_adc0 = readadc(adcnum, SPICLK, SPIMOSI, SPIMISO, SPICS)
-
- # convert analog reading to millivolts = ADC * ( 3300 / 1024 )
- millivolts = read_adc0 * ( 3300.0 / 1024.0)
-
- # 10 mv per degree
- temp_C = ((millivolts - 100.0) / 10.0) - 40.0
-
- # convert celsius to fahrenheit
- temp_F = ( temp_C * 9.0 / 5.0 ) + 32
-
- print "read_adc0:\t", read_adc0
- print "millivolts:\t%d" % millivolts
- print "temp_C:\t\t%.1f" % temp_C
- print "temp_F:\t\t%.1f" % temp_F
-
- time.sleep(1)
+class Pin(object) :
+ def read(self) :
+ raise NotImplemented
+
+class TMP36(Pin) :
+ F = 0
+ C = 1
+ K = 2
+
+ """
+ Choose a scale if you don't want C. K and F also available as constants in the class scope.
+ """
+ def __init__(self, temp_scale=None) :
+ if temp_scale is None :
+ temp_scale = self.C
+
+ self.temp_scale = temp_scale
+
+ @classmethod
+ def c2f(self, c) :
+ # convert celsius to fahrenheit
+ return ( c * 9.0 / 5.0 ) + 32
+
+ @classmethod
+ def c2k(self, c) :
+ # celsius to kelvin
+ return c + 273.15
+
+ def setup_mcp3008(self, mcp, channel_number) :
+ self.mcp = mcp
+ self.channel_number = channel_number
+
+ def read(self) :
+ millivolts = self.mcp.readadc(self.channel_number)
+ c = ((millivolts - 100.0) / 10.0) - 40.0
+ if self.temp_scale == TMP36.C :
+ return c
+ elif self.temp_scale == TMP36.F :
+ return TMP36.c2f(c)
+ elif self.temp_scale == TMP36.K :
+ return TMP36.c2k(c)
+
+class MCP3008(object) :
+ def __init__(self, mv_aref) :
+ self.mv_aref = mv_aref
+ self.channels = [None] * 8
+ # set up the SPI interface pins
+ GPIO.setmode(GPIO.BCM)
+ GPIO.setup(SPIMOSI, GPIO.OUT)
+ GPIO.setup(SPIMISO, GPIO.IN)
+ GPIO.setup(SPICLK, GPIO.OUT)
+ GPIO.setup(SPICS, GPIO.OUT)
+
+ # read SPI data from MCP3008 chip, 8 possible adc's (0 thru 7)
+ def readadc(self, adcnum, clockpin=SPICLK, mosipin=SPIMOSI, misopin=SPIMISO, cspin=SPICS):
+ if ((adcnum > 7) or (adcnum < 0)):
+ return -1
+ GPIO.output(cspin, True)
+
+ GPIO.output(clockpin, False) # start clock low
+ GPIO.output(cspin, False) # bring CS low
+
+ commandout = adcnum
+ commandout |= 0x18 # start bit + single-ended bit
+ commandout <<= 3 # we only need to send 5 bits here
+ for i in range(5):
+ if (commandout & 0x80):
+ GPIO.output(mosipin, True)
+ else:
+ GPIO.output(mosipin, False)
+ commandout <<= 1
+ GPIO.output(clockpin, True)
+ GPIO.output(clockpin, False)
+
+ adcout = 0
+ # read in one empty bit, one null bit and 10 ADC bits
+ for i in range(12):
+ GPIO.output(clockpin, True)
+ GPIO.output(clockpin, False)
+ adcout <<= 1
+ if (GPIO.input(misopin)):
+ adcout |= 0x1
+
+ GPIO.output(cspin, True)
+
+ adcout /= 2 # first bit is 'null' so drop it
+
+ return adcout * ( self.mv_aref / 1024.0)
+
+ def setup_channel(self, channel_number, pinobject) :
+ pinobject.setup_mcp3008(self, channel_number)
+ self.channels[channel_number] = pinobject
View
10 temperature_example.py
@@ -0,0 +1,10 @@
+import time
+import mcp3008
+
+a2d = mcp3008.MCP3008(3300.0)
+temperature = mcp3008.TMP36(mcp3008.TMP36.C)
+a2d.setup_channel(0, temperature)
+
+while True:
+ print '%0.1f C' % temperature.read()
+ time.sleep(1)
Please sign in to comment.
Something went wrong with that request. Please try again.