activating the PWM pulses

Hello
My name is Fatjon and im new in Arduino programming..
I need to know if the PWM pulses from Arduino can be delayed or first we should activate the pulses.
Thank you

The PWM pulses start when your code calls analogWrite() and stop when you call digitalWrite() on the same I/O pin.

I'm not sure if that answers the question that you had in mind. If not please explain more clearly what you want to do.

..R

have a look at the Fading sketch exaple which can be found in the Analog category of examples.
It fades an LED from min to max and back again
I think you might learn a few things by reviewing the code !

It is definitely possible to change the frequency of the PWM but it's definitely wiser to start walking before running.
The example below will get you going with PWM

  • dan
/*
 Fading

 This example shows how to fade an LED using the analogWrite() function.

 The circuit:
 * LED attached from digital pin 9 to ground.

 Created 1 Nov 2008
 By David A. Mellis
 modified 30 Aug 2011
 By Tom Igoe

 http://www.arduino.cc/en/Tutorial/Fading

 This example code is in the public domain.

 */
int ledPin = 9;    // LED connected to digital pin 9

void setup() {
  // nothing happens in setup
}

void loop() {
  // fade in from min to max in increments of 5 points:
  for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }

  // fade out from max to min in increments of 5 points:
  for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
    // sets the value (range from 0 to 255):
    analogWrite(ledPin, fadeValue);
    // wait for 30 milliseconds to see the dimming effect
    delay(30);
  }
}

Thnx both of you
You have answered my first question.
Now i want to know if i can modifie the width of the PWM pulses?
Second i want to know if i can count those pulses from the same Arduino that create the PWM pulses?
You know one arduino create and count pulses.
Thank you

Now i want to know if i can modifie the width of the PWM pulses?

The value(0-255) in the analogWrite (pin, value) changes the width of the pulse--that is the time it is HIGH and the time it is LOW.

Do you mean modify the frequency of the pulses repeating? That is done by modifying the timers which generate the pulses. There are plenty of examples to be found in the forum search ("modify pwm frequency") or Google

Second i want to know if i can count those pulses from the same Arduino that create the PWM pulses?
You know one arduino create and count pulses.

Yes that is possible, and there are a variety of methods to do so. Some of them will get you into understanding the datasheet of the timers in order to set them up.

What are you trying to do? At what frequency are you wanting to run the PWM? How do you want to use the counting of the pulses?

In this file attached you can see my topic.
So comparing the sawtooth signal with measured value we will get the signal with number 1.
When this signal is ON then you count a train pulses with an fixed frequencies .
The picture shows my idea

OK but:-

What are you trying to do?

X Y Problem

If you are generating the PWM you have no need to measure it because you know exactly what you are generating.

You don't need a sawtooth to generate pulses like that.
Comparing input voltage to a sawtooth is analogue stuff.
Arduino can read an input voltage, and make any pulse you want.
Try to understand analogRead, digitalWrite, analogWrite and PWM.
Lots if info in the leaning area on top of this page.
Leo..

Hi,
Look at the topic on the OP sheet.
Then go and read about Slope Integration.

The OP topic is to use the circuit shown, to see what the digital output value would be and compare it to the analog input. (This final bit would be the lab bit where the experimenter uses pen and paper or spread sheet to compare the results, not the arduino)
post #5 says it all.

He needs the arduino to produce the 1kHz pulses I would think, and PWM with filtering would generate the sawtooth.
The comparator and AND gate are external hardware components doing the slope bit.
The arduino counts the gated pulses and that represents the level of the analog input.

Tom.... :slight_smile:

Whoa! The hardware aspects of this project have quickly moved beyond my understanding.

I do know that the 1khz pwm is very straight forward to achieve on a 16mhz Arduino on timer 0 or timer 2 using a prescaler of 64 and Mode 7 --fast pwm to a count of 249. That gives 250 ticks at 4 us each.

The pulse counting can be done by using a Timer Overflow Interrupt Vector which increments at the end of every cycle.

If this application needs very stable and accurate timing, the resonators in the current Arduino's may not be good enough. The older Duemilanova's had a crystal oscillator and were very accurate.

Tom has understood my idea.
You know 1KHz PWM pulses (for example )are produced by arduino.
Sawtooth is produces by external hardware and by comparing sawtooth signal with the signal wich we will measure we get signal wich have number 1 in photo.
When this signal is ON then Arduino counts pulses (from 1KHz ). This number of pulses represent the value measured.
Until now i have done the sawtooth circuit.

Fine but how does that relate to the original question?

I need to know if the PWM pulses from Arduino can be delayed or first we should activate the pulses.

It seems to me that all you need to do is to time the width of the compactor output, there is no need for any 1KHz pulses to be generated or counted.

The value of 1Khz was as example.

Fation:
The value of 1Khz was as example.

It might be but there is no need to count anything when you can time the width of the comparator pulse with the Arduino.

Until now i have done the sawtooth circuit.

Do you mean to say that you have built an external analog circuit to generate a sawtooth waveform?
Would it be possible to explain your intentions with this external circuit ?
Is it in order to generate a PWM signal ?

I am probably in left field, but perhaps it would be a thing to read about the AnalogWrite function of the IDE.
AnalogWrite is used to change the PWM ratio /duty cycle on a PWM pin.
AnalogWrite functionality is explained here.
For example, the line "analogWrite(ledPin, 64)", would give you a 25% duty cycle ratio on the ledPin.

On the drawing you had attached, you wrote that "the pulses will be counted by Arduino" .
That is exactly correct. PWM is achieved by using the pwm mode of the timer.
The timer counts pulses which means that the value of the TCNTn will increase ,this is your digital sawtooth waveform.
The value of the TCNTn is compared to the value of the timer which you have loaded in the timer register and the pwm pin will be turned on/off depending on the comparaison of that timer value.

I am probably in left field, but you can refer to section 18.7.3 of the datasheet and you can also refer to the attached picture

-dan

My apologies for being the slow kid in class, seems like I am indeed in left field! Please disregard my previous post.

Dan....

My intetion is to create a voltmeter by using slope integrator method based on Arduino.

Attached you have an simplified circuit of that method.
All i want to know if Arduino can count pwm pulses from itself or should i use an external circuit for those pulses?

Capture.PNG

All i want to know if Arduino can count pwm pulses from itself

Yes. The pulse counting can be done by using a Timer Overflow Interrupt Vector which increments at the end of every cycle. Whether or not you need to count, or whether an external counter will be better, I leave for others to determine :wink:

Chose the timer you want to work with, and post some code setting up your timer for the pulse train you want, and we can help you add this. pulseCount needs to be declared as volatile because it is modified within an interrupt.

TIMSKX |= 1<< TOIEX; //enable Timer X overflow vector interrupt
ISR(TIMERX_OVF_vect) //overflow vector on Timer X
{
  pulseCount++;
}

Thank you
I will be need to set up the scetch also.
For the moment i will built the saw toth signal (Alredy done in multisim), then i will start with the code .
Thanks a lot

Whether or not you need to count, or whether an external counter will be better, I leave for others to determine :wink:

Yes there is nothing like some one who has so made up his mind that when he is given the right answer he totally ignores it.

I suspect he only asked the question so that some one could say exactly what he was thinking. We like people who come here to learn.