Saturday, April 6, 2013

NewPing library on ATtiny

Well now we have come to the point where I need to address Problem 3. If you don't remember that one, it was mentioned back a few post ago.

We need to run the NewPing library (for my HC-SR04) on the ATtiny85. First things first, we will need some code to test it with so we know it is working. It doesn't have a convenient serial interface like the Mega. Enter NewPingLED.

Its purpose is to turn on an led when an object gets within 20cm of the sensor. This is something that the ATtiny85 can manage.

Next, I got it running on my Mega. Not too difficult. I did note that this sensor was a lot less accurate (and precise) than the other ones I got. Sometimes you get what you pay for. It was like $2.50 on Ebay.

I posted a video in case you don't believe me. Obviously, the Mega can also run any of the example programs that come with NewPing 1.5 as well.

Now on to the ATtiny. Well the code doesn't compile right out of the box. The problem is with some of the timers that the ATtiny doesn't have. This is discussed HERE. It sounds like a fix will be coming up shortly in version 1.6, but in the meantime you just have to comment out the stuff about the timers. If you want the library with that stuff pre-edited out, its HERE. Note that you won't be able to use those parts of the code on any other board until you change it back. Hopefully 1.6 isn't far away.

Now that we have that out of the way, we just need to change the pins to something usable, get rid of the serial stuff, and wire it up. I'm going to assume you can handle that (remember the resistor on the LED!). If you are having trouble with basic wiring and such, check out Jeremy Blum's stuff HERE.

The new code is HERE. As you can see, it works well. It's no miracle of science, but it's a step toward doing what I want to do. Just for fun I also did a Fade program. Next, I think it would be cool to do some basic communication with my Mega. Sending Ping values back and such. I also have some other projects on my mind though.

In case you didn't know, next week is National Robotics Week. That's sure to be a blast. Also, in the world of FIRST robotics, nationals are coming up. Be sure to check out the webcast HERE. There are sure to be some awesome Frisbee shooters there.

That's all I have for now. Check in again,


  1. This comment has been removed by the author.

  2. Hi Matthew,

    great project! Got it working on my ATtiny85. :)

  3. Excellent Matthew, especially with the links to reference material, and sharing how you went about it. Just want I was looking for. Saves me some time.

  4. Thanks a lot! This really helped. Thanks again.

  5. Any ideas on why i get only 0s and 1s as distances?!