Recent

Author Topic: Generate square / sinusoidal waves  (Read 6965 times)

Ramses

  • New Member
  • *
  • Posts: 41
Generate square / sinusoidal waves
« on: January 12, 2021, 12:53:20 am »
Hi all, im new to this forum, and im a beginner with the raspberry pi (but i have programmed with lazarus/delphi till many years on windows system)

i need some help to generate square and sinusoidal waves

Im using a Raspbery Pi 3 and i dont figure out how to generate my wave on this board  %)...

I want to make a very simple program with 2 button:

Button1: generate a 30hz squared wave
Button2: generate a 55hz sinusoidal wave

Thats it!

What is the easiest way to generate those wave? Witch pin is the easiest to use?

Thanks for your help

jamie

  • Hero Member
  • *****
  • Posts: 7300
Re: Generate square / sinusoidal waves
« Reply #1 on: January 12, 2021, 01:22:14 am »
If it were windows I could show you how to do that using the sound system, at lease the Sine wave that is.

But you have a little mini there I am sure it does have some sort of facility for audio out.. But if you have a DAC (Digital To Analog Converter) you can gain access to as an output pin, I don't think 55Hz is too fast of a change for it..

 You can create the Sine wave using time sliced (timer) broken into steps (PCM) to set the output value of the DAC pin.

Does this sound like something you understand ?
The only true wisdom is knowing you know nothing

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #2 on: January 12, 2021, 01:45:58 am »
Sure i understand, the problem is that i not familiar with raspberry board.. on windows/delphi is a way more easier!

i dont know if its possible to do that only by code, or with a special library, or if i have to use an external circuit to generate wave.. this board is new to me..

The only thing i need is a code exemple that generate square/sinusoidal wave... (if its possible to do this only by code :) )

jamie

  • Hero Member
  • *****
  • Posts: 7300
Re: Generate square / sinusoidal waves
« Reply #3 on: January 12, 2021, 01:58:19 am »
I've taken AVR and PIC chips and done that using PWM (Pulse width modulation) through a Resistor and CAP network into a buffer OP-Amp to
generate the SINE wave by calculating the a PWM at high frequency rate so to create a Analog output and set the output to represent a sine wave.

 I am sure libs exist to do this but the pi is a little more than just a micro chip

 They made a few different Pi's which processor do you have ?
The only true wisdom is knowing you know nothing

Fred vS

  • Hero Member
  • *****
  • Posts: 3714
    • StrumPract is the musicians best friend
Re: Generate square / sinusoidal waves
« Reply #4 on: January 12, 2021, 02:17:32 am »
Hello.

There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS.

https://github.com/fredvs/alsa_sound.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #5 on: January 12, 2021, 02:25:25 am »
I've taken AVR and PIC chips and done that using PWM (Pulse width modulation) through a Resistor and CAP network into a buffer OP-Amp to
generate the SINE wave by calculating the a PWM at high frequency rate so to create a Analog output and set the output to represent a sine wave.

 I am sure libs exist to do this but the pi is a little more than just a micro chip

 They made a few different Pi's which processor do you have ?

i use Raspberry Pi 3 Model B(CPU: 4× ARM Cortex-A53, 1.2GHz)

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #6 on: January 12, 2021, 02:33:08 am »
Hello.

There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS.

https://github.com/fredvs/alsa_sound.

Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...)

Fred vS

  • Hero Member
  • *****
  • Posts: 3714
    • StrumPract is the musicians best friend
Re: Generate square / sinusoidal waves
« Reply #7 on: January 12, 2021, 03:09:47 am »
Hello.

There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS.

https://github.com/fredvs/alsa_sound.

Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...)

If you want to produce a sine-wave between 20 Hz and 18.000 HZ you may "visualize" it with your ears and so the easier is with the build-in sound card output of your Rpi (via the output mini jack or via the output hdmi).

libasound.so is the low level audio interface of most Linux distro, included Raspbian OS.
I use Lazarus 2.2.0 32/64 and FPC 3.2.2 32/64 on Debian 11 64 bit, Windows 10, Windows 7 32/64, Windows XP 32,  FreeBSD 64.
Widgetset: fpGUI, MSEgui, Win32, GTK2, Qt.

https://github.com/fredvs
https://gitlab.com/fredvs
https://codeberg.org/fredvs

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #8 on: January 12, 2021, 04:16:49 am »
Hello.

There are some demos of sine wave that use only libasound.so library that is included in the Raspbian Pi OS.

https://github.com/fredvs/alsa_sound.

Thanks for that, so if it use the sound, the output is probably the sound card output? That look good, i wil ltry to install this `alsa sound` to see if it work for what i need (but im not very familiar with installation of this on linux...)

If you want to produce a sine-wave between 20 Hz and 18.000 HZ you may "visualize" it with your ears and so the easier is with the build-in sound card output of your Rpi (via the output mini jack or via the output hdmi).

libasound.so is the low level audio interface of most Linux distro, included Raspbian OS.

alright, so did you have an simple exemple code that i can put in my button code to generate a 35hz tone for exemple?

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: Generate square / sinusoidal waves
« Reply #9 on: January 12, 2021, 10:17:14 am »
What are you expecting it to come out of? Are you looking for an audio signal, a frequency on one of the GPIO pins, or what?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: Generate square / sinusoidal waves
« Reply #10 on: January 12, 2021, 10:52:28 am »
I am sure libs exist to do this but the pi is a little more than just a micro chip

A Raspberry Pi is a fully-fledged Linux-based computer, rather more than "just a micro chip".

What that means in practice is that it's not possible to ackle signals directly, you've got to go through the OS hence

https://wiki.freepascal.org/Raspberry_Pi#Native_hardware_access

plus other links on that page.

One detail is that there are two different kernel APIs for accessing the GPIO pins: one goes through /sys and the other through /dev. I've played with the former a bit and I think it's possible to get a few 10s of Hz out of it, the latter should be more efficient.

I don't know whether the underlying chip (or the OS support for it) has a counter/timer circuit which can generate a square wave directly.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #11 on: January 12, 2021, 02:38:04 pm »
What are you expecting it to come out of? Are you looking for an audio signal, a frequency on one of the GPIO pins, or what?

MarkMLl

I think that an audio signal is better for me, but a frequency on one pin is ok too, as long as i can read the signasl with an oscilloscope, its ok for me

MarkMLl

  • Hero Member
  • *****
  • Posts: 8504
Re: Generate square / sinusoidal waves
« Reply #12 on: January 12, 2021, 02:52:31 pm »
I think that an audio signal is better for me, but a frequency on one pin is ok too, as long as i can read the signasl with an oscilloscope, its ok for me

In that case look at the stuff that Fred's given you, alternatively my equivalent https://github.com/MarkMLl/asound

Broadly speaking both of those came from the thread at https://forum.lazarus.freepascal.org/index.php/topic,49502.msg383415.html#msg383415 My code's got a lot of extra verbosity in it since it's intended to be usable with both static an dynamic linkage, but I think you'll find the specific example at https://github.com/MarkMLl/asound/blob/main/examples/alsapcmdemo.pas useful which is applicable to any ALSA interface.

I'd not recommend ackling a GPIO bit since it will be fairly erratic unless you have an RTOS (Real-Time Operating System) which Linux isn't.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

avra

  • Hero Member
  • *****
  • Posts: 2566
    • Additional info
Re: Generate square / sinusoidal waves
« Reply #13 on: January 13, 2021, 12:07:48 pm »
Delphi code for generating frequency based wave forms can be found here:
http://delphiforfun.org/programs/SoundGen.htm

You would need to adapt it first to Lazarus, then to Linux, and then to Pi.
ct2laz - Conversion between Lazarus and CodeTyphon
bithelpers - Bit manipulation for standard types
pasettimino - Siemens S7 PLC lib

Ramses

  • New Member
  • *
  • Posts: 41
Re: Generate square / sinusoidal waves
« Reply #14 on: January 13, 2021, 04:51:33 pm »
Broadly speaking both of those came from the thread at https://forum.lazarus.freepascal.org/index.php/topic,49502.msg383415.html#msg383415 My code's got a lot of extra verbosity in it since it's intended to be usable with both static an dynamic linkage, but I think you'll find the specific example at https://github.com/MarkMLl/asound/blob/main/examples/alsapcmdemo.pas useful which is applicable to any ALSA interface.

Thanks for that, it worked perfectly for sinusoidal wave, i can set the frequency and duration, so it is exactly what i needed!  :D

Now i have to produce square wave too, any idea how to do that?

 

TinyPortal © 2005-2018