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

Overview

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

Procedure

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
    #include<SoftPWMServo.h>
    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
      digitalWrite(8,LOW);
      //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
      digitalWrite(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
      delay(5000);
      //Now lets brake the motor
      digitalWrite(8,HIGH);
      //Give the motor a chance to settle
      delay(500);
      //change directions
      digitalWrite(13,LOW);
      //and run the motor for about 5seconds in the other direction
      delay(5000);
      //Again, we brake the motor
      digitalWrite(8,HIGH);
      //give the motor a chance to settle
      delay(500);
      //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)

USB Bootloader

The PIC32-avrdude-bootloader communicates over either a serial port (UART) or the USB port (on those pic32 chips that support USB.)  It can be built for either serial or USB communication with the PC. The bootloader communicates with the avrdude executable on the PC side of the connection. The following boards have been tested using the PIC32-avrdude-bootloader:
  • Microchip PIC32 USB starter kit
  • Microchip PIC32 Ethernet starter kit
  • UBW32 – MX460
  • UBW32 – MX795
  • Fubarino SD
  • Fubarino Mini
In order to use this bootloader, you can either download the full source from github, or just pick and choose the HEX file that is right for your board. There are three bootloaders that have been created for chipKIT and chipKIT compatible boards:
  • PIC32-avrdude-bootloader – This new bootloader for MPIDE/chipKIT PIC32 boards is buildable for all currently available  chipKIT boards (UNO32, MAX32, uC32) as well as other PIC32 based boards like the UBW32 and Fubarino boards.
  • pic32-Arduino-USB-Bootloader-original – This is the original version of the bootloader that works only if compiled with the C32 v1.xx version due to changes in the way that newer version of C32/XC32 handles the linker scripts files.
  • pic32-Arduino-Bootloader – This is the older original version of the bootloader which supports only Digilent uno and mega pic32 boards.This bootloader is compatible with v5.8 of avrdude.
  • For Digilent pic32 boards, it is highly recommended to download the Digilent official bootloader from the Digilent website at http://www.digilentinc.com/.
VN:F [1.9.22_1171]
Rating: 0.0/10 (0 votes cast)
VN:F [1.9.22_1171]
Rating: 0 (from 0 votes)

Using USB

The PIC32MX3xx series parts do not have a USB controller. The other PIC32 series (i.e. PIC32MX4xx/5xx/6xx/7xx) all have a USB controller. The Uno32 uses a PIC32MX320F128H and therefore does not have a USB controller. The Uno32 and Max32 have standard FTDI serial to USB interface chips (FT232R) to keep consistent with the Arduino way of interfacing. However, it is also nice to be able to use the built in USB port. Many other PIC32 boards have this USB port brought out to a standard connector such as the Microchip USB Starter Kit and the Digilent Cerebot 32MX4 and Cerebot 32MX7. The latest version of HardwareSerial.cpp now supports the first serial port (Serial.begin(), Serial.print() etc) can be reconfigured to use the USB port instead. In order to take full advantage of this, first you have to program the board with the USB bootloader, then use the appropriate board setting in the BOARDS menu. If you are using a custom board in the boards file you can just add the following to your board description: custom_pic32.compiler.define=-D_USE_USB_FOR_SERIAL_ When using the USB for Serial, UART1 becomes Serial0 Serial1 etc, are still there normal configurations Serial.begin(baudrate); //The baudrate is ignored, when doing real USB, there is no baudrate. Receive is interrupt driven and behaves EXACTLY like regular Serial. NO CODE CHANGES REQUIRED. If you want to see how this is done, look at HardwareSerial.h and HardwareSerial.cpp, the actual usb code is in HardwareSerial_cdcacm.c and HardwareSerial_usb.c. The USB code was written by Rich T (http://www.cpustick.com/) More documentation will be provided on how to do this soon. (Created 7/3/2011)
VN:F [1.9.22_1171]
Rating: 9.0/10 (1 vote cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)