** SOLVED** Change PWM frequency

Hi,

I have a small project where I am controlling the speed of a 4 wire PWM fan.

I had problems using the Arduino Uno at first where when the fan was running slower I was getting a high pitch sound from the fan. After some research I added the following code and then the sound was gone, Great! :slight_smile:

TCCR2B |= _BV(CS20);//set bit
TCCR2B |= _BV(CS21);//set bit

Everything is working how I wanted it to from the Uno..

The problem I have now is my intention was to run the fan from an Attiny85 micro controller, Does anyone know if there is similar code you can run on the Attiny to change the PWM frequency in a similar way to remove the awful sound of the fan?

Thanks for you help

Mike

MikePL:
The problem I have now is my intention was to run the fan from an Attiny85 micro controller, Does anyone know if there is similar code you can run on the Attiny to change the PWM frequency in a similar way to remove the awful sound of the fan?

Without seeing you code I can only offer suggestions...
The tiny85 does not have a timer2 and timer0 us used by the core for millis/micros so best option is timer1.
The timer1 control register is TCCR1 and the values to mess with are CS13, CS12, CS11 & CS10

TCCR1 |= _BV(CS10);//set bit
TCCR1 |= _BV(CS11);//set bit

should compile but might need CS13/CS12 defining.

Thanks for the response Riva

It does compile but it makes no difference to the whining noise from the fan :confused:

I have tried the code you posted and also tried..

TCCR1 |= _BV(CS12);//set bit
TCCR1 |= _BV(CS13);//set bit

I'm I doing this part correctly?

Thanks

I don't know off the top of my head what the default value for CS22 on the UNO is but your setting CS21 & CS20 means either a 32x or 1024x prescaler.
A 32x prescaler on the tiny85 is CS12 & CS11 and a 1024x prescale is CS13, CS11 & CS10. You may need to clear the other bits that do not need setting as they may already be set.

Yeah I'm pretty sure I'm setting it to 32..

Am I clearing the other bits correctly? I'm using this..

TCCR1 &= ~_BV(CS10);
TCCR1 &= ~_BV(CS13);

Also are different pins on different timers? It looks like different pins on the Attiny84 are linked to
different timers but I can't see any mention of it for the Attiny85.

Thanks

When I was testing diffferent bit settings on the Arduino I was getting a different noise from the fan each time until I managed to get rid of the noise.

It doesn't matter what settings I am using here the fan always sounds the same which makes me think I'm doing something wrong and nothing is being changed.

Are both of the PWM pins already using timer 1 or do I need to set them to use timer 1 somehow?

I dont seem to be getting anywhere here, I had it working pretty quickly on the Arduino :frowning: :frowning:

What core are you using for the tiny85 as some only have 2x PWM and others have 3x PWM.

What pins are you using for the fan. Might be worth drawing a picture as the tiny85 pins differ between cores.

Also as mentioned in an earlier reply it would help to post your code or a working example if you don't want to release it all.

Im pretty sure I have a 2 x PWM chip, pin 0 & 1.
The PWM feature works perfect on both of those pins but the fan has a whining noise at slower speeds like it did on the Arduino before I added to PWM frequency change code.

I have a PWM fan wired to a 12v PSU and the tiny85 powered from a 5v source and they share a common ground. Then I have the fan PWM wire on pin 0 which is working as it should but it's whining. I have also had it working on pin 1.

I don't have much code to show, I am not going to write the code until I can get rid on the noise of the fan using the tiny85 like I did with the Arduino which is working perfectly but here is what I'm using to test it.

void setup() {

//Arduino code that removed the fan whining noise

//TCCR2B |= _BV(CS20);//set bit
//TCCR2B |= _BV(CS21);//set bit


//Tiny85 code to remove the whining but it is not working

TCCR1 |= _BV(CS12);//set bit
TCCR1 |= _BV(CS11);//set bit

TCCR1 &= ~_BV(CS10);//clear bit
TCCR1 &= ~_BV(CS13);//clear bit


pinMode (0, OUTPUT);

}

void loop() {

analogWrite (0, 50);
delay (3000);
analogWrite (0, 100);
delay(3000);
analogWrite (0, 255);
delay(3000);


}

I'm not convinced the tiny85 code I'm using to set the bits is even doing anything to be honest, Am I writing it correctly? Or Is the PWM not using timer 1 maybe?

Thanks

Mike

The code you posted above seems to be for the tiny85 as you have the TCCR1 stuff (UNO uses TCCR2B) but your analogWrite is for pin 11 (a neat trick on an 8 pin device :wink: )

EDIT:
The core here supports PWM on 3 pins of the tiny85

and only pin PB4 are linked to timer1 so pin 0/1 are no good for the frequency change unless you alter timer0 instead.
Code from this core...

const uint8_t PROGMEM digital_pin_to_timer_PGM[] = 
{
 TIMER0A, /* OC0A */
 TIMER0B, /* OC0B */
 NOT_ON_TIMER,
 NOT_ON_TIMER, 
 TIMER1B, /*OC1B*/
 NOT_ON_TIMER,
};

Sorry I was testing it on the Arduino on pin 11 last before I posted the code and I forgot the edit it back to pin 0.

I thought the pins I were using might not be using the timer 1, I'm just picking the kids up from school but will try PB4 when I'm back in about an hour, I'll let you know if it works.

Is that code you posted a replacement for the code I'm using already?

Thanks

Mike

MikePL:
I thought the pins I were using might not be using the timer 1, I'm just picking the kids up from school but will try PB4 when I'm back in about an hour, I'll let you know if it works.
You need to ensure the tiny85 core your using is that version (or something compatible) as not all cores support 3 PWM pins.

Is that code you posted a replacement for the code I'm using already?
No, It's the entry from the tiny85 core I linked, to do with PWM. I posted it so you can see what pins support PWM and what timers they use by default.

Hi Riva

I've had some success but it still needs tweaking.

PWM is working on PB4 and with 11 & 12 on and 10 & 13 cleared I get a higher pitched hum but it's even more annoying if anything but at least it's changing so I know I'm on the right track :slight_smile:

I'm so so grateful for your help, I was close to just giving up.

Any idea how I can get it higher, I believe that's what I need to do to make it inaudible?

Ignore that last message, I've done it...

I removed

TCCR1 |= _BV(CS12);//set bit

and it is now silent :slight_smile: :slight_smile: :slight_smile:

You have honestly made my day Riva

Thank you very much!

Glad you got it working okay.
Just for future reference I will post the clock divider table from the tiny85 datasheet though.
Untitled.jpg