Analog read a component, and digital writing to the same component

I'm working with a ceramic ultrasonic transducer. In case you're not familiar:


I'm essentially sending a signal to the piezoelectric transducer. I start a timer. The ultrasonic wave bounces off a barrier then comes back, and my analog read pin goes to an expected value from the wave hitting the sensor. The timer is stopped, yielding the time of flight.

My question is: can I have an analog input pin connected to the same wire as a digital output pin?

Would switching the digital output to digital input after it sends the signal fix my problem (or is there a better way)?

Yes, but you don't need to. You can make a pin a digital output and then switch it to a analog input afterwards :slight_smile:

But keep in mind the ADC is nu mega fast. So continuously reading it probably gives a significant error. To make it faster I would use an external comperator. And if you need to alter the level of the comperator you can use a PWM pin with a RC-filter as the other reference :slight_smile:

Wait, an analog input can also be a digital output?

By the way, I'm using the Arduino Due, I don't know if that impacts the speed of the ADC by running at 84 MHz. And I'm looking for an accuracy of 1 us.

Thanks for your quick reply!

I'm essentially sending a signal to the piezoelectric transducer.

What is that signal?? Typically an ultrasonic transducer has a resonant frequency, typically 40 Khz, and the sending signal is that frequency.

The received signal is then also that 'ultrasonic' frequency.

You can not read a signal of that frequency directly with an Arduino Analog input.

Typically even a low-cost sensor board like THIS: has a lot of hardwired electronics to successfully receive the bounced signal.

I think you may want to rethink this...

Yes, analog is just an extra function of that pin. Just like PWM is an extra function :wink: And it’s all under software control.

That will be faster but it can’t beat the speed of a digitalRead or, even better, a interrupt. So your resolution will be limited. And 1us is pretty fast. For that you’ll have to look up the specs of the ADC in the Due and the overhead that’s done when reading the ADC. Or if you just want to (roughly) now it check the time before and after a analogRead.

Ow, and I don’t know how it’s done on a Due but you might want to look at the resolution of micro if you want to use that. On a Uno for example the minimal resolution is 4us.

And further more I have to agree with terryking228, distance measurement with sound is not THAT easy.

terryking228:
The received signal is then also that 'ultrasonic' frequency.

Sorry, I wasn't quite clear.

First off: the resonant frequency is 1 MHz.

What I meant is that I'm sending a quick pulse. I don't really need to verify that the signal I'm receiving is of the correct frequency, just that I received a pulse back. In essence, it is stopping the timer the moment I receive the crest of a short pulse back.

septillion:
Yes, analog is just an extra function of that pin. Just like PWM is an extra function

Thanks, that’s great to know.

septillion:
That will be faster but it can’t beat the speed of a digitalRead or, even better, a interrupt

I’m not sure the intensity of the rebounded pulse will be able to induce a digital HIGH, but it’s worth a shot.
And I’ll have to look into analog interrupts if that doesn’t work.

It looks like the sample rate of the Due’s ADC is 1 MHz, but not entirely sure on that yet.

That's why I said to use a comperator. External or internal (don't know if the Due has one but I guess it does).

septillion:
That's why I said to use a comperator. External or internal (don't know if the Due has one but I guess it does).

I think I will be going that path. I've found that its closer to 500 KHz, so an external comparator with a digital interrupt will be the fastest path.

Thanks for all your help!

You haven’t posted the datasheet of your ultrasonic transducer - we know almost nothing about it.

Come to that we know nothing about what you are trying to achieve - typically a high voltage
oscillator is needed to drive such a transducer and a high-gain band-limited amplifier to pick up the
response (which might be 100dB down).

Hi,
Did you look at this site?
http://arduino-info.wikispaces.com/UltraSonicDistance

And consider using one of these?

And there are numerous bits of code around that do what you want to do, unfortunately not ar 1MHz.

What is the application, what are you measuring and where?

Thanks.. Tom.. :slight_smile: