differences between Uno and Beetle (=Leonardo)?

Hi,

With my Arduino Uno I can change the PWM frequency on pin 9, 10 and 11 like this: ... TCCR1B = bit (CS12); // T = 8 ms f = 125 Hz TCCR2B = bit (CS21) | bit (CS22); // T = 8 ms f = 125 Hz ... When I try this on my Arduino Beetle (which is more or less the same as a Leonardo), I get a compiler error about unknown CS21 and CS22. Why is that and how can I manage the frequency on the Beetle board?

With kind regards, Dick van Fulpen, Houten (NL)

Perhaps they are called different. Read the datasheet about the TCCR1B and TCCR2B register. As far as I know, the avr gcc compiler uses the same names for the bits as in the datasheet.

Peter, thanks for your reply. I have found this document: Creating a variable frequency PWM output on Arduino UNO / by Sami Mughal, April 7, 2011 with other suggestion to program registers, but obviously for the UNO! So I have tried is in a sketch: ... // alternative mode for setting bits // see http://www.arduino.cc/playground/Main/TimerPWMCheatsheet //TCCR0B = TCCR0B & 0b11111000 | 0x01; //for timer 0, effects millis() and delay() TCCR1B = TCCR1B & 0b11111000 | 0x01; //for timer 1, pin 9 and 10 TCCR2B = TCCR2B & 0b11111000 | 0x01; //for timer 2, pin 11 (and 3) ... But that gives this error message from the compiler: pwm_9_10_11_125Hz.ino: In function 'void setup()': pwm_9_10_11_125Hz:37: error: 'TCCR2B' was not declared in this scope

I guess that there are only two PWM registers in the Arduino Leonardo? I did not yet check with an oscilloscope what the outputs do with using TCCR1B and TCCR0B, will do so tomorrow :-)

Kind regards, Dick van Fulpen, Houten (NL)

See the datasheet. TIMER0 is 8-bit, TIMER1 and TIMER3 are 16-bit. Number 2 does not exist.

Peter,

Thanks for the info! Using TCCR3B (and CS31 and CS32) does work indeed.

I will use the Beetke to control the speed of three model trains and a frequency of 125 Hz is quite good for this.

Thanks again. Kind regards, Dick van Fulpen

The standard Arduino function for pwm is analogWrite(). It uses a frequency of about 480Hz. That frequency might be audible in some motors, but a model train is already making noise when riding on the rail. Are you sure you want to use the direct register programming ?

I thought that all model trains have a controller inside these days ? (except my 3 Marklin locomotives).

Peter,

I have Märklin trains of scale Z (1:220), which are too small to have a little board built into it. And to control the motor with PWM it works better with a frequency of around 100 Hz.

Kind regards, Dick van Fulpen