Program an ATtiny with Arduino

 by randofo
Featured
main1.jpg
Follows are directions for programming the ATtiny microcontrollers using the Arduino IDE. In plain English, this is how to program 8-pin Atmel chips as you would normally an Arduino. This is cool because the ATtiny is tiny, and - well - this allows you to make tiny things that don't need a big ol' microcontroller.

The instructions I have posted here are pretty much the same as instructions given by the incredibly awesome High-Low Tech Tutorial. I posted my version of the instructions here because I plan to make a couple of upcoming projects using ATtiny chips and figured I would show my process.
 
Remove these adsRemove these ads by Signing Up

Step 1: Go get stuff

1A.jpg
You will need:

- Arduino
- Breadboard
- ATtiny85 (or ATtiny45)
- 10uF 16V electrolytic capacitor
- 220ohm 1/4 watt resistor
- LED
- solid core hookup wire
1-40 of 102Next »
infanati says: Jun 6, 2013. 8:02 AM
I've been trying to program my ATtiny13V but I keep getting "'OUTPUT' was not declared in this scope".

// Must include for servos to work
#include
// Elements is the amount of numbers you want to dedicate to the running average
// Increase the number for a slower reaction
#define elements 5

// Variables for loops
int i = 0;
int j = 0;

// The Analog Pins - C:Centre R:Right L:Left U:Up
int pinC = 1;
int pinR = 2;
int pinL = 3;
int pinU = 7;

// Variables to store the data from the photo-resisitors
int analogValueC;
int analogValueL;
int analogValueR;
int analogValueU;

// The change in position from the last reading
float posX = 0;
float posY = 0;

// The running average readings
// - Each element is made up of the difference between opposite photorestors
int x[elements], y[elements];

// Servos - X is rotation/spin, Y is the tilt servo
Servo servoX;
Servo servoY;

// Common servo setup values
int minPulse = 600; // minimum servo position, us (microseconds)
int maxPulse = 2400; // maximum servo position, us

void setup() {

// Turn on the pins, program doesn't work without it *shrug*
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
digitalWrite(6, HIGH);
digitalWrite(5, HIGH);
delay (200);

// Attach each Servo object to a digital pwm pin
servoY.attach(6, minPulse, maxPulse);
servoX.attach(5, minPulse, maxPulse);
delay(200);

// Sanity check!
servoX.write(90);
servoY.write(90);

// Start all the running average values to zero
for(i=0;i x[i] = 0;
y[i] = 0;
}

// Serial - good for troubleshooting
Serial.begin(9600);
delay(200);
}

void loop() {

// This will help to balance the values so the sun tracker doesn't Jump around
// during high contrast situations
int normal = (analogRead(pinC)/50);

// Read the photorestors
analogValueC = (analogRead(pinC)/normal);
analogValueL = (analogRead(pinL)/normal);
analogValueR = (analogRead(pinR)/normal);
analogValueU = (analogRead(pinU)/normal);
analogValueD = (analogRead(pinD)/normal);

// Check if the Centre photorestor is the brightest, if it is then set the change to zero
if((analogValueC <= analogValueL)||(analogValueC <= analogValueR)){
// Value is positive: go right, negative: go left
x[i] = analogValueR - analogValueL;
} else {
x[i] = 0;
}
if((analogValueC <= analogValueU)||(analogValueC <= analogValueD)){
y[i] = analogValueU - analogValueD;
} else {
y[i] = 0;
}

// The change in position is the average of all the elements
int totalX = 0;
int totalY = 0;
for(j=0;j totalX+=x[j];
totalY+=y[j];
}
posX = totalX/elements;
posY = totalY/elements;

// Send the values through the serial when it has gone through all the elements
if(i==0)avgDisplay();
//if(i==0)rtDisplay();

// Change the position of the tracker towards the light
simpleChangePos();

// Increment I through 0 to number of elements
i++;
i = i%elements;
delay(301);
}


// Class will display a cross on the serial monitor showing real time values of the resistors
void rtDisplay(){
Serial.print(" ");
Serial.println(analogValueU);
Serial.println(" /\\");
Serial.print(analogValueL);
Serial.print(" <= ");
Serial.print(analogValueC);
Serial.print(" => ");
Serial.println(analogValueR);
Serial.println(" \\/");
Serial.print(" ");
Serial.println(analogValueD);
Serial.println();
Serial.println();
}

// Class will display the change in position
void avgDisplay(){
Serial.print(" ");
if(posY>0){
Serial.println(posY);
Serial.println(servoY.read());
} else {
Serial.println(" ");
}
Serial.println(" /\\");
if(posX>0){
Serial.print(posX);
Serial.println(servoX.read());
} else {
Serial.print(" ");
}
Serial.print(" <= ");
if((posX==0)&&(posY==0)){
Serial.print("C");
} else {
Serial.print(" - ");
}
Serial.print(" => ");
if(posX<0){
Serial.println(-posX);
Serial.println(servoX.read());
} else {
Serial.println(" ");
}
Serial.println(" \\/");
Serial.print(" ");
if(posY<0){
Serial.println(-posY);
Serial.println(servoY.read());
} else {
Serial.println(" ");
}
Serial.println();
Serial.println();
}

// Named so, because I am planning on making a more complicated verson
void simpleChangePos(){
// Variables to store the current position of the servos
int readX = servoX.read();
int readY = servoY.read();

// If there is a change:
if(posX!=0){
// If the servo is going to change position past its range of motion
if((readX+posX)>180){
if(readX!=180){
servoX.write(180);
}
} else if((readX+posX)<0){
if(readX!=0){
servoX.write(0);
}
} else {
// If the change is a non-zero and not past the servo's limit, change the position
servoX.write((readX+posX));

}
delay(15);
}

if(posY!=0){
if((readY+posY)>180){
if(readY!=180){
servoY.write(180);
}
} else if((readY+posY)<0){
if(readY!=0){
servoY.write(0);
}
} else {
servoY.write((readY+posY));
}
delay(15);
}

}

If anyone got any ideas how to solve it pls let me know : )
randofo (author) in reply to infanatiJun 9, 2013. 11:38 PM
Not all of the code that runs on the Arduino will run on the ATtinys. When you start including libraries, like the servo library, the likelihood of it working diminishes.

Can you program one to blink an LED? If so, the problem is probably with some of the commands you are calling (presumably the servo code). You will need to figure out some alternative way of writing the code, or to modify the library to work with the chip.
eliobou says: May 29, 2013. 2:31 PM
Can I use a 10V 10uF capacitor ?
DirtyMex says: May 26, 2013. 11:44 PM
great project. Is there a possibility that you can program a PICAXE 08m2 using the same steps? If you could help that be of much help
randofo (author) in reply to DirtyMexMay 27, 2013. 10:33 AM
That is almost assuredly not possible. They use different bootloaders and chips. The PICAXE would have to support the Arduino environment, and you would then need core files for it.
Crash Plague says: May 2, 2013. 12:11 PM
Does the ATtiny need to have the Arduino bootloader put on it? Or can any ATtiny straight from digikey be put into this tutorial?
Enjoying Electronics in reply to Crash PlagueMay 8, 2013. 8:10 AM
Yep.
irulaja says: May 2, 2013. 3:53 AM
Thanks :)
mrmerino says: Mar 17, 2013. 3:21 PM
Are all of the I/O pins configureable as inputs and outputs? Or are woke of them dedicated inputs and others outputs?
leeseibert says: Mar 11, 2013. 4:48 AM
Here is a little extra help for those using the Mega variety of Arduino instead of Uno or Due.

Check it out:
https://www.youtube.com/watch?v=DebJ1s5I3QM
AT says: Feb 23, 2013. 8:19 PM
This looked like such a cool project. But I am running into an issue that I can't figure out. I get the error once and then it says:

avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.

When I connect the LED, nothin. :-(

zextron in reply to ATMar 8, 2013. 7:10 AM
I had your error with a tiny85 that I've already used to try this project earlier in 2012, but I think it was fried or I fried it, because then I tried with the same exact config but another one and it was up and running!
AT in reply to ATFeb 23, 2013. 11:25 PM
I have the 1.0.3 version and switched back to 1.0.1 and getting the same error. I tried the trick that hydronics did but that didn't work either.

I have ATTINY85-20PU
Arduino UNO R3
Board is set to ATTINY 85 (external 20 MHz Clock)
Programmer as Arduino as ISP

Tried Upload and Upload using Programmer

I was able to get the Burn Bootloader to run once with the proper error messages. Nothing else seems to be going as it should.

Binary sketch size: 834 bytes (of a 8,192 byte maximum)
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.
randofo (author) in reply to ATFeb 24, 2013. 10:33 AM
Also, try it without setting the external clock. Try using the slowest possible internal clock. Sometimes the external clock causes strange errors until you get it configured correctly.
randofo (author) in reply to ATFeb 24, 2013. 10:31 AM
Go even further back... Arduino 1.0.anything is not going to work. Go back to version 20 or earlier of the old IDE. For some reason the core files do not work with the new version. Unclear whether anyone is ever going to make an updated set of core files.
AT in reply to randofoFeb 25, 2013. 2:14 PM
I loaded up 20 and 22. These are working even worse. I can't even get the blink program to compile fore the ATTiny85. :-(
AT in reply to randofoFeb 25, 2013. 7:23 AM
That you for the tips. I will see about getting an older version. I have tried doing 1MHz and 8MHz and was getting the same issue. I will give these a try and comment back.
kapex says: Jan 29, 2013. 3:45 AM
Hello there!
And sorry if my english is not very good.
Tried to do this with Mega 2560 board and Attiny85v-10pu via ICSP pins (wired correct- double cheked!). Using Arduino 1.0.3 softw. and ATtiny master.zip (hosted by GitHub).
But got this when I click to burn the bootloader:
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85 avrdude: Yikes! Invalid device signature. Double check connections and try again, or use -F to override this check.
When trying upload Blink sketch, got the same message. :(
Please help!
AT in reply to kapexFeb 23, 2013. 8:19 PM
I am getting the same thing....

This looked like such a cool project. But I am running into an issue that I can't figure out. I get the expected error once and then it says:

avrdude: Yikes! Invalid device signature.
Double check connections and try again, or use -F to override
this check.

When I connect the LED, nothin. :-(

I tried a different ATTiny85 and get the same thing. I have checked and rechecked the connections three times.
hydronics says: Feb 12, 2013. 9:45 PM
yep I get the same error.
randofo (author) in reply to hydronicsFeb 13, 2013. 1:10 PM
Are you using the Arduino 1 software? Try downloading an older version of the software - perhaps 5 or 6 versions back. The core files don't seem to play nicely with Arduino 1.
hydronics in reply to randofoFeb 13, 2013. 5:49 PM
Ok, thanks for the reply. IT WORKS, IT WORKS! I redid everything and it worked and I could not reproduce the error from yesterday, although....
1) Each time you unplug the USB, when you re-connect the USB/Arduino back into your PC, FIRST remove the capacitor...
2) once the computer recognizes the Arduino (2-seconds wait) then replace the capacitor.
3) this was a problem once but then it didn't reoccur...kinda glitchy ;( maybe just my janky setup.

I'm running Windows, Duemilanove to ATTiny85. I got it to work on 1.03 and 1.01. THANKS for this lovely tutorial!! so many projects to do now...

Also, can anyone tell me why the SPI library does not work on Attiny?... I found some code on gethub to get around this but... Can anyone point me to a good reference material for programming that might answer these strange truths that I am oblivious to. My background is Mechanical Engineering and Arduino.
randofo (author) in reply to hydronicsFeb 13, 2013. 8:54 PM
Does the library gives errors or just not work? It can be because the registers names are different... or the clock speed of the ATTiny is much slower... or it is using interrupts which the ATTiny does not support... or a host of other reasons. I would have to really look at the library to know.
MakersBox says: Jan 2, 2013. 8:55 AM
Nice photos!
waterlubber says: Aug 16, 2012. 3:20 PM
I got OUTPUT was not declared in the scope. HOW COME?
RGandKR in reply to waterlubberOct 20, 2012. 2:50 PM
(removed by author or community request)
waterlubber in reply to RGandKROct 21, 2012. 4:50 PM
Some commands don't work with the Tiny.
nodoubtman says: Sep 30, 2012. 5:21 PM
is it possible to program an atmega328p chip with arduino code?

thank yoU!
marC:)
waterlubber says: Aug 12, 2012. 6:57 PM
Tiny prob: Instead of saying (with arduino as ISP) it says ATtinyXX(Xmtz XXternal clock)
I have:
Windows 7
Arduino1.0.1
an Arduino UNO REV 2
and I want to use a "standard" ATtiny45(no v)
What htz do a pic
midknight5000 says: Mar 13, 2012. 3:37 PM
Is it possible to control a 16x2 lcd with an attiny85 (since lcd's only need six pins right)?
randofo (author) in reply to midknight5000Mar 14, 2012. 5:41 PM
I don't know. You might need to remap the reset pin, which may be a bad idea.

Maybe there is a hardware solution or some sort of hack. I don't really know enough about these LCDs to help you.
midknight5000 in reply to randofoMar 14, 2012. 6:51 PM
ok no problem thx for at least telling me you don't know instead of telling a lie
compsfreak17 in reply to midknight5000Apr 12, 2012. 5:52 PM
there is an instructable for controlling an lcd screen with 3 pins instead of the normal 6, i just dont know if the libaray will owrk with it. Its worth a try though.
http://www.instructables.com/id/Hookup-a-16-pin-HD44780-LCD-to-an-Arduino-in-6-sec/
slayer04 says: Jan 26, 2012. 8:45 PM
Please Help

I've tried everything to get this to work. I've tried both arduino 1.0 and 022 along with a capacitor and not, nothing ever works and I always get this when I click
to burn the bootloader:

avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85
avrdude: please define PAGEL and BS2 signals in the configuration file for part ATtiny85


Then I get this every time I go to upload sketch:

avrdude: stk500_paged_write(): (a) protocol error, expect=0x14, resp=0x64
avrdude: stk500_cmd(): protocol error
jaky_shaky in reply to slayer04Apr 7, 2012. 7:26 AM
if using arduino 1.0 as shown in pic, you must change the heart rate delay to twent milliseconds instaed of forty this solved my problem.
hope this helps,
Jacob
Untitled.png
randofo (author) in reply to slayer04Jan 26, 2012. 9:54 PM
The first error you can ignore. The bootloader seems to be loading fine.

I am not certain why you are getting the second error. From my experience I have found that many ATTiny cores may not work with Arduino 1.0.

What version Arduino are you using?
slayer04 in reply to randofoJan 27, 2012. 6:30 AM
I've got an arduino uno and if it helps any, but this is the chip I'm trying to program:

http://search.digikey.com/scripts/dksearch/dksus.dll?KeywordSearch&site=US&keywords=ATTINY85V-10PU-ND
randofo (author) in reply to slayer04Jan 27, 2012. 2:44 PM
Try doing it without the capacitor. I am not sure why you are getting this error if the bootloader is uploading correctly.

Did you change the pin numbers from 13 to 0 in the blink example?
slayer04 in reply to randofoJan 30, 2012. 11:39 AM
Hmm I did it without the capacitor with the exact same results and I did change the 13 -> 0

I wonder if my chip is just bad but that seems unlikely.
slayer04 says: Mar 16, 2012. 3:41 PM
Nice instructible, but would you happen to know why my blink sketch runs about 5x faster when I upload it to the ATiny? I don't have any errors except for the ones you say to ignore, the sketch is set to delay 1000 like normal but it cycles about every .2 secs and not every 1 sec.

I have tried it with and without the capacitor and both ended with the result above.

Thanks for any help
1-40 of 102Next »
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!