Friday, August 23, 2013

Arduino Mega 2560 r3 Doesn't Show Up in Tools Menu

Recently, I have had problems with my Arduino Mega 2560. Serial communications would drop in and out. It would not show up in the Arduino IDE Tools menu. Annoying stuff. Looking around on the internet, it appears that this is a fairly common problem, so I thought I would post what I know about a solution.

If your Arduino Mega doesn't show up in the Tools menu, the first thing you need to do is open the device manager (on Windows. If you have a Mac then you should mail it to me and go by a Windows machine). From the device manager we can further diagnose the problem.

1) Your Arduino shows up nicely under the COM ports tab. This is quite possibly the most frustrating problem, and as far as I can tell no one really knows why this happens. Here are some things that I did that sometimes worked. Try a combination of them.

  • Disconnect and reconnect the Arduino
  • Re-install the Arduino Drivers 
  • Re-install the Arduino Drivers deleting them when you remove them. ( I believe there was an update to the Mega driver somewhere around Arduino 1.0.2)
  • Restart the Arduino IDE
  • Turn off  your WiFi (My computer has bluetooth built into the network card. Bluetooth COM ports can cause problems sometimes. Turning off networking solves this).
  • Reinstall the ATmega16U2 firmware. (Instructions HERE)
  •  Get frustrated and go work in your garden
2) Your Arduino shows up as an unknown device. Install the drivers. I can't think of any other reason this would happen.

3) Your Arduino shows up as an unknown Atmel device. The Arduino Mega 2560 r3 has a ATmega16U2 that acts as a USB to serial chip. If you are getting an unknown Atmel device it means that this chip has been put into DFU mode. This is not the end of the world. Usually you can just disconnect and reconnect the Arduino, and it will go back into it's normal mode. If that doesn't work see my previous post on this subject.

4) Your Arduino shows up as an Atmel device called ATmega16U2. Well this means the same thing as above. It is in DFU mode except you have already loaded the Flip programmer driver. You have already started the process, so go HERE and finish.

5) Your Arduino does not show up in the device manager at all. This is a pretty major problem, but I found this one almost freeing. You can try various things. If you have an external ISP you may be able to reflash the ATmega16U2 and save it. I believe there are some instructions for doing so in a README file in the Arduino firmware folder. You should try a different USB cable obviously. Wiggle the wires. Blow out the connection with air. However... the chip may be fried. What does this mean?

If it is just the ATmega16U2 that is trashed then you can still use your Arduino Mega! Now you will just need an external programmer to program it. I recommend having one of these anyway. A USBtinyISP is maybe $15. I have used it extensively with ATtinys and even detail how to use it with a Mega HERE.

But now you want to get that nice Serial Monitor for debugging. Well that again will require extra hardware. You will need an external USB to UART cable. I talk about them HERE. Mine was $4. It's a worthwhile purchase anyway.

If none of those things tickle your fancy, buy another one. Places like HobbyKing sell them for less than $20 (HERE). Look for "Arduino compatible" boards. No one likes a counterfeit.

One last problem that I want to throw in here. The Tools menu freezes or crashes. There are mentions of this HERE. Like I said above, turn off your WiFi. You can also look and see if there are any Bluetooth scanning programs running. I haven't found any quick way of doing this besides looking through the system processes and closing stuff that sounds suspicious. Believe me, I know how annoying this problem is, but that's life. Arduino 1.5 does seem to do a bit better.

If you have other ideas, suggestions, or words of encouragement comment, and let the world know. There may be other things out there you can try.I hope this brings you success. Good Luck!
-Matthew


Wednesday, August 7, 2013

Serial Sonar Sensor: ATtiny85, HC-SR04, and Arduino SoftwareSerial

In this post I will detail how I used an ATtiny85 as a controller for an HC-SR04 sonar module (Ping sensor). The controller reads the inputs from up to 3 HC-SR04 modules and transmits the readings to an Arduino Mega via serial communication. This allows the Arduino Mega to pick up the readings whenever it is convenient rather than having to worry about complex timings. Let's get started


First things first, we need to run the NewPing library on an ATtiny85/45/25. Until version 1.6 is released you will need to modify the library to do this. However, this removes some of the functions you might want to use on other boards. For this reason, I created the TinyNewPing Library. In it, I removed the parts that cause the errors on an ATtiny and gave it a new name so you can distinguish between the two. All the function names remain the same. If you still need more details check out THIS prior post.

Next we need the SoftwareSerial Library on the ATtiny. Luckily, this comes with the IDE and I already did a post on it. Funny how that works.

For a first attempt, try a single HC-SR04 sonar sensor. This simplifies the code a bit. Load THIS code on the ATtiny (sensor side) and THIS code on the Arduino Mega. Note the code uses both Serial and Serial1 (thus the Arduino Mega).

To wire it up, connect the echo and trigger pin together and then connect them to pin 0. Wire in 5V and ground to both the ATtiny and the HC-SR04. Then connect pin 4 (Software Tx) on the ATtiny to pin 19 (Rx1) on the Mega. Finally, plug the Mega into the computer via USB.

Now if this is working ok, we can move on to 3 sensors. For that, we need to load THIS code onto the ATtiny and THIS code onto the Arduino Mega. The ATtiny takes the readings and sends them to the Mega. The Mega updates it's variables and sends them to the Serial Monitor for us to see.

Wiring it is cumbersome but simple. Regrettably, I did not take a picture when I did it, but just do what you did before an extra two times. Note again that we are using the same pin for trigger and echo, so they need to be wired together. Then wire up power and grounds. Finally connect the ATtiny to the Mega and the Mega to the computer.

Now open the Serial Monitor and watch the numbers scroll by.

Potential problems: If the ATtiny gets out of sync with the Mega (it will), an occasional bad value will come up. I couldn't come up with a quick fix for this. If that is a problem, maybe you could take the median of a few readings. They are coming in pretty quickly. The plan is to make an I2C version of this soon, but I make no promises on a deadline. One last download, get the entire package of sketches used in this post in a zip file HERE.

A few quick notes before we go.

  • Why not I2C? That's next (Done! Check it out HERE). Serial is easier because it doesn't require a 3rd party library to work on the ATtiny line. 
  • Why only 3 sensors? Short answer, ease of use. You might be able to get 4 or even 5 out of an ATtiny45 if you're persistant. I don't have that many sensors nor that much patience. You have 5 regular IO pins plus the reset pin (which can be used as an IO). You might be able to use the serial Rx pin as an input as well. 
  • Why not just buy a I2C ping sensor and be done with it? Well this reduces the number of wires going to the Arduino Mega. It also gives you more options and increases the number of micro controllers on board your robot (cool factor). Other than that, it's cheap. With HobbyKing or Ebay, this project could be done for less than the price of one I2C sonar sensors.
Hopefully someone will find this useful. If anything goes great, please comment! If anything goes wrong, contact your local internet service provider. I blame them (That's a joke. You can comment below as well).

-Matthew

Friday, July 19, 2013

Whirly-Gig: Charlieplexing and SoftwareServo on ATtiny45

I recently completed a project involving an ATtiny45 and wanted to share it with the world. The idea came from the world of FIRST robotics. At competitions some teams have spinning banners ("whirly-gigs") above their pits to attract scouters, judges, and anyone else who might be interested. I had a friend who saw it and told me to make one for her birthday. Sounds like fun! Let's get started.

Hardware involved:

  • 1: ATtiny45
  • 1: HXT900 Servo (modified)
  • 1: Momentary Switch
  • 1: 10k Potentiometer
  • 1: 5V Voltage Regulator
  • 1: DC Power Jack
  • 1: 2 Way Switch
  • 1: 3 Battery AA Holder
  • 6: 5mm Colored LEDs
  • 3: 270 Ohm resistors
  • Wire
Software Involved
First, the ATtiny core. I have done several posts involving ATtinys in the past. Follow the instructions from the link above to use an ATtiny with the Arduino IDE.

Second, the SoftwareServo Library on the ATtiny. I have already done a post on controlling servos with an ATtiny. Find it HERE. Basically, the regular Servo library will not work because it doesn't have the necessary timer, so SoftwareServo must be used.

Next we have the Charlieplex Library. It takes care of setting the pins correctly to turn on the LEDs we will be using. I have done a post on this library as well. Find it HERE.

Last we come to my sketch. You can get it HERE. It may be a bit convoluted , so I tried to comment it to be understandable. The essence is this, there are 10 different LED modes that the user switches between by pushing a button. At the same time the servo which has been modified for continuous rotation as discussed HERE is controlled by turning a 10k pot.

I made a nice wooden base for the electronics and mounted the servo on 2 wooden uprights. The sign itself is made of dowel rods and home insulation foam cut into circles. I made it such that an 8.5 x 11 sheet of paper will fit on it when rolled horizontally.

I soldered together all the electronics. I put a 270 Ohm resistor on each LED pin. I also put a 10 uF electrolytic capacitor across the power leads and a diode protecting against unwary users plugging in power backwards, but those probably aren't necessary.

I did run into one problem in the course of this process. The servo would stop working when I set a certain LED pin LOW. I never did get to the bottom of it, but it is discussed in THIS forum thread. I ended up changing the pins I was using, and it solved the problem. I included a diagram of my pinout in the code posted above.

That's about all the details I am going to give you. The whole project took quite a while, and it's a DIY project. I'm not selling kits. If you want, you can view my project photos HERE, and I took a nice video for you to look at. I hope I inspired you to greatness.



-Matthew