Sunday, May 17, 2015

Arduino Blocks: Serial communications and an SD card reader

This is just a 2 minute quick video to get back in the game, it's been a while since I posted one of these.

I have a project in mind that will need a lot of storage. SD cards are a great way to do that. Here's a video showing that all you need to use an SD card is a card slot that costs about a buck and some wires.

The SD card design was intended to reduce costs by designing the card to speak the "language" that microcontrollers were already speaking anyway; serial communications, in this case SPI.  There is also a high speed mode, but it is much more complex and you need a license to use it, so we won't speak about it any more

Serial communications just means that even though we have a bunch of bits that we want to write or read to a device, we send those bits over one at a time. Given the speed that electronics goes at these days, that's not as slow as you'd think, and it allows you to send basically unlimited amounts of data over just a few wires.

Serial communications is VITALLY important to this sort of design. Pretty much all non-trivial devices speak serial. In fact we've already used serial communications, in the blocks with the 8 LED/8 switch panel - the way it controlled all those LEDs and switches with just a few wires was via a serial interface.

There are many serial communications types (or "protocols").  The two most important for microcontrollers are SPI and I2C. Both are heavily used in the industry and if you do much messing around, you'll eventually have to use both. However, it will be very unusual if you have to actually write the code that directly speaks to the device, though it doesn't hurt to know what's going on when you call the standard library functions.

At the bottom of this article I've embedded a video explaining some of the protocols a bit. Ben Heck does a great job explaining the basics. You should DEFINITELY watch this video; you're going to need to know this stuff.

SPI requires 4 lines. Chip select, which tells the chip "I'm talking to YOU", a clock line (every time it pulses, the controller is telling the device "the next bit is ready for you" or "Give me the next bit" then two lines, called MOSI (Master Out Slave In) which sends data TO the device, and MISO (Master In Slave Out) which pulls data from the device to the controller

The Arduino environment already comes with a library that can speak to SD cards and read standard FAT and FAT32 filesystems (the standard for SD cards). I only ran the first sketch here to make sure things work. There are several others there, these should give you most everything you need to get going.

No comments: