Arduino with Python LESSON 11: Graphing and Plotting Live Data from Arduino with Matplotlib

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello guys i'm pulmicort ER and I am here with lesson number 11 on using Python with the Arduino and we've got an exciting lesson today what we're doing is we're streaming live data from the bed of fruit bmp180 pressure and temperature sensor through the arduino into python and then we are plotting that data live in real time in the in python and so this is an exciting lesson we're going to get you to the point that you can do this on your own just to show you here that this thing really is working if I come down here and put my finger on the sensor you can see that my most warm finger warms the sensor up I take it off and then you can see in here red in red in real time the temperatures the temperature of the sensor dropping back down so it's just really powerful to be able to plot live data from sensors or other projects in your Arduino and so we're going to take you through it step by step today and how to do that if you haven't been with us on the earlier lessons you probably need to catch up on a few of the prerequisites so you'll be up with us you can do that by going to my website www.howtouseashoppingcart.com/kickstart because in lesson 9 show you how to wire up the sensor and then we give you some code that will get that sensor up and running and seeing that date over to your over your serial porting so you want to do that that lesson 9 if you have an on if you haven't done it done it already and then the final thing is is that the other lesson that with the other library that we need is a library that is called draw now and that draw now is the magic library allows map plotline to plot things live and so if you just go through there ripple things in these earlier lessons you get your Python V Python numpy be met plot light and then drawn out libraries installed if you have all those things installed then you should be able to follow along with me step by step from here on out so let's go on back and what we want to do is we want to revisit real quickly this circuit that we have down here for for operating the sensor you can see this is how I'm now in where am I I am python with arduino lesson 9 i show you how to it up and then i give you some code that makes this thing work well if you were with me in lesson 9 I'm going to kill this at this point it doesn't not want to die but I will kill it there okay if you were with me in lesson 9 ah this is the program that we wrote for controlling for controlling the bmp180 sensor and it's it's the the code that then streams the data over the serial port there's a couple of adjustments that I want to make here I want to run this thing a little faster because we got a whole lot going on and so I want to change the baud rate here to faster 115 200 and because of all the graphing and charting and all the stuff we want to do I don't want to be waiting for that data I want to get the data coming in as quickly as I can so I'm going to go ahead and step up that baud rate to 115 200 also what we were doing here Oh Mia let me go ahead and download this and call it up on the serial monitor so you can see it I'm hoping it'll download yep it did and then I need to change this to 115 200 okay this is what we did in lesson 9 and you can see that I'm reading the temperature and the pressure here and I'm just creating a stream over the serial port well this is great for watching it over the serial port but if I'm going to be pulling it into Python I don't want all this other stuff that it's nice for a human to look at all these strings that are telling me what I'm looking at so I want to simplify this code a little bit and where I come in I just want to print very simply I don't want to print the temperature is I'm going to take that out and I want to say serial print line temp F that's good I'm going to change that to a print line and then I'm going to take out that string and then I what I want to do is just print a comma okay and then I don't want to print any of this and then I just want to do a print I'm going to make that a print line okay so what I'm trying to do is I'm trying to get on one line a string that is the temperature in Fahrenheit a comma and then the pressure and that way I'll read that line in Python all at once and so what we do is serial.print temp F okay serial print the comma and then serial print line pressure so that'll take us to the next line now okay so let's look at this so this should this be a data stream at this point and it'll be a lot easier for us to parse over on the Python side so let's look at this okay so what you can see is I've got temperature I've got a comma and I've got pressure and so that's what I want so what we need to do now is we need to go over to the Python side and we need to read that in there are some libraries that we are going to need on the Python side okay if we are going to read from the serial port we need to import PI serial which we do just as import serial okay so this is import the serial library okay import the serial library okay also we're going to do some pretty good array mess and pretty sophisticated array math and so we're going to need to import numpy okay and and you should have numpy if you did that lesson two because we imported the Python and numpy comes with the Python but you'll need that library we need to import mat plot live dot type lot we're going to import that as PLT I like to import it as PLT because then that PLT is just a real simple object to work with so we're going to import the mat plot glide library all right and then there is that magic library that allows us and I think we installed this in Lesson number nine if I'm not or lesson number ten that was yet is lesson number ten over here let's just check and make sure that that is rough yes lesson number ten is where we learned how to put in drawn out so from draw now we want to import everything okay so we've imported our lives cereal which is the PI serial library numpy mat plot line and draw now those are the libraries we need to make all this work okay now what I want to do is I just want to go down and make sure that I'm getting that data over because that's the first thing that we got to do and so I'm going to create a while loop that just says while true okay and what do I say while true we'll true is always true so this is going to be a while while you that loops forever okay and so that's what I want to do and now I want to go and I want to read the data from from the serial monitor but I don't want to read it unless there is data there so I'm going to wait at this point I'm going to create a while loop and let's see I need to do one other thing up here before I get into this I need to create that object that'll allow me to read from the serial port so I'm going to make an object that I'm going to call our dueño data okay and the Arduino data is going to be serial because that's what we called the library that that's this library here we imported serials we're going to do serial dot serial which is the function that we are going to that we are going to do and then we got to tell it where the comdata is where is the comdata well I'm going to come over to our dueño and I'm going to look under tools I'm going to look under port and it is telling me that for me my Arduino is talking on port 11 on comm 11 when you do this it's probably going to be different but whatever yours is you need to put that here and so mine is on comm 11 and then I need to tell it how fast I'm talking and if you remember on our program here we stepped it up to 115 200 for the baud rate so I need to work at the same level here 115 200 and so this is just a little bookkeeping to create an object that I can read from the serial port and so that serial port object I'm calling Arduino data okay with that set up now I'm going to have a loop here where I'm just going to be looping and reading the data but now I don't want to read the data unless there's data there and so what I want to do is I want to create a while loop here that just hangs and doesn't do anything until there's data well how do I know if there's data well I look and I say Arduino data in waiting is equal equal zero all right and then wait here until there is data okay so what this is doing is and always our loops here I need to I mean on a on a loop in Python we indicate that with a : just like I put a call in there okay so this one this first while loop is going to loop forever and then this while loop is just going to hang until there's data there and then so what am I going to do in this loop I'm going to do absolutely nothing I'm just going to say pass do nothing okay so when I get here if there is no data if Arduino data which I named the object here in waiting is equal equals zero just sit here and loop so I'm sitting at this point waiting for data to come in as long as there's no data I am just sitting there looping but when there is data then I want to come down and I want to read that data and so I'm going to read that data into a variable called our dueño string okay I'm going to read that variable into I'm going to read that serial data into a variable called Arduino string so our dueño string is going to be equal to or do we are we know theta dot read one okay my Arduino data because that was the name of that object that is connected to the serial port so I can take that object and I can do a command called red line so Arduino data read line will go out and read the serial port and then it puts it in the variable Arduino string so I sit here in loop as long as there's no data and then as soon as there's data I read it and then Arduino string is just my variable and remember in Python we don't have to declare our variables and so we just we just make up our variables as we go along and I read it I think I have just read the data I'm gonna see though I want to kind of run this just to see if I'm doing this Arduino string and so I wait for the data when the data is there I read it and then I'm going to print it and let's just run this just to see if we got something that's working Oh Arduino data is not defined I probably our Arduino I need to spell this right Arduino so this has to be called the same as this and so our dwee no data is what I named the object so I need to read Arduino data so you see I like to run things as I go so I can find those little mistakes that I make ooh still not happy Arduino data I'm still not spelling it right ro I put that there's that you I was missing our dwee no data like I told you guys I am severely dyslexic so it is very hard for me to not make little transcription errors like that so let's take another look at it I think it's going to run this time well it is going to run oh no it's not I'm still still misspelling this Arduino data our tween Oh data okay oh here it is our queen Oh data so now I have them all spelled the same in all three places I apologize for the delay but we're going to get it this time okay so look what we got now we've got the temperature data and we've got the pressure coming in okay and look at this as I put my finger there the temperature goes up I take my finger off the temperature goes down so we are getting a lot of data from our circuit into Python and we're printing it in Python I just like to get some of this simple stuff taken care of just so that I know that it's working before we get into our hard core plotting but the good thing is we've sort of hit a milestone here in the milestone that we have hit is that we have read the data coming from the pressure sensor into Python and so what do I want to do now well that was like that that was reading a long string so when I do Arduino data dot readline it just reads a string but if we're going to plot we've got to turn that string into numbers and so I'm going to take my print comma out here my print line out here so the first thing I need to do is take the long string and split it into two strings I want to split it into the during that was the temperature and I want to split it into the string that was the pressure and so how do we do that I need to create a new variable and I'm going to call my new variable data array and what data array is going to be is it's going to be this variable Arduino dot string all right when you know I'm going to try to spell it right this time string and then what I'm going to do is I'm going to do the function split so I'm going to take this string Arduino string and I'm going to dot split it and where do I want to dot split it I want to dot split it at the comma because I had a comma between those two numbers and so what this is going to do is this is going to create an array and that array is going to have two values and it's going to have two values based on splitting this variable at the comma okay it's going to do it based on having splitting that variable at the comma and so I then I think I could place a print data array and I could print data array the first one we start counting in Python it's 0 the first one will be 0 and then print data array 1 ok and then I'm just going to do a print to print a blank line but this should print temperature and this should print pressure and so let's look let's look here yeah ok so you see what I'm doing is I am printing stop that I'm printing temperature and I am printing pressure and I'm printing a blank line and let's see if I come in and stick my finger there yes it starts heating up so we're getting our data in and we have split this string into two strings but it's still a string ok it's still a string so what I want to do is I'm going to take these print statements out and what I want to do is actually put these in variables now and so now that I can split this into an array I want to put it into a variable and so what I'm going to do is I want to have a variable called in temp is going to be equal to that first string but now I really don't want it to be a string I want it to be a real number so when I do this I'm going to convert it so this float command will take this data array which is a string of characters okay and it's going to convert it to a real number a floating-point number and the same thing here I want now pressure I think I want I'll just call it P for short capital key capital P is going to be equal to float of that second string okay so I wait for the data when there's data there I read the whole line as a string from this object which I create here and then once I have it read in as a string I'm going to take that string and I'm going to split it at the comma and then the first element of data array which is data array 0 I'm going to put into temp and the second one I am going to put into P so now I should have temp and I should have P so what if I say print temp okay that's a number now and then comma printed comma and then key let's see what happens okay what is it uh forgot my comma there this should work now okay so look at this what is the big deal here what the big deal is is that now this is not a string 81.6 8 this is a number 81 point six eight and this is a number so now I've got two floating point numbers that I can actually work with I have two floating point numbers I can actually work with and so that is good okay one of the things that I want to do now is that since I have those two numbers I want to print I want to make a graph and remember in from our lesson on matplotlib when we graph things we want to graph a raise and so I need to create two arrays I need to create a temperature array and I need to create a pressure array to hold those values so that I can plot them so up here I'm going to create two I'm going to create two empty arrays okay I'm going to create a temp F I'm going to say that that's going to be equal to an empty an empty array so just open bracket close bracket and then I want to create pressure and that's going to be equal to an array with nothing in it so I created two empty arrays why do I create am empty because the data to go in those is going to come from this data string and so I create them empty and then once they're empty to put something in them here I can say temp F is basically what I just do is I say temp F dot append so I take this array and I append to it what I want to append to it ten so the array type F I append to that array this number temp so as I go through here and loop that array is going to get longer and longer and longer because it's going to keep adding the latest temperature data to the end of the array okay and then what I can do here is I can do the same thing and I can say pressure that's this pressure array I want to append the number what key okay so let me just do this just to show you how this is working I'm just going to print one of them temp F as this goes through each time the array should be longer and it'll print it out here and it should be the temperature data yeah I've got one you see how I'm getting a longer and longer and longer list because I'm going to pending more and more and more data to it so that is working and you guys if you're good you don't have to go as slow as I am but this is really sort of for new I like to stop and make sure things are working the way I think I wanted to work as I go along and so that's why we go a little bit slow all right so I'll take that so now I have a temp F array and I have a pressure array okay I have a temp appareil and I have a pressure all right and so now what we want to do is we want to actually start thinking about plotting okay and this is the key to plotting lab data what you want to do is you want to create a function and in that function you want to create your plot like imagine you just wanted to do the plot one time like we did in less than what was it lesson number nine is that where we did the familiar no it was yeah lesson number nine you know it was lesson number eight where we go in and sort of learned how to use learned how to use matplotlib and remember in lesson number eight we could go in and we could basically create all types of different charts and graphs okay well what we want to do here is we want to imagine we're just creating one chart and we want to create a function that would make that one chart so I want on a function I know we haven't talked a lot about functions but basically you create a function by just saying define that means I'm going to define a function and then I'm going to make it give it a name well what does the function going to do the function is going to make a fig okay make a figure you could name this whatever you wanted but now if I call make a fig it will do everything that is defined here and we don't pass it any parameters and this is sort of like a while loop or a for loop you put that colon there so what we are going to do here is you're going to create a function that makes our desired plot okay so this is just going to inside this function squared where you're going to do this I need to be a good boy and I need to go back here and put some comments in here and so what were we doing here we read the line of text okay from the serial port and then what did we do here okay we split it into an array called data oh right and then what did we do here we are convert first element to floating number but then okay you guys you really got to do your comments because I'll forget what I'm doing if I don't do my comments okay what do we do here we convert second element to a floating number and put in P and then we are going to build our F array by count wings tanks and then what we are doing here is we are building our initiative in power building our pressure array by appending P readings okay so now we've got our common sense I'm sorry I should have been doing that as I want to long and what we are doing here is we are creating our serial object named Arduino data okay and so we've got all that we've got all that bookkeeping taken care of and so now we are going to make our function and this function is going to be a function that would just if you just did at one time it would just plot a figure and so what we are going to do is we're going to call this function make figure and then let's do something relatively simple at first let's just plot let's see how simple we could make this let's just plot ah what would be the simplest thing to plot what if we just plotted our temperature and remember our temperature variable is 10 F what if we just plotted that just plot and that's not I need to put plot dot t oo t and this PLT is because we imported this as PLT and we tell it what to do we want to plot and all I'm going to do is just try to plot this temp F okay and so I think that that would probably be about the simplest thing that I could possibly do okay and then I need to come down here and what I need to do is I need to call that function that would uh that would plot that and so what I need to do is I need to say draw now remember that was that library and what do I want to draw okay I want to draw now this function up here which was make fig okay like that and so when it comes here it says draw now draw now what makes big let's make figure well it's this alright there's one other thing that you have to do to plot lab data and that is up here at the top you have to tell it that you are going to be plotting lab data till that plot lie you want interactive mode to plot five theta okay and I hope this isn't too confusing because that's really pretty simple so we import our libraries we're creating an empty a temp F array and an empty pressure array and then we create our serial object and then we say hey Matt plot light I want to plot live data okay then we create the function that is the plot that we want to create then we're going to loop through here reading data and then calling draw now to go out there and plot the data life okay and so I am just going to run this and find out where all my mistakes are okay convert it doesn't like data array one convert second element lets out of range okay it doesn't like that so what did I do wrong there ah float data array one we split this at the comma float is equal to data array 0 data array 1 let's try this again I'm not sure one that's not right okay sometimes like you just get a bad read and I probably need to go in and I probably need to put some error trapping in there but it just got a bad line and we were just sort of unlucky there okay and I should not have done that and I know what I did wrong okay okay after you do this and so now let's put our comment in here and then we'll put our comment and then what this is call a draw now to update our live graph alright and then what you got to do is after you do that you got a PLT dot pause just for a very small amount of time this is like 0.0001 if you put that pause and then it gives you a chance to move things around it's just sort of a little quirk about this draw now after you call it you need to pause plot dot pause for a very small amount of time this is going to work and also the thing I saw that I didn't like was on our plot what I want to do here is I want to kind of tell it what I want to plot and so I'm going to go like a red eye I think I want a red dot and a red dot so our 0 and a line and then like that and so now I should get a little dots and a line coming across so this says I want it read that little says I want dots and that says I want to lie so now let's running okay look at this we are getting live data now look at this we are getting live data graphing it's the temperature what if I come over and put my finger whoa look at that take my finger off we get the nice exponential decay of temperature like we would expect all right we have got the basics working so I hope this wasn't too confusing and it doesn't want to die we got to kill it from the shell I hope this wasn't too confusing and I hope if you look this you could now make it go do the things that you would want to make it do okay you could go make it do all the things that you would want it to do so what do we do we import our libraries I create empty arrays I create my object for reading the serial port I tell Python that I'm going to I tell Python and Matt plot lied that I want to plot in interactive mode so interactive mode is on I've got to tell it that I've got to create this function that inside this function would be imagine you just wanted to make a plot one time this is what I would do okay so all the things associated with how you want your plot to look you put here I did this just the simplest thing just to show you how to get the pieces working what do we do now we create a loop that loops forever what do we do we wait here until we get data at the serial port when there's data at the serial port we read it then we have it in Arduino string we split that at the comma into an array the first value in the array is temperature the second value in the array is pressure we do float so that we convert the strings into real numbers then we built up our temp array and we build up our pressure array and then we tell draw now to go out and do that function make that figure and then we tell it to pause okay this is pretty exciting let's take a look at this again okay let's just take a look at this again alright so here's my data coming in let's see if I put my finger look at that it heats up and then it goes back down okay one thing that you can see that is going to be a problem if we sit here this just continues to add more and more and more data okay it's adding more and more and more data and so what happens if I'm not going to sit here for an hour but what if we sat here for an hour all the sudden the data is just so scrunched up if I did something like put my finger on it it gets to where I can't really see the details because I'm getting all this old data that I don't want you see what I care about now is I care about what's happening now or I care about what's happening in the last few minutes I don't want all of this old old old data what if this thing ran for three days and then I would have all this old data to work really what I only cared about is I only cared about the new data and then it starts getting to where I can't really see any detail on my new data because I have so so so much old data so what you would really like is you would like to any when you're doing this this charting type of stuff you would really like to be able to just plot like the last 50 points or just plot the last hundred points or just plot the last twenty-five points okay and let me show you how to do that because this is something we got in this business we got to take care of right now because before we really go on you can let it plot all the data if you want but in almost any of the things that I can imagine you wouldn't want to plot all the data so what can we do here let's find a little place here let's make a counter up here so we sort of know how many times we've done this I'm just going to say count is equal to zero so I'm creating a counter and then down here I'm going to say count is equal to count plus plus one and then what if count is greater than I'm going to keep the last 50 points so if count is greater than 50 what do I want to do if count is greater than 50 what I want to do is I want to get rid of the first element in the array because the first element in the pressure array or the the first element in the pressure array or the first element in the temperature a right temp F array or pressure array the first element is the oldest one and I want to get rid of the oldest data point okay and so how do I just take that array and just kick out that first element well what I do is I say temp F I want to pop out the zeroth element because that's the first one pop says kick it out of there and everything scoots down one so if I have an array of 50 it takes the first one out which is the zero element and it scoots everything up down by one so I want to pop out of there that zero element and then I want to I want to that's my other one pressure I want to dot pop out of there the first element okay and so now let's run this and see what happens okay so at first it looks like it did earlier it's just building my data up let's come in and see look at that yeah okay and so I'm at 30 35 40 45 data points 50 data points but then look at this it doesn't get more data points it starts moving my chart to the left and so now I'm losing that old data and by losing that old data what that lets me do is that lets me get a nice clear picture of the new data that is coming in now isn't that slick here I'm just plotting the most recent data and you can plot like I'm plotting the last 50 points you could plat plot the last hundred points you could plot the last 500 points but it's right in here it's like how many do you let build up before you start popping it out every time popping that first value out and I said 50 if you said 500 here it would go to 500 let me just show that let me let me make it the last ten and so this will be kind of crazy because we're not keeping much data here delicious do it just for fun okay so I'm just keeping the last ten data points now and so you see it's really really very responsive because I'm just keeping the last ten data points but it's actually kind of like too fast because you're just keeping so view few data points you're really not getting a very a very good view of things and so what I normally like to do is I normally like to keep about 50 points okay and so that works pretty well so well that is pretty neat what are some things that I did not like very much about that well in my figure I didn't like how it kept rescaling things because it was confusing to me when it kept when it kept rescaling things and so what I am going to do here is I am going to kind of set the accesses so it doesn't just keep doing that and so what I am going to do to set the access I'm going to say ah I'm going to say plot and then I'm going to say Y limit and that'll set sort of the y limit the Y min and the y max it's about 80 degrees in here so I'm going to say the low end is going to be let's say 70 I let's say 75 and then the high is going to be a okay so I want to set the limit on my y-axis to go from 75 to 85 and I wanted what I want I don't want it to be rescaling for me so I'm going to set that okay what else am I going to want to do I am going to want to put a label on here remember how we could put a title so I'm gonna say plot dot title okay and then what am I going to call it my live stream sensor data and why because I don't want to call it temperature at the top because remember in a minute we're going to plot pressure as well so I want to plot pressure and temperature so I'm just going to call this my livestreaming sensor data because that you know that will be good for both for both things you know what I always like to put the grid I like to have the grid and I want to put the grid as true so in this function I'm doing everything just as if I was just going to plot it one time and then I'm going to put PLT dot y label and I'm going to put that as aa let's say that what would be a good label I'm going to call it Tim count F so that I know that it's F so let's take write that let's take a look at this and see how our graph looks now okay there it is look at that my livestreaming sensor data temp F I think I'm going to come out 250 points and then it doesn't change and then if I come in here and one thing is is that you can see that it's about eighty degrees in here and so I'm going to go ahead and make it 80 to 85 because that that would be a little bit more suitable for the range that we have okay so let's go ahead here and I'm going to go on my wild limit from 80 to 85 all right and that should work a little bit better also I want to put a legend on here and remember how we put a legend I'm going to put comma and then what do I say label is equal to and then what did my label what should my label be mmm I think my label should be degrees F and so that's a little label that we'll put on the ledge and then I need to tell it to plot the legend so I'll say plot legend so I want it to show the legend and I'll show you something else too I can tell it where to put the legend I can say location is equal to I think upper left and so you could do kind of the possibilities upper left upper center upper right probably lower left lower center lower right and maybe you could even do like center center or something like that but but these I know let me where I wanted so let's look at this chart now and see what I can get look at that just like I told it upper-left degrees F then I have temperature F I want to get fifty point mild live streaming data and is it live yes ladies and gentlemen it is live okay and look at that I might even need to go like 80 to 90 because my warm finger makes that thing really go up and we put it in there yeah I think I just I'm kind of particular about these things I don't look at ninety and see if that runs a little better okay let's run it okay now if I come in and I'll touch it yeah now now I feel under pressure to get the temperature up there higher okay yeah all right and so then I take it off and it comes down and look at that I just love it how we're there at 50 points and I can see the last 50 points and then I can see that kind of nice exponential decay decay as the temperature comes on down and so this is looking good this is looking wonderful okay so now I like all that stuff about it uh but I think the other thing that I am going to want to do is I think that I want to plot pressure and temperature so how would I create a second y-axis because I can't print pressure in Pascal's which is like ninety seven thousand or some crazy number like that on range from 80 to 90 so I different need a different scale I need a second y-axis the way you can create a second y-axis is created is like PLT two and so I can create this object PLDT two and I can say what is that that's PLT twin X and so what I want to do is I kind of just want to create a twin of the PLT so this is all the PLT so I want to create a twin of it that I'm going to call PL t2 and so if you don't understand it don't worry just do it now I have a new object called PLT to you and I can do all the things that I did before and so let's say I take PLT - and what do I want to do I want to plot and what do I want to plot I want to plot pressure okay and I'm going to give it a let's make it blue and mate let's make it little dots and then let's do something different let's make it triangle shift six we'll make it triangles and then let's make it a line like that and then make it like that and now I should get two data streams coming through the suspense builds oh yes look at that okay so here's my temperature and here's my pressure okay if I touch it I change both of those okay because you can imagine I'm pressing the top or that little hole is and I'm changing the pressure in there I'm getting live streaming data I'm getting live streaming data of temperature and pressure and I just really love that okay so temperature is working great I love that and you know how to set the scale on temperature well the thing is the pressure is kind of keeps rescaling and doing this confuse I like to set the scale I don't like Auto scale so I need to go in and I need to set that scale on that second y-axis because I don't like this Auto scale nonsense so on this one let's come in let's say PLT dot y limit and I probably should have looked at the values I guess I could just try 90,000 to 100,000 and don't put commas in here because it okay 90,000 to 100,000 and then we'll look and then we'll come up with a better range because you want to tighten that up a little bit okay so let's say it's between ninety two thousand and nine and ninety four thousand you know what I really I just need to take a second here and I probably just need to print I need to print aa big P and that way let's just look at it and then maybe better figure out what the range should be okay so it's 93 593 500 and I if I go up it should go down so probably 93 502 I'm putting it down low now 93 525 I'm sorry but we got to get our scales right okay so that would be ah and you know if you're running this for days you would want it much greater range than this because the barometric pressure is going to be changing but over a short time I want to be able to look at those little short-term variations and so I'm going to go from ninety three thousand five hundred to ninety three thousand five twenty five and that's going to set my range there let's go ahead and give this a label while we're at it okay label is equal to pressure and Pascal's that Pascal was a person so we will show due respect and capitalize his name okay so label is pressure Pascal the string and then I close that this should put the labels on there okay so if we look at this now this is not auto-scaling and so that's good and so even though it went off range it didn't auto scale the temperature is behaving beautifully and uh the live streaming data is looking good I got this legend but I didn't get my other legend so let's see why I'm not getting my pressure legend okay to get my pressure legend you see I turned that I said plot legend I plotted the legend for PLT but I didn't do the peel to my second one so PLT - what do I need to do dot legend okay and then loc equals upper-left why would that be stupid that would be stupid because it would put it on top of the other one okay we don't want it on top of the other one so let's say upper right and that should put it in the upper right okay okay look at that I've got my legend here for the blue I've got my legend here for the red I come in here and things update and things change just like I want them to look at that so this is just really really great this is doing exactly what I want it to okay now what is it that I don't like about this well what I don't like is notice how matte plot live decided to rescale this with this goofy of like plus nine point three five e to the four I mean this is crazy don't rescale this okay don't rescale this if I want to put a scale I'll scale the data and I'll do the scale but one of the interesting quirks about matplotlib it's very very hard to get it to not do this goofiness when you start dealing with big numbers it doesn't have a good intuition for big numbers and so we got to force it to not rescale the y-axis so I will kill this and with a little luck I will show you how to do that and one thing is I need to take this print statement out here that we put right because we don't want to print this thing well that was just a debug okay what I think we'll do this will be if we come in and write when we created this PLT 2 dot object what I want to do is say PLT 2 and what I want to say is dot tick label underscore format and then use offset equal false and with a little look what this is going to do is it is going to say on the tick label don't do an offset and I know this is kind of crazy but this is just one of those things that if you know this command usually it will work force that plot line to not bottom scale y-axis okay and this is let's say let's just put some comments in here okay what are we doing we are set Y min and Max Val values okay and what are we doing here we are ah-ha plot and pot the title and then what we are doing here is we are going to turn the grid on but what we want to do here we are going to set Y labels okay and then we will plot the temperature and then plot the legend this I'm sorry but it's just we really got to be good about this okay what this did was create a second y-axis and then what we're doing here is to set limits of second y-axis and then what we are doing here is to AH plot D fresh pressure data and then we are doing a legend here so we will plot the legend okay so now let's run it and see we might have to still tweak it a little bit okay look at this now I have the number without any goofy offset for the pressure and then my temperature is down here so let me see what happens I come up here and look at that that goes up and then I take my finger off and it comes back down well look at this okay so with that that went a little bit high but that's okay because that was artificial with me sticking that thumb on it that's not kind of the natural thing that you would want to do with your with your pressure sensor but let's see if we can actually see pressure so what I'm going to do is I'm going to take this my little handy dandy circuit and I'm going to come over here and let's see if you can watch me and watch the data and so watch that pressure what's that pressure data okay what should the pressure do if I lower the circuit the lower you go the more air there is and so the pressure should be greater if I lower the circuit and so I'm going to put the circuit on the floor and look at that look at this you can see the pressure dropping I put it down lower by about three feet in that sensor picked it up let's see if I go up here high the pressure should go low right if I bring this up about six feet pressure just about goes off the scale and then when I bring it low it comes back and then when I put it here on the table it is in the middle and so that is pretty incredible and you can see that I've really got a very a very exaggerated or a very sensitive scale here but the neat thing is is that I can clearly see a difference of about six feet I can see a difference of three feet with this sensor because let's look at this this is averaging about 93 510 here and then I'm going to go down to the floor with that sensor and when I go down to the floor with that sensor I can see that goes from 93 5 10 up to you know probably about 93 515 and so I can definitely see a difference I hold it up and that pressure drops so we have an incredibly incredibly sensitive pressure sensor here we are so sensitive in fact that we are seeing differences of a few feet okay now what are some things that we could do here if we really wanted to do this probably one of the things that you could do is we could come in and make our measurements a little bit faster and then average them and that would probably get rid of a little bit of this noise but wow that is really neat come in with my finger again on temperature watch that go up this is just absolutely incredible so we have learned a lot today we have learned it I did die I did not put a y-axis label on my second y-axis and so I'm going to go in and do that just because I can and so I need to come in and I need to say PLT dot I peel t2 dot y label and I'm going to call that pressure in Pascal's okay okay label set on axis all right let's run that again this has been a long list I'm sorry guys but there was just a whole lot of stuff that we had to learn on this one and I got one of those bad data reads I'll show you later how you can put traps in there it's like if I get in the middle of a I know I did that that is wrong so what did not plot to wile Abel okay plot to label second y-axis oh okay on this one the way that we have to do this I think is because this is a second one this is a little porky if I remember I have to do a set while label because this is the object that we created the command is a little bit different for PLT to that second one this should work though okay so I can't go in there you can see you got the label I can't go in and explain all to use all the you know I can't make you a master of matte top line but some of the things you just kind of pick up and that is is that when you have a second y-axis this is how you set the label different than the first one okay so this is just pretty incredible we got two streams of data that that are coming from the Arduino we're plotting them both live and we have complete control of our graphs this is so much better than those silly packages that you get like like National Instruments LabVIEW where you fill out charts here when you program it up yourself you can make it work exactly the way you want it this has been a really important lesson this has been incredibly fun lesson it's one I enjoyed it's one I hope you enjoyed you guys think about think about subscribing to my channel okay think about subscribing my channel maybe give me a thumbs up on the video all right we will see you tomorrow and on tomorrow we will be doing lesson we will be doing number what we will be up to island 12 tomorrow okay take it easy we will see you tomorrow for life
Info
Channel: Paul McWhorter
Views: 105,099
Rating: undefined out of 5
Keywords: BMP180, Python, Arduino, Matplotlib, Graphing Live Data, Plotting Live Data
Id: zH0MGNJbenc
Channel Id: undefined
Length: 59min 25sec (3565 seconds)
Published: Thu Jul 24 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.