PWM pulse generator

Hey guys,
I was trying to make a pulse generator for a project. My goal was to create a square pulse generator of 150 kHz and 5V, using this code:
void setup(){
Serial.begin(9600);
pinMode(2,OUTPUT);
digitalWrite(2,HIGH);
}

void loop(){
digitalWrite(2,LOW);
asm("nop\n nop\n nop\n nop\n nop\n nop\n");
digitalWrite(2,HIGH);
asm("nop\n nop\n nop\n nop\n nop\n nop\n");
}

The purpose of this pulse generator is to connect it to a IRF540N transistor in a DC-DC booster; however, the PWM generated only 2.5V and not the desired value of 5V.

I was wondering whether there is a code I can use to make the PWM generate 5V pulses with frequencies of at least 150 KHz.

As measured with a multi-meter? with a 50% duty cycle? Going from 0 to 5V? That's a good reading.

1 Like

How about adjusting this example from
TimerOne/FanSpeed.ino at master · PaulStoffregen/TimerOne · GitHub

#include <TimerOne.h>

// This example creates a PWM signal with 25 kHz carrier.
//
// Arduino's analogWrite() gives you PWM output, but no control over the
// carrier frequency.  The default frequency is low, typically 490 or
// 3920 Hz.  Sometimes you may need a faster carrier frequency.
//
// The specification for 4-wire PWM fans recommends a 25 kHz frequency
// and allows 21 to 28 kHz.  The default from analogWrite() might work
// with some fans, but to follow the specification we need 25 kHz.
//
// http://www.formfactors.org/developer/specs/REV1_2_Public.pdf
//
// Connect the PWM pin to the fan's control wire (usually blue).  The
// board's ground must be connected to the fan's ground, and the fan
// needs +12 volt power from the computer or a separate power supply.

const int fanPin = 4;

void setup(void)
{
  Timer1.initialize(40);  // 40 us = 25 kHz  <<<<< 6us = 166kHz  Serial.begin(9600);
}

void loop(void)
{
  // slowly increase the PWM fan speed
  //
  for (float dutyCycle = 30.0; dutyCycle < 100.0; dutyCycle++) {
    Serial.print("PWM Fan, Duty Cycle = ");
    Serial.println(dutyCycle);
    Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
    delay(500);
  }
}

That way of timing events depends on the controller system clock. 8, 16 or more MHz.... the result will be different, very different.
We used such code at the micro computer first experiments 50 years ago....

Hi, @malekabido
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".

This will help with advice on how to present your code and problems.

Can you tell us what model Arduino are you using?
How will you check if your output is at the correct frequency and duty cycle?

Thanks.. Tom.... :smiley: :+1: :coffee: :australia:

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