Problem with Arduino as digital glue between motor driver / controller..!

Hello all,

I'm having trouble connecting a step motor controller (with STEP/DIR outputs) to a motor driver (with CW/CCW inputs) using an Arduino-bootloaded ATMega328P, and the root of the problem seems to be that I am also attempting to use an optoisolator to keep this particular motor/driver separate from three others one the same controller. (They use STEP/DIR input drivers so they're all set...)

Here's the test code I'm using:

void setup() {
pinMode(10, INPUT);     //direction input
pinMode(0, OUTPUT);    //indicate CW direction
pinmode(13, OUTPUT);  //indicate CCW direction
}

void loop() {

if (digitalRead(10)) {     //controller output = CW
   digitalWrite(0, LOW);
   digitalWite(13, HIGH);
   }
else {                         //controller output = CCW
   digitalWrite(0, HIGH);
   digitalWite(13, LOW);
}
}
}

I have tried several permutations of the NPN phototransistor section, like pulling the Ardiuno pin down instead of up, reversing the connections to pins 4 and 5, and probably others, but I'm obviously not understanding something important because I am consistently unable to change the state of digital input 10.

Any hints would be greatly appreciated!

Thanks,
Patrick

The "stepping motor controller" DIR and STEP are outputs? You show the LED side connected to the board that way. The LEDs make the phototransistor turn on, not the other way round.

Where's pin 10 in your drawing? Pin 0? Pin 13?

[The H11AA1 is for an "AC input". (It's OK to use it for DC, too, just "underutilised".)]

Why do you feel this "isolation" is necessary?

if (digitalRead(10)) 
   {     //controller output = CW
   digitalWrite(0, LOW);
   digitalWite(13, HIGH);
   }

can't be actual code.
if (digitalRead(10)) what?
Why not copy and paste what you're actually using?

Hi, and thanks for the reply. I'm sorry if my drawing was confusing- the motor controller is an interface board that is connected to a PC parallel port. I'm using the STEP and DIR signals that the board subsequently provides to trigger the optoisolator, and that part seems to be working fine. It's the output of the opto that isn't being interpreted correctly (OK, at all) by the microcontroller. Pin 16 (digital pin 10) is the one connected to the output of the opto, and digital 0 and 13 are running the LEDs.

I was at a different computer when I posted, so I re-wrote the code instead of pasting it. Here is what I'm actually using:

void setup() {                
 
  pinMode(9, INPUT);     //step
  pinMode(10, INPUT);     //direction  
  pinMode(0, OUTPUT);    //indicate dir=cw
  pinMode(13, OUTPUT);   //indicate dir=ccw
}


void loop() {
  
if(digitalRead(10)) {   //if direction = CW
     
     digitalWrite(0, HIGH);
     digitalWrite(13, LOW);
     }
  
else {  //direction = CCW
 
     digitalWrite(13, HIGH);
     digitalWrite(0, LOW);
   
}
}

I believe that at any given time, digitalRead() returns a Boolean value, does it not? So I thought I was using the IF this way:

If (direction pin is HIGH) {
//indicate this by lighting one of the LEDs
}
else {
//light the other LED
}

Lastly, the reason that I need the isolation is that one of the four step motors hanging off the controller (the one on the CW/CCW input driver) runs off a separate (24V) power supply. The other three are at around 65V.

Thanks again, and I hope I answered everything...!
Patrick

Your digitalRead understanding is incomplete.

Declare a variable --
byte val = 0; // the result of the digitalRead

inside your void loop() --
valDIR = digitalRead(10);
if (valDIR = 1)
{
// do this
}
else
{
// do other
}

Hey there, Pancake...thanks. You were right- when I declared a variable and had digitalRead() return its value to it, the if statement works as expected.

Now the only question is "in what programming language IS it possible to use a function without first declaring a return variable?" Seems to me that in the past I have gotten away with this, but I don't recall the circumstances.

Patrick

That programming language would be C :wink: You can indeed use it as you were. digitalRead returns HIGH or LOW which are 1 and 0 respectively so you can test for them in an if. Depending on how much it bothers you depending on those values never changing in later versions of the IDE, you may prefer to do this:

if(digitalRead(10)==HIGH)