Generate a Sine wave

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
Post Reply
Husyn
Posts: 7
Joined: Sun Jan 23, 2022 8:27 am

Generate a Sine wave

Post by Husyn » Fri Feb 18, 2022 8:50 am

Hi
I can generate a sine wave with this code on ESP32. But can't change the frequency. It s always 26.12Hz.
How can I change the frequency.
Or
How can I generate a sine wave that can change its frequency
import ulab as np
from math import*
import _thread as th
from machine import DAC, ADC, Pin
import time

f=10
T=1/f
dt=1/100
t=np.linspace(0, T-dt*T, 100)
sinout=DAC(Pin(25,Pin.OUT))
##########################
def sin_gen(phi):
s=[]
for n in t:
s.append(int(128+127.99*sin(2*pi*f*n+phi)))
return(s)
############################
sine=sin_gen(0)
cosine=sin_gen(90)
############################
def pin_write():
while True:
for n in sine:
print(n)
sinout.write(n)
############################
pin_write()

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Generate a Sine wave

Post by pythoncoder » Fri Feb 18, 2022 10:04 am

The general approach is as follows. Create a bytearray to hold a single cycle of sine wave, and populate it with data scaled to lie in the range 0-255 (for a 8-bit DAC). Then use a timer interrupt to output a value from the array and increment the array pointer (modulo the length of the array). The timer rate then determines the frequency.

This won't work particularly well on ESP32 as interrupts are soft, meaning that their timing is imprecise. A Pyboard has the means to do this with high precision and speed: it is possible to create sinewaves of tens of KHz.
Peter Hinch
Index to my micropython libraries.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Generate a Sine wave

Post by mattyt » Sat Feb 19, 2022 11:24 am

See #5514 for an alternative way to generate a sine wave using extra features of the ESP32 DAC.

Note that this is a non-standard MicroPython feature (since it's quite particular to the ESP32) and my guess is that it's unlikely to be merged. An alternative that might have a better chance would be to add the cosine methods to the esp32 module and have them take a DAC object...

Husyn
Posts: 7
Joined: Sun Jan 23, 2022 8:27 am

Re: Generate a Sine wave

Post by Husyn » Sun Feb 20, 2022 6:16 am

pythoncoder wrote:
Fri Feb 18, 2022 10:04 am
The general approach is as follows. Create a bytearray to hold a single cycle of sine wave, and populate it with data scaled to lie in the range 0-255 (for a 8-bit DAC). Then use a timer interrupt to output a value from the array and increment the array pointer (modulo the length of the array). The timer rate then determines the frequency.

This won't work particularly well on ESP32 as interrupts are soft, meaning that their timing is imprecise. A Pyboard has the means to do this with high precision and speed: it is possible to create sinewaves of tens of KHz.
#####
mattyt wrote:
Sat Feb 19, 2022 11:24 am
See #5514 for an alternative way to generate a sine wave using extra features of the ESP32 DAC.

Note that this is a non-standard MicroPython feature (since it's quite particular to the ESP32) and my guess is that it's unlikely to be merged. An alternative that might have a better chance would be to add the cosine methods to the esp32 module and have them take a DAC object...
Thank you for your answer.
Now I tried to do it using Ad9833 wave generation module. But I could not change 11,12 and 15th lines in the test code here according to Esp32.I was able to generate the signal with arduino-Esp32. but I couldn't do it with micropython.

my main goal is actually implement of this lockin amplifier block diagram. What do you think. I've been struggling with it for over 2 months.

Can it be done or should I give up? I depressed and tired thank you
Attachments
images-1.png
images-1.png (31.22 KiB) Viewed 9018 times

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Generate a Sine wave

Post by pythoncoder » Sun Feb 20, 2022 1:38 pm

It rather depends on the frequency you want to measure. Also, as I said above, I have my doubts about the ESP32 for this type of application.
Peter Hinch
Index to my micropython libraries.

Post Reply