Producing sinewave output on Photon

Hey,
I am using a particle photon. I want to generate an output which is in the form of a sine function. It’s not a pure sinewave, but a function of sine and cosine. Right now I am using the analog write feature of zerynth. I tried it with analogWrite(), But I am not getting what I exactly need.
Also, Can I use FFT to generate the output ?

import gpio
import dac 
import streams
import adc
import math

buf = bytearray(100)
for i in range(len(buf)):
buf[i] = 128 + int(127 * math.sin(2 * math.pi * i / len(buf)))

myDAC = dac.DAC(D8.DAC)
myDAC.start()

myDAC.write(buf,400*len(buf),MILLIS,circular=True)

I tried importing the cosine and sine libraries using,
from math import sin
But that didn’t work either.

Could you specify what kind of error occurs?
What is the output with an oscilloscope?

Also, Unfortunately, Direct FFT is not yet implemented in the MATH library.

1 Like

Hey, I’ll be more specific here. This is the function to who’s output the output signal should be mapped too.
1.2525*((0.0449949095399884+0.110299998345786cos(tw)-0.0727808875940510cos(2tw)-0.0825773728054780cos(3tw)

In my case, the value of w = 7.5 and t will be the current time. So how do I map the output of this function to the analog write. I plotted the function in matlab using FFT, and both outputs are way different.

Hey. Do you have any idea about it?

I checked your formula using Zerynth studio on my ESP32 Devkit,

result= [None]*100
w = 7.5    
for j in range(100): #J represents time.
    result[j] = 0.0449949095399884+0.110299998345786*math.cos(j*w)-0.0727808875940510*math.cos(2*j*w)-0.0825773728054780*math.cos(3*j*w)

Since the output vary with very small values [-0.1:0.2] you can add scale and bias to the list.
Like multiplying each item in the list with a weight and adding some constant to it,

Then applying the DAC function to that list.
my_dac.write(result_Scaled,WAIT_TIME,MILLIS,circular=True)
Let me know if you need further help.