Martin's corner on the web

Toying with ATTiny84 and SD card in Arduino IDE

As I mentioned in my previous post, I tossed in a SD card port for the TinySensor v1.1. I wanted to give it a live test and see if/how it works prior to submitting the PCB to the fab house. So I  did a breadboard setup as per TinySensor v1.1’s schematic (MCU+SD part only). Next, I decided that I want to do this in the Arduino IDE because it is much more accessible to the general public than AVR-GCC stuff. I hope that one day to sell the TinySensors in the store, so this is a step in preparing for that moment. So, for Attiny processors the logical step was to use the Petit FAT FS library that I also used in the SD card bootloader project:

Petit FatFs is a sub-set of FatFs module for tiny 8-bit microcontrollers. It is written in compliance with ANSI C and completely separated from the disk I/O layer. It can be incorporated into the tiny microcontrollers with a small memory even if the RAM size is less than sector size.

Features

  • Very small RAM consumption (44 bytes work area + certain stack).
  • Very small code size (2K-4K bytes).
  • Supports FAT32.
  • Single volume and Single file.
  • File write function with some restrictions.

As I mentioned earlier, I wanted to use the Arduino IDE and so Google revealed an Arduino wrapper for the library that was exactly what I needed.

I modded the spi_init, rx and tx functions accordingly as per the ATTiny84 datasheet and loaded some test code that would read a file “test.txt” from the SD card and output it contents to a serial output via an FTDI cable (Attiny84 supports virtual serial).

It didn’t work. I tried over several times, check and double-checked my wiring but to no joy. The I researched on the subject and came across this forum discussion that explained the cause of my troubles: The SD card pins labeled MISO and MOSI don’t go to ATTiny84’s pins labeled MISO and MOSI.. they need to be swapped. Basically this outlines it:

The MOSI and MISO signals are for ISP mode only. Sharing the same pins are DO and DI which can be used by the USI. When working with the USI, pretend MOSI and MISO do not exist. DO is always the output, DI is always the input.

So I swapped the wires on the breadboard and it was working 🙂

That of course called for changing the TinySensor plans accordingly, the PCB designs are now updated to v1.2.

Here are some pictures

The Petit FAT FS library port for Arduino with the examples I used are available on github

 

Tagged on: , ,

14 thoughts on “Toying with ATTiny84 and SD card in Arduino IDE

  1. Bruce MacIntyre

    I downloaded your files and compiled them on my MEGA 2560. At first a got an error with Wprogram.h. I read somewhere to replace that with Arduino.h. That eliminated
    my compile errors for the MEGA 2560. But when I compiled it for ATtiny84, I got “Serial” command errors. When it did compile for the MEGA the sketch was still way over the 8K available on the ATtiny84. I am just trying to read one FAT16 file from a SD card and send the data out a serial pin on the Attiny85 to the serial input pin on the MEGA. (headless CNC project) I am using the MEGA as an ISP for the 84. Thanks for listening.

      1. Bruce MacIntyre

        I did add the Arduino wrapper code at the end of your sketch to define rx, tx and spi_init. It compiles for the mega at 11,492 bytes. I can understand how it would compile to a different size to as a ATtiny84 since the library file code is different (attiny core ?) Is there something I need to include to enable the serial print commands to compile without error when I select the ATtine84 (8HZ) as the board?

  2. Bruce MacIntyre

    I did a fresh install of 1.0.3, installed the tiny files in my sketch book hardware folder and I still get the same compile errors. I deleted and reinstalled 1.0.3 and installed the MIT version as mentioned by another reader, (that’s how I had it originally installed) and the results are the same. The two Attiny85 programs I wrote still compile and run just fine. Aside from the Petit Fat code that is giving me compile errors, I don’t understand how your Serial commands compile. I thought the only way to use
    serial in the Arduino IDE/tiny environment was to use SoftwareSerial? Anyway, thanks for listening and responding. Although it’s overkill, I have a program running on my MEGA 2560 using the standard SD library. I just thought it would be neat to use a ’84 or ’85 instead. Thanks again.
    Bruce

  3. Bruce MacIntyre

    I started to download your Arduino 1.0.1 (that was my original config too), but it REALLY complained about doing damage, so I aborted the download. Since my tiny files
    are in my sketches directory your IDE shouldnt have any effect. Just so we are talking apples to apples, I tried compling your example program #1. Still dont get how any of your serial.print commands would work when the IDE compiled for Attiny85, it would, perhaps, for other computers, but the Attiny85 doesn’t have hardware serial. I use Softwareserial and that works ok on the ’85. I really dont think my IDE is bad since I’m already compiling and ISP downloading several ’85 programs and they run fine of the tiny85 and tiny84. But I do wonder why Petit Fat is using Wprogram.h, when the Ard documentation says that Arduino.h should be the file for version 1.n and beyond.
    Anyway thanks for listening and I’m going to keep checking into this further to see what I’m doing wrong. My biggest problem is that I only know how to use Arduino commands, not any C stuff. (I’m an old school COBOL programmer, so the Arduino stuff seems pretty easy) Again thanks, Bruce

  4. Bruce MacIntyre

    Ah Ha! That makes perfect sense. I was not aware of that subroutine. Yes I would have to use Softwareserial because the CNC program, I want to use, sends an “OK”
    over a serial port to indicate to the sending computer to send the next packet of commands. So send and receive are necessary. Well, I’m going to keep at it until I can get your code to compile. Not really sure what to do next, but I have written over 50 Arduino programs, so far, and nothing has “stumped” me yet. Thanks again for your help. I’ll let you know what I find. – Bruce

  5. Bruce MacIntyre

    Booya! I figured it out. I’ve been loading the MIT version of Attiny. It ports all the major Arduino commands to the Attinys. But only the pure Arduino commands. It doesn’t load the attiny “core” libraries that you are using in your program. I loaded the attiny cores and your program now compiles except I get the PCREL erros at the end of the compile as you mentioned in your write up. I’ll go ahead and update the avr tool chain and that will probably do it. I don’t know if I’ll keep my software this way because I have only been writing pure Arduino code with next to none C stuff. As I mentioned, I have a program running on my MEGA that does most everything, so far, and it is all pure Arduino code. I just thought the 84 could do what I wanted, but I’m not sure the code complexity to run on the 84 is worth it to me.
    Thanks for your time. I’ll keep looking at your site to see what I might need in the future. I think you are doing some very interesting stuff. Best to you. Thanks again,

    Bruce

  6. Arthur

    Hello Martin
    I am a novice in Arduino world, so I don’t get it all together. I want an ATtiny84 to make measurements with an oxygen sensor and write the data to an SD-card. Do you have a sample sketch that can help me to start with? I’m confused because there are different suggestions of how to adapt petit_fatfs routines. The discussion above goes beyond my knowledge… Please help me, regards, Arthur Rep

  7. Pingback: Martin's corner on the web

  8. Martii

    Did you ever try to do RAW writes with Attiny13? I’m thinking about battery tiny wind meter that would monitor wind speed for 2 years non stop on 2-3 AA’s.