[Solved] Timer1.pwm issue

Sorry! I know this is a very popular question but I couldn’t find what I exactly need…
(Sorry for my English… I never know if it is understandable)

I have to do an application which reads and count an high frequency (up to 50 KHZ) pulse train and it works fine (I’ve done it with attachinterrupt). On the other hand the same Arduino should control a stepper motor with a PWM.

I would really use Timer1.pwm() because it is direct, simple and I think it doesn’t involve TCCR0, not influencing the first part of my program. It’s better for me not to use “digitalWrite” because the processor is working at his limit.

The problem is:
PWM is analogic - How can I check whenever a pulse has been sent to my stepper (for increasing a counter)? If I collect and plot data from my pwm (with analogRead) I have a sinusoid.
Is there an easy code way to say “Ehy, from here I’ve sent something we can consider an HIGH pulse, now let’s wait for the next one!”

I’m posting this part of my code but it’s just a few lines and I don’t think it is the correct way…

[initialization, etc, etc]

int pin_pulse_stepper = SIGNAL_PULSE_STEPPER;
int pin_direction_stepper = SIGNAL_DIRECTION_STEPPER;
int duty = STEPPER_INIT;
int period = 2500;     // iniziale

void setup()
{
  Serial.begin(9600);

  if (ENABLE_STEPPER_CONTROL) {
    Timer1.initialize(100);
    Timer1.pwm(pin_pulse_stepper, duty, period);
  }
}

[ bla bla bla ]

// ------------------

void loop()
{

  if (ENABLE_STEPPER_CONTROL && duty > 0)
  {
    digitalWrite(pin_direction_stepper, dir);
    do {} while (analogRead(pin_pulse_stepper) == LOW)

    Serial.println("Bang!!!")

    do {} while (analogRead(pin_pulse_stepper) == HIGH)
    
  }
}

Thank you in advance!

The problem is:
PWM is analogic - How can I check whenever a pulse has been sent to my stepper (for increasing a counter)? If I collect and plot data from my pwm (with analogRead) I have a sinusoid.

PWM is purely digital. Your code would already know the duty and period of the PWM signal. Is the data you collect similar to data from a sine table or does it basically jump from near 0 to approx 1023?

How is the signal connected to the analog input? (type of wire/cable, length)

EDIT:

OK, I see in the spec sheet for your stepping motor drive is uses "adaptive microstepping", so depending on what you're measuring, I guess a sinusoidal type signal is what to expect.

This is confusing:

The problem is:
PWM is analogic - How can I check whenever a pulse has been sent to my stepper (for increasing a counter)? If I collect and plot data from my pwm (with analogRead) I have a sinusoid.
Is there an easy code way to say "Ehy, from here I've sent something we can consider an HIGH pulse, now let's wait for the next one!"

Stepper motors do not work with PWM unless there is some other interface I do not know about you haven't shared with us. Can you give us a wiring diagram/picture so that we understand how your PWM output is connected to your stepper motor.

Z

Ok, I'll anticipate some answer before drawing a schematic. I'm interfacing Arduino to my stepper with an Italian digital microstepping drive whose name is "Azionamento R.T.A. open-frame A-CSD 04.V" (The centre one in the picture in pdf below)

http://store.lipsiasoft.com/uploads/352693_A-CSD_ita.pdf

I've connected this shield to a 24V power supply and to Arduino:

Arduino: A-CSD
Pin 9 (PWM) -> Pin 3 (Step Input)
Pin 8 (digital) -> Pin 4 (Direction)
Ground -> Pin 11 (Equipotential ground reference)

Everything works fine! I can also change the period of my pwm in order to speed up (or down) my motor even during the execution: using Arduino's "serialevent" I can give a period value to the program and update the PWM by recalling it with the new period.

So whatever I'm sending, it's working. I just need to count rising edges of my PWM in order to understand how many steps have I done...

All connections are wired.

My data collect it's absolutely a stretched sine wave (like a 2*sin(x)) between 250 and 350 butt unfortunately not much similar to a 0 - 1023 approximation.. Actually I was expecting something jumping from near 0 to near 1023 and it would have been much easer and more accurate to detect rising edge even with my "do {//nothing}" while (analogRead()==LOW)"...

Maybe I should not use analogRead... don't know... :cry:

Thanks a lot guys to tolerate my confusion! I'm a newbie... :slight_smile:

Oscia:
So whatever I'm sending, it's working. I just need to count rising edges of my PWM in order to understand how many steps have I done...

You could connect a jumper from the Pin that is producing the step signals to one of the Arduino external interrupt pins and write a short ISR to increment a variable for every pulse.

...R

I really like your hardware approach! It works!

Anyway before reading your answer I tried to use another clock (the OC2 - to pin11), changing the frequency of PWM with the library "FrequencyTimer2" and it works fine!
The library has also an interrupt method that suits me!!

I read in some other query that is better not tu use clock 2 because changing his frequency could have been a problem for other schedule functions, like delay etc. (that's why I used OC1)... but for now it doesn't seem to be my case.

Now I just have to check what solution is the most performing between this two!

Thanks a lot!!!! :slight_smile:

It's timer0 that runs delay and millis. You can mess with timer2 all you want.