Simple mass storage for your microcontroller project using pic

Simple mass storage

Do you want gigabytes of storage for your microcontroller? Would you like a simple way to tranfer files from your PC to your picaxe or arduino or other micro project?

This little projects demonstrates the uDrive that can be set up as an extra drive on a PC. Copy files to and from the SD card, then put it in the uDrive and talk to it with 5 simple commands:

Read – Read a file off the drive
Write – Write a file to the drive
Dir – get the directory listing of files on the drive
Erase – erase a file
Initialise – send 1 byte at your preferred baud rateSimple mass storage

Step 1: Connecting to your microcontroller

Connection uses 5 wires: Gnd, Power, Tx, Rx and an optional reset line. The reset line can be left unconnected if no reset is needed.

The uDrive works from 3 to 5V so can interface to a wide range of controllers.

Step 2: Copying files across

Copying involves taking the micro SD card out of the uDrive and putting it into a micro-to-standard SD adaptor. This goes into a USB adaptor that plugs into the PC and then the SD card appears as another drive on the PC. The micro SD is the small purple part seen on the left of this picture. The USB to SD adaptor cost $5 including shipping on ebay. Most micro SD cards come with micro to standard adaptors and the total cost of the package is under $10 for gigabytes of storage.

Step 3: Accessing the files from a microcontroller

Microcontrollers all have different languages; assembly, C, Basic, Spin etc. You will have to write your own exact code, but below is an example using vb.net. This is still a bit complicated, but it is a lot easier than trying to talk to a SD card using bit-bang SPI code.

For the purposes of testing, we have built a little uDrive to RS232 converter. We are actually talking back to a PC via the PC serial port (or via a USB to serial adaptor), which seems a bit pointless, but it at least enables the device to be tested quickly and code debugged.

The Max232 converts the 0V/5V levels from the uDrive into valid RS232 voltage levels.Simple mass storage schematic

Step 4: Testing using 4dsystems’ software

The first test is to run some testing software from 4dsystems http://www.4dsystems.com.au/prod.php?id=22

This has pre-written test routines that not only check the device is working, but also indicate the bytes going back and forth. This makes it much easier to work out how to write your own code.

You can also talk to the card in raw mode, but using DOS mode that a PC can understand is a lot simpler. A file can be tiny – just 1 byte if you wish.

Step 5: Writing the Code

Writing code is a bit complicated, but let’s start simple. Send the uDrive just 1 byte, a character U, at your nominated baud rate, and you will either get three things back. A 06, which means it worked, a 15 which means it got the byte but didn’t understand it, or nothing, which means the drive is not connected or not wired up right.

Here is some vb.net code to do this:

SerialPort.Open() ‘ open the port
SerialPort.DiscardInBuffer() ‘ clear the input buffer
OutPacket(0) = Strings.Asc(“U”) ‘ send a U and this sets the baud rate
SerialPort.Write(OutPacket, 0, 1) ‘ send data in outpacket start 0, 1 byte
Sleep(100) ‘ wait for response
SerialPort.Read(InPacket, 0, 1) ‘ read 1 byte back, should be 06
SerialPort.Close()

From now on, all comms will be at this baud rate. Rates from 300 to 38400 work talking to a PC with a 3 metre cable. With a shorter cable it would easily go faster.

The code below is the complete source code in vb.net (vb.net is available for free). The main complications are the way the uDrive splits files up into pieces – you need to watch the arrays and the integer division and remainder division and write these routines in your preferred language for your micro.

Cost of the uDrive is $30US.

The uDrive can also work in Raw mode, where you read and write to individual sectors. This is useful where you want to interface to an existing operating system, such as CP/M. More brainstorming is happening at the Vintage Computer forum, the Picaxe forum and the N8VEM forum.

Stay tuned, as the next little project will be to get a $3 picaxe 08M to write a tiny file that can be read by a PC.

Have fun!

Read more: Simple mass storage for your microcontroller project

Leave a Comment

Your email address will not be published. Required fields are marked *