Professional Documents
Culture Documents
uk
http://www.cl.cam.ac.uk/freshers/raspberrypi/tutorials/temperature/
Co nnect pin 1 to the gro und GPIO pin (labelled GND o n the AdaFruit co nnecto r). Co nnect pin 2 to the GPIO pin 4 (labelled #4 o n the AdaFruit co nnecto r). Put the 4.7k resisto r between pin 2 and pin 3 o f the temperature senso r. Turn the Pi o n, then put yo ur finger against the senso r. If it is co nnected the wro ng way ro und, then it will get very ho t within a seco nd o r two , in which case, turn the Pi o ff o nce it has bo o ted, and wait a while (it was still ho t after 10 minutes when I did it) fo r it to co o l do wn, and then take o ut the senso r and put it back in again the right way ro und.
The AdaFruit ribbo n cable co nnecto r saves us fro m having to attach wires directly to the Raspberry Pi bo ard, as this wo uld just be really fiddly. It also has better labelling o n the GPIO pins, which makes it easier to wo rk with. The temperature senso r we are using here, the DS18S20, has three legs. Two o f these pins put the senso r in a circuit between a high vo ltage pin and gro und. This co nnectio n pro vides the po wer fo r the senso r to measure the temperature. The third pin co nnects to a GPIO pin, and it is with this co nnectio n that we can co mmunicate with the senso r. The resisto r that we have put between this data pin and the high vo ltage pin is what is kno wn as a pull-up resisto runless the data leg gro unds itself to send a 0 alo ng the wire, its vo ltage o n the wire will be "pulled-up" to 3.3V. This makes it simpler fo r the senso r to co mmunicate with o ur Pi, as to send a 1 signal, it do esn't have to do anything at all.
The first step here was to run the co mmand mo dpro be. This co mmand can lo ad individual mo dules that do particular things into the kernel, so we can use them. If yo u want to avo id typing the mo dpro be co mmands every time yo u start up yo ur Pi, yo u will need to tell Linux that yo u want these mo dules to be lo aded auto matically when it starts. This can be do ne by o pening up the file /etc/mo dulesyo u need to be ro o t to do this, so type sudo leafpad /etc/mo dules into the terminal. No w add the lines "w1-gpio " and "w1-therm" to the end o f this file, save it and clo se. The business with finding the right fo lder and then reading the file to get the temperature might seem reaso nably simple, but as yo u might guess, there is a bit o f wo rk being hidden here. What actually happens when yo u try and read what is in the w1_slave file is that the Pi sends a message to the temperature senso r asking it what the temperature is. The senso r spends half a seco nd o r so wo rking it o ut and sending it back (which is why there is a slight delay after yo u enter the co mmand), at which po int the Pi puts this info rmatio n in the file and gives it back to yo u. Try o pening the file like any o ther, in the graphical file manager interface. Open up the file bro wser, go to the directo ry the file is in and o pen up the file in Leafpad. The same text as befo re will be displayed.
# The first two characters are "t=", so get rid o f tho se and co nvert the temperature fro m a string to a number. temperature = flo at(temperaturedata[2:]) # Put the decimal po int in the right place and display it. temperature = temperature / 1000 print temperature There are quite a few Pytho n co mmands in this sectio n, which might be unfamiliar. In the Pytho n IDLE terminal, type help(so mething) and a descriptio n o f whatever was put between the brackets will display and so me general info rmatio n abo ut what can be do ne with the co mmand. Fo r example, with tfile.read(), there exists also tfile.readline() and tfile.readlines(), which as the name suggests do different things. If the variable assignment (the temperature = bit) is no t included in the lines written abo ve, the result o f the .split metho d will be o utput. In the co mmands just no w, the text is slo wly bro ken do wn until just the temperature values remain, with several lines o f co de. This many lines o f co de is no t necessary, temperaturedata = text.split("\n")[1].split(" ")[9] wo uld also wo rk, o r splitting by spaces fro m the start, no t newlines. The co de may be harder to read if it is squashed o nto o ne line, ho wever. This o peratio n can be made fasterplay aro und with it, and if a quicker way beco mes apparent, maybe a classmate can wo rk o ut what it do es? See here fo r my example co de.
cd /sys/class/gpio / echo "18" > expo rt cd gpio 18 echo "o ut" > directio n echo "1" > value The LED sho uld no w be turned o n. To turn the LED o ff, type: echo "0" > value cd .. echo "18" > unexpo rt We have o pened up a ro o t terminal here to co ntro l the LED. Why did we have to do this here, but no t when we were wo rking with o ur temperature senso r? With the temperature senso r, all we co uld do was ask fo r the temperature, which is pretty harmless. With the LED, we wanted to change so mething abo ut the hardware (make the GPIO pin we had cho sen into an o utput), which co uld lead to bad things happening if we changed so mething we sho uldn't have, o r changed the wro ng pin number. With the Raspberry Pi, it is designed in a way so that yo u can't break anything just by changing the wro ng GPIO pin, but the restrictio n is left o ver fro m mo re expensive co mputers that are mo re easily bro ken. Once we've o pen a ro o t terminal, first we have to tell the Pi that we want to do so mething with a particular GPIO pin, which we do by using echo to write the pin number into the expo rt file. The Pi no tices we've do ne this, and sets up a fo lder fo r that pin to allo w us to co ntro l it. Then we tell the Pi that we want this pin to be an o utput pin, again using echo , so that we can turn the LED o n and o ff. Writing the number "1" to the value file makes the pin turn o n, so that current flo ws o ut o f that pin. Once we've go t o ur LED wo rking, we turn it o ff by echo ing "0", and then telling the Pi that we are do ne wo rking with that pin by writing the pin number to the unexpo rt file. We've just seen ho w to turn o n and o ff the LED using the terminal, so no w it's time to make it wo rk with Pytho n. The pro gram needs to write to a file. Here's an example o f writing "Hello " to a file named "example". Build upo n that. # Open the file and write to it, hence "w" f = o pen("example", "w") f.write("hello ") f.clo se() Run this pro gram in a terminal using sudo pytho n fo r the reaso ns explained abo ve. It sho uld turn the LED o n. See here fo r my example co de.
detail o f what needs to be do ne to get the GPIO pins turned o n and o ff. When the Pytho n co mmand GPIO.setup(18, GPIO.OUT) library go es away and do es all the things that we did befo rewriting the pin number to the expo rt file, and writing "o ut" to the file called directio n but they are just hidden away. The setmo de co mmand tells the library that we are labelling o ur pins like they are o n the ribbo n cable bo ard. The bo ard refers to the pins in terms o f what they do , which is why the pins have labels like "3V3", "GND", "5V0". The alternative, which wo uld be do ne with GPIO.setmo de(GPIO.BOARD) is to number the pins in the same way the ho use numbering o n a street is do nethe pins o n o ne side have the numbers 1,3,5 all the way up to 25, and the pins o n the o ther side have the numbers 2,4,6 all the way up to 26.
Step Nine (A): Checking for the Button Press from the T erminal
Turn the Pi o n. Open a ro o t terminal. Type in the fo llo wing, replacing "17" with the pin yo ur butto n is co nnected to : cd /sys/class/gpio / echo "17" > expo rt cd gpio 17 echo "in" > directio n cat value If the butto n is no t pressed, this co de sho uld print "0". Ho ld the butto n do wn and run the pro gram again: the number "1" sho uld have printed o ut. No w the testing is co mplete, type: cd .. echo "17" > unexpo rt
As we did with the LEDs, we have to tell the Pi that we want to do so mething with a particular GPIO pin, which we do by using echo , just like earlier. Then we tell the Pi that we want this pin to be an input pin, by us echo ing "in" rather than "o ut", so that we can measure whether o r no t the butto n is pressed. When we read the file "value", we are checking whether the input is high o r lo w (1 o r 0 respectively). Finally, the "unexpo rt" the pin as befo re.
Step Nine (B): Checking for the Button Press Using Python
Launch Pytho n as ro o t, and type: impo rt RPi.GPIO as GPIO # Use the pin numbers fro m the ribbo n cable bo ard GPIO.setmo de(GPIO.BCM) # Set up this pin as input. GPIO.setup(17, GPIO.IN) # Is the butto n pressed o r no t? GPIO.input(17) # Ho ld do wn the butto n, run the co mmand again. The o utput sho uld be "true". GPIO.input(17) The instructio ns abo ve have sho wn ho w to check fo r a butto n press. Ho w abo ut putting this into a lo o p, so that every time the butto n is pressed, so mething happens, like a message printing to the screen. Ho wever, the Pi will be able to print o ut a message and mo ve o n to checking the butto n again much faster than a finger can be taken o ff the butto n. So , yo ur pro gram will have to sit and wait fo r so me seco nds, maybe with a "while" lo o p and the "pass" co mmand. Once this wo rks, try to make the pro gram the temperature whenever the butto n is pressed. See here fo r my example co de.
We need ano ther LED, and ano ther 220 resisto r. The two LEDs will be used to sho w when o ur pro gram is ready to start measuring temperature, and when it is actually lo gging it to a file. To make this mo re o bvio us, a different co lo ur LED wo uld be useful here. With these extra parts, wire the LED up to ano ther free pin, just like the first LED is. Open up text edito r, and fo llo w these steps: Impo rt the RPi.GPIO library. Set up the three GPIO pins (the butto n and two LEDs) as input and o utputs. Turn o n o ne o f the LEDs to indicate we are ready to start lo gging. Use the co de fro m the previo us exercise to check fo r a butto n press, and wait fo r the butto n to be un-pressed. When the butto n is pressed, we want o ur first LED to turn o ff and the o ther to co me o n, to indicate that o ur Pi is no w taking data. Next, o pen up a file which we can write, to give us so mewhere to put the data. Add ano ther while lo o p to wait until the butto n is pressed again, and in the lo o p read the temperature in the same way we've do ne befo re. Instead o f printing the temperature, we will write the temperature to the file which we o pened earlier. One change we will need to make fro m ho w we learnt to write to files earlier is that no w, we want each temperature value to be o n a newline. This can be do ne by adding "\n" to the end o f the string we are writing, like str(temperature) + "\n". When the last while lo o p exists, we want to clo se the file, as we are finished with it, and we do this with datafile.clo se(), and turn o ff the LED. See here fo r my example co de. Tuto rial o ver! Enjo y yo ur temperature mo nito ring, and see belo w fo r Appendices.
Appendices
Pro grams can almo st always be impro ved, perhaps by adding so me mo re co de so that it can do so mething new, o r just by changing what is there to make it better, o r mo re readable. This is what this sectio n is all abo utpo inting o ut things that do n't wo rk as well as they co uld, o r things which wo uld be better do ne in a different way.
Yo u will also see that the co mmand to o pen the file has gained an extra o ptio n, the "1" at the end. If yo u run the o ld co de we had, and lo o k in yo ur file bro wser while do ing so , yo u'll no tice that the file appears as so o n as we start lo gging, but is listed as having a size o f zero bytes until we press the butto n to sto p lo gging. This happens because Pytho n sto res all the data in memo ry so mewhere, and o nly actually puts it do wn o n the SD card when yo u clo se the file. The issue with this behavio ur is that, if we were to suddenly pull the plug, o r sto p the pro gram running, we wo uld lo se all the data we had co llected, because no ne o f it was do wn o n the card. By adding the o ptio n "1" to o ur o pen co mmand, we tell Pytho n that we wo uld like it to o nly keep a single line o f text in memo ry at a time, and that it sho uld put write each bit o f text to the SD card when the line ends. See here fo r my example co de.
beco ming co nfused. Yo ur co de fo r this sectio n will pro bably lo o k a bit like: if (time_2 - time_1) < measurement_wait: no _o f_sleeps = int(ro und((measurement_wait - (time_2 - time_1)) / 0.1)) fo r i in range(no _o f_sleeps): time.sleep(0.1) if GPIO.input(BUTTON_GPIO_PIN): butto n_pressed = True break if butto n_pressed:
Where Next?
No w we've go t a co mplete temperature senso r pro gram, what can we do next? Well, having written all this co de just to measure temperature, wo uldn't it be go o d if we co uld just write so me small piece o f co de to use a different type o f senso r, and then have this just wo rk? Fo rtunately fo r yo u, this is exactly what the next page is all abo ut. So let's mo ve o n and explo re ho w we can start using plugins.