Arduino Stack Exchange is a question and answer site for developers of open-source hardware and software that is compatible with Arduino. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am controlling an ADC with my Arduino Uno. I would like the clock of the ADC to be the same frequency of the Arduino. Is there any way that I can have a constant clock output from one of the Arduino pin ?

Thanks,

Liam

share|improve this question
    
Sounds a lot like this question – Gerben Oct 10 '15 at 18:38
up vote 1 down vote accepted

This outputs 8 MHz on pin 9:

#ifdef __AVR_ATmega2560__
  const byte CLOCKOUT = 11;  // Mega 2560
#else
  const byte CLOCKOUT = 9;   // Uno, Duemilanove, etc.
#endif

void setup ()
  {
  // set up 8 MHz timer on CLOCKOUT (OC1A)
  pinMode (CLOCKOUT, OUTPUT); 
  // set up Timer 1
  TCCR1A = bit (COM1A0);  // toggle OC1A on Compare Match
  TCCR1B = bit (WGM12) | bit (CS10);   // CTC, no prescaling
  OCR1A =  0;       // output every cycle
  }  // end of setup

void loop ()
  {
  // whatever 
  }  // end of loop

How can add a prescaler?

You change the prescaler bits. You can look at the datasheet or my cheat sheet here:

Timer 1 bits

You may not need a prescaler, depending on the frequency. Change OCR1A to some number between 0 and 65535 to slow it down.

share|improve this answer
    
This is exactly what I needed . How can add a prescaler ? – Liam F-A Oct 10 '15 at 20:39
    
See amended reply. – Nick Gammon Oct 10 '15 at 22:10

You can use one of the PWM pins on Arduino to output a PWM signal. If you want a constant clock, you need to set the duty cycle of the PWM to be 0.5, i.e. 50%.

Syntax: analogWrite(pin, value) where the parameter "value" is the duty cycle ranges from 0 (always off) to 255 (always on) since it is a 8-bit PWM generator inside Arduino.

If you need a PWM wave with duty cycle of 0.5, you need to set the "value" above to be 127, which is exactly in middle of 0 and 255:

analogWrite(clkpin, 127);
share|improve this answer
    
analogWrite only works on PWM pins, not on the analog pins. – Gerben Oct 10 '15 at 18:37
    
You are right, my fault. – Penthrite Oct 11 '15 at 6:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.