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


This post is intended to demonstrate compatibility of the chipKIT Pi with certain existing Arduino shields. In the second part of this post, we will also demonstrate how to communicate with the chipKIT Pi over a simple I/O line on the Raspberry Pi®, from a terminal window, to control the Arduino™ shield connected to the chipKIT Pi.

Hardware/Software Needed


Let’s begin by simply controlling a common Arduino shield. NOTE: Always check the electrical characteristics of any shield that will be connected to the chipKIT Pi. As with the Raspberry Pi, this is a 3.3V system. Therefore, if a shield outputs voltages greater than 3.3V there is a possibility that you could damage the chipKIT Pi or the Raspberry Pi. Connect the Arduino Motor Control Shield as shown:
motor shield 1
  1. Start a new sketch in MPIDE
  2. We will be using Brian Schmalz’s SoftPWMServo library for this application. This is a very flexible library that will enable a PWM (square wave) output on any pin we like. This library comes already included as a core library with the MPIDE. Therefore, to use, simply add the header file to the library at the top of your sketch as follows:
      #include <SoftPWMServo.h>
  3. The remainder of the sketch follows set up as per the Arduino Motor Control Shield specifications. I’ve added comments to explain each line of code.
    //Include the SoftPWMServo Library
    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
    void loop() {
      //Turn the motor
      // First we disengage the brake for Channel B
      //Depending on how your motor leads are connected to the Arduino
      //motor B header, the direction could be clockwise or counter clockwise
      //So let's just start by calling this direct 1 and drive pin 13 HIGH
      //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);
      //Let's run the motor for about 5 seconds
      //Now lets brake the motor
      //Give the motor a chance to settle
      //change directions
      //and run the motor for about 5seconds in the other direction
      //Again, we brake the motor
      //give the motor a chance to settle
      //and repeat
So, this is nothing really special and can be done on any chipKIT Board. However, we can make something pretty interesting by introducing some Python-based communication from the Raspberry Pi to the PIC32 MCU on the chipKIT Pi. Proceed to the next page to continue.
VN:F [1.9.22_1171]
Rating: 6.5/10 (11 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Analog Read using MPLAB X IDE


In this project you will learn to use a basic functionality of chipKIT board in MPLAB X IDE, which is to read an analog value and output the corresponding digital reading for it. A potentiometer is a  three-terminal resistor with a sliding contact that forms an adjustable voltage divider. Using a potentiometer measuring instrument, you could measure the electric potential. In this example we use the potentiometer in the chipKIT Basic IO shield. Find the same project using MPIDE here.

Hardware Used:

To do the analog read project, you require the following hardware devices.

  • chipKIT Uno 32
  • chipKIT Basic IO shield
  • PICkit® 3 In-Circuit Debugger/Programmer
  • USB cable
  • 6 pin header to connect chipKIT board and PICkit® 3


The reference guide for each board, schematics and other resources are available on their individual homepages:


  1. Connect the hardware devices. The PICkit® 3 should be connected to the chipKIT Uno32 board through a suitable header and use the USB cable to connect the PICkit® 3 to your PC.
  2. Place the IO shield on top of the Uno32 board with a bit of a firm press. A0-potentiometer of the IO shield will be used as peripheral for Uno32 board, in this excercise.
  3. Once the hardware setup is made and the device drivers are updated for PICkit® 3 on your computer, launch MPLAB X (Start>All Programs>Microchip>MPLAB X IDE>MPLAB X IDE vx.xx on Window® Machines ).
  4. Create a new project through File>New Project. In the pop up new project window, select Standalone Project  under Projects  and click Next. Under Family scroll and select 32-bit MCUs (PIC32). Note the processor number from the chipKIT board’s reference manual and enter it under the Device  option. Click Next.
  5. Select PICkit3 in the Hardware Tools, click Next. Select XC32 (v1.20) under Compiler Toolchains, click Next. Give a project name to create the project.
  6. In the Project tab on the left side of the MPLAB X IDE, right click on Source Files > New > C Main File.. and give a file name for main source code. Once the dot c file opens, select all the codes in the file and replace it with the codes below.
    #include <plib.h>
    #include <xc.h>
    #pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1, FWDTEN = OFF
    #pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1
    #define SYS_FREQ (80000000L)
    unsigned int channel4;	// conversion result as read from result buffer
    unsigned int offset;	// buffer offset to point to the base of the idle buffer
    int main(void)
        // Configure the device for maximum performance but do not change the PBDIV
        // Given the options, this function will change the flash wait states and
        // enable prefetch cache but will not change the PBDIV. The PBDIV value
        // is already set via the pragma FPBDIV option above..
        // configure and enable the ADC
        CloseADC10();    // ensure the ADC is off before setting the configuration
        // define setup parameters for OpenADC10
                    // Turn module on | ouput in integer | trigger mode auto | enable autosample
        // define setup parameters for OpenADC10
                    // ADC ref external    | disable offset test    | disable scan mode | perform 2 samples | use dual buffers | use alternate mode
        // define setup parameters for OpenADC10
        //                   use ADC internal clock | set sample time
        // define setup parameters for OpenADC10
        // do not assign channels to scan
        #define PARAM4    SKIP_SCAN_ALL
        // define setup parameters for OpenADC10
                    // set AN2 as analog inputs
        #define PARAM5    ENABLE_AN2_ANA
            // use ground as neg ref for A | use AN2 for input A
         // configure to sample AN2
        SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN2); // configure to sample AN2
        OpenADC10( PARAM1, PARAM2, PARAM3, PARAM4, PARAM5 ); // configure ADC using parameter define above
        EnableADC10(); // Enable the ADC
      while ( ! mAD1GetIntFlag() ) { } // wait for the first conversion to complete so there will be vaild data in ADC result registers
        // the result of the conversion is available in channel4.
        while (1)
            channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
        return 0;
  7. To read the potentiometer value, access the watch window from Window > Debugging > Watches or you can use the shortcut key Alt+Shift+2. In the watch window panel that opens on the MPLAB X IDE, enter the new watch as channel4, in the blank <Enter new watch> area as shown below in the figure.                                                                                                                                                          analogread mplab1
  8. In the program editor window, put a breakpoint by clicking on the line number after the code line
     channel4 = ReadADC10(0);          // read the result of channel 4 conversion from the idle buffer
    That is on the line with the closing braces of the while loop. This should create a red box in the number line as shown in the figure below,                                                     analogread mplab2
  9. Click on Debug Project icon shown below,                                                 analogread mplab3
  10. Once the program halts its execution at the line with the breakpoint, click on Watches output window, right click on the expand button under Value column and select Display value column as > Decimal. The value that appear in this column for channel4 variable corresponds to the potentiometer reading which is read in the range of 0 to 1023. A value 0 corresponds to 0 volts and a value 1023 corresponds to 3.3 volts.
  11. Now, click on Continue button from the top pane and observe the value of channel4 to change and reflect the potentiometer reading as described before.
  12. If you would like to change the potentiometer knob and see a different value, then, change the potentiometer knob and click on the Reset button on the top pane. This will initialize the variables back to zero. Clicking on the Play button on the top pane, will get your changed  potentiometer value on the Watches window. Below is a screenshot showing the potentiometer value for 3.3 volts being read in the Watch window.  analogread mplab4
VN:F [1.9.22_1171]
Rating: 5.7/10 (6 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)