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,

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,

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.

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!

Friday, March 22, 2013

Arduino Mega as ISP- broken Arduino

Well it has been a while since I posted anything on here, and there are several good reasons for that. One, last week was spring break, and it was beautiful. I did a lot of stuff outside and never really touched my computer. Two, right before spring break I managed to break my Arduino again. Here's how.

As I have mentioned I got an ATtiny85 to play around with. I thought it would be a natural next step to start doing stuff with just the bare chip. I am by no means an expert in such things and thus went searching for someone else's work that I could shamelessly copy in the name of education. I found this website HERE and began to follow their instructions.

Now to program these chips you need an external programmer. Being a college student and not having a whole lot of money that I just long to spend, I decided to go with the Arduino as ISP route. Well you can go to the website if you don't know what that entails, but to the right there's a picture to give you a general idea. Basically you are piggybacking off of the Arduino's programmer.

This went great! I plugged in all the wires based off of the charts on the high-low tech page, followed the rest of the instructions, and clicked go. Off the races I went, loading blink. I unplugged everything but the power and ground and got a blinking light! Yay!

Now that's cool. I plugged the wires back in and loaded fade. Error. Try again. Error. Sigh.

Now I unplug everything and try to load blink onto the arduino. It took forever to try to upload. Error. I believe it was something like the one below. Its funny that I saw it so many times but never actually copied it down.

avrdude: stk500v2_ReceiveMessage(): timeout
avrdude: stk500v2_getsync(): timeout communicating with programmer

Then I got thouroughly depressed and didn't touch it for a few days. Later I began trying to fix it.

Attempt 1: The reset doesn't appear to be working. Google such things. Nothing. Manual reset with a resistor on reset pin. Nothing. I hope this isn't the problem.

Attempt 2: Somehow I decided it might be the ATmega16U2 firmware (the smaller chip near the usb port). Download necessary stuff and reflash it. This took a while for me to figure out, but I finally did it. Successful firmware update, but no results. I never found a good tutorial on how to do this on a Mega, so I will post one later based on what I learned next time.

Attempt 3: Re-download Arduino IDE. Yes, this should have been my first step, but for various reasons it was not. Well this did not fix it either, but then a few days ago I notice that there was a new version 1.0.4. The release notes yielded this, "Fixed a bunch of bugs on Mega2560's bootloader (Mark Sproul)." Bingo I think. My problems are solved.

Well to upload the bootloader I needed a programmer. So now I bought what I should have bought in the first place, a usbtinyisp. I got mine off eBay from a US seller for $15 I think. You can also get kits HERE. There's a picture if you're wondering.

I uploaded the bootloader (go HERE for now). I may do a tutorial on this later too. Who knows. Anyway, I upload the bootloader and it appears to go well until I get an error message. It didn't work. However, I notice that the led is blinking on the Arduino. That's odd. Before it was just on all the time. It wouldn't blink even if  I pushed the reset button. I plugged in the USB cable to the Arduino, and its alive again.

I have no idea what the problem was or what fixed it, but it now works again. The moral of this story (for me anyway) is spend the $15 and get an isp. $15 could have saved me many hours. Then again, I learned a lot through this process. I learned a lot about flashing firmware, loading bootloaders, and maintaining patience.

There will be more posts to come now. I hope this helped someone.

Tuesday, March 5, 2013

LCD Shield: Calculator

     Today I have for you another project I did with my HobbyKing LCD Shield and my Arduino Mega 2560. I made a calculator! One day I decided that my Texas Instruments calculator was simply not enough. Necessity is the mother of invention so..

     Anyway. I did all I was going to do on my toolbox alarm, so I decided I would start on something new. I thought a calculator would be fun. That's what I did.

What it does now:

  • The user selects add, subtract, multiply, or divide
  • The user inputs the first value
  • The user inputs the second value
  • Answer is calculated and printed to screen

Work for the future:

  • Select number by digit (ie. 1000 = 1-0-0-0. Current model would involve 1000 button pushes)
  • Add push and hold to scroll functionality
  • Add other functions: sine, cosine, power, etc.

      Now I realize that much of the way I did things was a bit impractical. Creating a separate function to add two values is kinda useless. However, this was meant to be a learning exercise. The code that runs the LCD is still mostly the example code found HERE. I did create the menu and such, and that was really the focus of the exercise. I knew I could do it; I wanted to prove that to myself.

Stuff you might want:

     If you are using my code, note that I make no guarantees that it is perfect. If you are just buying the shield, note that I have a review that might interest you. If you have questions, comments, or concerns, post a comment or send me an email. 

     If you choose to undertake this project, I hope you find it interesting. I don't know that I learned anything specific while working on this, but I honed my debugging skills and such. I think this may be the last of my LCD shield projects for a while. I have one more in mind, but that may wait. I've got my ATtiny's waiting on me as well as my IMU that I have been playing with. So much to do, so little time. Anyway. I hope you found this interesting.

Until later,

Saturday, March 2, 2013

LCD Shield: Toolbox Alarm

Well I’m back. As you all know, I received my new Arduino and have been playing with it. I must admit having it not tied to a little robot is quite nice, so I haven’t put it on the bot yet. Instead I have been playing with my forgotten LCD shield from HobbyKing. Oh yes you say. I remember that one. Well I have been doing some more stuff with that and decided it’s time to share that with the world. Here it is.
            Today I will talk about the first project I embarked on. It is called the toolbox alarm. This project is a copy of a similar project that is used here at Tech in one of the classes. I just stole it and decided that if I’m going to do this stuff, I should be able to do the less interesting things too. Well I was a bit surprised. I actually found this project slightly fun.

Project Description: Create an alarm system for a toolbox. It should prompt for a 4 digit button sequence and then allow you an amount of time to close the lid. Next, a light sensor will read the light level. If the light level changes (ie. the toolbox is opened) the user will have a certain amount of time to re-enter the passcode before a buzzer sounds.

Well I should tell you that I did not do that. My alarm has no light sensor or buzzer. It just gives you a pat on the back if you get it right and an incorrect if you don’t. Basically, mine is completely useless as opposed to just extremely impractical.

Anyway, I really liked working on this. I got more accustomed making functions and such. The more time you spend looking at code and working on it the better.

I guess that’s about all I have for today. If you want the code I created, get it HERE. It’s free and all that jazz. Go forth and do with it what you wish. If you somehow find a way to make lots of money on it, send me a nice gift or something (I’m pretty confident that won’t happen). The video below is of it running.

            If you need the example code it is HERE. If you need to buy the LCD shield it is HERE. If you want my review of the LCD Shield it is HERE. I can’t think of anything else you’d want. You can find info on the LCD library on your own at

            I have at least one more LCD shield project I will talk about soon as well as the return of the IMU (That is what I was working on when I fried my last board) and all the sonar sensor stuff. I can’t say which will be next. It depends on my mood. I hope you’ll check back in to see that. If you have any questions about this project, post in the comments or send me an email.