Float/hi-z A21 and A22 input after analogWrite on T3.6

Status
Not open for further replies.

Delventum

Member
Hello.

I have a Teensy 3.6 and I am using Arduino 1.8.8 and Teensyduino 1.45.

I was wondering whether it is possible to change pins A21 and A22 to be high-impedance (floating) after having used them with analogWrite (which obviously causes the pins to be driven to a specific voltage)?

I ran the following test code:
Code:
void setup()
{
    analogWrite(A22, 128);
    analogRead(A22);
}

void loop()
{
}

I then verified that A21 (or any pin other than A22) was floating by measing 0V between the pin and GND as well as measuring 0V between the pin and 3.3V. I then measured pin A22 and I get 1.65V between the pin and GND, as well as 1.65V between the pin and 3.3V. This means that it is still being driven, even though it has been used with analogRead() after the analogWrite() call.

To ensure nothing was being optimized away or there was timing/race issue I changed to the code to:
Code:
void setup()
{
    Serial.begin(0);
    analogWrite(A22, 128);
    delay(1000);
    int val;
    val = analogRead(A22);
    delay(1000);
    Serial.printf("A22 value: %i\r\n", val);
}

void loop()
{
}
and compiled with the "Optimize: Debug" option. The serial output was "A22 value: 513" and the pin remains at around 1.65V.

I searched the forum and the internet and didn't find answers to this. I know I can't use pinMode() with these pins. I took a look at the source code for analogRead (line 438 in analog.c) and I didn't see an obvious place where the pins would be "muxed back" from the DAC function which analogWrite uses, although I'm out of my depth there since I haven't looked at the MCU's datasheet.

Any help, thoughts, ideas, suggestions, workarounds welcomed.
 
An analogWrite() will leave the pin connected to the DAC output (to maintain the output signal). An analogRead() on the same pin will disconnect the pin from the DAC and connect it to the ADC signal multiplexer which is also not true hi-Z.
I’d try to disable the DAC module to free up the pin after analogWrite() by writing 0 to the DACx-C0 register.
 
I’d try to disable the DAC module to free up the pin after analogWrite() by writing 0 to the DACx-C0 register.

Thanks for the pointer! Disabling the DAC by writing zero to the DACx-C0 register seems to release the pins so that they are free to float, which is just what I needed. In case somebody is interested, this is the syntax:

Code:
void setup()
{
    analogWrite(A21, 128);
    DAC0_C0 = 0; // Disable DAC on A21
    
    analogWrite(A22, 65);
    DAC1_C0 = 0; // Disable DAC on A22
}

void loop()
{
}
 
Glad to see that it works for you! But I can't still understand why you would output a voltage, just to kill it a few CPU cycles later...
 
Status
Not open for further replies.
Back
Top