Raspberry Pi Temperature Sensor
Written by Matthew Kirk. In this tuto rial, we will be building a circuit to co nnect a temperature senso r to o ur Raspberry Pi, and writing a pro gram to read the senso r data. The circuit we will build is go ing to co nnect to the Raspberry Pi using the GPIO pins. GPIO stands fo r General Purpo se Input/Output. General purpo se because all they are is simple co nnectio ns that can be either high o r lo w, a binary cho ice. This means we can easily do things that invo lves binary cho ices, and it will still be nice and simple to understand what is go ing o n. In this tuto rial, we are go ing to be turning LEDs o ff and o n, and checking whether butto ns are being pressed—all very binary actio ns, which makes them ideally suited fo r GPIO pins. Ho wever, this is no t all that GPIO pins can be used fo r. A large range o f hardware devices can co mmunicate by transmitting binary data alo ng a co nnectio n, and this can also go straight o nto the GPIO. If we have the right pro gram to wo rk o ut what the data means, we can do even mo re co mplicated things. We are go ing to be making use o f a temperature senso r that sends the temperature info rmatio n into a GPIO pin, and so me so ftware to co nvert this binary data into a useful fo rmat. Once we have built o ur circuit, the next step is to write a pro gram to read the temperature, and give it to us in a nice fo rmat. Once that is do ne, we will add so me LEDs and a butto n to o ur circuit, and find o ut ho w to measure butto n presses and turn the LEDs o n and o ff fro m o ur pro gram. Finally, we will put all these bits to gether to make a butto n co ntro lled temperature lo gger—a pro gram that will measure the temperature every seco nd and put this in a file, which can be started and sto pped by the press o f a butto n.

Step One: Updating the Kernel
The first step is to change where o ur Pi updates fro m, by editing a text file. We need to o pen up the file /etc/apt/so urces.list.d/raspi.list as ro o t, so type: sudo leafpad /etc/apt/so urces.list.d/raspi.list No w change the line in this file so that it reads "deb http://archive.raspberrypi.o rg/debian/ wheezy main untested", then save and clo se the file. Next, do the fo llo wing co mmands: apt-get update apt-get upgrade What we are do ing here is making the Pi updte itself using the very latest kernel. The kernel is the lo west level bit o f so ftware, that makes the co nnectio n between the actual hardware, and all the o ther so ftware that can run. Once the Pi kno ws it can update itself fro m this brand new place, we upgrade the kernel. This new kernel has all the extra co de it needs to access the temperature senso r we will be using.

Step T wo: Connecting the T emperature Sensor
Turn o ff the Pi—it is generally a bad idea to co nnect wires while there is electricity running thro ugh the pins. Obtain: An AdaFruit ribbo n cable co nnecto r. A DS18B20 temperature senso r (o r this versio n if yo u want to be able to mo ve yo ur senso r aro und). A 4.7kΩ resisto r, a breadbo ard, and a selectio n o f wires. So lder, and a so ldering iro n. So lder the ribbo n co nnecto r parts to gether, and attach the ribbo n cable co nnecto r to the Pi and breadbo ard. Co nnect pin 3 o n the senso r to the 3.3V GPIO pin (labelled 3V3 o n the AdaFruit co nnecto r).

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 r—unless 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.

Step T hree (A): Reading the Sensor from the T erminal
Turn o n the Pi. Once a user is lo gged o n to the Pi, type these co mmands into a terminal, o r just at the pro mpt given upo n lo gin and befo re typing "startx": sudo mo dpro be w1-gpio sudo mo dpro be w1-therm cd /sys/bus/w1/devices/ ls The entry o n the screen that is mo stly numbers is the serial number o f the senso r. The senso r used fo r this tuto rial has the serial number "10-000802824e58". Replace the serial number in the next co mmand with the o ne fo r the senso r used. cd 10-000802824e58 cat w1_slave Two lines o f text will be printed. On the seco nd line, the sectio n starting "t=" is the temperature in degrees Celsius. A decimal po int go es after the first two digits, so the example value o f "t=22250" is in fact "t=22.250" degrees Celsius:

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 dules—yo 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.

Step T hree (B): Reading the T emperature from Python
Open a terminal and type pytho n. At the pytho n IDLE co mmand pro mpt, type the co mmands belo w, igno ring the lines starting with #—they explain what the co mmands do : # Open the file that we viewed earlier so that pytho n can see what is in it. Replace the serial number as befo re. tfile = o pen("/sys/bus/w1/devices/10-000802824e58/w1_slave") # Read all o f the text in the file. text = # Clo se the file no w that the text has been read. tfile.clo se() # Split the text with new lines (\n) and select the seco nd line. seco ndline = text.split("\n")[1] # Split the line into wo rds, referring to the spaces, and select the 10th wo rd (co unting fro m 0). temperaturedata = seco ndline.split(" ")[9]

# 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, 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 faster—play 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.

Step Four: Write a Script to Print Out the T emperature.
Install a text edito r such as Geany o r SciTE o r use the preinstalled Leafpad o r equivalent. Open the edito r and co py the co de written abo ve (o r the versio n with the different splitting that was suggested). Save the file as in /ho me/pi. Open a terminal and run pytho n The pro gram sho uld o utput the temperature as it did befo re. What we've do ne here is put the co mmands we typed o ut earlier into a script—so that we can easily run the pro gram witho ut having to type in all the co mmands again. Here, it is advised to install a different text edito r fo r writing co de. The reaso n fo r this is that the default text edito r—Leafpad—that the Pi co mes with isn't really designed fo r writing co de in. If yo u o pen up yo ur Pytho n script in IDLE and in Leafpad, yo u'll see that IDLE has added co lo ur to the co de—this is called syntax highlighting. This is so mething that almo st all text edito rs designed fo r writing co de in do , because it makes it easier to see what is happening in the pro gram. SciTE o r Geany are reco mmended in this tuto rial. SciTE is a bit quicker to o pen files, and lo o ks simpler to start with, whereas Geany has lo ts mo re features that start to be useful when yo ur pro gram get a bit lo nger, such as listing all the names o f variables in yo ur pro gram do wn the side, which makes it easier to remember what names have been used.

Step Five: Connecting an LED
Turn o ff the Pi. Obtain an LED, a 220Ω resisto r, and pro bably so me mo re wires. Pick o ne o f the numbered pins o n the ribbo n cable that co nnects yo ur Pi to the breadbo ard and co nnect the lo ng leg (o n the o ppo site side to the flat edge o f the LED base) o f the LED to this pin. Co nnect the 220Ω resisto r between the gro und pin o f the Pi and the o ther (lo nger) leg o f the LED. What we have do ne here is set up a circuit fro m o ne o f the GPIO pins, thro ugh the LED and resisto r, do wn to gro und. With the Pi, we can switch o n a specific GPIO pin, so it has a high vo ltage, and current will flo w thro ugh the LED and resisto r. The resisto r we've put in sto ps to o much current flo wing, which co uld heat up the LED so much that it wo uld burn o ut.

Step Six: T urning On the LED
Turn o n the Pi. Open a ro o t terminal fro m the menu, o r by running sudo su in a no rmal terminal. No w, type these co mmands, where "18" in this example is replaced by whichever pin the LED is co nnected to :

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.

Step Seven: T here is an Easier Way!
We are go ing to install a library fo r Pytho n that will let us turn o n GPIO pins, witho ut having to go thro ugh the hassle o f o pening and clo sing lo ts o f files like in the last exercise. Install the package with sudo apt-get install pytho n-rpi.gpio in a terminal. Launch Pytho n as ro o t with sudo pytho n 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 the pin yo u are using ("18" is an example) as o utput. GPIO.setup(18, GPIO.OUT) # Turn o n the pin and see the LED light up. GPIO.o utput(18, GPIO.HIGH) Turn o ff the pin to turn o ff the LED. GPIO.o utput(18, GPIO.LOW) This library that we have installed is just a piece o f co de, this time written in the C pro gramming language, that hides away all the

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 re—writing 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 ne—the 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 Eight: Connecting a Button
Here, a push-to -make butto n and a 1kΩ will be needed. Pick ano ther free input pin and co nnect this pin to o ne side o f the butto n, and the o ther side o f the butto n to the 3.3V pin. Put the resisto r beteen the input pin and the gro und pin o n the Pi. The type o f butto n we are using here is called push-to -make, which means that the two sides o f the switch are no t co nnected unless yo u are pushing it. Push-to -break butto ns exist to o , which are always co nnected unless the butto n o n them is pushed. We are also using a resisto r to co nnect the input GPIO pin to gro und, which makes sure that the input is fixed to o ff when the butto n isn't pressed, rather than the vo ltage flo ating aro und so mewhere between high and lo w. We need a larger resistance than we might no rmally use fo r o ther things, to sto p current flo wing straight thro ugh to gro und when the butto n is pressed.

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.

Step T en: Writing a T emperature Logging Program
The final step in this tuto rial is putting to gether all the different things we can do to make a temperature lo gging pro gram— so mething that will reco rd the temperature to a file fo r as lo ng as we want.

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.

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 ut—po 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.

Appendix One: Name of the Data File
The same filename is always used fo r the script, so o ld data disappears everytime there is so me mo re. Filenames can be made unique with "temperaturedata1.lo g", "temperaturedata2.lo g", but a mo re helpful way is to add what is kno wn as a timestamp to the name. To do this in Pytho n: Impo rt the "time" mo dule into Pytho n, find o ut the current time fo r the lo gging file and put it in an easy to read fo rm, then append this to the file name, like so : impo rt time # This displays the time in the fo rmat year-mo nth-day, ho ur-minute-seco nd. timestamp = time.strftime("%Y-%m-%d-%H-%M-%S") # Put to gether the different bits o f the filename into o ne, and o pen the file. filename = "".jo in(["temperaturedata", timestamp, ".lo g"]) datafile = o pen(filename, "w", 1) There are quite a few mo re co mplicated bits in this new bit o f co de. The strftime() functio n returns the time fo rmatted in a particular way, a list o f which can be fo und in the Pytho n do cumentatio n. The fo rmat in the example means that if the files are so rted alphabetically, the newest files co me last and the o ldest first. The jo in functio n is a functio n that all strings have, that allo ws us to take any list o f strings and quickly jo in them to gether, putting whatever string we are using to call jo in o n between every item in the list. Here we use an empty string so that the bits just get put straight to gether. This is generally the quickest way to co nnect a bunch o f text to gether in Pytho n.

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.

Appendix T wo: Changing the GPIO pins
Ano ther idea is that up to here, the pin numbers fo r o ur two LEDs, butto n, and temperature senso r have been put straight into the pro gram as needed. Which wo rks fine, but what happens when we mo ve stuff abo ut o n the bo ard? Suddenly, everything is co nnected to a different GPIO pin, and all o ur clever instructio ns are being sent to the wro ng things—we'll be checking fo r a push o f an LED, and trying to light up a butto n. Fixing this means go ing tho ugh the who le o f o ur pro gram, checking fo r each time we used a pin number, and changing each o f them to the new o ne. So instead, ho w abo ut we fix this pro perly? At the beginning o f the script, put in a few variables called, say, LED1_GPIO_PIN, LED2_GPIO_PIN, and BUTTON_GPIO_PIN. Give them the value o f the appro priate pin number. Then, replace all the o ccurrences o f actual pin numbers in the co de with these variables. So no w if we change the po sitio n o f the pins o n the Pi, all we have to do is change the value o f these new variables, and everything will still wo rk. In this new bit o f co de, capital letters are used fo r the variable names. This is reco gnised by mo st pro grammers as meaning that these variables are co nstants that aren't suppo sed to change during the co urse o f running the co de. See here fo r my example co de.

Appendix T hree: Logging at a variable rate
With o ur pro gram as it currently is, the temperature is always measured as fast as po ssible, which is abo ut o nce every threequarters o f a seco nd (this is basically ho w lo ng it takes the senso r itself to measure the temperature, rather than any slo w wo rking by the Pi). If we are measuring temperature o ver 30 seco nds, o r a few minutes perhaps, this sampling rate is fine. But if we wanted to lo o k at ho w the temperature varied o ver an ho ur, o r even a day, then we wo uld end up with tho usands, maybe tens o f tho usands o f readings, which wo uld change quite slo wly. In this case, it wo uld be nice to be able to make o ur pro gram o nly make measurements at much larger intervals. If we want to do this, we need to make o ur pro gram wait fo r the right amo unt o f time between measurements, so that they are do ne at the right intervals. Using the functio n time in the Pytho n time mo dule, we can wo rk o ut ho w lo ng has passed since we last asked fo r the temperature, and then co mpare this to ho w lo ng we want to wait between measurements. So in yo ur co de, add in a variable that sto res the time just befo re yo u read the temperature, and at the end o f the while lo o p. Yo u can co mpare these two times, and if the difference is less than the wait we want, make the pro gram wait fo r ho wever lo ng it needs to make up the difference. In Pytho n, making yo ur pro gram wait fo r a while is as simple as using the sleep co mmand in the time mo dule, and giving it the length o f time yo u want to sleep fo r. But there is a pro blem with this. When the pro gram is sleeping, it isn't paying attentio n to anything else—in particular, no t the butto n which tells o ur pro gram to sto p lo gging. This means to sto p o ur pro gram, as it currently is, requires yo u to press the butto n during the small amo unt o f time when the Pi is no t sleeping o r reading the temperature. To so lve this, we will make the Pi do lo ts o f small sleeps, and check the butto n between each o f them. Replace the single sleep with a fo r lo o p, that will sleep fo r 0.1 seco nds ho wever many times is required to make up the difference, and check fo r the butto n press after each little sleep. If the butto n is pressed, yo u will want to use the break co mmand to exit the fo r lo o p, but first, set so me variable to True, so that we can test this variable in the o uter while lo o p, and break o ut o f that is it is True. Do n't fo rget to define this variable as False befo re the lo o p starts, to sto p Pytho n

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:

break See here fo r my example co de.

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.

Sign up to vote on this title
UsefulNot useful