How to use a PIC I/O audio square wave to drive headphones - diyAudio
Go Back   Home > Forums > Amplifiers > Solid State

Solid State Talk all about solid state amplification.

Please consider donating to help us continue to serve you.

Ads on/off / Custom Title / More PMs / More album space / Advanced printing & mass image saving
Reply
 
Thread Tools Search this Thread
Old 30th November 2003, 11:51 PM   #1
diyAudio Member
 
Join Date: Nov 2003
Location: fort collins co
Default How to use a PIC I/O audio square wave to drive headphones

Am developing a PIC microcontroller app that generates audio tone that varies between 350 and 750 hz.

What kind of low cost circuit using no more than 3 volts can be used to filter, attenuate, and drive 16 - 32 ohm headphones?

Any ideas?
  Reply With Quote
Old 1st December 2003, 06:21 PM   #2
diyAudio Member
 
Join Date: Feb 2003
Try making a voltage divider with two low value resistors between 0v and 3v to get 1.5v. Make this the headphone ground. Then you could connect the headphone + to a data line. The chip should be able to handle the current, if not, use a buffer. If your clock speed is high enough, you should be able to generate the wave using software.
  Reply With Quote
Old 1st December 2003, 07:20 PM   #3
diyAudio Member
 
Join Date: Nov 2003
Location: fort collins co
Default Driving headphones with PIC microcontroller

You are saying that the walkman-type headphones need around a 1 1/2 volt signal. OK, so I generate a square wave with the PIC, then I could use a bndpass filter to smooth it to a sine wave to sound better, then direct drive the phones, using the voltage divider you describe.

But, need a way to adjust volume, and prefer to use two PIC lines to read two pushbuttons, then maybe drive I/O lines to a transistor somehow to control volume.

Any ideas on how to do this?
  Reply With Quote
Old 1st December 2003, 11:46 PM   #4
diyAudio Member
 
Join Date: Feb 2003
Well if you want a sinewave then sure, use a filter but be aware that it will be frequency dependent and won't be a perfect sinewave at all frequencies.

For volume control you can use a data port as a DAC. I'm assuming you are working with 8 bits here. If you want to use all 8 bits of precision for volume, between your headphone line and data lines use resistors like this:

1 ohm on D0
2 ohm on D1
4 ohm on D2
8 ohm on D3
16 ohm on D4
32 ohm on D5
64 ohm on D6
128 ohm on D7

Values don't have to be exact and you'll probably need 2 resistors in series to get close. If you don't need this much precision you could leave out as many as you wanted. Anyway for silence on the output, you could set the output to 128. if you wanted a quiet signal you could write 130 and 126 to the output alternately. For the loudest signal, you would write 0 and 255 alternately. You would probably need a comparator and latch to hold the output and assign a memory address to the sound output.
  Reply With Quote
Old 2nd December 2003, 12:39 AM   #5
diyAudio Member
 
Join Date: Nov 2003
Location: fort collins co
Thanks for helping again!

What is the reason for 2 resistors in series "to get close"?

Why set output to 128 for silence? Is setting it to any value continuosly the same result - silence? Wouldnt I just set all PIC pins to input mode or low to get silence?

For quiet signal you suggest "130 to 126 alternately". Why not 0 and 4 - is it the same result?

I understand your suggestion to use memory address for each sound level, but what is the purpse of a comparator and latch? If the PIC pins are simply held in the chosen state (on or off) for the right amount of time there is not need for the latch and comparator, right?

I have 4 I/O pins available, and think I can come up with two more, toal 6. As you advise limit of filter across frequency 300 to 800 hz, want to use the 4 - 8 pins to control volume and make pseudo sine wave - step function. Im thinking about how to do this now.

Thanks again for helping,and if you could help with my questions here I would surely appreciate it.

Ransom Peek
  Reply With Quote
Old 2nd December 2003, 11:49 AM   #6
diyAudio Member
 
Join Date: Feb 2003
Quote:
What is the reason for 2 resistors in series "to get close"?
Well first I suggested starting with a 1 ohm resistor. I have no ideas what the current drive capabilities of your PIC are so you may want to take all those values and multiply them by 10 to be safe. What I meant with two resistors to get close is this: You can probably find a 10 ohm resistor for D0. For D1 it may be difficult to find a 20 ohm resistor so put 2 10 ohm resistors in series. For the 80 ohm resistor you'll have to use 33 and 47 ohm resistors in series.

Quote:
Why set output to 128 for silence? Is setting it to any value continuosly the same result - silence? Wouldnt I just set all PIC pins to input mode or low to get silence?
Yes, you could. I was assuming there weren't any caps in the picture. The reason for setting the output to 128 is becase it would give 1.5v on the output and 0v across the headphone leads.

Quote:
I understand your suggestion to use memory address for each sound level, but what is the purpse of a comparator and latch? If the PIC pins are simply held in the chosen state (on or off) for the right amount of time there is not need for the latch and comparator, right?
My suggestion was to use a single memory address for sound output. I'm used to processors like 68000 or Z80, I've never done anything with a PIC so if you have dedicated data lines then you would not need a comparator for the address.

If you are sure that the value is held throughout the complete clock cycle then there is no need for the latch. On most processors, the data lines go dead on a low clock cycle which would introduce noise in your output. If your PIC holds the output and changes the value immediately with no dead time on the output then you don't need the latch either.

Quote:
I have 4 I/O pins available, and think I can come up with two more, toal 6. As you advise limit of filter across frequency 300 to 800 hz, want to use the 4 - 8 pins to control volume and make pseudo sine wave - step function. Im thinking about how to do this now.
You should use all the pins available as simply "audio out" and treat it like a DAC. You can control the volume in software.

Quote:
For quiet signal you suggest "130 to 126 alternately". Why not 0 and 4 - is it the same result?
With a capacitor, it will probably have the same result, but remember that your headphone's ground is at 1.5v. Switching between 0 and 4 will alternate the output between -1.5v and -1.45v (with 1.5 as a reference). So you have a large DC voltage being fed into the headphones that could be fixed either in software or with a cap. The reason going between 130 and 126 is better is because it will cause a 0.05VAC signal on the output with no DC offset.
  Reply With Quote

Reply


Hide this!Advertise here!
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sine wave - Square & Triangle wave generator using Transistors / OP-Amps lineup Solid State 20 9th October 2006 01:15 AM
Square wave Prune Everything Else 11 28th June 2004 07:17 AM
Microphone turns triangle wave into square wave? Circlotron Analogue Source 18 25th January 2003 07:44 AM


New To Site? Need Help?

All times are GMT. The time now is 11:26 PM.


vBulletin Optimisation provided by vB Optimise (Pro) - vBulletin Mods & Addons Copyright © 2014 DragonByte Technologies Ltd.
Copyright 1999-2014 diyAudio

Content Relevant URLs by vBSEO 3.3.2