3 Replies Latest reply on Jun 2, 2014 2:36 AM by SpiderKenny

# [Galileo] How to output a square wave of a desired frequency?

Hi,

I feel like this is a basic question, but I'm having a lot of trouble finding this online.

I am using the Galileo board. I need to control some step motors for a project, and to do so I need to output a square function of varying frequencies: at around 500-1000 [Hz].

I tried running the following sketch:

```int funcPin = 3;  // pin num of output function
int freq = 500;  //[Hz]
double period = 1000.0/freq;  //[ms]

void setup() {
pinMode(funcPin,OUTPUT);
}

void loop() {
// set to run at a period defined above
digitalWrite(funcPin,HIGH);
delay(period/2);
digitalWrite(funcPin,LOW);
delay(period/2);
}

```

For all frequencies in the range i wanted, the board output a square wave with a frequency of around 200 [Hz]. I read online that the regular pins can only output up to around 230 [Hz], and that i need to use the PWM pins but other places said all GPIO pins can be programmed to do so. I was not able to find an easy way to program them, however.

So basically im looking for a simple way to output a square wave of a frequency over 500 [Hz].

All help is appreciated!

• ###### 1. Re: [Galileo] How to output a square wave of a desired frequency?

From the schematic, it looks like pins IO3, IO[7:6], snf IO[11:9] can be used as PWM.  I would suspect that the developers of the Intel Arduino stack have it set up such that if you use the analogWrite() function, it'll set the muxes up behind the scenes and program the PWM.  Keep in mind, though, that PWM in general is about changing the duty-cycle of the square wave to vary how much power is being delivered to the load.  The frequency stays constant.  In Arduino, I don't believe you have the ability to specify what the actual PWM frequency is, just the duty cycle.  On an AVR, the underlying hardware can be programmed such that you can approximate various frequencies (and also specify the duty cycle).  But that is, of course, specific to that architecture.

The Galileo boards provide GPIO via a Cypress 40-bit I/O expander (CY8C9540A, datasheet at http://www.cypress.com/?docID=31413).  The Galileo Board User's Guide says it's on the I2C bus at address 0x21 with its EEPROM at 0x51.  If you pulled J2 on the board, the addresses change to 0x20 and 0x50.  Pages 12 and 13 in the Cypress datasheet has the registers that you're going to want to be writing via I2C.  Use the Arduino 'Wire' library to send the commands to the Cypress part to program PWM directly.

• ###### 2. Re: [Galileo] How to output a square wave of a desired frequency?

Is there any reason why you cannot use one of the PWM pins? That would give you a square wave with very precise control over frequency and duty cycle.

Using a loop inside a sketch is going to give you significant jitter and non-deterministic timing at best.

• ###### 3. Re: [Galileo] How to output a square wave of a desired frequency?

Additionally, you can program the PWM fully from within arduino by sending commands to the file system.