# generating 2 mhz clock with arduino

how would i generate a 2mhz clock with the arduino?

Lots of ways but it depends on what else you want to do with the arduino.

The simplest way is to use one of the counter timers, work out the right ratio and let it toggle the output pin.

how would i do that?

Google Arduino, playground timer1 and timer2

Read this and the source of the libraries carefully and you have almost all the knowledge you need.

thanks ill do that

i just red the docs and it says the max clock is 1mhz is there a way to up that to 2mhz?

What about a “blink” program without delay ? I don’t know “how” fast it would be. Here my math : 2 Mhz 1 / f = t

1 / 2 000 000 = 0.5 u second <–Full period time 0.5 u second / 2 = 0.25 u second ← For each period ( ON-OFF) @ 50 % Duty

Well that one way to do it to generated a square wave. I don’t know the time the ATmega will take to do one instruction like digitalWrite. Well if know, you may calculated the frequency…Heh it just an idea.

``````void setup()
{
pinMode(12,OUTPUT);
}

void loop ()
{
digitalWrite(12, HIGH);
digitalWrite(12, LOW);
}
``````

the problem is wail generating the signal it also has to read spi data.

@grandel

You right, it may not have enought time.

``````const unsigned long halfPeriod =250UL * 1000UL;
const int clockPin  = 2;
void setup ()
{
pinMode (clockPin, OUTPUT);
}
void loop ()
{
digitalWrite (clockPin, HIGH);
delay (halfPeriod);
digitalWrite (clockPin, LOW);
delay (halfPeriod);
}
``````

Like in the morality tales, be careful what you ask for, and how you ask for it. ]:)