Chris does a quick overview of the Pycom Expansion Board: a handy piece of kit that fast-tracks prototyping with Pycom hardware.

Transcript

G'day guys Chris from Core Electronics here. If you ever wanted to use an expansion board 2.0 with your PyCom microcontroller, we're going to go through that today. In our getting started guide we're going to show you how to use an SD card, to make a log file and even automatically fetch the correct time and date to put against every item in your log file. As always down below is the link to the documentation, you can take your time working through that. Let's get started and have a look at what's on the bench.

Of course we have an SD card, because that's what it's all about, the SD adapter I just used to check on the file system on the card to make sure it's compatible, the USB cable is hooked up to the PC, we have a wireless access point for internet access, the star of the show is their expansion board 2.0 and we're using a WiPy with it today.

So I'm going to go ahead and stick the microSD into the expansion board, as always the guide line with the microcontroller is to keep the LED, at the top at the same end as the USB and I'll turn that around and plug that in if we get a flashing blue on our LED everything is well, there it is good. Good, we can go to the PC and get started.

Okay so on my screen here on the left you can see that I've got the documentation for today's tutorial, on the right I have the Atom IDE, I'm not going to cover in detail how to set up your device or how to set up the Atom IDE. I've mentioned in the text there how you can do that have a look at the IOT from scratch tutorial. So, we're going to go straight ahead into looking at how to use the SD card. A good place to start always is to go straight to the PyCom documentation and if you have a look down the long list of things you can learn in here, you can get down to 6.2.1.11 SD, which is exactly what we want. When we jump into the SD documentation, there are some notes about which pins are required, of course, they're wired up automatically on the expansion board 2.0. There's a limit on file system must be FAT16 or FAT32 and down the bottom, it does tell you that 32GB is the maximum card size.

What's awesome about this documentation is that we have a quick example usage code provided, we're going to scoop that whole thing up and copy it, and then on the right-hand side of the screen I'm going to create a new file in my Atom IDE. I'm going to paste that code straight down. Now, we don't get any highlighting here until we save the file, so let's call it PB2.PY now that we've saved it as a Python file, it'll show us the pretty highlighting. Alright, so we'll go ahead and run the code just to see what happens, nothing happens. I haven't even started communications with the device yet, so I'm just going to do a hardware reset, disconnect the IDE, connect again. Right should have started with the correct prompt in the first place. Now when I run the code, it runs, and it doesn't print anything interesting, except what usually is printed at the end of every program run. So, we go up into the code and have a look.

We have an object to represent the SD card, we mount the file system on the card, we list the directories on the card, open a file and write into it, a line and close it, open the same file for reading, read it all out and close it, but in all of that we're printing nothing out to the console. So, in our rebel box at the bottom nothing is printed, that's okay because we haven't even had an error, so let's run it again to make sure that it runs every time we, oh it doesn't run every time we want it to run. So here we have file standard in line six in module, it's giving us where the error is and below that is the description OS error, the requested operation is not possible. So, we jump up into the code and have a look at line six and its blank. This happens quite a bit I think in Python programming, that the interpreter gives you back the start of the line after the error. If for any reason you've got two lines and you're confused about which one is causing the error, feel free to double-spaced out your program and then if you are given a blank line as an error, you jump up to the previous line.

Right so, jumping up from line 7 to line 6. Mounting the SD card failed a second time, if we try to run it a third time, it fails again. So, it seems this code runs only the first time, I'm going to press the hardware reset button, on the WiPy, just to get everything back to start and run it again. It works, run it a second time, it fails.

Okay so we seem to have a problem here, where once the code has run it says left the microcontroller in a state where it can't remount the SD card. Now I know a little bit of Linux, and I have understood that whenever you mount a file system you need to unmount it. So thankfully I've got that experience, I'm going to use that to help us solve the issue today, but if at this point you're stuck, you would of course go and Google it. Look if anybody else has published articles about PyCom and SD cards, you would of course go to the PyCom forums, at forums.Pycom.io and of course read the forum's first and make sure somebody hasn't already asked the same question, because the answer might be there, don't just Barrel in and ask the question first. Of course, ask the question if you can't find the answer immediately.

So, if I go to the PyCom documentation at the top left of my screen and type in unmount because I suspect that is the problem, I'm lucky here that micro OS has got a reference to the unmount command. I see references to flash and SD this looks good, skimming over on the way down there's a little code sample that has an OS mount and a UOS mount. So, it seems what we should be doing is unmounting after we've mounted. So, I'll go to the bottom of my program and add UOS.unmount and because I don't have a variable called path, I'm going to explicitly say the SD card.

Now we're implementing a fix that is going to run now in the context of what has already happened, so we know that the first time this code runs it leaves the SD card in a way that we can't remount it. So that we can test our fix we have to hardware reset, so I'll press the reset button on the device again, so now we're back to square one, we expect that it should work the first time but not the second. So, we run it, it failed, so here we have down in the ripple prompt again, named UOS is not defined.

Now I'm going to guess that UOS is a reference to some micro Python library, from which the Python devices are derived. So probably I would need to either, import micro OS into my code here or else hope that the OS implements the same method. So back in the code I've taken the U off the front of OS, so now I have the line OS unmount/SD.

Again, we've changed the code we need to know where we're starting from we hit the reset button, to make sure we're back to the start, we expect it should run the first time and if it's still failing, it will fail on the second time through. There we go, I've run it and it's run the first time, if we've really fixed it, it can run a second time and yes it seems we fixed it, we run a third time. Great.

Now if we really want to be sure what's failed here, I'm going to ctrl slash and comment out that line, we'll run it, it works the first time run it, it fails the second time. So that is the line that has solved our exact problem, uncomment that to bring that back in.

So now I'm going to take a giant leap forward, I'm going to give you a code file that includes all of the other code, that I mentioned at the start affecting to WiPy, connecting to a network time protocol server, setting the time on real-time clock, creating a log file, adding entries to the log file, so they all have a timestamp on them. So, unfortunately, I can't go through all of that in detail, but there is commenting on the code, so you can read through that and see how it works back to the PC now.

We'll open the code file copy all of that and paste that over what we've just done, jump to the top of the file I want to make sure my Wi-Fi password is correct for this instance and I'll run it. Now see that failed do you see what I did wrong, I didn't reset the device, so I'm going to reset the device, now back to the start, my code should work the first time and there we go it's running. So what's printed out to the screen is that, the program is started the SD card was mounted, the real-time clock isn't set, when it first started up but it connected to Wi-Fi okay, then you can see a couple of items that are added to the log file and then fetching the current time from, au.pool.ntp.org and it fetched the time.

Now it's to go into log with the next 10 button events, so if I come back to the device here the button, we're talking about is the one here on the expansion board when I press that down the PC shows a button event at pin 10, when I let it go, I get another one. What normally happens is I get a list of them, the button bounces and each of those bounces is logged and you can see in the print out there, that each has its separate time stamp. So, the microcontroller has had to react to the button being lifted and then if it bounced back down and it reacted again and bounced back up and it reacted again. So, we need some code to get rid of the button bouncing, so we call that D bouncing and I'm not going to go into that.

So, I hope you can have a look at the code there and see how that works, you can with this just with a WiPy and with an expansion board 2.0, you can build a log file, which will keep track of everything your controller is doing.

Thank you so much for watching this, again there's documentation down below. Please take your time to work through that.

Thanks for watching Coreelectronics.com.au

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.