Shop OBEX P1 Docs P2 Docs Learn Events
Square Waves and Basic Frequencies! — Parallax Forums

Square Waves and Basic Frequencies!

SONIC the HedgehogSONIC the Hedgehog Posts: 321
edited 2012-01-31 12:14 in Propeller 1
So I was experimenting earlier, and made a function that generates square waves.
' ' File: SquareWave.spin

CON
         
        _clkmode            =xtal1 + pll1x
        _xinfreq              = 5_000_000


PUB Main

          SquareWave(1, clkfreq/1000, clkfreq/2000)
          SquareWave(1, clkfreq/2000, clkfreq/4000)
          SquareWave(1, clkfreq/4000, clkfreq/8000)
          SquareWave(1, clkfreq/8000, clkfreq/16000)
          SquareWave(1, clkfreq/16000, clkfreq/32000)


PUB   SquareWave(pin, tHigh, tLow) : success | tL, tH

          outa[pin]~
          dira[pin]~~

tHIgh := tH
tLow  := tL

          Repeat clkfreq + cnt
          outa[pin]~~
          waitcnt(tH + cnt)
          outa[pin]~
          waitcnt(tL + cnt)
I wired pin one to a loudspeaker, and then to the other end of the loudspeaker I gave +6 volts.
The result was some pretty interesting sounds, but they are square waves. So my question is how would you use spin to create saw waves? Would that even be possible or would you be limited to square and triangle waves?

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-01-30 19:19
    SONIC wrote:
    I wired pin one to a loudspeaker, and then to the other end of the loudspeaker I gave +6 volts.
    Don't do that! The Propeller pins can be damaged if they see a voltage much above 3.3V.

    -Phil
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:23
    No youve misunderstood. The propeller pins don't see the +6 volts. I actually used the same setup with 9 volts no resistors on the free end of the loudspeaker because it is part of the learning lab kit and the book told me to do. I used some kind of 4117 or something. The point of the +6 is to amp up the signal that comes from the prop so it goes through the loud speaker. The pin is set to output, not input anyways.
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:26
    The diagram looks like this

    Propeller pin
    loudspeaker
    +6 volts. It seems the prop would see the six volts, but it doesn't. I've done many circuits using CMOS devices running at 4.5v and below using a loudspeaker that sees 6 volts and greater.
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-01-30 19:31
    Are you sure? Have you checked on a scope? A speaker is a low resistance, much lower than the prop pin, and an inductance. I'm amazed you haven't put the I/O pin on the prop into latch-up.

    Connect the other side of the speaker, through a capacitor (100uF or so), to 0V, and it should be just as loud.
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-01-30 19:33
    The diagram looks like this

    Propeller pin
    loudspeaker
    +6 volts.
    Care to post a photograph to really frighten us?
    It seems the prop would see the six volts, but it doesn't. I've done many circuits using CMOS devices running at 4.5v and below using a loudspeaker that sees 6 volts and greater.
    Why do you say it doesn't? Have you put a scope on it and measured it? You still won't see 6V at the prop pin because of the internal protection diode in the chip, but you will see 4.2 or so, as the protection diode is forward-biased through the low impedance of the speaker. And, by the way, the speaker will allow enough current to flow that you could, very well, damage the I/O pin on the prop.
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:36
    Well, the book had me use a 555 timer chip to generate a square wave and send that to a speaker, which was connected to +4.5, but it added a tip that said you can increase the volume by increasing the voltage that goes straight to the loudspeaker.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-01-30 19:38
    A 555 has a much higher voltage tolerance than the Propeller. Again: don't do that! Also remember: Propeller pins, upon reset, are inputs. They will see the full brunt of that 6V. Finally, don't be misled by recommendations to use a cap in series with the speaker. That will not help except, perhaps, after the cap has charged during operation. Before that? PFFFT! Magic smoke gets released; and you won't need to call Orkin, because you've already got a dead bug.

    -Phil
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:39
    Huh. Never thought of that. Well, I havent seen any smoke come out of it or it get overly hot, but you guys have more experience so I will take your word and drop to 3 volts or use your capacitor idea. Very ingenious I might add. Well I would but no camera = no picture. So I will get one soon and post schematics in every post when necessary.
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:40
    Wait, if they are both CMOS devices......?
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:41
    Once again you guys are experts so I will be careful with my experiments......
    But still back to topic. Any ideas on sawtooth waves?
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-30 19:43
    I was thinking.......a sawtooth wave has a elongated attack, instead of just going straight to the sustain like in square. So is there a way to gradually increase the voltage outputted by the propeller pins? Say from maybe like 1volt to 3.3 volts?
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-01-30 19:54
    Here's a simple one-transistor speaker driver that should work and provide adequate protection to the Propeller:

    attachment.php?attachmentid=89103&d=1327982028

    -Phil
    280 x 266 - 1K
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-01-30 20:05
    ... so I will take your word and drop to 3 volts or use your capacitor idea.
    Do both. Note, I said to run the speaker to 0V.

    When you run the speaker straight from the prop pin to 0V, the constant DC across it will push the voice coil out of the magnetic gap and make the speaker less sensitive. The capacitor changes the speaker from getting 0-3.3V, to -1.65-1.65. That way, the average is 0, and the coil stays well within the gap and it has higher efficiency.
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-01-30 20:07
    Wait, if they are both CMOS devices......?
    The original 555 isn't CMOS, but the CMOS versions are designed to run at a higher voltage. CMOS doesn't mean 3.3V. The original CMOS logic (4000 series) could handle 18V. All the newest chips from Intel/AMD are CMOS, but you'd better not run them above 1.4V.
  • CircuitsoftCircuitsoft Posts: 1,166
    edited 2012-01-30 20:08
    I was thinking.......a sawtooth wave has a elongated attack, instead of just going straight to the sustain like in square. So is there a way to gradually increase the voltage outputted by the propeller pins? Say from maybe like 1volt to 3.3 volts?
    Look into one of the PWM objects on OBEX, and drive it with a ramp from a loop in spin.
  • average joeaverage joe Posts: 795
    edited 2012-01-30 22:11
    It's safe to hook a speaker directly between the prop and ground. Phil's amplifier would be bare minimum to me if I wanted it to be louder. A pot connected as a voltage divider between the prop pin and the transistor would provide volume control. NEVER hook the prop up to dc greater than it's input voltage, even through a speaker. Also, the speaker SHOULD be connected to ground. Connecting it to supply voltage won't make it louder and will degrade performance for a lot of complicated reasons. You could always hook it up to your home theater. I would suggest reading about the counters. They make great ADCs DACs. Are you playing with audio waveform generation? If this is the case you could use wavetables and have any waveform you can imagine *within reason*. A little more about what you're trying to do?
  • Ahle2Ahle2 Posts: 1,179
    edited 2012-01-31 01:53
    I'm sorry to say, but spin is by FAAAR to slow to do DDS at an acceptable sample rate. (my definition of acceptable is 30 kHz)
    Even a very simple single channel DDS with basic waveforms and some kind of volume adjustment or envelope will produce a less than acceptable sample rate.

    There are, of course, different ways of doing DDS with acceptable results on the Propeller.

    *For a simple square wave tone, you can use the two counter modules in a cog to achieve extremely good results; Almost no jitter or aliasing at all thanks to the very high sample rate
    The problem is that you can't adjust the volume

    *You can use the video generator in a cog to playback samples with 1, 2 or 8 bit resolution as long as you feed it with data fast enough; Almost no jitter or aliasing distortion (apart from the sample itself)
    You can't adjust the volume, but the sample itself may have a natural declining amplitude.

    *You can do DDS in pasm and launch it as a software peripheral in a separate cog. This is the prefered way to achieve more complex synthesis.
    The results can vary very much; From a very simple PSG with 3 channel square waves and 16 step volume adjustment to very complex sound synthesis with 8+ channels with envelopes and different kind of modulations.
    The general rule is that simple sound synthesis will run at high sample rates with very low little jitter and aliasing problems; Simple sounds, but Hifi!!
    Complex sound syntesis can sound very good but will often have lower sample rates; Complex sounds, but less Hifi!!
    It's quite hard to find the perfect balance between complexness <-> Hifiness

    /Johannes
  • Ahle2Ahle2 Posts: 1,179
    edited 2012-01-31 02:10
    The best way of doing a saw wave is by using a phase accumulator/numerically controlled oscillator.
    It's basically just: 32bitRegister += 32bitFrequency.
    32bitRegister can be feed directly to a dac or a pwm output to produce a saw wave signal.

    * The sample rate will be the rate at wich you call "32bitRegister += 32bitFrequency" and then output the value of 32bitRegister to the dac.

    * The oscillator frequency can be calculated by this formula: Freq = SampleRate * (32bitFrequency/(32^2))

    /Johannes
  • average joeaverage joe Posts: 795
    edited 2012-01-31 02:30
    Ahle2 wrote: »
    *You can do DDS in pasm and launch it as a software peripheral in a separate cog.
    You almost read my mind there. I have been thinking about using something like this for a synth. I was thinking to implement HARDWARE VCA and VCF which should solve some of problems.
  • Heater.Heater. Posts: 21,230
    edited 2012-01-31 03:04
    In general one should not be applying direct current to a speaker.
    This only results in a DC current flowing through the speaker which at worst will blow the speaker coil or whatever is driving it, the Prop in this case.
    Don't forget that for DC the speaker has just the low resistance of the voice coil.
    At best the DC current simply drives the speaker to one end of its range of movement. In which case the range of movement available to create sound is reduced and will result in distortion.
    Hence you will often see a capacitor in series with the speaker to block DC if your driver can only produce positive voltage swings (or only negative). Or you will see that the driver amplifier can swing its output both positive and negative with the signal giving a zero DC bias.
  • evanhevanh Posts: 16,113
    edited 2012-01-31 04:07
    Heater is right, speakers are stressed by a DC offset. That said, a cheapo tweeter sized speaker is prolly not going to get hot on such low voltages.

    Also, the 555 output used was prolly the open-collector (Pin 7). Raising the supply voltage to the speaker when driven from an open-collector will indeed increase the volume without hitting any clamping diodes, just like Phil's example - http://forums.parallax.com/showthread.php?137615-Square-Waves-and-Basic-Frequencies!&p=1070597&viewfull=1#post1070597
  • Mark_TMark_T Posts: 1,981
    edited 2012-01-31 07:38
    Lets be more clear here: A piezo speaker will be fine to connect, they draw very little current and aren't inductive.

    A moving coil speaker should never be connected directly to logic outputs - its asking for trouble. If you want to get sensible levels of audio out use a buffer amp (I'd favour the MIC4422 MOSFET driver personally). Connect a large electrolytic in series with the speaker to lose the DC component.

    Someone mentioned the 555 - thats a bipolar chip rated at 300mA if I remember correctly, far more robust than the prop (and much cheaper to replace!).

    A fast buffer like the MIC4422 (which goes up to 18V, 9A, and has significant latch-up protection) allows class-D operation too ;)
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-01-31 08:47
    Here's a simple one-transistor speaker driver that should work and provide adequate protection to the Propeller:

    attachment.php?attachmentid=89103&d=1327982028

    -Phil
    Don't do that!
    The speaker could be damaged by high current if you use that circuit. If you drive the output pin high for an extended period of time the voice coil will be dissipating 3 watts at 8 ohms, or 6 watts at 4 ohms. You may burn out the transistor as well. The maximum current for the PN2222 is 600ma. An 8-ohm load would be right arund that current, and a 4-ohm load would be twice that value.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-01-31 09:05
    Dave Hein wrote:
    The speaker could be damaged by high current if you use that circuit. If you drive the output pin high for an extended period of time...
    This is the circuit I used in the SoundPAL (except for the BJT and base resistor -- the SoundPAL uses a MOSFET). With the proper speaker, it works fine under controlled circumstances when driven by square waves. But you're right: a continuous high on the base could cause excessive current dissipation in a low-impedance speaker, assuming the transistor's saturation voltage at that current was low enough. The circuit's main advantage is its effectiveness for a small footprint and small price.

    -Phil

    Reference: http://www.puiaudio.com/resources-white-papers-electromechanical-transducers.aspx
  • SONIC the HedgehogSONIC the Hedgehog Posts: 321
    edited 2012-01-31 12:14
    You guys really are knolwdeable, and thanks so much for providing with me helpful information
Sign In or Register to comment.