Go Down

Topic: Raspberry Pi / Arduino Motor Control (Read 6394 times) previous topic - next topic

Im creating a robot using a raspberry pi for the main processing and arduino with shield for motor control. Ive got them connected but there is interferance and im not sure how to resolve it.
My raspberry pi and arduino are connected to each using the ground pins. Then ive connected one gpio from the pi to a digital input pin on my arduino with an led in between. Ive coded my arduino to move forward when the input pin is high. Which is done using Webiopi through the raspberry pi.
As i mentioned it does work but the input pin on the arduino can be triggered really easily.
I think my circuit is too simplistic.
Can any one help?
Thanks, jason.

PaulS

Quote
Then ive connected one gpio from the pi to a digital input pin on my arduino with an led in between.

What is the LED for? Do you have a current limiting resistor, too?

Ive got an led for two reasons. One, so that i know when a signal is sent over the line and two, to stop a reverse signal just incase my arduino outputs a signal to my pi. You mentioned a limiting resistor. Do i need this inline in line with the led? My electronics knowledge is not great, but i assumed because the pi outputs 3v and the arduino can receive 3v or 5v this should be ok.
Thanks,
Jason

PaulS

There is a big difference between a diode, for reverse voltage protection, and an LED. LEDs draw current. A diode does not.

How much current? As much as it can get, until it burns out the Raspberry. But, hey, it's not my Raspberry. If it was, there would be a current limiting resistor to limit the amount of current that the LED can pull.

Thanks PaulS.  The current goes from my Raspberry to my Arduino (to active a motor), with the LED stopping it going backwards, just in case I have my input/output the wrong way around.  It works but is subject to interferance, which can active the motors randomly.  Would reducing the current help? Or do I need a pull down/up resistor?
Thanks, Jason.

PaulS

Quote
As i mentioned it does work but the input pin on the arduino can be triggered really easily.

Haven't seen any code, yet...

Quote
Would reducing the current help?

If the LED is only acting as a diode, and you never set the Arduino pin as OUTPUT and HIGH, then you never have any current flow to worry about.


Here is my code:

Code: [Select]
int pwm_a = 3;  //PWM control for motor outputs 1 and 2 is on digital pin 3
int pwm_b = 11;  //PWM control for motor outputs 3 and 4 is on digital pin 11

int dir_a = 12;  //direction control for motor outputs 1 and 2 is on digital pin 12
int dir_b = 13;  //direction control for motor outputs 3 and 4 is on digital pin 13

int forward = 4;
int backward = 5;
int left = 6;
int right = 7;

int f_val, b_val, l_val, r_val = 0;

int fast = 255;  // motor speed 100%
int med = 175;   // motor speed 75%
int slow = 100;  // motor speed 39%
int stopped = 0; // motor stopped 0%


void moveForward(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

}


void moveBackward(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, HIGH);  //Set motor direction, 3 high, 4 low

}


void turnRight(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, HIGH);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, HIGH);  //Set motor direction, 3 high, 4 low

}


void turnLeft(int speed)
{

   analogWrite(pwm_a, speed); // set motor speed
   analogWrite(pwm_b, speed);   // set motor speed

   digitalWrite(dir_a, LOW);  //Set motor direction, 1 low, 2 high
   digitalWrite(dir_b, LOW);  //Set motor direction, 3 high, 4 low

}


void stopRobot()
{
    analogWrite(pwm_a, stopped);   // set motor speed
    analogWrite(pwm_b, stopped);   // set motor speed
}



void setup()
{
  Serial.begin(9600);
 
  //Set control pins to be outputs

  pinMode(pwm_a, OUTPUT); 
  pinMode(pwm_b, OUTPUT);
  pinMode(dir_a, OUTPUT);
  pinMode(dir_b, OUTPUT); 

  pinMode(forward, INPUT);
  pinMode(backward, INPUT);
  pinMode(left, INPUT);
  pinMode(right, INPUT);
 
  stopRobot();
}


void loop()
{
  f_val = digitalRead(forward);
  b_val = digitalRead(backward);
  l_val = digitalRead(left);
  r_val = digitalRead(right);
  if (f_val == HIGH)
  {
    moveForward(slow);
  }
  if (b_val == HIGH)
  {
    moveBackward(slow);
  }
  if (l_val == HIGH)
  {
    turnLeft(slow);
  }
  if (r_val == HIGH)
  {
    turnRight(slow);
  }
 
}


As I mentioned before, i'm using http://code.google.com/p/webiopi/ to switch the GPIO (output) pins of my Raspberry on and off which active the INPUT pins on my Arduino.

My Raspberry is currently powered using a 2A 5v USB supply and my Arduino is powered seperately through my motor sheild with 8 AA batteries.

Thanks, Jason.

Uncle-Ed

I am quite uncomfortable with the LED in the communication line.  An LED can have a voltage drop sometimes approaching 3 volts and quite normally 2 volts, though it will vary with the amount of current through the diode.  That is, given the way this is connected, the voltage at the Pi end will be different from that at the Arduino end by a serious fraction of the total voltage available.   If the Pi is only putting out 3 volts, as you say, then getting a good logical high is going to be uncertain.  BTW, the voltage drop across the LED will not be a linear function of the current; it is not a pure resistance by any means.

If these are systems working off 5 volts, the input on the Arduino is probably expecting 3.3 volts or more for a logical high, or 1.6 volts or less for a low.  It is possible the drop across the LED is bringing the high down to a value between 1.6 and 3.3, where the behavior will be erratic--in fact, in terms of logic, it is undefined. 

In a separate concern, if the Arduino is powered by the same supply as the motor, electrical "noise" from the motor may be being introduced into the power for the chip.  You might try powering the Arduino with a separate battery or other supply to see if that reduces some of the undesired triggering.     

If you insist on a diode in the line to prevent a reverse current, try a small silicon diode, such as a 1N4001 or 1N4002, 03, etc.  The drop across such a diode is 0.6 to 0.7 volt, so it will have less effect on the signal.   However, as was pointed out, if the Arduino pin is set as INPUT and can never be an OUTPUT, there will not be the chance of the reverse current. 

Please let us know what you find as a solution for this.   I will be interested.

oric_dan

Besides what everybody else said, The Led will not even be turned on and conducting, unless
you have a good current pathway, which in this case would be a resistor to ground at the
Arduino input pin.

Go Up