How to set Clock frequency for Arduino UNO

Arduino Uno has an inbuilt clock frequency upto 8Mhz whereas an external crystal frequency 16MHz is also available.

Example i do want to set the clock frequency of arduino uno as 5Mhz.. How to set the clock frequency?

please help with codes.. :cold_sweat:

I don't think you can get the internal clock to generate 5MHz so you will need an external clock source. 5MHz is not a speed the arduino core is written for so bootloader will need replacing with something to suit and other core functions like millis() will be wrong and this may effect some timings/libraries.

Arduino Uno uses external 16 MHz oscillator. You can adjust the system clock prescaler to lower it: 8 MHz, 4,2,1 etc. I need to look to see if that is only changeable by setting fuses using a ISP Programmer, or if it can be changed via user sketch.

If you want to use the internal 8 MHz oscillator, that is definitely a fuse change.

User settable prescaler:

9.11 System Clock Prescaler The ATmega48A/PA/88A/PA/168A/PA/328/P has a system clock prescaler, and the system clock can be divided by setting the ”CLKPR – Clock Prescale Register” on page 367. This feature can be used to decrease the system clock frequency and the power consumption when the requirement for processing power is low. This can be used with all clock source options, and it will affect the clock frequency of the CPU and all synchronous peripherals. clkI/O, clkADC, clkCPU, and clkFLASH are divided by a factor as shown in Table 29-16 on page 312.

Choices are 1,2,4,8,16,32,64,128, 256

See 9.12.2 CLKPR – Clock Prescale Register

I assume you'd have to adjust other settings, like serial speed being used. Say your code was talking at 4800 - then you adjusted the system speed from 16 MHz to 4 MHz. Your code would need to change the serial speed to 4800 x 4 = 19200 so it would still look like 4800 to external devices.

Using external crystal: if using other then 8 MHz or 16 MHz is used, Riva has addressed that already.

thank you…

Am new to this so my questions may be towards the very basic. Please help me out …

  1. By default Will the arduino take 16Mhz ??

  2. When i interface this arduino with a sensor which works between (5Khz to 8Mhz) how should i configure the clock speed of arduino and sensor??

  3. In the previous post : That was an example… But How to calculate that my code was talking at 4800 (any formula or calculations??)

I have enclosed the data sheet of the sensor that am using…

TSL1401.pdf (122 KB)

Your clock rate will be limited by how fast you can sample the analog input. Read up on using the free-running feature of the ATmega328p analog input pins. You can get an interrupt for each analog read and use that to clock your chip. See section 23 of the datasheet. Each analog read takes 13.5 analog clock cycles so you need a 67.5 kHz (or faster) clock to meet the chip's minimum speed of 5 kHz. The analog clock is derived from the system clock with a pre-scale of 2, 4, 8, 16, 32, 64, or 128. I would try a pre-scale of 128 for 125 kHz. That will get you about 9300 samples per second or all 128 pixels 72 times per second.

So this sensor outputs 128 analog levels? With 4000:1 dynamic range, so really want to sample the output with a 12-bit ADC (0 to 4095)?
I think I’d go with external ADC like MCP3201

See Section 6, read the data in two SPI transfers.
Say Port D bit 2 was used for chip select:

PORTD = PORTD & 0b1111011; // clear bit 2
dataArray[offset] = SPI.transfer(0);
dataArray[offset+] = SPI.transfer(0)'
PORTD = PORTD | 0b00000100; // set bit 2
// do what ever with the data

With SPI clock at 8 MHz, these two transfers will take <3uS, put them in a bigger while loop to capture all 128 outputs.
Fleshing out the other 2 control lines, say PORTD bit 3 for SI and bit 4 for the clock:

PIND = 0b00001000;  // toggle bit 3, SI
PIND = 0b00001000;  // toggle bit 3
while (x<256){
PIND = 0b00010000; // toggle bit 4, CLK
PIND = 0b00010000; // toggle bit 4
// capture a sample
PORTD = PORTD & 0b1111011; // clear bit 2
dataArray[x] = spdr(0); nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop; // 17 clocks
dataArray[x+1] = spdr(0); nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop;nop; // 17 clocks
PORTD = PORTD | 0b00000100; // set bit 2
// do something with all the data

Have to experiment a little, see if for loop is any faster/slower than while loop.
After capturing, turn the bytes into ints and do whatever is being done with the data.
Need this at the top of the sketch:

#include <SPI.h>
#define nop asm volatile ("nop")

Run that with logic analyzer, bet it will be pretty close to what’s needed.

Can you please tell me how to configure the CPU clock frequency to 12Mhz (example) using timer ??