Arduino Nano; 16 MHz or 8 MHz?

Long story short, I'm setting up Timer2 to a 1 millisecond duration.

I did this a while back. Developed on an UNO, then changed to a Pro Mini. That's when I found the OCR2A register is determined by the clock speed. For 16MHz UNO and MEGA set to 249, for the 8MHz Pro Mini set to 124.

I'm using the same code on an Arduino Nano, which according to the specs on the website here is 16Mhz, same as UNO and MEGA. So OCR2A should be 249, based on previous information. Setting to 249 was resulting in a 2x duration. I dropped to 124 and it is right on. Why 124 for a Nano, that is listed as 16Mhz? Is there another factor I have not yet learned?

Here is the relevant section of setup()

 TCCR2A = bit(WGM21);          // normal operation
  TCCR2B = bit(CS20) | bit (CS22);   // CTC, scale to clock / 1024
   // compare A register value (1000 * clock speed / 1024)
  OCR2A = Timer2_OCR2A;        // 124 @ 8 MHz, 249 @ 16MHz
  TIMSK2 = bit (OCIE2A);             // interrupt on Compare A Match

How do I check if the board is running at 8MHz or 16MHz (I don't currently have access to a scope).

How do I check if the board is running at 8MHz or 16MHz

Run the Blink example (File > Examples > Basics > Blink). If a complete cycle takes 4 seconds, you have an 8 MHz clock and a 16 MHz board selected.

When using the timers, you need to double check the clock divider setting as well as any other relevant timing constants.

It's supposed to be 16MHz (isn't it?). 5V is 4.96V. Any idea what gives or what to check?

And no I didn't get it from eBay, Ali, Amazon etc, but from a retailer on the Arduino list.

You will find NANOs being sold with both 8 and 16MHz clocks, both 3.3V an 5V, and both 328 and 168 MCUs. You have to know what you're buying...

Regards,
Ray L.

How do you "know what you are buying" when all you have to go on is the description on the webpage? It was listed as 5V 16MHz 328P. It is running at 5V and has a 328P chip, but appears to only be running at 8MHz. 328P cannot run 16Mhz at 3.3V, only at 5V (reliably per datasheet).

How can I confirm it is 8MHz to convince the retailer we both got taken?

The register settings will make it clear what the clock is, and you can confirm easily by timing 10 seconds using mills() and measuring it with a stopwatch.

Regards,
Ray L.

RayLivingston:
The register settings will make it clear what the clock is, and you can confirm easily by timing 10 seconds using mills() and measuring it with a stopwatch.

Regards,
Ray L.

Can you please expand on the register settings? Are they registers I can read programatically and send to the serial monitor?

The registers for ANY device that depends on the clock, which is pretty much all of them. The speed of the ENTIRE chip depend on the input clock speed, and the register settings. If your clock speed is wrong, EVERYTHING will be running either half speed, or double speed. This will affect the instruction rate, the UART, the I2C, SPI, all the timers, everything. If you have to set the BAUD rate to 19200 to get it to communicate with another known good board set at 9600, you can be pretty sure you're running at 8MHz.

Most of the sellers on EBay know NOTHING about the products they're selling. If you think the boards are wrong, request a refund for being "not as advertised", and you'll get your money back. You probably won't even have to return them, and eve if you do, it is up to the seller to provide a pre-paid mailing label.

Regards,
Ray L.

If you are using a bootloader, it is pretty hard to load a program on an 8MHz board if you have selected the 16MHz version, because the serial Baud rates won't be correct.

Are they registers I can read programatically

Of course you can, with Serial.print, for example. The timer registers are all named as found in the data sheet.

void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(LED_BUILTIN, OUTPUT);
  Serial.begin(9600);
  Serial.print("Timer 1 TCCR1A ");
  Serial.println(TCCR1A, HEX);
}

RayLivingston:
Most of the sellers on EBay know NOTHING about the products they're selling. If you think the boards are wrong, request a refund for being "not as advertised", and you'll get your money back. You probably won't even have to return them, and eve if you do, it is up to the seller to provide a pre-paid mailing label.

Regards,
Ray L.

see post #2. NOT from eBay, Ali, Amazon, but from a retailer on the Arduino approved list.

jremington:
If you are using a bootloader, it is pretty hard to load a program on an 8MHz board if you have selected the 16MHz version, because the serial Baud rates won't be correct.

There's no option in the Arduino IDE to select the clock speed for the Nano. Take a peek if you are using the Arduino IDE.

There's no option in the Arduino IDE to select the clock speed for the Nano. Take a peek if you are using the Arduino IDE.

That would depend on which board library (boards.txt) you have loaded. There are many.

jremington:

void setup() {

// initialize digital pin LED_BUILTIN as an output.
 pinMode(LED_BUILTIN, OUTPUT);
 Serial.begin(9600);
 Serial.print("Timer 1 TCCR1A ");
 Serial.println(TCCR1A, HEX);
}

Your output is:

Timer 1 TCCR1A 1

jremington:
That would depend on which board library (boards.txt) you have loaded. There are many.

I don't know of any. I would have to say I only use the one issued with the IDE. After the mess with the fmalparida I2C LCD library, I mess with the install as little as possible.

I don't know of any.

Good to know, thanks.

jremington:
Good to know, thanks.

You're welcome.

How are the results of your sketch interpreted? What is the TCCR1A == 1 tell us?

Have you cleared all the presets out of the timer registers before putting in the new settings.

//initialize registers prior to setting new modes
TCCR2A = 0;
TCCR2B = 0;

What do your print out values show for TCCR2A and TCCR2B?

cattledog:
Have you cleared all the presets out of the timer registers before putting in the new settings.

//initialize registers prior to setting new modes

TCCR2A = 0;
TCCR2B = 0;

Yes and shouldn't need to. The new values are fully assigned.

 TCCR2A = bit(WGM21);          // normal operation
  TCCR2B = bit(CS20) | bit (CS22);

cattledog:
What do your print out values show for TCCR2A and TCCR2B?

TCCR2A=2 bit(WGM21)
TCCR2B=5 bit(CS20) | bit (CS22)

Don't forget this works perfectly on the UNO, Mega, and Pro Mini. This isn't my first rodeo. The setup is copied from Nick Gammon's website. Hopefully we all feel that is a reputable source.

What is the TCCR1A == 1 tell us?

I can't believe you are asking this question.

TCCR1A = 1 tells you that bit zero of TCCR1A is set, which, if you look at the data sheet, is WGM01, corresponding to Timer Mode 1, phase correct 8 bit PWM (assuming that the other WGM bits are zero). See Table 16-4. Waveform Generation Mode Bit Description

If you are going to be programming timers, this sort of analysis should already be obvious to you. If it isn't obvious, come back when you have actually studied the data sheet.

Small wonder that you are having trouble!

jremington:
Small wonder that you are having trouble!

Trouble with what? Being sold a product with different specifications or trying to find a register (or couple of register) set inside the 328P that would indicate the chip is running at 8Mhz vs 16Mhz? And by set, I do not mean set by the sketch. Remember the target is to obtain evidence the board does not meet the specification as advertised and sold.

jremington:
I can't believe you are asking this question.

TCCR1A = 1 tells you that bit zero of TCCR1A is set, which, if you look at the data sheet, is WGM01, corresponding to Timer Mode 1, phase correct 8 bit PWM (assuming that the other WGM bits are zero). See Table 16-4. Waveform Generation Mode Bit Description

If you are going to be programming timers, this sort of analysis should already be obvious to you. If it isn't obvious, come back when you have actually studied the data sheet.

That's the obvious part. I know bit 0/WGM10 is set. What I am asking is how TCCR1A == 1 is relevant to 16MHz vs 8Mhz. The Purpose of the thread.

I guess it wasn't obvious enough from the OP that this isn't the first time I have setup and used the T2 registers. I have this working on several Arduino platforms. Currently, I'm thrown off by the fact that (a) the Arduino site only lists a 16MHz version, (b) the authorized distributor lists the same 5V 16MHz 328P, (c) it would appear, that unless I made two compensatory errors, I have it configured correctly to provide the correct timing, (d) the timing is exactly double, which leads to the circumstantial conclusion that this isn't a 16MHz board as advertised. I do not take (a) as comprehesive, but combined with (b) expected to only be purchasing authorized (likely geniune) boards.

I was hoping there was someone on board that was aware of a register or registers in the 328P that would indicate directly the clock speed. I did not see any in the datasheet, hence the post. Hoping that someone with even more experience would know of a register or set of registers to conclusively determine this and thus request a refund. I'm sure making a phone with the "evidence" I have no will get me as far as this thread has gone.

adwsystems:
Yes and shouldn't need to. The new values are fully assigned.

 TCCR2A = bit(WGM21);          // normal operation

TCCR2B = bit(CS20) | bit (CS22);



TCCR2A=2 bit(WGM21)
TCCR2B=5 bit(CS20) | bit (CS22)

Don't forget this works perfectly on the UNO, Mega, and Pro Mini. This isn't my first rodeo. The setup is copied from Nick Gammon's website. Hopefully we all feel that is a reputable source.

Several methods have been put forward in this thread that allow you to determine the actual CPU clock speed.

The Blink example, reply #1, is one of them.