ATtiny85 + Arduino

Hi guys,

Great forum you have here. I've jumped in at the deep end with a very basic grasp of electronics. :astonished: I apologise in advance because I'm sure my questions have already been answered here. I'm posting because I hit a brick wall a few days ago and I haven't been able to get past it by myself.

I'm trying to build a very small circuit board for an art project with a microphone and an LED which lights up when a sound peak is detected. I decided to use a micro controller for this for the flexibility they provide, plus it's great opportunity to learn about the technology. I've done a lot of research and managed to write a sketch which works the way I want on an Arduino Uno and a breadboard.

However, a crucial part of this project is to reduce the size and cost of the whole thing, so getting my sketch onto an ATtiny85 seems like a good way to do it. But there doesn't seem to be a definitive, idiot-proof guide for complete beginners like myself. I've done a lot of research and managed to get Blink working using the HLT tutorial and core files (http://hlt.media.mit.edu/wiki/pmwiki.php?n=Main.ArduinoATtiny4585). I found I didn't have to disable the auto reset to get the ATtiny85 programmed.

But after uploading my sketch, the LED just constantly stays at maximum brightness as soon as the ATtiny85 powers up. I was able to re-program it with Blink to make sure it was working, but after doing this twice, I can no longer re-program it. The sketch appears to upload properly, but the chip behaves the way described above. I've disabled the auto-reset, but it doesn't seem to make a difference. I'm sure there's something basic I'm missing, so again I apologise for being so utterly clueless.

I wasn't aware of the HLT port of the Arduino libraries (thanks for that!) I downloaded and installed it per the instructions, put an ATtiny85 and some LEDs on a breadboard, and used a USBtinyISP to program it. Worked great, it was a piece of cake!

Need some more details to help you, though. What are you using for a programmer? Please show us your sketch. What do you have connected to the ATtiny85?

Did you change the fuses?

@Jack: The HLT instructions are the best I've found. The HLT core is based on this from Alessandro Saporetti... http://sites.google.com/site/elettronicaarduinoesperimenti/Home/arduino-ide-and-attiny45

There's another core and more information available here... http://code.google.com/p/arduino-tiny/

[quote author=Coding Badly link=topic=62403.msg452124#msg452124 date=1306545351] There's another core and more information available here... http://code.google.com/p/arduino-tiny/ [/quote]

You bet. Just got it a little while ago. Took all of five minutes and I had a sketch running on an ATtiny85. Fantastic!

I was having issues uploading to one of my attiny85 chips this morning, turned out I had a bad connection on the MOSI pin.

Fire up a command prompt and go to whatever directory AVRDude got stuck in (not sure where it goes in windows), then type this

avrdude -C /etc/avrdude.conf -p ATTINY85 -c stk500v1 -b 19200 -P /dev/ttyUSB0 -v

You’ll need to replace “/dev/ttyUSB0” with whatever COM port your Arduino is pretending to be at the moment, and “/etc/avrdude.conf” with the path to your avrdude.conf file.
If all goes well it will give you a readout of how happy the attiny is, as well as listing all the fuse bits and such. If it tells you it’s an unrecognized chip, you may well have a connection issue.

Alternatively, doublecheck your connections.

Thanks for the replies! It's been a long day so I'll try your suggestions and respond to everyone in more detail tomorrow.

I just wanted to quickly mention that I didn't change the fuses. Will I need to in order to get my sketch working on an ATtiny85?

Also, is the process really supposed to be as simple as it is in the HLT tutorial? Apart from disabling the auto-reset, does it miss out any other fundamental information?

Eli_Ben: I just wanted to quickly mention that I didn't change the fuses. Will I need to in order to get my sketch working on an ATtiny85?

From the factory, ATtiny85 processors are configured to run from the internal oscillator (8 MHz) divided by 8. So long as you use a 1 MHz board option then you can leave the fuses as they are.

Also, is the process really supposed to be as simple as it is in the HLT tutorial?

I have not used an actual Arduino but I have used other boards to program ATtiny processors. As far as I can tell the HLT instructions are accurate. The answer is "yes", it really should be that simple.

Apart from disabling the auto-reset, does it miss out any other fundamental information?

As far as I can tell, no, nothing fundamental is missing.

The HLT tutorial is what got me started. It's worked great for me.

The process is really, really simple. Amazingly simple, really.

Don't worry about using Coding Badly's core(s) with the HLT tutorial, thats what I do and it works great.

You’re welcome :slight_smile: I’m using an Arduino Uno in ISP mode as a programmer. At the moment I’m trying to get Blink working, so I just have VCC, GND (5V from the Arduino) and an LED + resistor on Pin 0. I’ve also tried powering it with a 9V battery via a 5V regulator, and the result is the same. When trying to get my sketch working, I just had the pins connected as described in the comments of my sketch, and power connected as above. Here’s my sketch:

/*
The purpose of this sketch is to immediately illuminate LEDs when it detects sound over a 
certain threshold. The LEDs will stay on for a fixed time, then fade out. The cycle will start any time a 
sound peak is detected, including during a fade.

5/17/11 - The sketch functions as intended on an Arduino Uno.
*/

// User set variables

const int     micPin       =   2 ; // Input into analog pin 0 (from a mic through an LM358 op-amp circuit.)
const int     ledPin        =   1 ; // Output to LEDs via pin 11 (through a resistor.)
const int     thresholdPin=   4 ; // Input to set threshold via a potentiometer.

 int     threshold   =   120 ; // Sound level at (or above) which LEDs are illuminated.

const int     onTime      =  1000 ; // The LEDs will stay at maximum for at least this long.
const int     fadeTime    = 2635 ; // The LEDs will take this long to fade out.
const int     fadeInterval=   30 ; // Time interval between LED brightness increments.
const int     fadeAmount  =    3 ; // Amount by which to fade, each loop cycle.

// Automatically set variables

int           thresholdIn =    0 ; // Potentiometer to set the threshold.
int           soundLevel  =    0 ; // Current sound level.
unsigned long lightStart  =    0 ; // Time the LEDs are lit.
unsigned long fadeStart   =    0 ; // Time at which to start fading the LEDs.
unsigned long fadeEnd     =    0 ; // Time at which to finish fading the LEDs.
unsigned long timeThen    =    0 ; // Time reference for fading             
unsigned long timeNow     =    0 ; // Time reference for fading                      
int           fadeLight   =    0 ; // LED brightness value for fading.
           
             void setup() 
{
 
  pinMode(micPin, INPUT)  ; // Set mic pin as an input.
  pinMode(ledPin, OUTPUT) ; // Set led pin as an output.
  pinMode(thresholdPin, INPUT) ; // Set threshold pin as an input
  
  //Serial.begin(9600)      ; // Serial monitor.
  
}
              void loop() 
{
 
  soundLevel = analogRead(micPin);        // Read the sound level, store it in an int.
  threshold = analogRead(thresholdPin);

  //Serial.println(threshold);
       
     // Sound peak

 if (soundLevel >= threshold)             // If the sound level is equal to or greater than the threshold.
  {
  analogWrite(ledPin, 255);               // Set the LED output to maximum brightness.
  lightStart   = millis();                // Store the time in an int.
  fadeStart    = lightStart + onTime;     // Store the time at which to start fading the LEDs.
  fadeEnd      = fadeStart  + fadeTime;   // Store the time at which to finish fading the LEDs.
  Serial.println(soundLevel);             // Send the sound level to the serial monitor.
  fadeLight    = 0;                       // Reset the fade brightness int.
  }

     // Start to fade

 if (millis() >= fadeStart && millis() <= fadeEnd)                // If the time the LEDs are supposed to be at max for has elapsed.
  {
  timeNow = millis() - timeThen;          // Store in an int, the difference in time between the current time and the time the previous cycle ended.
   if (timeNow >= fadeInterval)           // Check that the amount of time specified by 'fadeInterval' has elapsed (to give a smooth fade).
    {
    fadeLight    = fadeLight - fadeAmount;// Reduce the LED brightness int by an amount specified by 'fadeAmount')
    timeNow      = millis();              // Reset timeNow to the current time.
    timeThen     = millis();              // Reset timeThen to the current time.
    }
  analogWrite(ledPin, fadeLight);         // Set the LED brightness to the amount now specified by the 'fadeLight' int.
  }

     // End fade
    
 if (millis() > fadeEnd)                  // If the time the LEDs are supposed to lit for has elasped.
  {
  analogWrite(ledPin, 0);                 // Turn the LEDs off.
  fadeLight = 0;                          // Reset the 'fadeLight' int.
  }
  
}

Bobnova:
Fire up a command prompt and go to whatever directory AVRDude got stuck in (not sure where it goes in windows), then type this

avrdude -C /etc/avrdude.conf -p ATTINY85 -c stk500v1 -b 19200 -P /dev/ttyUSB0 -v

You’ll need to replace “/dev/ttyUSB0” with whatever COM port your Arduino is pretending to be at the moment, and “/etc/avrdude.conf” with the path to your avrdude.conf file.
If all goes well it will give you a readout of how happy the attiny is, as well as listing all the fuse bits and such. If it tells you it’s an unrecognized chip, you may well have a connection issue.

Alternatively, doublecheck your connections.

Wow, I actually managed to get that to work! Great tip, thank you :slight_smile: I didn’t see anything obviously wrong, but I don’t understand most of the information it gave me. Shall I copy and paste it here? The connections all seem to be ok, but I don’t have another Attiny85 to test until tomorrow.

Using the HLT port, there’s only one option for the ATtiny85 and Arduino ISP, but I can’t tell if it’s 1MHz. Do you know what it is?

It’s really interesting to learn how simple this process should be. But I’m not sure what I need to do to protect the ATtiny85, so maybe I’ve damaged it by trying to get my sketch uploaded to it prematurely? In any case I have some more arriving tomorrow, so the fun can continue. :smiley:

soundLevel = analogRead(micPin); // Read the sound level, store it in an int. threshold = analogRead(thresholdPin);

I can't remember if it was Alessandro's core that had the problem but I recall that at least one ATtiny85 core had problems with analogRead. Or maybe it was an ATtiny84 core. Maybe it was both. Bugger. I can't remember enough details.

In any case, try testing with a simpler Sketch (one that does not use analogRead), a single LED on pin 3 or 4, and only the Arduino ISP connected to pins 0, 1, or 2.

Using the HLT port, there's only one option for the ATtiny85 and Arduino ISP, but I can't tell if it's 1MHz. Do you know what it is?

The core came with a file named "boards.txt". Open that file. If there is an entry with ".build.f_cpu=8000000L" then the core is designed to work at 8 MHz and you will have to modify the fuses (or change that entry). If there is an entry with ".build.f_cpu=1000000L" then the core is designed to work at 1 MHz and you can use the processors they way they arrive from the factory.

[quote author=Coding Badly link=topic=62403.msg453286#msg453286 date=1306691277] I can't remember if it was Alessandro's core that had the problem but I recall that at least one ATtiny85 core had problems with analogRead. Or maybe it was an ATtiny84 core. Maybe it was both. Bugger. I can't remember enough details.

In any case, try testing with a simpler Sketch (one that does not use analogRead), a single LED on pin 3 or 4, and only the Arduino ISP connected to pins 0, 1, or 2. [/quote]

Oh, that would really put a dent in my plans! But at the moment I can't even get Blink working on the chip I have. Didn't receive my delivery of new chips today due to a bank holiday I forgot about. :/

[quote author=Coding Badly link=topic=62403.msg453286#msg453286 date=1306691277] The core came with a file named "boards.txt". Open that file. If there is an entry with ".build.f_cpu=8000000L" then the core is designed to work at 8 MHz and you will have to modify the fuses (or change that entry). If there is an entry with ".build.f_cpu=1000000L" then the core is designed to work at 1 MHz and you can use the processors they way they arrive from the factory. [/quote]

Thanks for that, I can confirm that it's designed to work at 1MHz. Does this mean that my sketch will behave any differently when running on the ATtiny85 instead of the Arduino?

Oh, that would really put a dent in my plans!

Why? If you have problems with analogRead, just use a different core. There are at least three available.

But at the moment I can't even get Blink working on the chip I have.

Use a different pin for the LED. Either 3 or 4.

Thanks for that, I can confirm that it's designed to work at 1MHz. Does this mean that my sketch will behave any differently when running on the ATtiny85 instead of the Arduino?

No. It means you have 1 / 16 the CPU time available. For the vast majority of applications, it will make no difference.

If the CPU time becomes an issue, the fuses and core can be changed so the processor runs at 8 MHz. Or, you can use an external crystal.

[quote author=Coding Badly link=topic=62403.msg453981#msg453981 date=1306778009]

Oh, that would really put a dent in my plans!

Why? If you have problems with analogRead, just use a different core. There are at least three available. [/quote]

Oh yeah, of course there are! I've actually just tried to use the 'arduino-tiny-0022-0008' core found here http://code.google.com/p/arduino-tiny/(which I believe is yours?), but no luck uploading. The Arduino IDE appears to get stuck on 'uploading to I/O board', and displays this error message:

java.lang.NullPointerException at processing.app.debug.AvrdudeUploader.getProgrammerCommands(AvrdudeUploader.java:106) at processing.app.debug.AvrdudeUploader.uploadUsingPreferences(AvrdudeUploader.java:68) at processing.app.Sketch.upload(Sketch.java:1603) at processing.app.Sketch.exportApplet(Sketch.java:1568) at processing.app.Sketch.exportApplet(Sketch.java:1524) at processing.app.Editor$DefaultExportHandler.run(Editor.java:2293) at java.lang.Thread.run(Thread.java:619)

[quote author=Coding Badly link=topic=62403.msg453981#msg453981 date=1306778009] Use a different pin for the LED. Either 3 or 4. [/quote]

I found that Blink 'wasn't working' because of a stupid wiring error on my part. :roll_eyes: So I was able to get Blink working normally again via the HLT core, but using the HLT core to upload my sketch still produces the same result at before i.e. Whichever pin I set as the LED pin, it's always on at maximum brightness. I've also just noticed some more strange behavior: When the potentiometer is turned low (roughly 0-10%), the LED instantly turns off without fading. When the pot is set higher, the LED is always on. This is using a brand-new ATtiny85 chip, just in case I did something terrible to the previous one.

[quote author=Coding Badly link=topic=62403.msg453981#msg453981 date=1306778009] No. It means you have 1 / 16 the CPU time available. For the vast majority of applications, it will make no difference.

If the CPU time becomes an issue, the fuses and core can be changed so the processor runs at 8 MHz. Or, you can use an external crystal. [/quote]

Thanks, that's reassuring :) Does 1MHz use less power than faster options?

I've actually just tried to use the 'arduino-tiny-0022-0008' core found here http://code.google.com/p/arduino-tiny/(which I believe is yours?), but no luck uploading. The Arduino IDE appears to get stuck on 'uploading to I/O board', and displays this error message:

Did you follow the instructions in the readme.txt file?

Whichever pin I set as the LED pin, it's always on at maximum brightness. I've also just noticed some more strange behavior: When the potentiometer is turned low (roughly 0-10%), the LED instantly turns off without fading.

The LED is connected to a pin that does not support PWM. Or, you have a bug in your Sketch.

Does 1MHz use less power than faster options?

Yes. In addition it allows the supply voltage to be lowered dramatically reducing the power consumption.

I did, although there was one step which I don’t really understand:

“Open the “boards.txt” file and change both of the “upload.using” entries to the appropriate value for your setup.”

I had a go at it, but I guess I was wrong. :blush:

Aah right, not all pins support PWM! I’m learning. :stuck_out_tongue: When set to pin 0, the LED behaves the same way described in my previous post except that turning the pot low doesn’t turn the LED off. However, if the circuit is powered up with the pot turned low, the LED will be off until the pot is turned up to about 10%.

My sketch runs fine on the Arduino, but is there something other than the pins that I should change?

I know I'm missing something incredibly obvious, I just can't figure out what it could be. My sketch runs absolutely fine on the Arduino, but on the ATtiny85 it behaves very oddly. If anyone has any sort of suggestion I'd love to hear it, no matter how simple it is! I've really been tearing my hair out trying to get it work. =(

It might be worth adding a filter/decoupling cap between VCC and GND pins. 0.1uf is about right. Your house may be especially noisy or something, who knows?

What is the sketch doing now?

Eli_Ben:

[quote author=Coding Badly link=topic=62403.msg454834#msg454834 date=1306864990]
Did you follow the instructions in the readme.txt file?

I did, although there was one step which I don’t really understand:
“Open the “boards.txt” file and change both of the “upload.using” entries to the appropriate value for your setup.”
I had a go at it, but I guess I was wrong. :blush:[/quote]

You will need to modify the section that starts with this…

attiny85at1.name=ATtiny85 @ 1 MHz (internal oscillator; BOD disabled)

Remove the pound-sign from the “arduino:arduinoisp” line and add a pound-sign for the “pololu” line…

The following DO work (pick one)…

attiny85at1.upload.protocol=avrispv2

attiny85at1.upload.using=arduino:arduinoisp
# attiny85at1.upload.using=pololu

I’m now able to upload using your core for the first time! My sketch now runs almost perfectly (see below), so a huge thank you for making and sharing your core, as well as your time and patience helping me. :slight_smile:

Bobnova:
It might be worth adding a filter/decoupling cap between VCC and GND pins. 0.1uf is about right.
Your house may be especially noisy or something, who knows?

What is the sketch doing now?

The smallest cap I have is 1uf, which I have between the VCC and GND pins. Is that too high?

After uploading my sketch using Coding Badly’s core there’s been major progress. It now works almost perfectly, but the LEDs don’t fade out at all. Instead, at the time they are supposed to start fading, they simply turn off. I have the LEDs connected to pin 1 (physical pin 6) which supports PWM, but maybe somehow PWM isn’t enabled…?

Edit: Also tried the LED output via pin 0, and it behaves the same.