ugnen

git clone https://git.tarina.org/ugnen
Log | Files | Refs

commit c4d7b388c39f52e5ebdb91feed1d77e0b6af5724
parent b4eaec0cfaf00ecb5c50cb393698fa15dc9911b7
Author: rbckman <rob@tarina.org>
Date:   Thu,  8 Sep 2022 11:19:02 +0300

reading temp with raspberry pi

Diffstat:
Amax6675.py | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atemp_read_1_sensor.py | 39+++++++++++++++++++++++++++++++++++++++
2 files changed, 99 insertions(+), 0 deletions(-)

diff --git a/max6675.py b/max6675.py @@ -0,0 +1,60 @@ +import RPi.GPIO as GPIO +import time +GPIO.setmode(GPIO.BOARD) +GPIO.setwarnings(False) + +# set pin number for communicate with MAX6675 +def set_pin (CS, SCK, SO, UNIT): + global sck + sck= SCK + global so + so = SO + global unit + unit = UNIT + + GPIO.setup(CS, GPIO.OUT, initial = GPIO.HIGH) + GPIO.setup(SCK, GPIO.OUT, initial = GPIO.LOW) + GPIO.setup(SO, GPIO.IN) + +def read_temp(cs_no): + + GPIO.output(cs_no, GPIO.LOW) + time.sleep(0.002) + GPIO.output(cs_no, GPIO.HIGH) + time.sleep(0.22) + + GPIO.output(cs_no, GPIO.LOW) + GPIO.output(sck, GPIO.HIGH) + time.sleep(0.001) + GPIO.output(sck, GPIO.LOW) + Value = 0 + for i in range(11, -1, -1): + GPIO.output(sck, GPIO.HIGH) + Value = Value + (GPIO.input(so) * (2 ** i)) + GPIO.output(sck, GPIO.LOW) + + GPIO.output(sck, GPIO.HIGH) + error_tc = GPIO.input(so) + GPIO.output(sck, GPIO.LOW) + + for i in range(2): + GPIO.output(sck, GPIO.HIGH) + time.sleep(0.001) + GPIO.output(sck, GPIO.LOW) + + GPIO.output(cs_no, GPIO.HIGH) + + if unit == 0: + temp = Value + if unit == 1: + temp = Value * 0.25 + if unit == 2: + temp = Value * 0.25 * 9.0 / 5.0 + 32.0 + + if error_tc != 0: + return -cs_no + else: + return temp + +GPIO.cleanup() + diff --git a/temp_read_1_sensor.py b/temp_read_1_sensor.py @@ -0,0 +1,39 @@ +# before import the max6675, you must save the max6675.py file at "/usr/lib/python2.7/dist-packages" + +# wiring +# Raspberry MAX6675 +# GND ------ GND +# 5V ------ VCC +# pin 18 ------ SCK +# pin 22 ------ CS +# pin 16 ------ SO + +# import max6675 module. +import max6675 + +# set the pin for communicate with MAX6675 +cs = 22 +sck = 18 +so = 16 + +# max6675.set_pin(CS, SCK, SO, unit) [unit : 0 - raw, 1 - Celsius, 2 - Fahrenheit] +max6675.set_pin(cs, sck, so, 1) + +try: + while 1: + # read temperature connected at CS 22 + a = max6675.read_temp(cs) + + # print temperature + print a + + # when there are some errors with sensor, it return "-" sign and CS pin number + # in this case it returns "-22" + + max6675.time.sleep(2) + +except KeyboardInterrupt: + pass + + +