Go Down

Topic: ATtiny85 + Arduino (Read 5864 times) previous topic - next topic

Eli_Ben

Hi guys,

Great forum you have here. I've jumped in at the deep end with a very basic grasp of electronics.  :smiley-eek: 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.

Jack Christensen

#1
May 28, 2011, 02:40 am Last Edit: May 28, 2011, 02:48 am by Jack Christensen Reason: 1
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?
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Coding Badly


Did you change the fuses?

Coding Badly

@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/

Jack Christensen


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


You bet. Just got it a little while ago. Took all of five minutes and I had a sketch running on an ATtiny85. Fantastic!
MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Bobnova

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
Code: [Select]
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.

Eli_Ben

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?

Coding Badly

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.

Quote
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.

Quote
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.

Bobnova

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.

Eli_Ben


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?


You're welcome :) 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:

Code: [Select]
/*
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.
  }
 
}



Fire up a command prompt and go to whatever directory AVRDude got stuck in (not sure where it goes in windows), then type this
Code: [Select]
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 :) 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.


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.


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.  :D

Coding Badly

Quote
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.

Quote
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.

Eli_Ben


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.


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. :/


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.


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?

Coding Badly

Quote
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
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.

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?


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.

Eli_Ben


Quote
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.


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)


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


I found that Blink 'wasn't working' because of a stupid wiring error on my part.  :smiley-roll-blue: 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.


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.


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

Coding Badly

Quote
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?

Quote
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.

Quote
Does 1MHz use less power than faster options?


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

Go Up