1.7k
In a previous example, we connected a DS18b20 to our Raspberry PI and measured and displayed the temperature, in a slight twist what about if we acted on this reading and if the temperature exceeded a minimum voltage we switched an LED on. In the real world it could be an audible warning or perhaps you would try and reduce the temperature somehow.
We add the LED and resistor to pin 18
Most of the instructions are as per our previous example, here is the python code
Note you will have to replace the 28-0000027a4334 value
[codesyntax lang=”python”]
import RPi.GPIO as GPIO import time GPIO.setwarnings(False) GPIO.setmode(GPIO.BOARD) ## Use board pin numbering GPIO.setup(18, GPIO.OUT) ## Setup GPIO Pin 18 to OUT while 1: tempfile = open("/sys/bus/w1/devices/28-0000027a4334/w1_slave") temptext = tempfile.read(); tempfile.close() tempdata = temptext.split("\n")[1].split(" ")[9] temperature = float(tempdata[2:]) temperature = temperature / 1000 print temperature if temperature > 24: GPIO.output(18,True) else: GPIO.output(18,False) time.sleep(1)
[/codesyntax]
Run the program, now touch the sensor and raise the temperature above 24 c. You may need to change the value