Pages: [1]   Go Down
Author Topic: Audio frequency generation on AtTiny85  (Read 4096 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
This couldn't be your ad.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm looking for a simple way to generate audio frequencies with an AtTiny85, so it plays an continuous sound (e.g. at 440hz) when an digital Input is pulled to ground. I found an frequency generation sketch in the playground that is described as simple, it also works on an AtTiny but seems to consist mostly of floating point math. And that's a little bit too hard for me since I am a bloody beginner in audio generation  smiley-confuse .
Logged

There is no blueprint for life, blueprints are for architects!

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Try software DDS, essentially an array.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Didn't quite finish it here.

Say that you want to generate a 400hz sine wave. Store that information in a 256 array.

Set up a timer that trips every 1/(400*256) = 10us. In the isr, increment an accumulator and output the value in the array indexed by that accumulator.

You are done.

If you point the array to other waveforms, you can generate any waveforms as you like.

256 points are a lot for even a good quality wave form. If you want to generate faster signal, you can go to 128, 64 or even 32 points, or simply change the increment to the accumulator.

The output can be anything, a r2r network, or dac or a digital pot.
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4094
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Is tone() an option? OP didn't specify a sine wave.
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

In the event of square waves, you can use software or hardware pwm (50 percent duty cycle).

Hardware pwm can generate signals well into the Mhz.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 1
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to generate pwm signal using analogwrite() in ARDUINO KIt 1.0 .I cant able to identify the pin which generates plsese help me
Logged

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 95
Posts: 4094
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to generate pwm signal using analogwrite() in ARDUINO KIt 1.0 .I cant able to identify the pin which generates plsese help me

Have you had a look at the doc? Might have something about pins there.
http://arduino.cc/en/Reference/AnalogWrite
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Offline Offline
Jr. Member
**
Karma: 0
Posts: 61
This couldn't be your ad.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Didn't quite finish it here.

Say that you want to generate a 400hz sine wave. Store that information in a 256 array.

Set up a timer that trips every 1/(400*256) = 10us. In the isr, increment an accumulator and output the value in the array indexed by that accumulator.

You are done.

If you point the array to other waveforms, you can generate any waveforms as you like.

256 points are a lot for even a good quality wave form. If you want to generate faster signal, you can go to 128, 64 or even 32 points, or simply change the increment to the accumulator.

The output can be anything, a r2r network, or dac or a digital pot.


That sounds simple enough, is there an tutorial on this somewhere? The output is just a speaker and I want to generate a sine wave, so this seems just to be the right thing.
Logged

There is no blueprint for life, blueprints are for architects!

Dubai, UAE
Offline Offline
Edison Member
*
Karma: 22
Posts: 1675
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Introduction here -

http://interface.khm.de/index.php/lab/experiments/arduino-dds-sinewave-generator/

The sinewave is the singularly least interesting of all waveforms, the tone library will produce a far more interesting squarewave, if all you want is a tone or simple tune use the standard tone function - its what you hear in this video and sounds 100 times better than a sinewave -



DDS Can make a great sound, but its an awful lot more work than suggested, here is a simple synth that uses DDS

Not a single sinewave in sight, its all ramps and squares -



More explanation here -

http://rcarduino.blogspot.com/2012/08/the-must-build-arduino-project-illutron.html

Duane B
« Last Edit: October 16, 2012, 11:09:29 am by DuaneB » Logged


Nice, France
Offline Offline
Full Member
***
Karma: 11
Posts: 237
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Say that you want to generate a 400hz sine wave. Store that information in a 256 array.

Set up a timer that trips every 1/(400*256) = 10us. In the isr, increment an accumulator and output the value in the array indexed by that accumulator.

You are done.

I see what you did there. Getting back to the question posed by the OP, with a specific value of 440Hz (standard concert pitch A, for tuning) rather than 400 Hz (which gives 10 μs, albeit with a 2.4% error) you would set up a timer that trips every 8.877841 μs.

Using the formula
Code:
compare match register = [ 16,000,000Hz/ (prescaler * desired interrupt frequency) ] - 1

with a prescaler of 1, this gives
compare match register = 16,000,000 / ( 440 * 256 ) - 1 = 141.045454545454545 = 141, as an 8-bit integer.
The error from integer roundoff is a pretty reasonable 0.03%.
I'm not sure what the allowable error is in the 16MHz Arduino clock oscillator (pointer, anyone)?

But that looks as if it would be suitable for tuning an instrument.
Logged

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

The approach I outlined can generate a sine or any other waveform. You can buffer it to drive a speaker.

There is also another approach: to generate a sine wave via pwm: you will need to load the compare register with the right value to produce a desired wave form. It has the advantage of being very fast but is more difficult to program.
Logged

Pages: [1]   Go Up
Jump to: