DC Motor Control using Raspberry Pi, chipKIT Pi and the Arduino Motor Control Shield

In this example, a General Purpose Input/output line on the Raspberry Pi Expansion Connector will be used to send a HIGH or LOW logic level from the Pi to the chipKIT Pi to turn the motor on or off.

 Hardware Setup

Simply connect a jumper wire from the J1 RPi-Connect header in the upper left corner of the chipKIT Pi. Here I’ve connected the third pin down on the bottom side of the header which is GPIO4 as per the diagram below.
PiConnection Header
We will connect this pin to an available input on the chipKIT Pi. Referring to the Arduino Motor Control Shield Schematic, we note that pin 4 is available. This conveniently has the same number as our GPIO on the Raspberry.
This connection is illustrated below. (Note that the Arduino Motor Control Shield is not shown to make it easier to see this connection.)
GPIO4_CVONN

Software Setup

In this application example, when the GPIO4 line is driven HIGH (3.3V) by the Raspberry Pi, the chipKIT Pi will read this and turn on the motor. When the GPIO4 line is driven LOW (0V) the chipKIT Pi will turn off the motor. Let’s setup the chipKIT Pi sketch first. The sketch is setup as it was in the previous example only adding Digital Pin 4 as an input that will receive the signal from the Raspberry Pi. In the loop(), the code first checks digital pin 4, if it reads a LOW/False/0V signal it calls the function allStop() which simply applies the brake signal on pin 8. If it reads a HIGH/True/5V signal it will call the turn() function that disengages the brake for 5 seconds, then reapplies the brake.  The full sketch is below:
    //Include the SoftPWMServo Library
    #include <SoftPWMServo.h>
    int Pi_GPIO = 0;
    void setup() {
      //set up channel B on the Arduino Motor Control Shield
      pinMode(13, OUTPUT); //Pin 13 controls direction
      pinMode(8, OUTPUT); //Pin 8 controls the brake
      pinMode(4, INPUT); //Input line from Raspberry PI GPIO
    }
    void loop() {
      //We need to send a PWM to the Arduino MC shield to start the motor
      //turning. We also define a duty cycle that will set the motor speed.
      //The higher the duty cycle, the faster the motor will turn. Duty cycle
      //is set between 0 and 255.
      //So we send the PWM to pin 11 according to the Arduino MC specs at say
      // a duty cycle of 100
      SoftPWMServoPWMWrite(11, 100);
      //Read the inputs from the Raspberry Pi
      Pi_GPIO = digitalRead(4);
      //Generate motor output based on inputs from the Pi
      if (Pi_GPIO == 0){
        allStop();
      }
      else if (Pi_GPIO == 1){
      turn();
      }
    }
    void turn(){
      //Set for directionA
      digitalWrite(13,HIGH);
      // First we disengage the brake for Channel B
      digitalWrite(8,LOW);
      //Let's run the motor for about 5 seconds
      delay(5000);
      //brake the motor
      digitalWrite(8,HIGH);
      //give the motor a chance to settle
      delay(500);
    }
    void allStop(){
      //brake the motor
      digitalWrite(8,HIGH);
      //give the motor a chance to settle
      delay(500);
    }
Place the chipKIT Pi into bootloader mode and download the sketch.
In order to communicate with chipKIT Pi, we will need to download the GPIO package for Python as follows:

 Installing GPIO Package for Python

  1. Open a terminal window and download the latest version of the GPIO Package for Raspberry Pi. At the time of this post, the latest version was RPi.GPIO-0.5.3a:
    • $ sudo apt-get update
      $ sudo apt-get -y install python-rpi.gpio
  2. Next step, open python within the terminal window (note Python 3 is used here):
    • $ sudo python
  3. Instead of typing the full RPi.GPIO name every time, we will set it up so that using just GPIO does the same thing:
    • >>> import RPi.GPIO as GPIO
  4. Next, we need to define the type of numbering we will use
    • GPIO.setmode(GPIO.BCM) — Broadcom GPIO numbering
      or
      GPIO.setmode(GPIO.BOARD) — board pin numbering
      Here I used BCM. If you need more of an explanation on this, there are tons of resources on the web to explain this.
      >>> GPIO.setmode(GPIO.BCM)
  5. Now we need to let the Raspberry Pi know that we will be using GPIO4 as an output:
    • >>> GPIO.setup(4, GPIO.OUT)
  6. Finally, to send a HIGH/True/3.3V over the selected pin:
    • >>> GPIO.output(4, True)
    Note that if all went as planned the motor connected should now start turning.
  7. To send a LOW/False/0V over the selected pin to turn off the motor:
    • >>> GPIO.output(11, False)
    The complete terminal code is show below:
      pi@raspberrypi ~ $ sudo python3
      Python 3.2.3 (default, Mar 1 2013, 11:53:50)
      [GCC 4.6.3] on linux2
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import RPi.GPIO as GPIO
      >>> GPIO.setmode(GPIO.BCM)
      >>> GPIO.setup(4, GPIO.OUT)
      >>> GPIO.output(4, False)
      >>> GPIO.output(4, True)
      >>> GPIO.output(4, False)
     
    VN:F [1.9.22_1171]
    Rating: 6.5/10 (11 votes cast)
    DC Motor Control using Raspberry Pi, chipKIT Pi and the Arduino Motor Control Shield, 6.5 out of 10 based on 11 ratings