Detect temperature using python on Raspberry Pi 3!

Introduction

My name is Ryosuke Kamei and I am an IT consultant based on the idea of "gentle IT"! Currently, in my work, I am doing upstream processes focusing on requirements analysis and requirements definition, but I also write programs! As part of our friendly IT activities, we will introduce "Raspberry Pi 3" (commonly known as Raspberry Pi), which has the philosophy of "providing inexpensive PCs that can be programmed for education"! This time, we will detect the temperature! !! !! [Razpai Magazine-June 2016 Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 ⑥ Digital Temperature Sensor](https://www.amazon.co.jp/ Razpai Magazine-June 2016 Issue-Nikkei BP PC Best Mook - the Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl ie = UTF8 & dpID = 61P3o6Agz + L & dpSrc = sims & preST = OU09__BG0,0,0,0_FMpng_AC_UL160_SR120,160 & psc = 1 & refRID = 31SKG5E0DGKBSF9BWNGV & linkCode = ll1 & tag = sr2smail-22 & linkId = 18fd04738627edef8d6fffd6c9b8f9c2)? I am referring to it. Finally, it's a story that seems to be a work story for my daughter (11 and 9) during the summer vacation! (I'm sorry for my personal cry)

procedure

  1. Wiring
  2. Preparation
  3. Program digital_temperature_sensor.py
  4. Run the program

1. Wiring

The wiring diagram is [Razpai Magazine-June 2016 Issue Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 ⑥ Digital Temperature Sensor](https://www.amazon.co.jp/ Razpai Magazine-June 2016 No.-Nikkei BP PC Best Mook-Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl? = 18fd04738627edef8d6fffd6c9b8f9c2) As it is.

If you get an image like this, referring to the picture of the circuit Raspberry Pi 3でpythonを使い温度を検出する!回路.jpg

I also made a video Click the video commentary →

Text version

--Raspberry Pi Pin 1 (3.3V) --Digital Temperature Sensor VDD 4 --Raspberry Pi 6th pin (GND) --Digital temperature sensor GND 1 --Raspberry Pi Pin 3 (SCA) --Digital Temperature Sensor SCA 2 --Raspberry Pi Pin 5 (SCL) --Digital Temperature Sensor SCL 5

2. Preparation

Install the required library (libi2c-dev).

libi2c-dev install


$ sudo apt-get install libi2c-dev

Write to the configuration file.

bash:/etc/modprobe.d/i2c.Write settings to conf


$ sudo sh -c 'echo "options i2c_bcm2708 combined=1" >> /etc/modprobe.d/i2c.conf'

Enables I2C.

Click the video commentary →

Check if I2C can be used

Confirmed with i2cdetect (it seems to be 0 depending on the partial version of 1)


$ sudo /usr/sbin/i2cdetect -y 1

3. Program digital_temperature_sensor.py

Program is also [Razpai Magazine-June 2016 Issue Special Feature 1 Akiba's Popular Parts Wiring Diagram Best 17 ⑥ Digital Temperature Sensor](https://www.amazon.co.jp/ Razpai Magazine-June 2016 Issue- Nikkei BP PC Best Mook -? Nikkei Linux-ebook / dp / B01EH2RX42 / ref = as_li_ss_tl ie = UTF8 & dpID = 61P3o6Agz + L & dpSrc = sims & preST = OU09__BG0,0,0,0_FMpng_AC_UL160_SR120,160 & psc = 1 & refRID = 31SKG5E0DGKBSF9BWNGV & linkCode = ll1 & tag = sr2smail-22 & linkId = 18fd04738627edef8d6fffd6c9b8f9c2 ) Was used as a reference. Almost as it is.

The source is uploaded to GitHub, so please use it as you like.

Clone with git


$ git clone https://github.com/RyosukeKamei/raspberrypi3.git

digital_temperature_sensor.py


#Library to control GPIO
import wiringpi
#Timer library
import time
#Call the library required to read from the I2C device
import os
import struct

#Create an instance of I2C
wiringpi.wiringPiSetup()
i2c = wiringpi.I2C()

#I2C settings
#Specify the I2C address of the device to communicate
temperture_dev = i2c.setup(0x48)

#Get 16-bit temperature data
#Others Set to register 0x03
i2c.writeReg8(temperture_dev, 0x03, 0x80)

while True:
    #Read 2 bytes of temperature sensor
    temperture_data = struct.unpack('2B', os.read(temperture_dev, 2))

    #The value is divided into 2 bytes, so combine them into one.
    temperture = ( ( temperture_data[0] << 8 ) + temperture_data[1] )

    #Convert numbers for negative values
    if ( temperture_data[0] >= 0x80 ):
        temperture = temperture - 65536

    #Calculate the temperature by dividing the acquired value by 128
    temperture = temperture / 128

    #Temperature display
    print ( "temperature" , temperture , "C" )

    #Every second
    time.sleep(1)

3. Run the program

I recorded the video of how it is moving. Click the video commentary →

Detect temperature


$ sudo python3 digital_temperature_sensor.py

The temperature is displayed in real time! I was surprised that the temperature was almost the same as the set temperature of the air conditioner (laughs). If you touch the sensor with your finger, the temperature will rise a little!

site map

Raspberry Pi 3 setup

Install Raspberry Pi 3 → Wireless LAN → Japanese input / output → Operate from Mac

Build a Python + MySQL environment with Docker on Raspberry Pi 3!

Install Docker on RaspberryPi3 Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Easy construction] Build a Python + bottle + MySQL environment with Docker on RaspberryPi3![Trial and error]

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Make an air conditioner integrated PC "airpi" with Raspberry Pi 3!

Let's play with Raspberry Pi 3 and python

Programming with Node-RED programming with Raspberry Pi 3 and programming normally Light the LED with python on Raspberry Pi 3 (Hello World) Detect switch status on Raspberry Pi 3 Run a servo motor using python on Raspberry Pi 3 Control the motor with a motor driver using python on Raspberry Pi 3! Detect slide switch using python on Raspberry Pi 3! Detect magnet switch using python on Raspberry Pi 3! Detect temperature using python on Raspberry Pi 3! Sound the buzzer using python on Raspberry Pi 3! Detect analog signals with A / D converter using python on Raspberry Pi 3! Detect "brightness" using python on Raspberry Pi 3! Detect "temperature (using A / D converter)" using python on Raspberry Pi 3! Output to "7-segment LED" using python on Raspberry Pi 3!

Rules focused on test-driven development

Coding rules "Let's write gentle code" (FuelPHP) Naming convention "Friendly to yourself, team-friendly, and unseen members after 3 months"

Web application development with Docker + Python

Install Python3, related libraries pip, virtualenv and frameworks Django, bottle, Flask on CentOS on Docker! With a Dockerfile that summarizes these!

Easy to develop environment construction (Docker + PHP)

PHP environment + Eclipse is linked to Apache using Docker Building FuelPHP development environment using Docker Create CRUD skeleton using initial settings of FuelPHP development environment using Docker and scaffold FuelPHP database migration

Recommended Posts

Detect temperature using python on Raspberry Pi 3!
Detect "brightness" using python on Raspberry Pi 3!
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
Detect slide switches using python on Raspberry Pi 3!
Run servomotor on Raspberry Pi 3 using python
Sound the buzzer using python on Raspberry Pi 3!
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Try using ArUco on Raspberry Pi
Detect switch status on Raspberry Pi 3
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Access google spreadsheet using python on raspberry pi (for myself)
Adafruit Python BluefruitLE works on Raspberry Pi.
Working with GPS on Raspberry Pi 3 Python
Control the motor with a motor driver using python on Raspberry Pi 3!
pigpio on Raspberry pi
Cython on Raspberry Pi
Try using a QR code on a Raspberry Pi
Make DHT11 available on Raspberry Pi + python (memo)
Display CPU temperature every 5 seconds on Raspberry Pi 4
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
Measure CPU temperature of Raspberry Pi with Python
Use BME280 temperature / humidity / barometric pressure sensor from Python on Raspberry Pi 2
Record temperature and humidity with systemd on Raspberry Pi
Indoor monitoring using Raspberry Pi
Build an OpenCV4 environment on Raspberry Pi using Poetry
[Raspberry Pi] Publish a web application on https using Apache + WSGI + Python Flask
Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform
Try debugging Python on Raspberry Pi with Visual Studio.
Raspberry Pi + Python + OpenGL memo
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
raspberry pi 1 model b, python
Install pyenv on Raspberry Pi and version control Python
Broadcast on LINE using python
Introduced pyenv on Raspberry Pi
Use NeoPixel on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Run AWS IoT Device SDK for Python on Raspberry Pi
Cross-compiling for Raspberry Pi Zero on Debian-Try using shared libraries
I tried using the DS18B20 temperature sensor with Raspberry Pi
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
Testing uart communication on Raspberry Pi
Use vl53l0x with Raspberry Pi (python)
USB over ethernet using Raspberry pi
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Introducing Python using pyenv on Ubuntu 20.04
Notes on using MeCab from Python
Preparing python using vscode on ubuntu
Install ghoto2 on Raspberry Pi (memo)
Study on Tokyo Rent Using Python (3-2)
Notes on installing Python using PyEnv
OpenCV installation procedure on Raspberry Pi
Notes on using rstrip with python.
Study on Tokyo Rent Using Python (3-3)
Power on / off Raspberry pi on Arduino
Install OpenMedia Vault 5 on Raspberry Pi 4
Using a webcam with Raspberry Pi
Install Python on CentOS using pyenv