Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter

I wanted to get sensing data from Raspberry Pi from Node.js as well as Arduino Firmata, but it seems difficult, so I decided to go back to Python. Since GPIO of Raspberry Pi can only input digitally, TMP36 and LM35DZ AD converters such as MCP3008 and PCF8591 when using analog sensors will become necessary. Let's write a Python program that measures temperature using TMP36.

Enable SPI

[SPI (Serial Peripheral Interface)](http://ja.wikipedia.org/wiki/%E3%82%B7%E3%83%AA%E3%82%A2%E3%83%AB%E3%83% BB% E3% 83% 9A% E3% 83% AA% E3% 83% 95% E3% 82% A7% E3% 83% A9% E3% 83% AB% E3% 83% BB% E3% 82% A4% E3% 83% B3% E3% 82% BF% E3% 83% 95% E3% 82% A7% E3% 83% BC% E3% 82% B9) is one of the serial communication standards. It seems that the method of enabling SPI on Raspberry Pi has changed from 3.18 of the kernel. Raspi-config in Enabling The SPI Interface On The Raspberry Pi It describes how to use and how to enable it manually. This time we will do it manually.

Update firmware

If the kernel is not 3.18.x, update the firmware. The package has also been updated.

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo rpi-update

Check the kernel version.

$ cat /proc/version
Linux version 3.18.11+ (dc4@dc4-XPS13-9333) (gcc version 4.8.3 20140303 (prerelease) (crosstool-NG linaro-1.13.1+bzr2650 - Linaro GCC 2014.03) ) #777 PREEMPT Sat Apr 11 17:24:23 BST 2015

Edit /boot/config.txt.

$ sudo vi /boot/config.txt
dtparam=spi=on

reboot.

$ sudo reboot

SPI is now enabled.

$ lsmod | grep spi
spi_bcm2708             6018  0

Breadboard wiring

Wire the MCP3008, Raspberry Pi and FT232RL on a breadboard.

mcp3008-tmp36.png

Wire with MCP3008 and Raspberry Pi.

Wire the TMP36.

Python installation

Install the Python development environment for Raspberry Pi.

$ sudo apt-get update
$ sudo apt-get install python-dev
$ python -V
Python 2.7.3

Install ez_setup and pip.

$ curl https://bootstrap.pypa.io/ez_setup.py -o - | sudo python
$ curl https://bootstrap.pypa.io/get-pip.py -o - | sudo python
$ pip -V
pip 6.1.1 from /usr/local/lib/python2.7/dist-packages (python 2.7)

py-spidev

Install py-spidev to operate SPI devices from Python.

$ cd
$ git clone git://github.com/doceme/py-spidev 
$ cd py-spidev
$ sudo python setup.py install
...
Writing /usr/local/lib/python2.7/dist-packages/spidev-3.0.egg-info

Sample project

Create a project.

$ mkdir -p ~/python_apps/spidev-spike
$ cd !$

Write a Python sample program by referring to the following site.

~/python_apps/spidev-spike/spi_tmp36.py


#!/usr/bin/env python

import time
import sys
import spidev

spi = spidev.SpiDev()
spi.open(0,0)

def readAdc(channel):
    adc = spi.xfer2([1,(8+channel)<<4,0])
    data = ((adc[1]&3) << 8) + adc[2]
    return data

def convertVolts(data):
    volts = (data * 3.3) / float(1023)
    volts = round(volts,4)
    return volts

def convertTemp(volts):
    temp = (100 * volts) - 50.0
    temp = round(temp,4)
    return temp

if __name__ == '__main__':
    try:
        while True:
            data = readAdc(0)
            print("adc  : {:8} ".format(data))
            volts = convertVolts(data)
            temp = convertTemp(volts)
            print("volts: {:8.2f}".format(volts))
            print("temp : {:8.2f}".format(temp))

            time.sleep(5)
    except KeyboardInterrupt:
        spi.close()
        sys.exit(0)

Program execution

Run a Python program. Displays the temperature in degrees Celsius at 5-second intervals.

$ python spi_tmp36.py
adc  :      232
volts:     0.75
temp :    24.84
adc  :      233
volts:     0.75
temp :    25.16
adc  :      233
volts:     0.75
temp :    25.16

Recommended Posts

Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
How to use Raspberry Pi pie camera Python
How to use the Raspberry Pi relay module Python
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
Use vl53l0x with Raspberry Pi (python)
[Raspberry Pi] Changed Python default to Python3
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
Detect analog signals with A / D converter using python on Raspberry Pi 3!
Use python on Raspberry Pi 3 to light the LED with switch control!
Use the Grove sensor on the Raspberry Pi
python: How to use locals () and globals ()
Use PIR motion sensor with raspberry Pi
How to use Python zip and enumerate
Detect temperature using python on Raspberry Pi 3!
How to use is and == in Python
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Use Grove-Temperature & Humidity Sensor (DHT11) on Raspberry Pi
[Python] How to use hash function and tuple.
Raspberry + am2302 Measure temperature and humidity with temperature and humidity sensor
Connect to MySQL with Python on Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
Record temperature and humidity with systemd on Raspberry Pi
From setting up Raspberry Pi to installing Python environment
[Python] [Django] How to use ChoiceField and how to add options
Create a color sensor using a Raspberry Pi and a camera
Easy IoT to start with Raspberry Pi and MESH
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Output to "7-segment LED" using python on Raspberry Pi 3!
Use raspberry Pi and Julius (speech recognition). ③ Dictionary creation
Raspberry Pi + python + IoT device, environment construction procedure to start image processing and machine learning
A story about trying to use cron on a Raspberry Pi and getting stuck in space
[Go language] Use OpenWeatherMap and Twitter API to regularly tweet weather information from Raspberry Pi
Easy to use Nifty Cloud API with botocore and python
Python beginner opens and closes interlocking camera with Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
[Introduction to Udemy Python 3 + Application] 36. How to use In and Not
Production of temperature control system with Raspberry Pi and ESP32 (1)
[Python] Summary of how to use split and join functions
Install pyenv on MacBook Air and switch python to use
Comparison of how to use higher-order functions in Python 2 and 3
Measure and compare temperature with Raspberry Pi and automatically generate graph
[Python] Use this to read and write wav files [wavio]
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
Translate I2C device driver for Arduino to Python for Raspberry pi
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Send the temperature, humidity, etc. measured by SensorTag to Ambient via Raspberry Pi 3 and graph it.
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
About the error I encountered when trying to use Adafruit_DHT from Python on a Raspberry Pi