How to use the DAC of Teensy ?

While there are many examples to use the PWM to simulate analog output, there is not any example to use the DAC of MCUs which come inbuilt with them like the Teensy.

Any help apprecaiated. Thanks.

I hate to be Captain Obvious here, but you're probably going to have much more success asking on the Teensy forums.

EDIT: or even just do a quick Google search.

I count 11 ADC example sketches in the Arduino IDE after you install Teensyduino. If you want to dig down into the low-level details, open up the ADC library.

gfvalvo: I count 11 ADC example sketches in the Arduino IDE after you install Teensyduino. If you want to dig down into the low-level details, open up the ADC library.

DAC, not ADC.

BJHenry: DAC, not ADC.

Dooohhhh.... my bad.

gfvalvo: Dooohhhh.... my bad.

So, I stand corrected, I don't immediately see any examples in the IDE. I'd start by looking at the code for the 'AudioOutputAnalog' class in the Teensy Audio library. It helps to have the device datasheet open at the same time so you can look up registers used.

https://forum.pjrc.com/threads/48188-Teensy-3-5-How-to-write-data-to-DAC-pins-easily

demkat1: https://forum.pjrc.com/threads/48188-Teensy-3-5-How-to-write-data-to-DAC-pins-easily

Good … can you do me a favour ? Just copy paste that post you referred.

Now before you get VERY angry let me explain : The PJRC forum has blocked IP addresses from some countries like India / Pakistan / China. I happen to be in one of them. Reason ? Understand the ISP in these countries send lot of spam. I am not guessing … this is exactly what Mr Paul Stroffgen said in a mail to me :-(

And this also answers the obvious fact as to why I did not ( I could not ) post in the Teensy forum :-|

That is actually a very good reason. Here's the best post from that thread:

You use the analogWrite function. If you are writing to a DAC pin (A21/A22 on the Teensy 3.5/3.6, A14 on the Teensy 3.1/3.2, or A12 on the Teensy LC), it does a real analog write (i.e. adjusts the voltage of the pin). You might need an amplifier on the pin(s) before connecting it to a speaker.

Due to poor naming of the function from original Arduino systems, analogWrite on a digital pin that supports PWM, rapidly turns the pin on/off, so that it approximates an analog signal (if you are looking at a LED, it will appear to dim the LED, as the light turns on/off faster than the eye can see). Typically you would need a low pass filter if you want a PWM output to better approximate an analog signal.

And yes, IMHO, the original Arduino should have called the second function something like pwmWrite, but they didn't.

It seems like it should be quite easy to use the DAC :)

BJHenry: That is actually a very good reason. Here's the best post from that thread:It seems like it should be quite easy to use the DAC :)

Yes thought so .. me too !! Tried the following and the A14 on the Teensy3.2 was reading 3.3 Always even though the LED was blinking :-(

const int ledPin = 13;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  pinMode(A14, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);   
  analogWrite(A14, 0xfff);
  delay(2000);                 
  digitalWrite(ledPin, LOW);    
  analogWrite(A14, 0x7ff);
  delay(2000);                  

}

OK this works as expected !!

const int ledPin = 13;

void setup() 
{
  pinMode(ledPin, OUTPUT);
  analogWriteResolution(12);
}

void loop() {
  digitalWrite(ledPin, HIGH);   
  analogWrite(A14, 0xfff);
  delay(2000);                 
  digitalWrite(ledPin, LOW);    
  analogWrite(A14, 0x7ff);
  delay(2000);                  

}