Teensy 4.1 Generate 40KHZ Sine Wave

Status
Not open for further replies.

pi123

Member
How do you generate a 40kHZ sine wave using teensy 4.1 that outputs to an analog pin? What is the max amplitude the sin wave could possibly produce? What are the options for this? Is there any example code?
 
How do you generate a 40kHZ sine wave using teensy 4.1 that outputs to an analog pin? What is the max amplitude the sin wave could possibly produce? What are the options for this? Is there any example code?

Let's first make sure you are not confusing analogRead-pins and/or PWM-pins with what you seem to want to do. (Digital-To-Analog conversion)

- On the Teensy 4.1 the pins named A0.. A17 are analogRead-pins. (they use either one of the chips Analog-to-Digital-Convertors (ADC) )
- The pins labeled with PWM are able to simulate an analog voltage by rapidly switching the pin's fixed output voltage (3.3V) on and off.
You will need to use a low-pass filter to get a reasonably smooth analog output voltage.

The chips used in the Teensy 3.1 and 3.2 have a 12 bit hardware DAC (Digital-to-Analog-Convertor) which is available on pin A14.
The hardware DAC makes it MUCH easier to create relatively high frequency sinewaves.
The hardware DAC is NOT available on the Teensy 4.0 and 4.1.

There is always the option to use external DAC's. Both parallel DAC's and SPI DAC's are available. It also depends on how many samples per full wave you want (temporal resolution of your sinewave)

Finally, the max amplitude of the sinewave directly from the Teensy is always going to be the V-out of the pins, 3.3V
If that is not enough you may want to use an external opamp.
Some of the external DAC's can produce an output that is directly linked to the supply voltage you connect them to.
(That is, they have internal amplifiers ... The DAC receives digital setpoint from the Teensy at 3.3V levels, but creates an output voltage that depends on the V-supply to the DAC chip)



Sorry if it does not fully answer your question.
 
Let's first make sure you are not confusing analogRead-pins and/or PWM-pins with what you seem to want to do. (Digital-To-Analog conversion)

- On the Teensy 4.1 the pins named A0.. A17 are analogRead-pins. (they use either one of the chips Analog-to-Digital-Convertors (ADC) )
- The pins labeled with PWM are able to simulate an analog voltage by rapidly switching the pin's fixed output voltage (3.3V) on and off.
You will need to use a low-pass filter to get a reasonably smooth analog output voltage.

The chips used in the Teensy 3.1 and 3.2 have a 12 bit hardware DAC (Digital-to-Analog-Convertor) which is available on pin A14.
The hardware DAC makes it MUCH easier to create relatively high frequency sinewaves.
The hardware DAC is NOT available on the Teensy 4.0 and 4.1.

There is always the option to use external DAC's. Both parallel DAC's and SPI DAC's are available. It also depends on how many samples per full wave you want (temporal resolution of your sinewave)

Finally, the max amplitude of the sinewave directly from the Teensy is always going to be the V-out of the pins, 3.3V
If that is not enough you may want to use an external opamp.
Some of the external DAC's can produce an output that is directly linked to the supply voltage you connect them to.
(That is, they have internal amplifiers ... The DAC receives digital setpoint from the Teensy at 3.3V levels, but creates an output voltage that depends on the V-supply to the DAC chip)



Sorry if it does not fully answer your question.

How exactly could I use a pwm signal to create 40khz sinewave? Any code examples?
 
Look at the PWM resolution table on the following page : https://www.pjrc.com/teensy/td_pulse.html

Consider the amplitude resolution (How many discrete output values do you want to create your sine wave)
Also consider how many samples you want to describe your sine wave.

It may prove very challenging to make a smooth sine wave at 40kHz using PWM. Because at 10 samples per full wave, each sample can still only be 0.0025[mS]. That is, the PWM frequency needs to be 400kHz.
With a resolution of 8 bits, that means that the minimum period of time the output is HIGH, is 1/255 * 0.0025[mS] (which is just under 0.01[uS], to give you an idea of what the CPU has to do)

In General, your code 'only' needs to handle the providing of setpoints (write a new analog output value to the PWM pin. In the example you will need to do this 10 times every 0.025[mS] (for 40kHz) )
The PWM hardware on the chip takes care of what happens after you write your setpoint.


I don't have code to copy-paste, but
- You could use a timer-interrupt at 40kHz * Nr_Samples . Then in the interrupt handler you could:
1) Calculate the digital output level using the sine(phase ) function
2) read the digital value from an Array of integers where you have stored pre-calculated samples. (not flexible if you want to change 40kHz to 45kHz or 35kHz)
 
A separate DDS module might be another approach - what do you want the 40kHz for? Pure tone or modulated?
How accurate does the frequency need to be? What sort of jitter and noise is allowable?
 
I agree with Mark. Depending on specs and use, $10 for an AD9833 module seems reasonable.
 
Status
Not open for further replies.
Back
Top