Let's Make Robots!

HC-SR04 and Instant Robot Shield w/ Arduino

Hi, I'm following along in the start here but doing everything in Arduino. I have a Arduino Uno3 with the Instant Robot Shield setup. Everything is working fine (Motors, servo, etc) EXCEPT the Ultrasonic sensor. I don't know if using this sensor makes a difference, because the labeling on the IRS says SR05F. I have connected the wires to the correct pins based on the SRF05, leaving out the mode pin. 

I tried setting the Trig to pin 13 and Echo to 12 in the Arduino code, but nothing is returning in the serial monitor. I'm lost on what pins to use in my code, or whether or not I can hook up the Ultrasonic sensor like this.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Here is some code to test your HC-SR04......

It uses no librarys and it does not matter if it is a PING))) with one wire...or the HC-SR04 with two....it will figure it out for you...

 

// trigger pin for head 1
const int trigA = 2;

// echo pin for head 1
const int echoA = 3;

// button pin
const int button = 6;

void setup()
{
    Serial.begin(9600);
    while (!Serial);
    Serial.println("Distance Measurer");
   
    // initialize the triggers to outputs
    pinMode(trigA, OUTPUT);
   
   
    // initialize the button to an input
    pinMode(button, INPUT);
   
    // if the echo pins are different from the trigger pins, set them as inputs.
    if (echoA != trigA)
    {
        pinMode(echoA, INPUT);
    }
 }

long ping(int trig, int echo)
{
    // This allows this to be used with 1-wire or 2-wire sensors.
    // If the trigger and echo pins are the same, change the mode.
    if (trig == echo)
    {
        pinMode(trig, OUTPUT);
    }
   
    // bring the pin low to give the trigger a clear pulse
    digitalWrite(trig, LOW);
    delayMicroseconds(2);
   
    // bring the trigger pin high for 10us
    digitalWrite(trig, HIGH);
    delayMicroseconds(10);
   
    // then set it low again
    digitalWrite(trig, LOW);
   
    // If the trigger and echo pins are the same, change the mode.
    if (trig == echo)
    {
        pinMode(echo, INPUT);
    }
   
    // return the pulse length from the echo pin
    return pulseIn(echo, 100000);
}

// Convert microseconds to CM
float usToCM(long us)
{
    return (us / 58.0);
}

// Convert microseconds to inches
float usToInch(long us)
{
    return (us / 148.0);
}

void loop()
{
    int flag = 0;
    long usA;
       
    // When button is pressed
    if (digitalRead(button))
    {
        // set the flag
        flag = 1;
       
        // and wait for the button to be released
        while (digitalRead(button)) delay(1);
    }
   
    // if the flag is set
    if (flag)
    {
        // read from the first head
        usA = ping(trigA, echoA);
       
        // wait 50ms to allow echo to dissipate
        delay(50);
       
       
        // Print the results
        Serial.print("Ping A: ");
        Serial.print(usA);
        Serial.print(" us, ");
        Serial.print(usToCM(usA));
        Serial.print(" cm, ");
        Serial.print(usToInch(usA));
        Serial.println(" in");
       
       
    }
}

a video about connecting and getting readings from an ultrasonic sensor.

Go look:
http://letsmakerobots.com/node/35581

Birdmun speaks the truth. It goes through how they work, how to wire them up and how to program them. A bonus is at the end I show you how to make a little thing to test it.

Ok, I read the manual and it seems that the shield you are using has a designated spot for the SRF-04. It also appears that this connection puts the SRF-04 into "single pin" mode, working more like a Ping))) sensor. Your HC-SRF04 will work, but you are going to have to fudge some stuff around. --Oh, and I can tell you that 12 and 13 are not the correct pins to use --they go to a darlington chip.

 

I would start by forgetting about the whole robot for a sec. Instead, I would do a search for "HC-SR04 Arduino" and simply follow a tutorial to get just your sensor working. Doing this will teach you how to connect it (and to what pins) and how to work with those pins via your code. Once you have gotten your sensor to work all by itself, you will quickly be able to find the things that need to be changed in your main code.

That's my advice, my friend. Just get the sensor working by itself, and all will become clear.

In theory, the HC-SR04 and the SRF-04/05 should be interchangable --If the SRF-04 is being used in the 4-pin mode (trigger/ echo). Let's assume it is.

You have your trigger and echo on your sensor and you have them connected to pin 12 and 13 on your board. This will work just fine if your code also reflects these pins. Does it?

Do the trig/echo pins in your code match the connections on the board, and then your sensor?

Well, the pins on the Instant Robot Shield for the SRF05 do not have numbers labeled so it's kind of a hit and miss for guessing the correct pins. I have no idea what pins on the SRF05 correspond to on the Arduino board.

 

I have the VCC (+) into the first pin, Trig second, Echo third, and ground on the fifth on the shield. I just assumed it would be pin 12/13 based on the PICAXE tutorial, but it didn't seem to work. So maybe the pins on the shield aren't pin 12/13 on the arduino board?

Ok, I dove back in the manual and this time, scrolled down far enough to find what I needed. Its pin 5. Pin 5 is connected to the SRF-04 / Ping connection on the edge of your shield. And no, its not "hit and miss, guessing", its opening the manual and finding the information one needs.

Ok, it looks like you can use these SRF-04 connections on the edge of your sheild, but you are going to have pick up an extra digital pin. (the SRF-04 connector only uses one connection. You simply need to use another digital pin to go to your sensor. It does not matter if the SRF-04 connection is the trig or this new pin is trig, just as long as these pin numbers are reflected in your code.

Also, could you give me a link to the particular picaxe tutorial you are following? Is it the Start Here v2?

Start here v3

 

http://letsmakerobots.com/node/33757

 

edit: I tried pin 5 as well. Forgot to mention that.

Ok, you have a sensor that is wired differently than the one used in the tutorial. You will need to deviate from the tutorial a bit here to get your sensor working. In addition, the code used in the tutorial may have to be changed. Here's what you need to do:

You need to find a tutorial for an Ardunio and a HC-SR04. I would search, "Arduino HC-SR04". I would also sorta "forget" that you are using this picaxe shield. At the end of the day here, we are connecting a HC-SR04 to an arduino. That's it. It does not matter what pins are used, as long as they correspond in terms of what is connected in real life and what is reflected via code. 

To get your sensor to work with this tutorial, you will need to wire it differently and also change some code. The only way to know what to wire and what to change is to know how your sensor works. This is why I suggest a "stand-alone" test of the sensor using a "stand-alone" tutorial. Do this, familiarize yourself with the code and connections, then go back to the Start Here tutorial. You will now have a much better shot at knowing where to change what.

 

--Note:  There is a physical modification you could do to your sensor (others have done it successfully), but I refrain from suggesting it here --I think it will just serve to confuse things further. Again, just find a HC-SR04 tutorial and you will be all set.

After a little experimentation, it seems the Instant Robot Shield isn't even providing power to the ultrasonic sensor pins, therefore it has nothing to do with the coding.