Teensy LC DAC only outputting 0-1.5V?

Status
Not open for further replies.

gatheround

Well-known member
I'm have a strange issue with using the DAC on a Teensy LC. This code works fine on a Teensy 3.2, so I'm a bit confused.. maybe I'm missing something obvious?

The code basically reads the ADC value at A9 (at 16 bits) and then outputs the reading as 12 bits to the DAC pin. It works fine from 0 to 1.5V and then just holds 1.5V as the readings from A9 continue to increase.


Code:
#include <ResponsiveAnalogRead.h>
ResponsiveAnalogRead ring(A9, false);
uint8_t dacpin = A12; // pin for DAC (A14 on Teensy 3.2, A12 on Teensy LC)

void setup()
{
    Serial.begin(115200);      // start Serial connection for debug console
    delay(2000);               // chill for a bit

    analogWriteResolution(12); // DAC
    pinMode(dacpin, OUTPUT);   // DAC
    analogWrite(dacpin, 0);    // set DAC to 0V.

    ring.setAnalogResolution(65536);

    analogReadAveraging(16); // set analog read averaging (1 - 32) 16 tested really good here.
    analogReadRes(16);       // 0 - 65535
}

void loop()
{
    ring.update();
    dopitchfreeze();
}

void dopitchfreeze(void)
{
    analogWrite(dacpin, ring.getValue() >> 4);

    Serial.print("Dac to ");
    Serial.print(ring.getValue() >> 4);
    Serial.print("\n");
}

I'm getting:

Code:
Dac to 625 (.5V)
Dac to 1254 (.995V)
Dac to 1634 (1.3V) 
Dac to 1906 (1.475V)
Dac to 2360 (1.480V)
Dac to 3193 (1.480V)
 
The issue is this line:
Code:
pinMode(dacpin, OUTPUT);   // DAC
Just delete that line and the DAC outputs the full voltage range. See this post for details.

Regards,
Paul
 
Not sure this line is needed or helping :: pinMode(dacpin, OUTPUT); // DAC

Try adding this line in setup() : analogReference(EXTERNAL);
 
The issue is this line:
Code:
pinMode(dacpin, OUTPUT);   // DAC
Just delete that line and the DAC outputs the full voltage range. See this post for details.

Regards,
Paul

That worked thanks Paul. Should this line ever be used to configure a DAC on any teensy or should it be removed even when a Teensy 3.2 is the target?
 
AFAIK :: Setting pinMode() is needed for DIGITAL usage. When used as an ANALOG the I/O functions prepare the pin as needed to function, and doing pinMode() on them only interferes.

PaulS got the answer faster because it seemed it might be an analogReference() issue - and went looking for that (reading the thread title) before reading the code more closely and seeing pinMode()
 
Status
Not open for further replies.
Back
Top