Categories

Raspberry PI and DS18b20 with LED warning

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

pi and ds18b20 and led

pi and ds18b20 and led

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

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)

Run the program, now touch the sensor and raise the temperature above 24 c. You may need to change the value

DS18b20 and the Raspberry PI example

In this example we connect a DS18b20 temperature sensor to our Raspberry PI and read in the value, here is the schematic for this.

 

pi and ds18b20

pi and ds18b20

Start the terminal and entering the following

sudo modprobe w1-gpio
 
sudo modprobe w1-therm
 
cd /sys/bus/w1/devices
ls
 
cd 28-xxxxxxxxxx (change this to match the serial number that pops up)
 
cat w1_slave

You should see something like this

terminal_ds18b20

terminal_ds18b20

In the output above the t=18875 is the temperature, in this case this is equal to 18.875

Now we will try this in python

Open the terminal and type in the following

sudo idle

Wait for IDLE to open,  then click File > New to open a new window. Or you can use CTRL + N.

Now type in the following code

import time
 
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
 
 time.sleep(1)

Click File > Save when you are finished (Ctrl + S).

To run your program, click Run > Run (Ctrl + F5), your should output like the following

ds18b20_out

ds18b20_out

 

More Raspberry PI LED examples in python

In our previous example we flashed an LED on, here are another couple of examples

This will flash the led on and off

import RPi.GPIO as GPIO
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
 
state = True
 
# endless loop
while True:
 GPIO.output(7,True)## Turn on GPIO pin 7
 time.sleep(1) ## 1 second
 GPIO.output(7,False)## Turn off GPIO pin 7
 time.sleep(1)## 1 second

And no we will flash the LED 3 times

import RPi.GPIO as GPIO ## Import GPIO library
import time
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
 
for i in range(0,3):## Run loop 3 times
	GPIO.output(7,True)## Switch on pin 7
	time.sleep(1)## Wait 1 second
	GPIO.output(7,False)## Switch off pin 7
	time.sleep(1)## Wait 1 second

Flash an Led using your Raspberry Pi and Python

In the following example we will connect an LED and resistor to pin 7 of P1 on our Raspberry PI and we will flash an LED on, here is a picture of this

pi and led_bb

pi and led_bb

OK we will use python, this tutorial assumes you already have Raspbian , Python and the GPIO library installed. I downloaded the latest NOOBS image and this all appeared to be installed by default

Open the terminal and type in the following

sudo idle

You should see something like this

sudo idle

sudo idle

 

Wait for IDLE to open,  then click File > New to open a new window. Or you can use CTRL + N, you can see this below.

idle window

idle window

Now type in the following code

import RPi.GPIO as GPIO ## Import GPIO library
 
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD) ## Use board pin numbering
GPIO.setup(7, GPIO.OUT) ## Setup GPIO Pin 7 to OUT
GPIO.output(7,True) ## Turn on GPIO pin 7

Click File > Save when you are finished (Ctrl + S).

To run your program, click Run > Run (Ctrl + F5), your LED should light