How can i use DAC0 for analog input and DAC1 for audio output?

ovedmo

Member
Hi
How can i use DAC0 for analog input and DAC1 for audio output?
i need to use analogRead(A21), and use audio output on DAC1.
thank you
 
This works for me on T3.6
Code:
void setup() {
  analogReadResolution(12);
  analogWriteResolution(12);
}

void loop() {
  for (int i = 0; i < 4095; i += 64) {
    analogWrite(A22, i);
    delayMicroseconds(3); // DAC settle
    Serial.printf("DAC out %d  A21 %d\n", i, analogRead(A21));
    delay(1000);
  }
}

jumper A21 to A22
 
I use audio library so the write to the dac is via the play function (playing wave files) ... so what adjustments i have to do to make it work...

this is the base functions i use:

Sound.play(file name on sd);

analogRead(A21);

when reading A21 with no sound library included it work ok!!
 
So we don't have to guess what your sketch is doing, obey the forum rule above:
Forum Rule: Always post complete source code & details to reproduce any issue!

The audio lib is probably configuring both DAC pins for output, precluding use with analogRead(). You may have to modify the audio lib.
 
This ia the code

Code:
#include <Audio.h>
#include <Wire.h>
#include <SD.h>
#include <Time.h>
#include <TimeLib.h>
#include <TimeAlarms.h>

int sensoreCount = 24;

int boardActive = 1;//Will toggle Board Activation
#define SDCARD_CS_PIN    BUILTIN_SDCARD

const int knockSensors [24] = {A0, A1, A2, A3, A4, A5, A10, A11, A8, A9, A6, A7, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A23, A24};
const long soundInterval = 300;
unsigned long currentTiming = millis();

AudioPlaySdWav     Sound[12];
AudioMixer4              mixer3;
AudioMixer4              mixer2;
AudioMixer4              mixer1;
AudioMixer4              mixer4;
AudioOutputAnalogStereo  dacs1;
AudioConnection          patchCord1(Sound[0], 0, mixer1, 0);
AudioConnection          patchCord2(Sound[1], 0, mixer1, 1);
AudioConnection          patchCord3(Sound[2], 0, mixer1, 2);
AudioConnection          patchCord5(Sound[3], 0, mixer1, 3);

AudioConnection          patchCord4(Sound[4], 0, mixer2, 0);
AudioConnection          patchCord6(Sound[5], 0, mixer2, 1);
AudioConnection          patchCord7(Sound[6], 0, mixer2, 2);
AudioConnection          patchCord8(Sound[7], 0, mixer2, 3);

AudioConnection          patchCord9(Sound[8], 0, mixer3, 0);
AudioConnection          patchCord10(Sound[9], 0, mixer3, 1);
AudioConnection          patchCord11(Sound[10], 0, mixer3, 2);
AudioConnection          patchCord12(Sound[11], 0, mixer3, 3);

AudioConnection          patchCord13(mixer3, 0, mixer4, 0);
AudioConnection          patchCord14(mixer2, 0, mixer4, 1);
AudioConnection          patchCord15(mixer1, 0, mixer4, 2);
AudioConnection          patchCord16(mixer4, 0, dacs1, 0);
AudioConnection          patchCord17(mixer4, 0, dacs1, 1);



int knockState [24] = { 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 , 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 };
int sensorReading[24];
unsigned long timing[24] = { 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 , 0 ,  0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 };
int sensetivity = 500 ;
int instrument = 1;

void setup() {

  Serial.begin(9600);
  //Set New Instrument
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(13, OUTPUT);

  if (!(SD.begin(SDCARD_CS_PIN))) {
    while (1) {
      Serial.println("Unable to access the SD card");
      Alarm.delay(500);
    }
  }

  AudioMemory(25);
  for (int Vol = 0 ; Vol < 4; Vol++) {
    mixer1.gain(Vol, 0.1);
    mixer2.gain(Vol, 0.1);
    mixer3.gain(Vol, 0.1);
    mixer4.gain(Vol, 0.1);
  }
  for (int i = 0; i <= 10; i++) {
    playMusic(i);
    Alarm.delay(500);
  }
  delay(500);
//increase Volume
  for (int Vol = 0 ; Vol < 4; Vol++) {
    mixer1.gain(Vol, 0.4);
    mixer2.gain(Vol, 0.4);
    mixer3.gain(Vol, 0.4);
    mixer4.gain(Vol, 0.4);
  }
    
  Serial.println("Finish Set UP");
  Alarm.delay(1500);
}

void playMusic(int note) {
  if (!boardActive) return;
  String directory = "drum";
  char HoldName[15];
  char fullName[15];
  Serial.println(instrument);
  switch (instrument) {
    case 1: directory = "piano";  break;
    case 2: directory = "drum";  break;
    case 3: directory = "droid";  break;
    default: directory = "piano";  break;
  }
  char filename[15] = "piano/1.wav";
  int currentNote = note + 1;
  sprintf(filename, "/%i.wav", currentNote);
  directory.toCharArray(HoldName, 15);
  sprintf(fullName, "%s%s", HoldName, filename);
  Serial.println(fullName);
  Serial.println(note);
  AudioNoInterrupts();
  if (note <= 11)
    Sound[note].play(fullName);
  else
    Sound[note - 12].play(fullName);
  Serial.print("Audio Memory : ");  
  Serial.println(AudioMemoryUsageMax());  
  AudioInterrupts();  
}

void loop() {
  Alarm.delay(1);
  int dr2 = 0;
  int dr3 = 0;
  int dr4 = 0;
  currentTiming = millis();
  for (int count = 0; count <= sensoreCount - 1 ; count++) {
    sensorReading[count] = analogRead(knockSensors[count]);
    if ((sensorReading[count] > sensetivity) && (knockState[count] == 0) && (currentTiming - timing[count] >= soundInterval)) {
      timing[count] = currentTiming;
      knockState[count] = 1;
      playMusic(count);
      delay(300);
    }
    else {
      if (sensorReading[count] < sensetivity) knockState[count] = 0;
      if (currentTiming < timing[count]) timing[count] = 0;
    }
  }
  dr2 = digitalRead(2);
  dr3 = digitalRead(3);
  dr4 = digitalRead(4);
  if (dr2 == LOW) instrument = 1;
  else if (dr3 == LOW) instrument = 2;
  else if (dr4 == LOW) instrument = 3;
}
 
if you need stereo output through DAC0 and DAC1, i don't think you can do analogRead() on A22 or A21. It might work with mono output (DAC0) and using A22 rather than A21 for analogRead(). For mono change AudioOutputAnalogStereo dacs1; to AudioOutputAnalog dacs1;

Using the WaveFilePlayer example on DAC0 I was able to analogRead(A22).

you also have A25 and A26 on the backside
 
Last edited:
... It might work with mono output (DAC0) and using A22 rather than A21 for analogRead(). For mono change AudioOutputAnalogStereo dacs1; to AudioOutputAnalog dacs1;

Using the WaveFilePlayer example on DAC0 I was able to analogRead(A22).

you also have A25 and A26 on the backside

I Don't need streo, just need to play sound thru A22 and Read analog A21 !!

This looks to provide a possible answer to the question - and an alternative?
 
i cant change the input output now, can you upload the code you used?
I think it read wrong data like it have "noise" not the analog data i send.
When i try to read analog(set any signal on the A21) when sound play and it stop playing(vol 0).
 
I Don't need streo, just need to play sound thru A22 and Read analog A21 !!

You'll have to modify the Audio library to use DAC1 for AudioOutputAnalog in place of DAC0 in
hardware/teensy/avr/libraries/Audio/output_dac.cpp

Good Luck
 
Back
Top