However, I quickly realised that I want my intervalometer to be smaller, more portable and battery powered (no USB cable required). I started googling and discovered that people use their Arduinos as In-System Programmers (ISP, see here) to program smaller AVR chips like ATtiny85. What a brilliant idea! I quickly ordered some ATtinys and started building. Here are my results - who knows, maybe somebody will find them useful?
- First of all, you'll need to have the Teensy 3.1 configured and working with the Arduino IDE. To make sure everything is working as it should, please follow PJRC's tutorial. If you have already set up your Teensy 3.1, you can obviously skip this step.
- Open the Arduino IDE (I'm using version 1.0.5-r2). You should already see the ArduinoISP sketch in the Examples section (File -> Examples -> ArduinoISP). Select it and take a quick look at the source code. You should see a section where RESET, LED_HB, LED_ERR, etc. macros are defined. Since RESET's original value is SS, let's change it to 10. Additionally, make sure that the function start_pmode() calls spi_init() before anything else. Save the sketch and upload it to your Teensy 3.1.
-
Now, let's wire the programmer's LEDs up. They will give you an indication of what your ISP is currently doing. And if it's not doing anything, you can always rely on the heartbeat LED. There are 3 LEDs I'm using:
- Heartbeat (YELLOW, LED_HB, pin 9)
- Error (RED, LED_ERR, pin 8)
- Write (GREEN, LED_PMODE, pin 7)
-
Now, the next step is to install the ATtiny85 definition in your Arduino IDE. David A. Mellis was kind enough to prepare ATtiny support libraries for the Arduino IDE and share it on GitHub (you can get the zip directly here).
Once you download the zip, extract it and you should see a folder called attiny inside the package (attiny-master -> attiny). Copy the attiny folder to your Arduino's sketchbook folder's hardware subfolder (it should go like this: Arduino -> hardware -> attiny). Start or restart your Arduino IDE. You should see a list of newly added ATtiny definitions in the Board menu (Tools -> Board -> ATtiny...)
-
Make sure your Teensy acts as a programmer from now on by choosing appropriate Programmer option (Tools -> Programmer -> Arduino as ISP):
Select the basic Blink example and choose your board from the new list. I'm working with ATtiny85, so my choice is ATtiny85 (internal 1MHz clock) (I don't want it to consume too much energy, as it's going to be battery powered).The last thing before connecting the ATtiny is to make sure we are using correct pin number to drive the LED. ATtiny doesn't have pin 13 (otherwise it wouldn't be so tiny!), so let's change the value of the led variable to 0 (which, funnily enough, is ATtiny's pin number 5).
-
Now, let's connect the ATtiny. Disconnect the Teensy and stick the ATtiny on a breadboard. Look closely at the top surface of the chip, you should see a small circular shape indicating where the RESET pin is. It will look like this:
Connect the pins as follows:
ATtiny85 Teensy 3.1 RESET (1) 10 VCC (8) 3.3V GND (4) GND 7 13 6 12 5 11
using the ATtiny's pinout sheet as a reference:
Your connections should look like this:
-
Now you are ready to program your ATtiny85. Connect your Teensy 3.1 again and, having the Blink example in front of you, simply upload it. If you connected everything correctly, avrdude should upload the Blink code with no problems reporting something like this:
...
...
...
avrdude: verifying ...
avrdude: 836 bytes of flash verified
avrdude: Send: Q [51] [20]
avrdude: Recv: . [14]
avrdude: Recv: . [10]
avrdude done. Thank you. -
Now your ATtiny85 is programmed and you can test it:
Thanks and happy hacking!
The &*%^%& signature problem .. drove me mad! Untill ... ArduinoISP sketch has an odd failure it seems: init_spi() is called before setting up MISO/MOSI etc. Put it after setting up in/outputs and the signature problems are gone.
ReplyDeleteThanks for your comment and for reminding me - I totally forgot I had changed something in the sketch as well. Will check if it had any effect and if it did, I'll update the tutorial.
ReplyDelete@gartnl Not sure if I changed anything or not after all, but I'll put that information in my tutorial.
ReplyDelete@piotr What I needed to do was the following:
ReplyDeleteoriginal code:
void start_pmode() {
spi_init();
// following delays may not work on all targets...
pinMode(RESET, OUTPUT);
pinMode(MOSI, OUTPUT);
spi_transaction(0xAC, 0x53, 0x00, 0x00);
pmode = 1;
}
I changed it to:
void start_pmode() {
// following delays may not work on all targets...
pinMode(RESET, OUTPUT);
pinMode(MOSI, OUTPUT);
spi_init();
spi_transaction(0xAC, 0x53, 0x00, 0x00);
pmode = 1;
}
Don't know if this is Teensy specific, but it did the trick. No more signature failures.
Thanks for your comment. That's very interesting - I'm using the original version - like code which I changed from something like you have right now. Glad you got it sorted!
DeleteIve used your method before and it has worked, but I've was trying it again today and while the code loaded fine to the teensy, I had an issue with the Tiny
ReplyDeletewhen i upload the code I get;
avrdude.exe: ser_open(): can't open device "\\.\com10": The system cannot find the file specified.
any idea about this?
Are you using the same version of Arduino IDE? Haven't tried this code with the latest version.
DeleteIm using 1.0.5
DeleteI just tried again tonight and still nothing.
heart beat is present on the leds but nothing else
I've never seen such issue, interesting. A couple of ideas:
Delete- can you program your teensy at all?
- can you pinpoint which line/command causes this?
- do you have another teensy or arduino to test this on?
- have you tried playing with avrdude from the command prompt level?
Thank you for such an excellent article. I was able to follow your tutorial to use my Teensy3.1 to program ATtiny13a chips. I had to go dig up the ATtiny13a board for the Arduino IDE, which was easy to find. Otherwise, much was the same. Thanks again, for giving me a head-start. Nothing like the satisfaction of seeing that LED blink for the first time!
ReplyDeleteGlad you found it useful! :)
Delete