Please Help, stranded. I need to put out 5V square wave of 179Hz

Help, I'm stranded with an alternator this isn't working. From everything I've write the alternator needs a PWM single of 5v at 179HZ.

I've tried every PWM library I can find, and I just don't understand enough about the timmer vars to figure out how to do this.

Can someone walk me through how I could output 179HZ on pin 9 on an Arduino Uno R3?

THANKS!!!

void setup(){
pinMode (9,  OUTPUT);
}
void loop(){
PINB = 0b00000100; // toggle output by writing to input port
delayMicroseconds(2792); // half of period of 179 Hz
}

Or look at Blink without delay code if you want to do anything else.

Thank you so much!!

So I don't see any "write" commands, so the PNB is doing that directly?

In other words is this the full code, or is this psedo code?

THANKS!!!!

So I found another example, so now I'm trying to generate a frequency on pin 9, and then measure it on pin 3 because, the alternator doesn't seem to be getting the correct value, because it's not adjusting it's output.

Is it possile to generate on one pin and measure on another? The frequecy being reported on the serial doesn't match 179, but I'm guessing that has something to do with the fact that this code changes the timer to adjust the frequency? Would that be true? If so how would I adjust to confirm that it is in fact generating 179hz.

I'm just trying to confirm if this issue is in my code or in the alternator at this point.

THANKS!

#define DEFAULT_PWM           179

int16_t currentPWM = DEFAULT_PWM;
byte PWM_PIN = 3;
 
int pwm_value;

// Function Prototypes
void setPwmFrequency(int pin, int divisor);
 
void setup() {     
  pinMode(PWM_PIN, INPUT);    
  pinMode(DR44_PWN_PIN, OUTPUT);  
  setPwmFrequency(DR44_PWN_PIN, 256); 
  analogWrite(DR44_PWN_PIN, currentPWM ); // Start off with 60% duty cycle
  Serial.begin(9600);
  delay(3000);  //wait for engine to start
}
 
void loop()
{
  analogWrite(DR44_PWN_PIN, currentPWM );
  delay(100);
  pwm_value = readMagmeter();
  Serial.println(pwm_value);
}

float readMagmeter (){
  //let's try reading just one pulse :-P
  int durationLow = (float)pulseIn(PWM_PIN, LOW);  //returns period in MICROseconds between low and high pulses, i.e. half of square wave
  float durationMillis = durationLow/1000.000;     //converts to milliseconds
  float freQuency = 1000.000/(durationMillis *2.000); //frequency in Hz
  return freQuency;
}   //end magMeter

/**
 * Divides a given PWM pin frequency by a divisor.
 * 
 * The resulting frequency is equal to the base frequency divided by
 * the given divisor:
 *   - Base frequencies:
 *      o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
 *      o The base frequency for pins 5 and 6 is 62500 Hz.
 *   - Divisors:
 *      o The divisors available on pins 5, 6, 9 and 10 are: 1, 8, 64,
 *        256, and 1024.
 *      o The divisors available on pins 3 and 11 are: 1, 8, 32, 64,
 *        128, 256, and 1024.
 * 
 * PWM frequencies are tied together in pairs of pins. If one in a
 * pair is changed, the other is also changed to match:
 *   - Pins 5 and 6 are paired on timer0
 *   - Pins 9 and 10 are paired on timer1
 *   - Pins 3 and 11 are paired on timer2
 * 
 * Note that this function will have side effects on anything else
 * that uses timers:
 *   - Changes on pins 3, 5, 6, or 11 may cause the delay() and
 *     millis() functions to stop working. Other timing-related
 *     functions may also be affected.
 *   - Changes on pins 9 or 10 will cause the Servo library to function
 *     incorrectly.
 * 
 * Thanks to macegr of the Arduino forums for his documentation of the
 * PWM frequency divisors. His post can be viewed at:
 *   http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
 
Pins 5 and 6: controlled by Timer 0 in fast PWM mode (cycle length = 256)
    Setting   Divisor   Frequency
    0x01    1     62500
    0x02      8     7812.5
    0x03      64    976.5625   <--DEFAULT
    0x04    256     244.140625
    0x05    1024    61.03515625
    
    TCCR0B = (TCCR0B & 0b11111000) | <setting>;
Pins 9 and 10: controlled by timer 1 in phase-correct PWM mode (cycle length = 510)
    Setting   Divisor   Frequency
    0x01    1     31372.55
    0x02    8     3921.16
    0x03      64    490.20   <--DEFAULT
    0x04      256     122.55
    0x05    1024    30.64
    
    TCCR1B = (TCCR1B & 0b11111000) | <setting>;
Pins 11 and 3: controlled by timer 2 in phase-correct PWM mode (cycle length = 510)
    Setting   Divisor   Frequency
    0x01    1     31372.55
    0x02    8     3921.16
    0x03      32      980.39
    0x04    64    490.20   <--DEFAULT
    0x05    128     245.10
    0x06      256     122.55
    0x07    1024      30.64
    
    TCCR2B = (TCCR2B & 0b11111000) | <setting>;
All frequencies are in Hz and assume a 16000000 Hz system clock.
  
 */
void setPwmFrequency(int pin, int divisor) {
  byte mode;
  if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 64: mode = 0x03; break;
      case 256: mode = 0x04; break;
      case 1024: mode = 0x05; break;
      default: return;
    }
    if(pin == 5 || pin == 6) {
      TCCR0B = TCCR0B & 0b11111000 | mode;
    } else {
      TCCR1B = TCCR1B & 0b11111000 | mode;
    }
  } else if(pin == 3 || pin == 11) {
    switch(divisor) {
      case 1: mode = 0x01; break;
      case 8: mode = 0x02; break;
      case 32: mode = 0x03; break;
      case 64: mode = 0x04; break;
      case 128: mode = 0x05; break;
      case 256: mode = 0x06; break;
      case 1024: mode = 0x7; break;
      default: return;
    }
    TCCR2B = TCCR2B & 0b11111000 | mode;
  }
}

Outputing: 206 207 206 207

Do you want a 179 Hz square wave (50% duty cycle) or a variable duty cycle with a 179 Hz refresh rate (PWM)?
A 206 microsecond half cycle (412 uS period) would be a frequency of 2427 Hz.

Read again post #1 except that PINB is toggling pin 10 and should read PINB = 0b00000010;

As an alternative, you can set a timer up to give you the required delay using the prescaler and adjusting the value of OCRnA where n is the number of the timer, all clearly detailed in the datasheet. Stay away from using timer0 though as this is intimately linked to other Arduino functions.