Low PWM freqency

Hello,
is there a way to generate 1.5 or 2.5 Hz pwm signal without changing timers?

No they won't go that slow, due to the limit of the prescaller divider circuit.

Possibly something like below as a guideline

void loop()
{
  while(1)
  {
    digitalWrite(yourPin, HIGH);
    delay(200);
    digitalWrite(yourPin, LOW);
    delay(200);
   }
 }

Hello
Yes you can.
Take the example BLINKWITHOUTDELAY from the IDE and modify the blinkrate to you requirements.

This is a duty cycle program I wrote for Nano / UNO, might give you some ideas, type h in the serial monitor plus a number for Hertz or a p and number for duty cycle, sorry, whole numbers only.
Example: h3,p25 [ENTER] for 3Hz and 25% duty cycle.


unsigned long cycleStart,
          onTime,              
          cycleTime,
          hz;
int val;              
const byte powerPin = LED_BUILTIN;
byte percent = 50;
void setup()
{
  Serial.begin(9600);
  Serial.println(" Hz = 5  Duty Cycle = 50%");
  hz = 5;
  cycleTime = 1000000UL / hz;
  onTime = cycleTime / 2;  
  pinMode(powerPin,OUTPUT);
}
void loop()
{
  bitWrite(PORTB,5,micros() - cycleStart < onTime);  
  if(micros() - cycleStart > cycleTime)
cycleStart += cycleTime;
  // if there's any serial available, read it:
  if(Serial.available() > 0)
  {
 val = Serial.read();
if(val == 'h' || val == 'H')
  hz = Serial.parseInt();
else if(val == 'p' || val == 'P')
{
  percent = Serial.parseInt();   
}
else
  while(Serial.available() > 0)
    Serial.read();  
cycleTime = 1000000UL / hz;
percent = constrain(percent, 0, 100);
onTime = cycleTime * 0.01 * percent;
cycleStart = micros();
Serial.print(" Hz = ");

Serial.print(hz);
Serial.print("  Duty Cycle = ");
Serial.print(percent);
Serial.println("%");
  }
}

2.5 Hz is REALLY slow for a microcontroller. Any reason you can't bit-bang it?

Curious: Why are the timers off limits?

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.