Plug Ultrasonic Sensor HC-SR04 on sensor shield v2 mega

Hi

I have this problem, i try to connect this sensor on the mega shield, but this sensor use 4 pins (gnd, vcc, trig, echo) and the shield, the pins are grouped of 3 to 3,and y try connect only gnd, écho and vcc of pin 48 and trig in pin 49 but i got.:
0
0
0
......
Any solution of how i can to connect?

My code is here

Code: [Select]

#include <Servo.h>
#include "SR04.h"

Servo myservo1;  // crea el objeto servo
Servo myservo2;  // crea el objeto servo
Servo myservo3;  // crea el objeto servo
char numero = 0;
int pos = 0;
int bucle = 0;
char php = 0;
#define TRIG_PIN 49
#define ECHO_PIN 48
SR04 sr04 = SR04(ECHO_PIN, TRIG_PIN);
long a;



void setup()
{
  Serial.begin(9600);
  delay(1000);
  myservo1.attach(9);  // vincula el servo al pin digital 9
  myservo2.attach(11);  // vincula el servo al pin digital 9
  myservo3.attach(22);  // vincula el servo al pin digital 9

}


void loop()
{

  a = sr04.Distance();
    Serial.print(a);
    Serial.println("cm");
    delay(1000);
   
  if (Serial.available() > 0)
  {
    numero = Serial.read();
    //Serial.print(numero);

  }

  if (numero == '1')
  {
   
    for (pos = 90; pos <= 150; pos += 1)
    {

      myservo1.write(pos);
      delay(15);
    }
    delay(1000);
    if (a < 30) {

      for (pos = 150; pos >= 90; pos -= 1)
      {

        myservo1.write(pos);
        delay(15);
      }

    }

  }
  if (numero == '2')
  {
    for (pos = 90; pos <= 150; pos += 1)
    {

      myservo2.write(pos);
      delay(15);
    }
    delay(1000);
    for (pos = 150; pos >= 90; pos -= 1)
    {

      myservo2.write(pos);
      delay(15);
    }
  }
  if (numero == '3')
  {
    for (pos = 90; pos <= 150; pos += 1)
    {

      myservo3.write(pos);
      delay(15);
    }
    delay(1000);
    for (pos = 150; pos >= 90; pos -= 1)
    {

      myservo3.write(pos);
      delay(15);
    }
  }

  numero = '\0';

}

And the image of my board

Thanks

Maybe the sensor must be connected on PWM pins.
Try it again on any of this pins: 2 to 13, or 44 to 46 (especially pin 4 and 13).

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.