Hi Guys,
Thanks for reading my query.
I am completely new and I need to understand how I can calculate the duty cycle on a specific pin which generates PWM signal on Arduino Mega.
I have followed few posts, but I understood it by default reads the PWM signal from PIN 2.
How to read and calculate duty cycle from Specific PIN ?
/ Ravi
I have followed few posts, but I understood it by default reads the PWM signal from PIN 2.
What is "it"? If you have code, post it. Where us the PWM signal coming from?
Please read the "How to use this forum-please read" stickies to see how to post code and some hints on what we need to know in order to help you. See #7 & 11, especially.
Get the time for the first rising edge, first falling edge, and second rising edge. Then calculate the cycle time tc from the rising edge times, the pulse duration tp to the falling edge, and then calculate tp/tc. You can use millis() or (better) micros() for getting the times.
Thanks for your responses.
I have found few solutions from the google but its not clear of how to calculate the duty cycle on a specific pin which supports PWM signal.
BUT I might need some guidance from the below example, of to calculate the duty cycle ?
http://www.benripley.com/diy/arduino/three-ways-to-read-a-pwm-signal-with-arduino/
#include <PinChangeInt.h>
#define MY_PIN 5 // we could choose any pin
volatile int pwm_value = 0;
volatile int prev_time = 0;
uint8_t latest_interrupted_pin;
void rising()
{
latest_interrupted_pin=PCintPort::arduinoPin;
PCintPort::attachInterrupt(latest_interrupted_pin, &falling, FALLING);
prev_time = micros();
}
void falling() {
latest_interrupted_pin=PCintPort::arduinoPin;
PCintPort::attachInterrupt(latest_interrupted_pin, &rising, RISING);
pwm_value = micros()-prev_time;
Serial.println(state);
}
void setup() {
pinMode(MY_PIN, INPUT); digitalWrite(MY_PIN, HIGH);
Serial.begin(115200);
PCintPort::attachInterrupt(MY_PIN, &rising, RISING);
}
void loop() { }
Regards
Ravi
Crap code. You should not be printing from inside an interrupt service routine.
Now if you could answer the questions in reply #1 then maybe we could get somewhere. There is something about the way you are asking the question that suggests you want to measure the duty cycle of PWM being generated from one of the Arduino’s pins, which is silly as you already know the duty cycle because you set it in the first place.
Hi,
Welcome to the forum.
Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html
What is the frequency of the PWM?
What model Arduino are you using?
What is the amplitude of your PWM?
Thanks.. Tom... 
poojariravi:
I have found few solutions from the google but its not clear of how to calculate the duty cycle on a specific pin which supports PWM signal.
If the Arduino is generating the PWM signal (which I presume is why you are using a PWM pin) then the duty cycle is determined by the value used for analogWrite() with 0 representing 0% and 255 representing 100%
...R