Temperature Sensor and Analog to Digital Converter with Raspberry Pi
In this project we will be using Raspberry Pi to measure temperature using Analog Devices TMP36 and MCP3008 – 8-Channel 10-Bit ADC With SPI Interface. TMP36 is an analog device and unlike many other micro controller boards, Raspberry Pi does not have any analog input or on-board Analog to Digital converter. We will be using MCP3800 to read analog input from TMP36 and then convert it to digital serial data for Raspberry Pi. The diagram below shows MCP3800 and TMP36 connection to Raspberry Pi, it is broken down into 3 steps for clarity:
This IC has 16 pins:
1 – Channel 0
2 – Channel 1
3 – Channel 3
4 – Channel 4
5 – Channel 5
6 – Channel 6
7 – Channel 7
8 – Channel 8
9 – Digital Ground
10 – Chip Select
11 – Data In
12 – Data Out
13 – Clock
14 – Analog Ground
15 – Reference Voltage
16 – Power In
- Connect analog ground, pin 14 to digital ground, pin 9. If you are using high precision sensors, you would want to keeps these separate, but for our purposes, we can tie them together.
- Connect Power In, pin 16 and Reference Voltage, pin 15 together.
- Connect Pin 1 of the TMP36 to Reference Voltage, pin 15 or pin 16 of MCP3008.
- Connect Pin 2 of the TMP36 to Ground, pin 14 or pin 9 of MCP3008.
- Connect Pin 3 of the TMP36 to Channel 0, pin 1 of MCP3008.
- Connect Power In, MCP3800 pin 15 or 16, to Raspberry Pi Pin 1.
- Connect Raspberry Pi pin 6 (ground) to any ground pin 14 or 9 (they should be tied together now)
- Connect Clock, MCP3800 pin 13, to Raspberry Pi Pin 13.
- Connect Data Out, MCP3800 pin 12, to Raspberry Pi Pin 7.
- Connect Data In, MCP3800 pin 11, to Raspberry Pi Pin 11.
- Connect Cable Select, MCP3800 pin 10, to Raspberry Pi Pin 12.
#!/usr/bin/env python # Adopted from code by Limor "Ladyada" Fried for Adafruit Industries, (c) 2015 # This code is released into the public domain import time import os import RPi.GPIO as GPIO GPIO.setmode(GPIO.BOARD) DEBUG = 1 # 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 >>= 1 # 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 = 13 SPIMISO = 7 SPIMOSI = 11 SPICS = 12 # 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) # 10k trim pot connected to adc #0 potentiometer_adc = 0; while True: # we'll assume that the pot didn't move trim_pot_changed = False # read the analog pin trim_pot = readadc(potentiometer_adc, SPICLK, SPIMOSI, SPIMISO, SPICS) print "raw:", trim_pot voltage = (trim_pot * 3.3) * 1000/ 1023 print "voltage:", voltage ,"mV" celcius = (voltage - 500) / 10 fahrenheit = celcius * 1.8 + 32 print "T - celcius", celcius print "T - fahrenheit", fahrenheit # hang out and do nothing for a half second time.sleep(0.5)