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
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
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.
Yes, analog is just an extra function of that pin. Just like PWM is an extra function 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.
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).