Show Posts
Pages: 1 ... 3 4 [5] 6 7 ... 86
61  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 28, 2013, 09:48:35 pm
Well, I've gone though the document and I think maybe ICRn is used when triggering an interrupt on some input on a pin, so I guess whether I should use it or use OCRnA or not depends on what I want to use pin 9 for.  If I want to use it for PWM then I'd use ICRn, and if I just want to use it for general input or output then it doesn't matter which I use.
62  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 28, 2013, 08:57:10 pm
Hm...

This is so simple yet so difficult to understand, but I think I'm starting to get it.



So if we look at mode 0 there, the TOP value is set to 0xFFFF.   That's all it can ever be, so when we set OCR1B to adjust the PWM duty cycle, it's relative to that. 

IE: 
OCR1B / 0xFFFF = PWM duty cycle (0 .. 100%)

And the TOP value (along with the prescalar, and choice of fast or phase correct pwm mode) defines our PWM frequency. 

You gave these equations for that:
- in fast PWM mode:    F_CLK/(prescaler * (1 + TOP))
- in phase-correct PWM mode:    F_CLK/(prescaler * TOP * 2)

So with a TOP value of 0xFFFF (65535), and a F_CLK of 16000000, and a prescalar of 1, we get 16000000 / 65536 = 244hz

And that PWM will be 244hz no matter what we set our duty cycle to with OCR1B.  The duty cycle merely adjusts the ratio of how long the pulse is on vs. off.

Now, since I need a PWM frequency much higher than 244hz, for fast PWM (with a prescalar of 1)  I can calculate the TOP value I need like so:   16000000 / frequency = TOP.

So for 30khz I would need a TOP value around 530.  Or for 15khz, I would need a TOP value around 1067.  Since I like to stick to nice round binary numbers though, I'd choose 511 or 1023, which give me 31250 hz or 15625 hz.

Of course I can't use mode 0 if I want to change the TOP value.  I have to choose one of the other modes. 

I could choose a mode where I set the TOP value using OCRnA, but then I would lose the ability to do PWM on port 9.  Not that I want to; just noting it.   

Or I could choose a mode where I set the TOP value with ICRn.  Unfortunately I have no idea what that register is or what side effects that may have.  (And if there are none, then why would you ever want to use OCRnA?)

But what of these 8 bit, 9 bit, and 10 bit modes?

I have just realized that what those mean is that in those modes, TOP will be either 255, 511, or 1023.  No need to set another register to determine it's value.

So with fast PWM, I could choose 9bit or 10bit mode to get the 31250 hz or 15625 hz I need.

Or, if I choose a phase correct mode (with a precalar of 1), since TOP counts both up and down, my frequency will be halved.  So I would choose either 8bit or 9bit mode to get 31250 hz or 15625 hz PWM.

Now to see if I can figure out what ICRn is and why I would or would not want to use that all the time.
63  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 28, 2013, 01:48:18 pm
I don't want 16 bits of PWM resolution but rather to be able to set the top value to a value greater than 255.
64  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 28, 2013, 12:25:22 pm
I'm still confused about how to set this up.

I guess what I want to do is set up timer 1 to be phase correct, with a prescalar of 1 and a count of 511. 

But reading this tutorial it doesn't make sense:
http://arduino.cc/en/Tutorial/SecretsOfArduinoPWM

Quote
Similarly, the timer can be configured in phase-correct PWM mode to reset when it reaches OCRnA. Phase-Correct PWM with OCRA top The following code fragment sets up phase-correct PWM on pins 3 and 11 (Timer 2), using OCR2A as the top value for the timer. The waveform generation mode bits WGM are set to to 101 for phase-correct PWM with OCRA controlling the top limit.

Code:
  pinMode(3, OUTPUT);
  pinMode(11, OUTPUT);
  TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM20);
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 180;
  OCR2B = 50;

I see bit 0 being set in TCCR2A there.   And I see bit 2 being set in TCCR2B.  But the tutorial says "The waveform generation mode bits WGM are set to to 101 for phase-correct PWM".  Neither 001 or 100  are 101.

I also noticed it seems possible to set up the timer to have an 8 bit 9 bit or 10 bit count, and I suspect this code is setting it up to be 8 bit and I don't know how to set it to phase correct 16 bit.  There's so many options and I have no idea which are the right ones.  It mentioned phase correct AND frequency correct in one option and I didn't even know frequency correct was an option or how that affects the PWM.
65  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 20, 2013, 08:16:07 pm

Quote
A prescaler of 256 will give you a much lower PWM frequency. The output frequency is:

- in fast PWM mode:    F_CLK/(prescaler * (1 + TOP))
- in phase-correct PWM mode:    F_CLK/(prescaler * TOP * 2)

If you are looking for about 16kHz PWM, then if TOP = 255 (the default), you would need a prescaler of 4 in fast PWM mode, or 2 in phase-correct PWM mode - but the prescsaler does not support these values.

I'm still a bit confused here. 

In  fast PWM mode I have:
16000000/(prescaler * (1 + TOP))

If TOP is 0, then PWM frequency is 16000000/prescaler.   If prescalar is 1 then that would mean the PWM frequency is 16mhz.  But that can't be right, can it?  The frequency would be half that because it's set high for one cycle and then low for the next, wouldn't it?  So the max PWM frequency would be 8mhz. 

And that means if I used a prescalar of 1024 in fast PWM mode, my frequency would be half of 15625 or 7812 hz, right?'

So if I used a prescalar of 256 in fast PWM mode,  16mhz / 256 = 62500, which divided by 2 = 31250.

But perhaps when you talk of PWM frequency you're talking about how fast the pin toggles rather than counting full on/off cycles as you would if you were talking about the frequency of a sound? 
66  Using Arduino / Motors, Mechanics, and Power / Re: Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 20, 2013, 01:02:50 pm
Thanks for the info. 

I assume you meant to write 16000000 and not 16000 there. 

The motor driver I am using is the following:
http://www.pololu.com/product/2136

I don't see any specs as to the max drive frequency on either that page or in the chip's datasheet.

So if I use a TOP value of 511 and a prescalar of 1 in phase correct mode, I will get a frequency of 15656 hz..

Is there a benefit to using phase correct mode here?  I've read about that but I still don't really understand the difference aside from it taking twice as long to count up and then down again.  But perhaps that's the point here?  To emulate having a prescalar of 2 available? 

And if that's the case  could I not use the prescalar of 256, which when doubled would give me 512 like I need, thus allowing the use of analogWrite(), and all the pins labeled A, without requiring the use of the ICPn register?
67  Using Arduino / Motors, Mechanics, and Power / Need to set PWM frequency on Micro/Leonardo to 16khz for motor control on: November 19, 2013, 05:24:56 am
I have an Arduino Micro and I am controlling a motor with PWM but the 490hz frequency of the normal PWM can be heard coming from my motor and through my speakers (I have an MP3 module attached to my board as well).

I need to change one of the timers, and I'd rather it not be timer 0 because that would mess up millsecs(), or timer 4 because I may wish to switch to using a Pro Mini instead of the Micro.   And since the Micro doesn't have a Tmer 2, that leaves Timer 3 or Timer 1.

Here are the pin mappings for the timers on the Micro:

Quote
   TIMER0B,      /* 3 */
   TIMER3A,      /* 5 */
   TIMER4D,      /* 6 */
   TIMER1A,      /* 9 */
   TIMER1B,      /* 10 */
   TIMER0A,      /* 11 */
   TIMER4A,      /* 13 */

There's a few things I'm confused about at this point. 

First, if I set a prescalar of 1024 then that gives 16000000 / 1024 = 15625.  Now does that mean my max PWM frequency will be 15625?  In other words, will be pin be toggled 15625 a second?  Or, will it actually be toggled at half that speed?  I'm thinking it's half, because you can apparently set a prescalar of 1 on some timers and obviously the microcontroller can't toggle a pin on and off in the same cycle.  So that would mean my max PWM frequency would be 7812 hz which is too low. 

I guess one option to  is to use the 256 prescalar, but then my max frequency is gonna be 31250 hz.  If I set the output compare A I guess I can halve that to get 15625.  But that brings me to the second thing I'm confused about.

I believe I can't vary the PWM frequency on pins marked with an A if I want to have that finer control of my output frequency using the compare A.  Is that correct? 

If so, that would seem to leave me with only one option, if I want a PWM frequency of 15625 hz... use TIMER1B on Pin 10.

But is there any real downside to using a PWM frequency of 31250 hz with my motor?  It seems that since 15625 hz is still within range of our hearing that if I use that I may still be able to hear the noise on my speakers even if the motor doesn't make a sound at that frequency.  And if I use the higher frequency will that then allow me to use pin 5 (TIMER3A) and  9 (TIMER1A)?

And how do I go about setting this anyway?  I mean if I just change the prescalar can I just use the standard PWM functions?  Or are those limited in precision in some way and I should just set the compare B directly to adjust the frequency?   It seems timers 1 and 3 are 16bit on the Micro, but I think the pwm functions only use 8 bits of precision?   I guess 8 bits is sufficient, I only need to run the motor at a minimum speed of 1/256.
68  Using Arduino / Audio / Re: Has anyone else had trouble interfacing the Arduino Micro with the WT5001M02? on: November 10, 2013, 10:21:02 pm
Nevermind, it appears to have been an issue with the line-out.  For some reason, with one polarity the board won't work, and with the other it does.  Perhaps the amplifier input is not differential? 
69  Using Arduino / Audio / Has anyone else had trouble interfacing the Arduino Micro with the WT5001M02? on: November 10, 2013, 09:57:12 pm
I had the WT5001 connected to an Arduino Nano, and it was working fine.  I then connected it to an Arduino Micro and now I can't get it to work. 

I am using the Serial1 class instead of the Serial class with the Micro btw.  That's necessary because on the Micro the Serial class refers to the USB serial.  I have the module attached to the RX and TX pins. 

And yes I've made sure I've got the pins connected the right way round.  I have also checked that the module is getting power.  I read 5V at the power pins.  However, I did notice when I disable the serial and just set the TX pin high, I read only 4.7v, and on the RX pin  if I do the same I read only 4.15v.  The RX is not necessary to use the module though and I've tried disconnecting it.

I've even double checked that the module is still working by connecting the Nano back up to it using the same cables and it works just fine.

Here's the code:
Code:
const int buttonPinFire = 7;   // FIRE

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
 
    // initialize the button pin as a input:
    pinMode(buttonPinFire, INPUT);
   
    pinMode(2, OUTPUT);
    digitalWrite(2, LOW);
   
    pinMode(3, OUTPUT);
    digitalWrite(3, LOW);

    //analogWrite(3, 32);


/*
    pinMode(1, OUTPUT);
    digitalWrite(1, LOW);

    pinMode(0, OUTPUT);
    digitalWrite(0, HIGH);   
*/



 // initialize serial communication:
    Serial1.begin(9600);
   
    Serial1.write(0x7E);
    Serial1.write(0x03);
    Serial1.write(0xA7);
    Serial1.write(0x1F); //  volume max
    Serial1.write(0x7E);
   
   
    // Play all songs in order.
    Serial1.write(0x7E);
    Serial1.write(0x03);
    Serial1.write(0xA9);
    Serial1.write(0x02);
    Serial1.write(0x7E);   

    // start sound
    Serial1.write(0x7E);
    Serial1.write(0x04);
    Serial1.write(0xA0); // A0 for SD card
    Serial1.write((byte)0x00);
    Serial1.write(0x01); // track number
    Serial1.write(0x7E);
    //delay(3000);
   
}
   
void loop()
{
                       
  /* 
  buttonState = digitalRead(buttonPinFire);
   if (buttonState == HIGH) {
 
    Serial1.write(0x7E);
    Serial1.write(0x04);
    Serial1.write(0xA0); // A0 for SD card
    Serial1.write((byte)0x00);
    Serial1.write(0x01); // track number
    Serial1.write(0x7E);
    delay(3000);
  }
  */
 
}

 
70  Using Arduino / Audio / Re: Question about high pass filters on: November 08, 2013, 11:51:27 am
Well I tried adding a second 0.1uf capacitor in parallel with the first and I do get more bass, but it seems to overpower the speaker a bit, so I guess a 200hz rolloff here is best, if that is indeed what I'm getting.
71  Using Arduino / Audio / Re: Question about high pass filters on: November 08, 2013, 11:38:17 am
Sorry, I didn't read the post quite closely enough. However, the first filter you posted is a low pass, not a high pass. What did you actually connect between the sound module and the amp?

I just linked to the wrong page, I did actually construct a high pass filter:
http://sim.okawa-denshi.jp/en/CRtool.php
72  Using Arduino / Audio / Re: Question about high pass filters on: November 08, 2013, 12:40:37 am
I didn't use the first filter with a speaker.  It was placed between the sound module and the amp. 
73  Using Arduino / Audio / Question about high pass filters on: November 07, 2013, 07:40:33 pm
Hey guys,

I've got one of those WT5001-28P mp3 modules hooked up to my Arduino, and playing some tracks through a 12V 10W amplifier.  My speakers are 4 ohms, and kind on the small side.  One is 3" and the other 2.5".   They sound decent, but they're only good down to 100hz.  So when I played the unfiltered tracks which had a lot of bass, the speakers would rumble like crazy.

I figured the way to solve this was to use a high pass filter and filter out the frequencies below 100hz.  So, I went to this page:
http://sim.okawa-denshi.jp/en/CRtool.php
.
..and having a limited set of caps on hand and a limited selection of resistors above 10K, I chose a 10K resistor and a 0.1uf cap which gave a cuttoff of 159hz.  Close enough.

I fitted the components between the module and amplifier, and they seemed to do the job.  But then I remembered that I'd seen speaker crossover designs that only used a cap... no resistor.  So I found the calculator I'd seen that on and punched in 100hz and 4 ohms for my speakers:

http://www.apicsllc.com/apics/Misc/filter2.html

395uf.  Hm... well, I didn't have a 395uf cap on hand, so I couldn't test it but it got me wondering... why did I need the resistor in one filter but not the other?  Is it the inductor?

So I pulled the resistor I had going to ground off my first filter and suddenly the sound I was getting from the speaker got a lot louder, with more bass... but the rumble didn't return.   And now I'm wondering what the heck I've done.

That first calculator won't let me put in 0 for R, but if I put in 1 and set the capacitor to 0.1uf, it tells me the cutoff frequency is over a million hz, and I can assume as R gets lower that would only go up, so why am I getting any sound at all?   And why does it sound like what I might expect a 100hz HPF to sound like, with a lot of bass, but not so much that the speaker starts clipping badly?

I thought maybe the second calculator would give me the answer, so I adjusted the cuttoff frequency to see how it affected the capacitor value and to get anywhere near 0.1uf I have to make it really high, which again would suggest that I should not hear anything but the highest frequencies, if anything.

So what's going on here?  How is a single 0.1uf cap between a DAC and the ampifier input doing this?

(Note that this mp3 module does have PWM outputs, but I am not using those.)

If it helps, this is the amplifier:
http://www.parts-express.com/pe/showdetl.cfm?partnumber=320-604
74  Using Arduino / General Electronics / Re: Build your own solenoid / linear voice coil actuator? on: October 16, 2013, 06:09:53 pm
Yeah, but I'm a little behind on constructing Wheatley so it's all good. smiley
75  Using Arduino / Audio / "Up to 24db gain, which is pretty loud. Hurts your ears." on: October 16, 2013, 08:51:15 am
So I saw LadyAda released a new amplifier:
http://www.adafruit.com/products/1552

But her comments in the video confused me.  I have a 3W amplifier on my boards.  And I would in no way refer to it as "loud".  More like "any quieter than this and I might as well not bother, so why the hell is almost every speaker 8 ohms?"

In a quiet room, 3W is okay... but not awesome.  And definitely nowhere near earsplitting volume.  Hell, even 15W isn't earsplitting.  Standing close to a 20W?  Okay now you can say that hurts your ears.

So what's she on about here?  Specifically, what's this about gain?

My understanding of how amplifiers work is this...  Assuming I have the input set up properly, if I input +2.5V then the amp outputs 5V.  Into a 4 ohm speaker that gives me 2-3W.  I'm not sure of the exact amount.  Furthermore, my DAC will output 2.5v if the sample I input to it is at the maximum value. 

Given this....  assuming I am inputting audio data where the volume has already been maxed out...  By maximizing the peaks, and possibly applying some compression...  What would a 24DB do for me except to create horrible clipping on my output?  I'm not even sure what  24B boost means in terms of how much the voltage is increased, but since the amp would be limited to a 5V output, a boost to the input when the input is already telling it to go to 5V is not desirable.  Cause then when you're telling it to go to 2.5v it's going to boost that as high as it can go as well and that's where your clipping comes from.

So am I missing something here?   Am I right that this boost is only useful if the input voltage is low, due to how the dac  or input to the amp is configured or due to the sound file itself?  Cause if so I don't see how that translates to a measly 3W amp hurting your ears.  Unless maybe she's talking about if you're wearing headphones?
Pages: 1 ... 3 4 [5] 6 7 ... 86