Saving Data Inside the PICO

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
buenos dias welcome to making stuff with chris day-hut today we're going to take a look at how to write data to the raspberry pi pico's internal file system now we're not talking about this type of file nor this type of file these are sort of museum pieces nowadays luckily for us the pico has an internal file system and we can write data to it and extract that data very easily and that's what we're going to cover today we're going to cover what the internal file storage is what you may want to write to that file limitations and locations of the file wiring in breadboard example for this really easy and simple circuit three micro program examples and finally how to format data so that you can import it into a spreadsheet now you may be wondering how it's a microcontroller why would i want to store data on it well think about some of the projects that you may be considering in the future perhaps a temperature logger you've got a temperature and humidity sensor and you want to record that over the course of a day a week a month etc you have a a room in your home that you want to determine when access is made to that room or it could be a closet or a safe a variety of things of that nature perhaps you want to measure the frequency of something changing could be temperature it could be an on off switch etc uh you may want to record how fast and how often your hamster or gerbil runs in its little wheel all that can be recorded and stored right on the pico without using any external devices your files are stored in the same location as your python program so when you create a data file and you write it it's going into that same area where you store your python programs very very simple you're limited to a total of two megabytes of space there and that's shared across all your data files that are stored on it so you do have to be careful of that more importantly the file size maximum is 128 000 kilobytes so you do have to be prudent about what you're putting in a data file and how much data you're trying to put into it otherwise you could corrupt your file for trying to write too much data in there wiring it could not be more simple we'll take a quick look at a fritzing diagram in a breadboard example as you can see there's nothing over here at the breadboard there's nothing just our usb cable that we normally program it with so this one's a super easy one for wiring up and everybody should be able to handle that one with it with ease for the first example we want to learn about the open write read and close statements or methods used within this program now the first thing we need to do is to create an object that will be for our class called open and i'm just using the word data file as a variable name to hold that and i'm giving the file name dot text seemed appropriate for what we're doing here of course you can call it whatever you want i would suggest keeping file names simple so that you maintain a greater chance of reliability uh the most important one is this uh quote w here quote unquote w and it's a lowercase w that denotes that we're going to be writing data instead of reading data so that whole statement there creates and opens a file if the file already exists we're going to overwrite it if it doesn't exist it will create it and prepare it for writing the next statement data file dot write puts the data into that file and we would just simply have parentheses and then inside there quotes uh whatever you want to put in your text file and then close quote that'll or a variable containing a string data just like that finally when you're done writing that statement you would close that file with the data file dot close method very very simple we're going to print a statement just so that we see the dividing line between writing data and then reading data down below to read this data we're going to use the same open statement but this time notice that it's quote lowercase r quote denoting we're going to read from the file and then we're going to have a variable that will read data in with the data file dot read method so when that command executes it will take the data from the file and put it into that variable here obviously we're printing it and then finally we're going to close that data file it's just very good housekeeping you open something later on in your program after you're done reading or writing to it you should close it and then finally a note that we're all done running the program so let's run it and see what happens it's pretty uneventful and that's it it wrote data to the file and then we read it and i printed it here and we're all done now you may may be wondering well where is the file uh can i look at it directly absolutely so we'll select open in thoni on the raspberry pi pico and this is the file that we just created mydata.txt so we'll open that and there's the data statement that we wrote into it now as you can see this is a pretty useless example because it's only writing one line of data into the file so in our next example we'll expand on that a little bit so we'll go ahead and close this in our second example we're doing many of the same things except for we're going to write multiple lines of data so we'll open the file we'll create a variable that contains the number of lines and we're going to count out where we can write five times in this while loop or five lines of data so we'll say a line is the string of the num lines variable converting an integer to a string of the text statement will equal the line plus text line of data and then you'll notice i added a a slash n here and in python and most operating systems the slash end is new lines so that way our it isn't one big long line of data it will be five separate lines of data and that's a very important part of what you have to do in this process and then we're going to write the file or write data to the file using datafile.writemethod and then the variable text and that prepares the data for writing it actually didn't put it into the file the next statement data file dot flush will physically write it into the system normally like in our last example that writing the data this prepared it but the close statement wrote it well we don't want to close it because then when we go to open it right we'll write from the beginning of the file again so we use flush to perform that action now we're going to repeat that five times as we write five separate lines of data to the file and then we'll print a notice that we've wrote the data and then we'll read it back in to a variable called text data and our open statement's the same as it was before we're going to do data file read as before we'll print it close it and then put a note to us letting us know the whole operation and program is completed we'll run it and as you can see we wrote data to the file and then one two three four five text line of data and that was here let's open that up as we did before and it should be the same file just containing our new data one two three four five text line of data just as we had shown right here hopefully that's pretty good for you so far very simple very straightforward and very similar to most other programming languages now finally for the next step our third example program is going to be something you're most probably going to want to do and we're going to write five lines of data this time even more complex data but we're going to prepare it for further analysis and a great program to do that with is a spreadsheet so let's go through the program and then we'll walk you through the whole process of getting this data into a spreadsheet we're opening the file just as we have been of course we're writing this time we're going to have a variable for the number of lines our loop our loop counter uh our statement of creating the text string that we're writing for each line and this time you'll notice it's line plus and i added a comma here text line of data comma and then we would put other numeric values uh using the plus statement all across here to add in other important pieces of information such as uh temperature humidity date whatever you wanted to record across here then we write that and flush it just as we did in the previous example and then we close the file to complete the operation now we go through on the next step here and we're just going to read it just as we did in the last example but it's also in a format that we can do more with so let's go ahead and run this i'm going to erase the data down here by click right clicking and select clear we will run this and here's all of our new data that's in that file and we can verify it by opening it here as you can see our commas are still in the file and that makes it very easy for us to import and manipulate this data in a spreadsheet but right now it's actually residing inside the pico we need to get it out of there so what we can do is simply click on file copy on this computer and i'm going to navigate to our internal file system folder that i've got set up on my windows program or on my windows computer and i'm going to save it into that directory and there's one there ready i was already doing this earlier to test it we'll now go to libra office calc that's the spreadsheet program i will select open there is our file uh that we want to open my data and it's going to take a minute and analyze it and it's going to say hey wait a minute this probably is data that we need to parse out for the person and it automatically comes up with the right suggestions as you can see down here so i will click ok and there is the data and each piece of data is in its own cell and then from there you can graph out your data further manipulate it mathematically or whatever you want to do with that data how handy is that how simple is that uh for a file system that's internal to the raspberry pi pico now i would like to mention that there is another way uh to store data and that's on a micro sd card and i've got another video on that as well that will be coming out in the near future so look for that one if you're into uh saving data from your raspberry pi pico hopefully you found that information useful and beneficial in your raspberry pi pico endeavors and journeys i would like to mention that the information presented in this the fritzing diagram as useless as it is and the micro python programs are available for download on our companion website there's a link down below in the descriptions there's also a table of contents on that companion website showing all the videos that we have available for you thanks again for watching i look forward to seeing in the next video
Info
Channel: Making Stuff with Chris DeHut
Views: 7,652
Rating: undefined out of 5
Keywords: PICO File System, Raspberry Pi Pico, RP Pico, Microcontroller, Electronics, Electronic components, Python, Micro Python, Interface, Real world computing, Maker, Making, Chris DeHut
Id: 54jVnMXGGLw
Channel Id: undefined
Length: 13min 11sec (791 seconds)
Published: Fri Jul 22 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.