Micro:bit & Thonny: Collecting, Storing, Analysing & Graphing Data

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay so first thing we're going to do is go to Microsoft make code.org head to microbit and in here you can make a new one called uh whatever you want to call it but I'm going to call it data collect micro from my microbit and I'm going to create it so uh what I want to do is I want to collect data so first thing I need to do is I need to go down to serial and select serial redirect to USB that'll be allow us to send signal down a micro cable next thing I need to do is send some data so I've got choices I could do light level or Compass or temperature I'll go for light level because I can change that pretty easily so back in serial I need to select serial right line or right value I'll go for right line right now just to it show us so I'm going to write the line there and the next thing you need to do is connect up your Microbus so I'm going to grab my Microbus and I'm going to plug it in so as it's connecting here uh might have just connected straight away I think because I've plugged it in before let's just see so it's giving zeros when I go to show data and when I shine a light hey there it goes now I downloaded this Pro program previously so just make sure again that you download it right um but in there's two show data simulator now that's going to just show 128 because that's over here and you can adjust that number here this is just a simulator so this is not actually happening on the micr whereas this one show data device you can see it's giving 0000 and I've got a torch in my hand and as I shine it over it's jumping up there to almost 255 and then going down so I'm just shining a light further and closer away there to really adjust and play with it okay so uh that's one thing we can write now the other thing we could also add to that would maybe be let's take a bit more detail and let's also add in a little pause so we're not just doing like a billion readings cuz we don't want to fill up a file so we might do a reading every second I'll also uh go crazy I could have done this serial right value and put the value as light and then get rid of the serial right line and now when I download it let's just have a little sample if I go to show device it's now going to say the word light and then what the value is okay so adding to this now we can also add in to say do uh temperature so I'll just duplicate this get rid of that and underneath I'm going to do for temperature so I'll say temp oops temp and I'll do just the builtin obviously you can connect external devices it works exact same except you'll pick your serial pin the appropriate one I'll do that in a separate video so here's the one is going to send the light ah no Steve did not download always remember to download so now it's sending both light and temperature okay every second it sends one and then another second and it sends the next okay so that's us now we've set it up that it is collecting uh data so the next thing we need to do is create our th code to analyze that data okay so uh here we have uh let's take a look we're going to start creating our th file to analyze our data so the first thing I'm going to do is import serial that's needed to connect uh so we can actually just take data from the serial port course I'm going to add these next few lines I'm copying and pasting from another bit just so I can talk through them as I go so we're going to update the serial from the Microbus so uh this shortens serial uh the function basically just to be sir and we need to have the correct signal coming in so this seems to be the correct one for most uh microbit computer things you can Google it if that's causing your a problem but so far it's caused me zero problems and then we need to connect to the right Port so these first couple of lines are all necessary uh so in order to connect to the right serial ports you need to go into your search and type device manager and in there um I have it here just as Comm 3 but I'm going to go to Ports then you make sure your microbits plugged in or it won't appear right and you'll see a US serial device and it's actually Comm 4 so what I need to do is change this three to A4 okay it could be it'll be different on your one and if you move it around the different USB ports you're going to get something different now next part I'll just drop this all together is we're going to do the following okay so first we're going to print uh just say typed control C safely exits that's how we exit taking the data and we're going to open the serial course okay so this means I'm opening it now and I'm ready to collect data so while this is happening I've created a TR accept here right so basically first off the try accept is is allowing for if there's any blank inputs right but if there is it'll just pass it so it won't break our code right so while it's here it's going to read the line so serial read line and it's going to read it as a string and it's going to save it to a variable called measurement and it's just going to print that measurement for me um and then the keyboard interrupt that's control and C this business earlier uh we'll put print a little message saying a we're closing it and then it'll safely close it okay that'll stop it from screwing up your data in later on in in your file when we go to save it so let me just try run this current script so it's type contr c to safely exit that looks good and then nothing seems to be happening so I'm just going to go in and check check oh I know what might be going wrong no I don't uh so what probably it is more more than likely is com Port 4 so I'll just stop the code there we go and I'll go back to my device manager and just ch double check oh was I in the wrong one port maybe let me go to you no okay we'll go back to your ports uh I'm going to try Port one sure why not When in Rome I think that's actually my headset though so I really don't think that's going to do anything so I press control and C to exit Oh no just getting a hassle there in general so I'll go back here I'm going to disconnect my USB and try plug it into a different one so that's plugging into a different one now it's refreshed I'll actually just close it and open again to make sure it's properly refreshed device manager uh ports and Port 4 so let's do that hey there we go so I don't know what was wrong with the other Port that happens oh look all this data is looking real weird what's going on it should say light but saying so actually I've noticed this problem and I googled it if you have make code.org open in the background it seems to screw up so I'm just going to close this and now after hey look all my data looks real nice and neat um except for obviously all this junk so the next thing I'm going to do is going to clean up that junk and get rid of the stuff we don't need but you will notice that light and temperature are coming in as full words unlike earlier where they're coming in actually to stop that where they're coming in all screwed up there we go um so up here they were screwed up Lig without the t uh Temp and all that jazz now the next thing we need to do is clean up this data so that'll be our next step okay so next we want to just do a little clean of the data so I'm going to add in a line here um now there's a few various ways but we're using the replace function basically and what I'm going to do first is uh I'm just going to tell it to cut and only pick up certain parts of the string so if you take a look down here at the string for example I don't need the first seems to be a b and then that so I'm going to cut zero and one index zero and index one so I'm going to start at two which should be the first letter and I'm going to take it to uh well it seems like there's a big long Gap that I can take but I'm just going to say up to six could even go a bit further actually um let's see two three four five actually I probably need a bit more so I'll say up to 10 right but you can play around with this and I'm going to replace any uh any space here any space with no space so let's just try run this now and see what happens so a it seems to recording the light and temperature now I need to test the light so I'm going to put my torch back on to make sure it's actually recording correctly and you can see ah now I've got my torch on fully and it's only recording light up to like 22 which I'm guessing means that I probably need to control and see this and then make this bigger so I might just go up to probably 11 is probably enough but I'll go up to 14 let's see what happens so now when I run it temperature 22 light zero now I'm going to shine my light on and now I seem to be getting the full measurement of light uh here 239 nice big numbers where it should be when I had the light on and it's back to zero cuz I'm just every time I'm taking the light off okay so that's how we can clean our data nice and simply in one line just say measurement is now just the index as we're doing a string SP uh string slicing and then we're replacing any empty space with nothing so that is uh your step for cleaning the [Music] data okay now that we've cleaned our data we're going to have to start collecting it so we're wanting to collect it but we also to realize we have two pieces of data we've got light and temperature so the first thing I'm going to do is actually make a little statement now I'm going to uh be a bit cheeky here and say okay if the string temp oops is in measurement then I want to do what I had previously done I will get oops uh measurement equals measurement so I clean it up that's fine and I'm going to add in another bit to that I'm going to say temp is equal to measurement and I'm going to do another replace but this time I'm going to replace uh temp with nothing so I'm now just sending no data or sorry no data hopefully sending just the number but without the temm parts uh I don't need I don't need to print it so I'm just going to get rid of that uh so if if it has temperature and measurement do that don't forget our colon and then else if it has light in it I'm going to do the opposite or you can just leave it as an else uh I'll do it as an L if sure why not when it R so if light and measurement and this time I'm going to make a new variable called light and I'm going to say replace light oops 1 2 3 four light with that now uh I've ran this before and checked it and it sometimes gives an error because it might do the first measurement and the first measurement might be light so it skips and then it's going to break so what I'm going to do up here very cheeky is just put in that temp equals 22 and light equals z now the thing about this is you're going to be collecting so much data that one or two data points wrong doesn't really matter so I'm just putting it as if I start it in darkness and then the temperature is room temperature so now we've got it identifying between the two differences and now we need to save it to a CSV file so I'm going to put a just drop a whole bunch of code here here we go uh make sure it's in line with uh your in the wild true statement so what we're going to do is we're going to read the measurement the string we're going to identify it's a temperature or light then we're going to open our CSV file so micro collect I called it CSV you call it whatever you want um and we're going to append the existing file on a new line and what we're going to do is we're going to append this is just basically writing the data uh this is just shortening CSV writer F to be just CSV zsv here uh right row so we're going to right row temperature and light so my first colume is going to be temperature and my second colume is going to be light and then it's going to close each time it'll reopen each time and close each time there's a measurement taken now what I'll say is technically it's going to do it's going to keep the value from the previous one let's say temperature was 22 and it's going to add the new light and then the next time it'll keep the same light and add the new temperature now if you want to get more accuracy with this then you could just shorten the weight statement that we had initially in the make code.org oh I closed that but you get the idea so uh right let's just run this and see what happens now when I run it nothing seems to happen so what I'm going to do is I'm going to press control and C to safely exit there it goes it's closed now I'm going to minimize this and where I've saved uh just check that if you can't find the file just check where you saved it so save as I saved mine to my desktop okay so on your desktop you should find the file micro collect and here it is sure enough there's the temperature colume and there's the light I'm just going to test it now I'm actually going to delete all these because I've done it before and I'm going to try run it again yes I'll save it or actually Jo I'll even go one step further I'll cancel and I'll just delete the whole file don't save and delete the whole file so now in my th when I run it uh I'm running the program now I'm going to get my torch and I'm shining it over my uh microbit right now uh and it'll take a little bit of time D dink because I want to get a few different measurements light are close and far away okay and I close my serial connection control and c and now on my desktop sure enough the file is back open it up and there we can see it's collecting temperature and it's collecting uh the range of light so if you want to get more data in that time all you need to do is go back to your original make code and shorten I put a 1 second weight between each of them so just shorten that make it less and you'll get more data collected in there so that's how we separate it into the two data types and save both into our CSV file okay last thing we're going to want to do is analyze the data and collect some statistics from it so uh firstly I'm going to go in and I'm going to import both uh pandas and statistics now actually I didn't emphasize this last time I had imported CSV sorry I done it in the background there um so obviously if you got an error about CSV missing you need to import if you're missing any of these modules just go to tools manage packages and type in the one you want to find so for example if it's pandis I go find package and I install the latest one now I've already installed it and statistics as well so down here I'm going to do the following I'm going to first create a nice little uh pandas table of data so this is going to tabulate it now I'm just going to drop everything in here and talk through it so um this all is after we've closed our serial connection so this is basically when we're done we're then going to run this part of the program because we're basically going to have all our data and now we're analyzing it so our column names right our column names are not measurements they are uh I think I said temp and light now you can I'm going to call it capital temp comma Capital light um I'm going to read the the data file this data file I used was called micro collect so I make sure to change this to micro collect um and I'm going to say uh names is column names which is the bit that I put here so basically it's identifying that the first the left hand column is temp temperature and the next colume is light okay um turn the given column into a list so I'm going to turn this uh measurement dot to list so that is not what I want here I want to say let's just do temperature first so I'm going to turn uh temperature CSV so I'm making a new uh thing called temperature CSV and I'm going to turn the temperature stuff all these numbers which is the number 23 and 22 into a list and then I could find the mean median mode of these so all I do is statistics mean and then I say here temperature. CSV or temperature CSV so I'm just going to copy paste that it's very exciting watching someone copy paste there we go and what should happen here is if I run my program okay so uh oh sorry it gave a good error to get right gave an error per per Mission denied right go up to here I have the the Excel file open just close it and now when you run it now just check it might have screwed up your file no it didn't Grant so just go here and run it again basically it can't access it while it's uh PL while you have the file open so I'm just going to shine some light on this again for a few seconds and let it collect a bit more data and I'm going to close control and C to safely close um data frame object with no attribute temp ah so here I have gone for lowercase temp but I call the column names uppercase so good mistake so let me just fix these up here now this is lowercase temp so that's fine to leave here let's run this again I should have dat it already so I'm going to press control and see and hey mean measurement 22 uh median 23 and mode 23 which kind of makes sense for the temperature so I can rinse or repeat this for light so I can just copy paste this and do it for light making sure to put Capital light here cuz I'm reading the column and then copy paste these things and change all of these to light CSV there we go now would I run it safely exit and there we go I've got Mode medium measurement for my light and I've got it for my temperature as well obviously I'm going to put in some words here to make this nicer and neater but I'll do that uh off screen okay now that we have uh all our uh statistics of our data and stuff we might want to do some graphing so the first thing we're going to do is go up to the top here and we're going to uh import npy and matte plot lib so basically what npy and Ma plot lab are doing is uh numpy is a it's pack it's for scientific Computing and this is for plotting graphs so you kind of generally need these guys hand in hand so what I'm going to do is I'm going to go for a uh let's just go for a bar chart now the thing you can do I'll just get it open up here there we go is you can go to W3 schools and you can look up matplot lib and get a whole tutorial tutorial excuse me of uh different stuff so there's obviously Scatter Plots there's bar charts there's histograms there's pie charts it's whatever you want right I'm going to go for bar charts uh and you can obviously steal this and just run it directly I'll just use one that I kind of played with earlier in a previous code so here first this this is just creating the figure size right that's fine you can play around with that yourself I'm assuming that's the X and that's the Y uh here plot the parts so I'm going to plot uh from our given column into a list this stuff is what we're plotting so I'm going to plot my temp CSV in my X and I'll plot my light CSV in my y so it goes X Y the color you want I'm gonna go for hot pink uh you can also look on I probably just type in colors and you'll probably find what you want and the width of each bar chart you can play around with this I'm going to go for 0 2 um adding the X labels well my X I said was temp so I'll go temp my y here is light so I'll change this to light and uh my title I'll go is light versus temp now for my data that we've collected I'm going to guess that this will make an absolutely garbage bar chart uh but let's just go uh plot. show is to present the graph so run your program it's collecting data uh we already have data collected so I'm just going to press control and see and sure enough there we go it's collected uh two bars now what's what's going on here uh well look zero was an answer that happened a lot for uh light if we remember so a lot of our answers are just down here at zero which is why we can't see them we only had 22 and 23 for our temp it's not going to be great but fundamentally that's it for doing a bar CH now if you want to play around you could uh whip this open and grab yourself a scatter plot okay which might be a bit nicer for doing and you probably get something that looks more like this the the idea is the same idea I'm just going to steal here this line of plot scatter so if I go deadling dink plot scatter uh XY instead of plot bar so I'll just directly drop that up here let me get rid of that and let me just put a hashtag on that and just see how we're getting on here yeah I'd say that should be fine except I probably need to tell it temp CSV for X oopsie and light CSV for y because it needs to know where it's where it's getting this data from um and yeah let's just see what happens there uh run end it and hey there there you go we're 2 minutes of playing with it not really sure I was doing managed to figure it out and you can see here it's plotted uh temperature versus light now this obviously again not good data because they are unrelated here I just had the temperature of the room which wasn't changing and I had a light where I was putting it on and off so this might be something done better with say uh If You Were Somehow having it related to maybe the temperature of a lamp that it was near and it was also changing the temperature as well as the light uh and you might need to use probably not the built-in temperature sensor you probably need to use a more sensitive one for that anyway that's the basics of how to get some graphing the key thing I'll bring your attention to is uh W3 schools tutorials on Matt plot lib if you just Google it you'll find it you can see there's an nu uh npy numpy uh thing numerical python as well that you can knock yourself out having a great time but the key thing here is really just to get it up and working for yourself so play try a few different ways that you might decide to plot it uh whatever is most relevant to your project
Info
Channel: The Grimes Teacher
Views: 473
Rating: undefined out of 5
Keywords:
Id: Rc8K730vmbo
Channel Id: undefined
Length: 22min 35sec (1355 seconds)
Published: Wed Feb 14 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.