10 years, 3 months ago.

AnalogIn and DigitalOut on same pin?

Hi all, With mbed, it is possible to use one pin like analog in and digital out or analog out? Here is my situation: i need to read a analog input when it is finish i need to set it to 0 V to reset my capacitor, and wait in input mode or even in intrruptIn mode. Some advice will be appreciated. Thank you Vilounha

3 Answers

10 years, 3 months ago.

I think something like this should work:

while(1) {
  DigitalOut out(pin);
  out = 0;
  wait(1);
  AnalogIn in(pin);
}

With new and delete you can also make them dynamically. Finally if speed is an issue, I think it might be faster if you connect two pins, and use one as AnalogIn, the other one as DigitalInOut, and switch that one between input and output.

10 years, 3 months ago.

Since the constructor of the DigitalOut or AnalogIn is responsible for changing the pin mode, you could do this more safely than the prior answer:

changing pin modes

{
   AnalogIn ain(pin);
   // do stuff with ain (just an example):
   while (ain.read() > 0.001)
       wait(0.001);
}
{
   DigitalOut dout(pin);
   dout.write(0);
}

10 years, 3 months ago.

Thank you for examples. It is good for me.

Here is my second question:

Can i use in the same pin with InterruptIn for example when i have interrrupt from this pin i can read the value from and then set it back to 0V and waiting for another interrupt ??? Thank again. Vilounha