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

Tuesday, March 26, 2013

Burn Bootloader to Arduino Mega 2560 rev3

This is quite a bit easier than updating the firmware on the ATmega16U2. Here's what I did.

Stuff you will need:

  • Your Arduino Mega
  • An USBtinyISP (mine says USBtinyISP v3.0)
  • Various software that I will mention
Step 1. Download the latest version of the Arduino IDE.

Step 2. Download and install the USBtinyISP driver found HERE.

Step 3. Download and install WinAVR, found HERE. Detailed instructions found HERE.

Step 4. Open the Arduino IDE. Select your board and the USBtinyISP programmer (do not select a COM port).

Step 5. Plug your USBtinyISP into the ICSP headers on your Arduino (the 6 male headers near the reset button). The red wire goes toward the pin 1 indicator mark. If like me, you had neither a red wire nor an indicator mark, see the *** section. 

Step 6. Plug the USB cable into the USBtinyISP. I left the jumper on it in place so the Arduino got power through the USBtinyISP. 

Step 7. Click Burn Bootloader and wait. It will take a few minutes.

***My USBtinyISP cable did not have a red wire so I had to do some snooping. First I got THIS diagram from HERE. Next I broke out the multi-meter and tested for continuity between the Arduino ground and 5V pins and the ICSP pins. Eventually I found them and worked out which way the cable plugged in. I then made an indicator mark on the Arduino and the cable in sharpie. The cable will only plug into the USBtinyISP one way on mine, but you could check that side with a volt meter too if you're worried about it. For me, the side of the connector that faced the USB socket on the USBtinyISP faced the USB socket on the Arduino.

As I mentioned previously, when I did this I got the error below, but the bootloader appeared to be burned anyway. If someone knows what happened feel free to fill me in. Hopefully it will work for you.

avrdude: verification error, first mismatch at byte 0x3e000
         0x0d != 0xff
avrdude: verification error; content mismatch

I'm not sure what I will write about next, but its sure to be exciting. Stay tuned.

Until Later,
Matthew

Sunday, March 24, 2013

Updating Firmware on ATmega16U2

Ok. I promised I'd tell how I updated the firmware on my ATmega16U2 on my Arduino Mega. Hopefully I will remember everything I did. Below are some of the websites I referenced to do this.
http://arduino.cc/en/Hacking/DFUProgramming8U2
http://andrewmemory.wordpress.com/2011/04/14/upgrading-the-arduino-uno-8u2-using-flip/
http://aphes.com/hardware/how-to-set-arduino-mega-2560-r3-into-dfu-mode/

Step 1. Download and install Flip programmer from Atmel.

Step 2. Make sure you have the latest version of the Arduino IDE. This will include the firmware you'll need.

Step 3. Plug in your Arduino and find it in the device manager. There are 6 male pins sticking up near the USB socket. Find the two nearest the socket and bridge them with some wire (as shown HERE and to the right). The Arduino should disappear from the device manager. Take the wire off. The Arduino should re-enter as an unknown device. It is now in DFU mode. If you unplug the USB, you will have to do this step again.

Step 4. Open the new device that is the ATmega16U2 in the device manager and load the driver for the flip programmer. On my install it was C:\Program Files (x86)\Atmel\Flip 3.4.7\usb. I just let it search that folder and it found it. Alternatively you could just say "have disk" and click on it. It should be similar to atmel_usb_dfu.inf.

Step 5. Open Flip. Click Load Hex File. You want "Arduino-usbserial-atmega16u2-Mega2560-Rev3.hex" (assuming you are using a Mega 2560 rev 3). It is in your Arduino folder. It can be found at this location. "arduino-1.0.4\hardware\arduino\firmwares\atmegaxxu2\arduino-usbserial". You should get a message something like "Hex file parsed" at the bottom.

Step 6. Click Select Target Device. Select ATmega16U2.

Step 7. Click Select Communication Medium. Select usb. Click open. This should bring up a new page with all sorts of interesting things to look at.

Step 8. Click Run. Assuming all goes well, you are done. Unplug your Arduino and plug it back in.

The most useful site I found was a blog HERE. Note that he was using an Uno. The Mega rev3 is easier to put into DFU mode. All you have to do is short the two pins I described above.

I hope this was helpful. If you find anything that I posted is wrong, let me know in a comment or by email. You can also email or comment if you have problems and can't get it to work. I will try to help as best I can.

Have fun in your endeavors!
Matthew