Friday, March 29, 2013

ATtiny85 Servo: SoftwareServo Library


Well I began work on my ATtiny85, and so far it has been quite the adventure. My initial thought was that it would be cool to use one on my differential steer robot, so I plunged into making this happen.
There are several problems with this that I have seen so far.
  1.  I will quickly run out of pins. I will probably want an ATtiny84. Maybe my 2313 will work. That’s another day.
  2.   The servo library will not work on the ATtiny85, reason being that the servo library relies on a 16 bit timer whereas the ATtiny only has an 8 bit timer.
  3.  The NewPing library does not work on the ATtiny. I believe this is the case anyway. The code will not compile. I haven’t looked into why this is. I’m assuming it has to do with the timers again. (Edit: check out my post on that HERE)
 Now I set out to solve all the problems of the world.

 Well I could just use an ATmega238 chip. All of the above problems solved, but that’s cheating.

Problem One: Like I said earlier, I will probably need to change hardware for this one. However, the ping sensors can be run on one wire for trigger and echo which I am not doing at the moment because my Arduino Mega had pins to spare. That would diminish the pin count by 2. I could also get my two bumpers on the front off of one analog pin by putting a different sized resistor on each one.

Problem Two: Now it gets fun. As it turns out, way back in the days of IDE 20ish there was a library called SoftwareServo library (more info HERE). All these young whippersnappers (Me) don’t know what that is because they got that newfangled timer based Servo library. Interesting. Well this can be used to our advantage. Long story short, download the library HERE. Stay tuned if you want to hear how I changed it.

 The changes are really quite elementary. All that had to be done was in SoftwareServo.h, change
            #include  <WProgram.h>       to      #include "Arduino.h"

This is to accommodate IDE 1.0+.  For those that might not know, you can use a variety of programs to do this. I used CodeBlocks because it was what was set was the default on my computer. You could probably manage with notepad on a change like this, but I’d go ahead and download a free compiler somewhere. Just Google C/C++ compilers. I’m really not experienced enough to recommend one.

Next I went to getting it running on the ATtiny. Well it was not as convenient to do so as some other libraries because it only came with one example, SerialServo. Well this is a problem because the serial library does not run on the ATtiny85 in its current form either, but that is a problem for another day.

I went typing away and eventually got it to work, so I added a few examples for lazy people like me. I already gave a link to it, but here it is again: SoftwareServo.

The rest is pretty simple. You do need to flash the ATtiny to run at 8MHz. That takes all of 15 seconds. Just connect it up, select the ATtiny85 (internal 8 MHz) option and hit burn bootloader. Details from High-LowTech.

It works really well. It’s perhaps a bit more jumpy than running on my Mega. That will happen though. I read an explanation on why that is, but it involves math and I really don't remember the details. I just know that the cut from a 16 bit timer to an 8 bit one cuts the resolution quite a bit.

Just for reference, there is also a Servo8bit library that I tried before I found the SoftwareServo library.I don’t believe it requires the Arduino IDE. I never got it to work, but maybe others can. Find details HERE. If you get it to work, let me know. I’d like to check it out.

That brings us to Problem Three. Well that is work for the future. I mainly just wanted to get a servo up and running. I succeeded with that. Yippee. Also, I got some new components and such. I will probably do a quick post on those soon too.

Happy Easter,
Matthew

12 comments:

  1. i didn't really understand how to use the refresh() in order to keep it updating.

    ReplyDelete
    Replies
    1. Check out the examples I added in the SoftwareServo library I posted above. Basically, every time you call refresh() the microcontroller tells the servos to go to the positions you have dictated. Just be sure to call it fairly often (every 50ms) or your servo will be jumpy.
      http://playground.arduino.cc/ComponentLib/servo

      Also, just to clarify, this is for the SoftwareServo library. There is no need to refresh the regular Servo library. It is automatic.

      Delete
  2. Thanks for this; I can finally control servo's using the ATTiny! I couldn't get the Servo8Bit library to work either...

    ReplyDelete
  3. I'm glad I could help!

    ReplyDelete
  4. Christian Jeffrey TicarFebruary 26, 2014 at 9:20 AM

    Hi Can I ask.. if you use your Arduino as ISP to program the Attiny85?

    ReplyDelete
  5. Christian Jeffrey TicarFebruary 26, 2014 at 9:51 AM

    I have an Arduino Duemilanove and I am using it to program an ATTINY85 to perform a Knob function in the servo. However, even if I am using this library the servo is not working eventhough the sketch is successfully uploaded

    ReplyDelete
  6. I did not. I use my USBtinyISP and a ATtiny adapter that I made. I initially tried using the Arduino as an ISP, but it seemed like a pain. For me, the frustration was not worth it. The USBtinyISP was less than $15. In the end, I'm glad I got it. You'll need an external programmer anyway if you ever plan on setting the fuses, etc to use the Reset pin as an IO.

    ReplyDelete
  7. Ok. Well the first thing I would check is to verify that the sketch is indeed uploaded. Maybe try uploading the blink sketch.


    If you have done that (it sounds like you have) I would check the pinout. Find the ATtiny pinout and note that the pins you declare in your Arduino sketch are not necessarily the same as the physical pin number on the chip (e.g. PB3 is actually pin 2.


    Also, be sure that you have the servo wired correctly. It will need an external 5V to the red lead. The white lead goes to your ATtiny. The grounds all get connected together, so connect the Black wire on the servo to the power supply ground to the ATtiny ground.


    Make sure you are running it at 8MHz. That could cause issues.


    Test the code on your Duemilanove. This is always handy. It's a lot easier to find a bad jumper wire with a "real" Arduino than a ATtiny.


    That's about all I can think of. If that doesn't do it, let me know and we can try again. You can also try posting on the Arduino forum. There are lots of people MUCH more knowledgeable than me on there that might be able help. If you do get it fixed, let us know what you did. It is nice to document problems that might arise with stuff like this.
    -Matthew

    ReplyDelete
  8. Also, I had forgotten that I did another post on this too. Maybe that would be helpful.

    http://projectsfromtech.blogspot.com/2013/04/softwareservo-library-attiny854525.html

    ReplyDelete
  9. Niklas MischkulnigApril 21, 2014 at 9:43 AM

    I have a breadboard power supply from sparkfun to power the attiny and the servo. But after some minutes, the voltage regulator gets hot and the servo moves in random directions. How can I power both of them?

    ReplyDelete
  10. If the power supply is indeed the problem, it sounds like you need a power supply that can provide more current. I'm guessing you already knew that though. I notice they have multiple breadboard power supplies there. They don't all make it obvious how many amps each can source.

    If your servo pulls less than 1.5A (I suspect it does) then you could just use a linear regulator like on this(https://www.sparkfun.com/products/114) power supply. Other than that you could get a 4 or 5 cell NiMH battery. Depending on what size cells you get, it might be an option.



    If you want to spring for something a little more versatile, you could get an adjustable lab power supply. Those can source more than enough for whatever servo you're running.

    ReplyDelete
  11. You good sir, are a godsend! Thanks a ton!

    ReplyDelete