 # Read PWM input Signal and MAP

Hi

at the moment im using a arduino and reading an analogue signal via the input and analogread function. I take a 5Vdc signal in, the input is 1023bit so i map the 5V 0-1023, to the output value of 0 255.

int val = analogRead(PotIn); //get the potentiometer value (range 0-1023) val= map(val,0,1023,0,100); // map pot range in the stepper range.

I want to change the input from DC 5Vdc signal to 0-5Vdc PWM signal, Duty cycle ranges from 10-90%. I will still use A0 as my input signal.

I have found the Pulsein code from the arduino forum that i would like to use.

int pin = 0; unsigned long duration;

void setup() { pinMode(pin, INPUT); }

void loop() { duration = pulseIn(pin, HIGH); }

So in relation to my first question can i map this value same as i did with my analog 5Vdc signal?

first: to use A0 : int pin=A0; (or 14 on the uno) To know the corresponding value (0..255) you'll need to know the frequency of the input signal. ..or u can time both LOW and HIGH times to determine

knut_ny: first: to use A0 : int pin=A0; (or 14 on the uno) To know the corresponding value (0..255) you'll need to know the frequency of the input signal. ..or u can time both LOW and HIGH times to determine

Thanks for your reply, yes i meant to put A0. The frequency of the signal is between 475Hz and 525Hz, should be close to 500Hz.

What you should get out of pulseIn() is the duration of a high pulse in the PWM.

Assuming the frequency of the PWM is fixed and known, you can calculate the expected duration - or be empirical, and put PWM input at various duty cycles in, and record the values you get.

Since pulseIn() gives you duration (in microseconds, and they note it's no good below 10us, so your pwm better be slow enough that that's okay 500 hz is fine), with constant pwm frequency, the duration will be directly correlated to duty cycle, so you can use that in map just like you did before (only with different numbers).

DrAzzy: What you should get out of pulseIn() is the duration of a high pulse in the PWM.

Since pulseIn() gives you duration (, with constant pwm frequency, the duration will be directly correlated to duty cycle, so you can use that in map just like you did before (only with different numbers).

So i assume it should read something like this

int pin = A0; unsigned long duration;

void setup() { pinMode(pin, INPUT); }

void loop() { duration = pulseIn(pin, HIGH); Float DutyCycle = 0.002/duration; // 0.002 = 1/500Hz val= map(dutycycle,0,100,0,100); // map duty cycle range 0-100% to 0 to 100 steps on output signal.

}