How to used analog pin setting the ping sensor?

I try to setting my Quadcopter,

but my arduino Quadcopter all sensor used analog pin,

i try to setting the Ultrasonic sensors but i don't know what to do,

i want to find any Ultrasonic sensors example used analog pin, but i can't find any,

please anyone can help me

The ultrasonic sensor sends a pulse, and receives an echo. The Arduino measures the time it takes for the echo pin to go high. The pin used to send the pulse just needs to be toggled on then off, after a tiny delay. The pin that receives the echo is set HIGH when the echo arrives. Any digital pin can be used for the pulse pin and any digital pin can be used for the echo pin.

Any analog pin can be used as a digital pin, too. On most Arduinos, anyway.

thanks you for your reply,

i know how to used digital pin,

but used analog pin..... difficult to me :cry:

i learn all way to used digital pin, but analog pin i don't have any chance to use it,

so i think if has a easy test program maybe i can understand

Just use the analogue pin as if it were a digital pin

pinMode(A0, INPUT);
digitalWrite(A0, HIGH);
byte x = digitalRead(A0);
//etc

I try to do the easy program,

like this:

const int trig = A0;
const int echo = A1;
const int inter_time = 1000;
int time = 0;

void setup() {
Serial.begin(9600);
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
}

void loop() {
float duration, distance;
analogWrite(trig, HIGH);
delayMicroseconds(1000);
analogWrite(trig, LOW);
byte x =analogRead(echo);
duration = pulseIn (x, HIGH);
distance = (duration/2)/29;
Serial.print("Data:");
Serial.print (time/1000);
Serial.print(", d = ");
Serial.print(distance);
Serial.println(" cm");
time = time + inter_time;
delay(inter_time);
}

i run the program just print 0.00 0.00 0.00....... over and over again,
what i wrong???

 float duration

  duration = pulseIn (x, HIGH);

The pulseIn() function does NOT return a float.

The pulseIn() function takes, as the first argument, a pin number. Why you are waiting for pin 0 or pin 1 to go high, when the sensor is connected to pin A1, is a real mystery.

Why you are trying to PWM the analog pins is a mystery.

The ONLY thing that you should have changed in the ping example is the pin numbers.

my Quadcopter all pin each have their own function,

so sensor pin is regular,

I can't change another pin~~

analog pin make me headache :confused:

thank you for your reply

analog pin make me headache

Just forget that A0 - A5 have anything to do with analogue input and use them as you would any other digital pin.

const int TRIGGER_PIN = A0;
const int ECHO_PIN = A1;
const int inter_time = 1000;

void setup() {
  Serial.begin(9600);
  pinMode (TRIGGER_PIN, OUTPUT);
  pinMode (ECHO_PIN, INPUT);
}

void loop() {
  unsigned long duration;
  float distance;
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TRIGGER_PIN, LOW);
  duration = pulseIn (ECHO_PIN, HIGH);
  distance = (duration / 2.0) / 29.0;
  Serial.print("Data:");
  Serial.print (millis() / 1000.0);
  Serial.print(", d = ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(inter_time);
}

johnwasser:

const int TRIGGER_PIN = A0;

const int ECHO_PIN = A1;
const int inter_time = 1000;

void setup() {
  Serial.begin(9600);
  pinMode (TRIGGER_PIN, OUTPUT);
  pinMode (ECHO_PIN, INPUT);
}

void loop() {
  unsigned long duration;
  float distance;
  digitalWrite(TRIGGER_PIN, HIGH);
  delayMicroseconds(1000);
  digitalWrite(TRIGGER_PIN, LOW);
  duration = pulseIn (ECHO_PIN, HIGH);
  distance = (duration / 2.0) / 29.0;
  Serial.print("Data:");
  Serial.print (millis() / 1000.0);
  Serial.print(", d = ");
  Serial.print(distance);
  Serial.println(" cm");
  delay(inter_time);
}

thank you a lot, but my quadcopter can't work, just run analog

thank you a lot, but my quadcopter can't work, just run analog

What does that mean?

wangbuy26:
I try to setting my Quadcopter, but my arduino Quadcopter all sensor used analog pin,

What Arduino is your Quadcopter using?
What sketch is running on that Arduino?

johnwasser:
What Arduino is your Quadcopter using?
What sketch is running on that Arduino?

used arduino nano but Quadcopter has Expansion Board,

Expansion Board give Ultrasonic sensors echo trig a0 a1 pin,

so i can't change another digital pin~

I used this Expansion Board:

i ask official but they just give me two little prompt,

so i want to know all about Ultrasonic sensors program architecture for analog pin.

so i can't change another digital pin~

Have you actually read the advice in this thread about how to use analogue pins as digital pins ?

You have answered the hardware question. The Arduino is an Arduino Nano. You have also shown pictures of an "Expansion Board" that appears to be designed for a two-wheel robot.

There was a second question that you did not answer:

johnwasser:
What sketch is running on that Arduino?

Please answer that question.

UKHeliBob:
Have you actually read the advice in this thread about how to use analogue pins as digital pins ?

ok, i trying to use 14(A0) be Digital output A1 used analog input,

thanke you for your reply

johnwasser:
You have answered the hardware question. The Arduino is an Arduino Nano. You have also shown pictures of an "Expansion Board" that appears to be designed for a two-wheel robot.

There was a second question that you did not answer:Please answer that question.

official has give me Quadcopter code,

so i just need to make the Ultrasonic sensors work,

final i want to make Quadcopter flying and automatic obstacle avoidance,

but now i just want Ultrasonic sensors work on Quadcopter,

than i can design after code~

thanks all,

my teacher find problem,

usb can't give power to Ultrasonic sensors,

so put on the battery than all work,

thanks

I have successfully used 2 digital pins to drive servo motors for my quadroped spider robot and connected those pins to my HC-SR04 sonar as well. However, you have to change the echo pin back to an output state before you return from the sonar function, if that digital pin is being used as an output.