Lesson 30 Temperature Measurement System

Share for us

Introduction

After having learnt so many modules, let’s use several together to make a comprehensive experiment – use an RGB LED, a buzzer and a DS18B20 to build an interesting and useful temperature measurement system.

Components

– 1 * Raspberry Pi

– 1 * Breadboard

– 1 * Active Buzzer

– 1 * RGB LED

– 1 * DS18B20

– Several jumper wires

Experimental Principle

When the ambient temperature is lower than the upper limit value, the buzzer will beep at a low frequency and the LED will flash blue; when the temperature is higher than lower limit value, the buzzer will beep at a relatively high frequency and the LED will flash red; when the temperature is between two values, the buzzer will keep silent and the LED will be green.

Note: The lower and upper limit values here can be defined and achieved by passing parameters to the main function.

Experimental Procedures

Step 1: Build the circuit

DS18b20 module connection:

DS18B20                                   Raspberry Pi

S  ———————————  GPIO7

RGB LED connection:

RGB LED                                     Raspberry Pi

R ————————————– GPIO0

G ————————————–GPIO1

B ————————————– GPIO2

Buzzer module connection:

Buzzer Module                              Raspberry Pi

– —————————————– GND

S —————————————– GPIO3

Step 2: Edit and save the code (see path/Rpi_SensorKit_Code/29_expand01/tempMonitor.c)

Step 3: Compile

              gcc  tempMonitor.c  -lwiringPi

Step 4: Run

              ./a.out  25  30

Now, when the ambient temperature is lower than the lower limit value (25℃), the buzzer beeps at a lower frequency and the LED flashes blue; when it is higher than the upper limit value (30℃), the buzzer beeps at a relatively higher frequency and the LED flashes red. When it is between the two values, the buzzer is silent and the LED keeps green.

tempMonitor.c 

#include <wiringPi.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h> #include="" <stdlib.h>="" <stdio.h>="" #define="" ledred="" 0="" ledgreen="" 1="" ledblue="" 2="" beep="" 3="" bufsize="" 128="" typedef="" unsigned="" char="" uchar;="" int="" uint;="" void="" beepinit(void)="" {="" pinmode(beep,="" output);="" }="" beepctrl(int="" t)="" digitalwrite(beep,="" low);="" delay(t);="" high);="" float="" tempread(void)="" temp;="" i,="" j;="" fd;="" ret;="" buf[bufsize];="" tempbuf[5];="" fd="open("/sys/bus/w1/devices/28-00000495db35/w1_slave"," o_rdonly);="" if(-1="=" fd){="" perror("open="" device="" file="" error");="" return="" 1;="" while(1){="" ret="read(fd," buf,="" bufsize);="" if(0="=" ret){="" break;="" if(errno="=" eintr){="" continue;="" perror("read()");="" close(fd);="" for(i="0;i<sizeof(buf);i++){" if(buf[i]="=" 't'){="" for(j="0;j<sizeof(tempBuf);j++){" tempbuf[j]="buf[i+2+j];" temp="(float)atoi(tempBuf)" 1000;="" ledinit(void)="" pinmode(ledred,="" pinmode(ledgreen,="" pinmode(ledblue,="" *="" ledctrl(int="" n,="" state)="" digitalwrite(n,="" state);="" main(int="" argc,="" *argv[])="" i;="" low,="" high;="" if(argc="" !="3){" printf("usage="" :="" .="" a.out="" [temperature="" lower="" limit]="" [upper="" limit]\n");="" printf("for="" example="" 29="" 31\n");="" 0;="" low="atoi(argv[1]);" high="atoi(argv[2]);" if(low="" >="high){" printf("parameters="" error,="" limit="" should="" be="" less="" than="" upper="" limit\n");="" if(wiringpisetup()="=" -1){="" when="" initialize="" wiring="" failed,print="" messageto="" screen="" printf("setup="" wiringpi="" failed="" !");="" ledinit();="" beepinit();="" printf("the="" of="" temperature="" %d\n",="" printf("current="" %0.3f\n",="" temp);="" if(temp="" <="" low){="" ledctrl(ledblue,="" ledctrl(ledred,="" ledctrl(ledgreen,="" 3;="" i++){="" beepctrl(500);="" &&="" high){="" beepctrl(100);="" <="" pre="">
</errno.h>>

Python Code

#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
import sys

# Define RGB LED pin
LedRed    = 11
LedGreen  = 12
LedBlue   = 13

# Define Buzzer pin
Buzzer = 15

def beep(x):
	GPIO.output(Buzzer, 0)
	time.sleep(x)
	GPIO.output(Buzzer, 1)
	time.sleep(x)

def tempRead():
	global ds18b20
	address = '/sys/bus/w1/devices/' + ds18b20 + '/w1_slave'
	tfile = open(address)
	text = tfile.read()
	tfile.close()
	secondline = text.split("\n")[1]
	temperaturedata = secondline.split(" ")[9]
	temperature = float(temperaturedata[2:])
	temperature = temperature / 1000
	return temperature

def map(x, in_min, in_max, out_min, out_max):
	return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min

def ds18b20Init():
	global ds18b20
	for i in os.listdir('/sys/bus/w1/devices'):
		if i[:2] == '28':
			ds18b20 = i

def setup():
	GPIO.setmode(GPIO.BOARD)
	# Buzzer setup:
	GPIO.setup(Buzzer, GPIO.OUT)
	GPIO.output(Buzzer, 1)
	# RGB setup:
	GPIO.setup(LedRed, GPIO.OUT, initial=GPIO.LOW)
	GPIO.setup(LedGreen, GPIO.OUT, initial=GPIO.LOW)
	GPIO.setup(LedBlue, GPIO.OUT, initial=GPIO.LOW)
	# DS18B20 setup:
	ds18b20Init()

def loop():
	if len(sys.argv) != 3:
		print 'Usage:'
		print '    sudo python 29_expand01.py [temperature lower limit] [upper limit]'
		print 'For example: sudo python 29_expand01.py 29 31\n'
		destroy()
		quit()
	low = float(sys.argv[1])
	high = float(sys.argv[2])
	if low >= high:
		print 'Parameters error, lower limit should be less than upper limit'
		destroy()
		quit()
	print 'System is running...'
	print 'The lower limit of temperature:', low
	print 'The upper limit of temperature:', high
	
	while True:
		# Read temperature from ds18B20
		temp = tempRead()
		print 'Current temperature:', temp

		# Under/Over limit alarm:
		if temp < low:
			GPIO.output(LedBlue, 1);
			GPIO.output(LedRed, 0);
			GPIO.output(LedGreen, 0);
			for i in range(0, 4):
				beep(0.25)

		if temp >= low and temp < high:
			GPIO.output(LedBlue, 0);
			GPIO.output(LedRed, 0);
			GPIO.output(LedGreen, 1);
			time.sleep(1)

		if temp >= high:
			GPIO.output(LedBlue, 0);
			GPIO.output(LedRed, 1);
			GPIO.output(LedGreen, 0);
			for i in range(0, 8):
				beep(0.125)

def destroy():
	GPIO.output(LedRed, 0)
	GPIO.output(LedGreen, 0)
	GPIO.output(LedBlue, 0)
	GPIO.output(Buzzer, 1)
	GPIO.cleanup()

if __name__ == '__main__':
	setup()
	try:
		loop()
	except KeyboardInterrupt:
		destroy()