Generate microSecond pulse

I am using Arduino uno and Nodemcu unit

I would like to know which can generate pwm signal of 50% duty cycle with 30uSec pulse
Is there any library or sample code associated with it

with https://github.com/PaulStoffregen/TimerOne/blob/master/examples/FanSpeed/FanSpeed.pde

#include <TimerOne.h>

const int fanPin = 4;
float dutyCycle = 50; // 50%
void setup(void)
{
  Timer1.initialize(60);  // 60 us
  Serial.begin(9600);
}

void loop(void)
{
    Serial.println(dutyCycle);
    Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
    delay(500);
}

*** edited to change duty cycle to a float

thanks a lot for sharing this.

@lightingandsupplies
Why are you creating such an odd frequency (16.666 kHz = 1/2*30 us) PWM signal instead of 20 kHz?

It's unclear why you think that this relates to Interfacing w/ Software on the Computer. Your topic has been moved to a more suitable location on the forum.

Which nodeMcu pin I should use in hardware

Can you explain me calculation part .

how can change the desired frequency

If you know the frequency, the period is its inverse:

  period_seconds =  1.0/frequency;
  period_us = 1000000UL/frequency;

This part i didnt understand actually
Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
I wanted to know if want to create 60% on time and 40% OFF time how can do it
I.e 60Us on time and 40US off time.

Which port I need to use in nodemcu I should use to achieve it

Since dutyCycle is a int, this may not do what you expect. You should force the intermediate results to floats i.e. 100.0 instead of 100

1 Like

I have used above code , But not getting any waveform. I am using Arduino Uno board for Testing

Sample code i tested.


// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(11, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(500);                       // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(500);                      // wait for a second
}

How can i do using PWM signal for required signal But i required 30US

If it is this code that you are referring to:

Try:

Timer1.pwm(fanPin, (float)dutyCycle * 1023.0 / 100.0 );  

If not, post the complete failing code.

Sorry. With above code also I am getting nothing.

Actual i need Turn On time 3Us and turnoff time variable one. With simple Blink code with Microsecond delay i can acheive 5.5Us pulse.

with code shared above i cant able to generate any pulse . It always high.
I am using 11 pin of Arduino Uno.
Can you send sample code to test it out

Please post the complete code you have tested, that is a complete runnable sketch which someone else could, it they so wish, load on their arduino and test.

3uS is over 300kHz and starts getting close to the minimum switching time of an arduino pin.

''```

This code i could able to get 4us on time.. 92Us off time But duty cycle was 50%

#define OUTPUT_PIN 11  // PWM/Signal output pin
float frequency;        // Frequency in Herz
float dutyCycle;      // Duty cycle (pulse width) percentage

void setup() {
  pinMode(OUTPUT_PIN, OUTPUT);

  // Set the frequency and the duty cycle. For most
  // purposes you will want to leave the duty cycle set
  // to 50%.
  frequency = 10000000;
  dutyCycle = 50;
}

void loop() {

  // Calculate the period and the amount of time the output is on for (HIGH) and 
  // off for (LOW).
  double period = 1000000 / frequency;
  double offFor = period - (period * (dutyCycle/100));
  double onFor = period - offFor;


  if( period > 16383 ) {
    // If the period is greater than 16383 then use the millisecond timer delay,
    // otherwise use the microsecond timer delay. Currently, the largest value that
    // will produce an accurate delay for the microsecond timer is 16383.

    digitalWrite(OUTPUT_PIN, HIGH);
    delay((long)onFor/1000);
    
    digitalWrite(OUTPUT_PIN, LOW);
    delay((long)offFor/1000);
  } else {
    digitalWrite(OUTPUT_PIN, HIGH);
    delayMicroseconds((long)onFor);
    
    digitalWrite(OUTPUT_PIN, LOW);
    delayMicroseconds((long)offFor);
  }
}

I am not able to get any waveform with below code.

My requirement 3Us On time and variable Offtime

``

#include <TimerOne.h>

const int fanPin = 11;
const int dutyCycle = 50; // 50%
void setup(void)
{
  Timer1.initialize(50);  // 60 us
  Serial.begin(9600);
}

void loop(void)
{
    Serial.println(dutyCycle);
    Timer1.pwm(fanPin,(float) (dutyCycle / 100) * 1023);
    delay(500);
}

I could able to get Max 4Us on time . Off time also 4us for range of 1-3value

// the setup function runs once when you press reset or power the board
void setup() {
  // initialize digital pin LED_BUILTIN as an output.
  pinMode(11, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(11, HIGH);   // turn the LED on (HIGH is the voltage level)
  delayMicroseconds(1);                       // wait for a second
  digitalWrite(11, LOW);    // turn the LED off by making the voltage LOW
  delayMicroseconds(100);                      // wait for a second
}

Maybe this partly explains the problems you have been having with your ESP8266 (NodeMCU) and contains some ideas:
https://www.esp8266.com/viewtopic.php?f=32&t=12034

Do you need the radio on when you are generating this waveform ?

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

Please change the duty cycle declaration to a float.

float dutyCycle = 50; // 50%

When I edited out the loop in the original code from https://github.com/PaulStoffregen/TimerOne/blob/master/examples/FanSpeed/FanSpeed.pde , I used the wrong declaration when I replaced it. I corrected my code up above.

The Time1.pwm() code takes a 0-1023 value. For 50% you could enter the 512 directly.

It works with the ((float) (dutyCycle / 100) * 1023) when duty cycle is an integer because of the cast to float makes the (float) (dutyCycle / 100) = 0.5 rather than dutyCycle / 100 = 0.

I'm sorry to have added unnecessary confusion.

I am using NodeMcu for Testing. I think TimerOne library is not supporting
I need 3Us Turnon Time and Variable Turnoff Time. Which is going to be input for pulse validation

#include <TimerOne.h>
const int fanPin = 16;
const int dutyCycle = 50; // 50%
void setup(void)
{
  Timer1.initialize(40);  // 40 us = 25 kHz
  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);
  }*/

  Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
}

Error

21 | Timer1.pwm(fanPin, (dutyCycle / 100) * 1023);
| ^~~
exit status 1
'class TimerOne' has no member named 'initialize'

I have tried this code .With this i could able to achieve 5us pulse . even set to 3us


#define LED 16           // Led in NodeMCU at pin GPIO16 (D0).
void setup() {
  pinMode(LED, OUTPUT);    // LED pin as output.
  Serial.begin(115200);
}
void loop() {
  digitalWrite(LED, HIGH);// turn the LED off.(Note that LOW is the voltage level but actually
  //the LED is on; this is because it is acive low on the ESP8266.
  delayMicroseconds(3);            // wait for 1 second.
  Serial.println("High");
  digitalWrite(LED, LOW); // turn the LED on.
  Serial.println("LOW");
  delayMicroseconds(3);
  
}

Can someone helpme out with sample code , Is there any library with Nodemcu which can generate 3us on-off pulse or 3uS on with variable pulse