Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 2 of 2

Thread: Function generator sweep on teensy 3.2 DAC

  1. #1

    Function generator sweep on teensy 3.2 DAC

    Hi,

    I am new to using teensy, and have been using arduino before for simpler projects.

    I'm working on a project where I need to create square wave signal with a sweeping frequency ranging from 20 to 20000 Hz. I need the frequency to bounce back to the startfrequency with a period of 30 seconds. I want the signal to repeat for 30 minutes before it stops.

    I want to output the signal through the DAC.

    I looked at the avaliable examples related to this, and am trying to reuse the code in Arduino IDE examples/audio/hardwaretesting/tonesweep

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    AudioSynthToneSweep myEffect;
    AudioOutputI2S      audioOutput;        // audio shield: headphones & line-out
    
    // The tone sweep goes to left and right channels
    AudioConnection c1(myEffect, 0, audioOutput, 0);
    AudioConnection c2(myEffect, 0, audioOutput, 1);
    
    AudioControlSGTL5000 audioShield;
    
    
    float t_ampx = 0.8;
    int t_lox = 10;
    int t_hix = 22000;
    // Length of time for the sweep in seconds
    float t_timex = 10;
    // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
    void setup(void)
    {
      
      Serial.begin(9600);
      while (!Serial) ;
      delay(3000);
    
      AudioMemory(2);
    
      audioShield.enable();
      audioShield.volume(0.5);
    
      Serial.println("setup done");
    
      if(!myEffect.play(t_ampx,t_lox,t_hix,t_timex)) {
        Serial.println("AudioSynthToneSweep - begin failed");
        while(1);
      }
      // wait for the sweep to end
      while(myEffect.isPlaying());
    
      // and now reverse the sweep
      if(!myEffect.play(t_ampx,t_hix,t_lox,t_timex)) {
        Serial.println("AudioSynthToneSweep - begin failed");
        while(1);
      }
      // wait for the sweep to end
      while(myEffect.isPlaying());
      Serial.println("Done");
    }
    
    void loop(void)
    {
    }
    I'm changing the following
    - im not using audioshield,
    - im not outputting via i2s, but DAC
    - new freq range and period
    - waveform1 for square wave signal.
    - moving code to loop to repeat infinitely (until I fix an overall time limit)

    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    AudioSynthToneSweep waveform1;
    AudioOutputAnalogStereo  dacs;          //xy=423,362
    
    AudioConnection c1(waveform1, 0, dacs, 0);
    AudioConnection c2(waveform1, 0, dacs, 1);
    
    //AudioControlSGTL5000 audioShield;
    
    
    float t_ampx = 0.8;
    int t_lox = 20;
    int t_hix = 20000;
    // Length of time for the sweep in seconds
    float t_timex = 300;
    // <<<<<<<<<<<<<<>>>>>>>>>>>>>>>>
    void setup(void)
    {
      
      Serial.begin(9600);
      while (!Serial) ;
      delay(3000);
    
      AudioMemory(2);
    
      Serial.println("setup done");
    
      if(!waveform1.play(t_ampx,t_lox,t_hix,t_timex)) {
        Serial.println("AudioSynthToneSweep - begin failed");
        while(1);
      }
      // wait for the sweep to end
      while(waveform1.isPlaying());
    
      // and now reverse the sweep
      if(!waveform1.play(t_ampx,t_hix,t_lox,t_timex)) {
        Serial.println("AudioSynthToneSweep - begin failed");
        while(1);
      }
      // wait for the sweep to end
      while(waveform1.isPlaying());
      Serial.println("Done");
    }
    
    void loop(void)
    {
    }
    I compile and upload it, but it does not work.


    ------------------------------ HARDWARE ----------------------------------------------

    I have the following hardware setup Click image for larger version. 

Name:	ZW pv0.2.1 basic test teensy and 1 amp_bb.jpg 
Views:	167 
Size:	59.7 KB 
ID:	20237

    ------------------------------ TESTING ----------------------------------------------


    I do not get sound when connecting a speaker to the amplifier

    I dont get a nice signal when I only have teensy connected to the USB port and probing GND and DAC with an oscilloscope.

    I have verified with a multimeter that teensy and the amplifer get 5.1V from the battery powerboost circuit.

    I have checked that the speaker works.

    Any obvious problems here? Grateful for some support!

    Best regards,
    Marcus
    Last edited by MarcusFromTheNorth; 05-23-2020 at 04:18 PM.

  2. #2
    Senior Member+ MichaelMeissner's Avatar
    Join Date
    Nov 2012
    Location
    Ayer Massachussetts
    Posts
    4,458

    Cool

    Quote Originally Posted by MarcusFromTheNorth View Post
    Hi,

    I am new to using teensy, and have been using arduino before for simpler projects.

    I'm working on a project where I need to create square wave signal with a sweeping frequency ranging from 20 to 20000 Hz. I need the frequency to bounce back to the startfrequency with a period of 30 seconds. I want the signal to repeat for 30 minutes before it stops.

    I want to output the signal through the DAC.


    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    #include <Bounce.h>
    
    AudioSynthToneSweep waveform1;
    AudioOutputAnalogStereo  dacs;          //xy=423,362
    That last line is wrong for the 3.2. The Teensy 3.2 only has one DAC, so you should use AudioOutputAnalog instead of AudioOutputAnalogStereo. You can only use AudioOutputAnalogStereo on the Teensy 3.5 and 3.6 which has two DACs on A20 and A21.

    Here is my sample code to use ToneSweep on the 3.2 (or LC):

    Code:
    /*
      Demo of the audio sweep function.
      The user specifies the amplitude,
      start and end frequencies (which can sweep up or down)
      and the length of time of the sweep.
    
      Modified to use the single DAC on the Teensy 3.1/3.2 and the first DAC on the
      Teensy 3.5 or 3.6.  The 3.0, 4.0 and 4.1 Teensies do not have a DAC.
    
      Pins:		Teensy LC	Teensy 3.2	Teensy 3.5/3.6
    
      DAC:		A12		A14		A21
      */
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code (edited by meissner afterwards).
    AudioSynthToneSweep	tonesweep;		//xy=102,174
    AudioAmplifier		amp;			//xy=324,172
    AudioOutputAnalog	dac;			//xy=452,189
    
    AudioConnection		patchCord1 (tonesweep, amp);
    AudioConnection		patchCord2 (amp,       0,   dac, 0);
    // GUItool: end automatically generated code
    
    const float		t_ampx	= 0.8;
    const int		t_lox	= 10;
    const int		t_hix	= 22000;
    const float		t_timex	= 10;		// Length of time for the sweep in seconds
    
    void setup(void)
    {
      // Wait for at least 3 seconds for the USB serial connection
      Serial.begin (9600);
      while (!Serial && millis () < 3000)
        ;
    
      AudioMemory (8);
      amp.gain (0.5f);
    
      Serial.println("setup done");
    
      if (!tonesweep.play (t_ampx, t_lox, t_hix, t_timex)) {
        Serial.println ("ToneSweep - play failed");
        while (1)
          ;
      }
    
      // wait for the sweep to end
      Serial.println ("Tonesweep up started");
      while (tonesweep.isPlaying ())
        ;
    
      // and now reverse the sweep
      Serial.println ("Tonesweep down started");
      if (!tonesweep.play (t_ampx, t_hix, t_lox, t_timex)) {
        Serial.println("ToneSweep - play failed");
        while (1)
          ;
      }
    
      // wait for the sweep to end
      while (tonesweep.isPlaying ())
        ;
      Serial.println("Done");
    }
    
    void loop(void)
    {
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •