Issue operating ultrasonic sensor on ATtiny2313a (Solved)

I’m having an issue getting the HC-SR04 Ultrasonic sensor functioning on an ATtiny2313a-PU.

Background:
I was trying to build this: http://www.instructables.com/id/ArtooR2-ATtiny2313-Wall-Avoiding-Robot/?ALLSTEPS
The core I’m using: https://code.google.com/p/arduino-tiny/ - the board I select is ATtiny2313 @ 1MHz
The ultrasonic library: http://stigern.net/forum/download/file.php?id=6&sid=6424f49bd53bbe87bfeb93fb0d827b6c - I also tried NewPing, but am having trouble getting it going.

I have identified the issue to be the ultrasonic sensor not functioning when connected to the ATtiny. The sensor, when connected to the Arduino Uno, works fine - when you bring your ear close to the sensor you can hear the ‘pings’ (clicking sound) happening at a very rapid rate.

However, when connected to the ATtiny, you can only hear a ping about once every second. i.e. the ATtiny is not operating the sensor and therefore not measuring the range. From the code below, only the LED on pin 7 remains on.

I have tested the ATtiny’s pins, all of which function correctly when lighting LEDs in a set frequency and duration. I have also tried getting this going with three separate sensors, three separate ATtinys, and 20 different pin combinations, but no luck.

It would be great if any of you had any ideas as to how I could get this working. Thanks in advance.

#include <Ultrasonic.h>
int Echo=11;
int Trig=10;
int Range;
int Dist;
Ultrasonic ultrasonic(Trig,Echo);

void setup () {
  pinMode(9, OUTPUT);
  pinMode(7, OUTPUT);
  Dist=15;
}

void loop() {
  Range = ultrasonic.Ranging(CM); // Range is calculated in Inches.
  if (Range > Dist){
  digitalWrite(9, HIGH);
  digitalWrite(7, LOW);
  } else{
  digitalWrite(9, LOW);
  digitalWrite(7, HIGH);
  }
  delay(20);
}

Arduinos are not generally run at 1 MHz so perhaps the ultrasonic library is not designed to handle that possibility? Someone will have to examine the source to find out.

Try selecting Tools->Boards-> "ATtiny2313 @ 8MHz" and Tools->Burn Bootloader to set the fuses to use the internal 8 MHz clock. That will be more like a real Arduino and more likely to work.

Edit: I looked at the ultrasonic library source and it should work at 1 MHz. The resolution at that speed is a little better than 1/2 cm.

The symptom you describe (one pulse per second) sounds like a timeout on the pulseIn(). Are you sure you have Echo connected to the right pin? Remember that physical pin number (on the chip) is seldom (if ever) the same as the Arduino pin number.

D7 -> Port D bit 5 -> physical pin 9
D8 -> Port D bit 6 -> physical pin 11
D10 -> Port B bit 1 -> physical pin 13
D11 -> Port B bit 2 -> physical pin 14

johnwasser:
Try selecting Tools->Boards-> "ATtiny2313 @ 8MHz" and Tools->Burn Bootloader to set the fuses to use the internal 8 MHz clock. That will be more like a real Arduino and more likely to work.

This solved it! Thanks so much John, I've spent a couple of weeks trying to make this work, you just made me super happy!

Now I can continue with my first Arduino project :wink: