The Raspberry Pi has definitely changed the way I develop for certain projects, and up until now, I have mostly used the little green board as a computing platform and have not ventured into using its GPIO pins very much.  This is mainly because I have been using the Raspberry Pi to build media streaming devices using Rasbmc, and it was not until recently that I had a need to control some external hardware with the Raspberry Pi.


RaspPI.jpg

 

A few months ago I began serving as a co-instructor for a local group called the YoungMakers, a club of middle school and high school aged teens who meet regularly at my local Makerspace, theClubhou.se, to learn about all things maker related. I joined up as an instructor for the Arduino Inventors Series, and stayed on for other lessons. After several of the teens in the group mentioned that they owned Raspberry Pi boards, I began thinking about ways to incorporate the Pi into future classes. This is where I came up with the idea to build a Rasperry Pi powered robot. 

 

Unfortunately all of the motor control solutions for the Raspberry Pi that I found involved custom boards and at the moment, I had no free time whatsoever to design and build a custom board. This led me into looking into ways to control an Arduino through a Raspberry Pi which would also control a motor driver shield.  It just so happens that around that same time, element14 asked me if I would like to take an early look at the new GertduinoGertduino. I responded immediately and a few weeks later, I had the Gertduino in my hands along with a new Raspberry Pi, Arduino Uno R3, and an Arduino Motor Shield R3.


boards.jpg

 

For those of you who are not yet familiar with the Gertduino, the board is basically an Arduino-compatible shield for the Raspberry Pi that features an onboard ATmega 328 as well as an ATmega 48 which is used as a Real Time Clock or an IRDA front end. The ATmega 48 is also fully programmable by the end user as well using a modified version of the Arduino IDE.  The Gertduino also features a RS232 to UART interface, and can be programmed using Arduino or AVR Studio on the Raspberry Pi via the GPIO pins.  Programming can be handled externally as well using an ISP.


Gertduino.jpg

 

Setting up the Gertduino for use is not that hard and simply requires installing the Arduino IDE in Raspbian by using the following commands to update Raspbian and then install Arduino.

 

sudo apt-get update
sudo apt-get install arduino

 

Now we need to install AVRDude to tie everything in together.  The following commands will download, install and setup AVR dude for use on your Raspberry Pi running Raspbian.

 

cd /tmp
wget http://project-downloads.drogon.net/gertboard/avrdude_5.10-4_armhf.deb
sudo dpkg –i avrdude_5.10-4_armhf.de


Once this process has completed, you will need to modify the Arduino IDE to recognize the Gertduino, and the process is almost identical to installing the Gertboard. This process has been thoroughly covered over on Drogon.net, so I will not attempt to reinvent the wheel. You can find the complete instructions as well as a script you must run at http://projects.drogon.net/raspberry-pi/gertboard/arduino-ide-installation-isp/

 

I had a little trouble getting things working the first time around and after hours of digging, I realized that I had not updated Raspbian in several months. This is why running apt-get update is so important before starting. Once everything was working fine, I was able to jump in and write a quick sketch in Arduino on the Raspberry Pi and upload it to the Gertduino. While this functionality is the same as if I had programmed the Gertduino via a desktop PC, the fact that it was done in Raspbian, and uploaded over the GPIO make this board immensely useful.


stack.jpg

 

For my test sketch, I plugged the Arduino motor Shield R3 onto the Gertduino, wrote a small sketch to rotate a motor in one direction at one speed, then reverse the motor at another speed. As you can see in the video posted above, this worked out quite well, and the Clubhou.se flag spins as it should. My original plan was to conclude this blog post with some information and video of the actual robot running through its motions and using a Ping sensor to avoid objects it might encounter. Unfortunately I ran into some issues with uploading code to the Gertduino after my initial upload.


After writing and verifying the code works on a normal Arduino Uno R3, I attempted to upload the robot code to the Gertduino via the Raspberry Pi, but I encountered an I/O error. I will write a more indepth post on this in the coming days, but it boiled down to not being able to include any libraries in my code. I could get code that requires no libraries to load by attempting to burn the stock Arduino bootloader. This would fail of course, but immediately after the burn failed, I could upload code that did not contain any external libraries. I still have not sorted this out, and have contacted the Raspberry Pi foundation, and engineers at Element14 which are working with me to find out why this issue is affecting me. Fortunately, I was able to write some simple code to get the motor spinning with out the use of an external library. The video below is my proof of concept that this combination of boards will work together, I just need to iron out my issues with the Arduino IDE, Raspberry Pi, and the Gertduino. For those interested, I have listed the basic code below for a single motor, and the code needed to make two motors turn.




Not much example code existed for the Motor Shield R3, but there is a handy instructable that shows an example on basic functionality.


The Arduino Motor Shield R3 breaks its pins down as follows:

 

 

FunctionChannel AChannel B
DirectionDigital 12Digital 13
Speed (PWM)Digital 3Digital 11
BrakeDigital 9

Digital 8

Current SensingAnalog 0Analog 1

 

Single Motor Code:

 

/*************************************************************
Motor Shield 1-Channel DC Motor Demo
by Randy Sarafan


For more information see:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/


*************************************************************/


void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin

}


void loop(){

  //forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed

  delay(3000);

  digitalWrite(9, HIGH); //Eengage the Brake for Channel A


  delay(1000);

  //backward @ half speed
  digitalWrite(12, LOW); //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);   //Spins the motor on Channel A at half speed

  delay(3000);

  digitalWrite(9, HIGH); //Eengage the Brake for Channel A

  delay(1000);

}

 

Dual Motor Code:

 

/*************************************************************
Motor Shield 2-Channel DC Motor Demo
by Randy Sarafan


For more information see:
http://www.instructables.com/id/Arduino-Motor-Shield-Tutorial/


*************************************************************/


void setup() {

  //Setup Channel A
  pinMode(12, OUTPUT); //Initiates Motor Channel A pin
  pinMode(9, OUTPUT); //Initiates Brake Channel A pin


  //Setup Channel B
  pinMode(13, OUTPUT); //Initiates Motor Channel A pin
  pinMode(8, OUTPUT);  //Initiates Brake Channel A pin

}


void loop(){



  //Motor A forward @ full speed
  digitalWrite(12, HIGH); //Establishes forward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 255);   //Spins the motor on Channel A at full speed


  //Motor B backward @ half speed
  digitalWrite(13, LOW);  //Establishes backward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 123);    //Spins the motor on Channel B at half speed



  delay(3000);



  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B




  delay(1000);


  //Motor A forward @ full speed
  digitalWrite(12, LOW);  //Establishes backward direction of Channel A
  digitalWrite(9, LOW);   //Disengage the Brake for Channel A
  analogWrite(3, 123);    //Spins the motor on Channel A at half speed

  //Motor B forward @ full speed
  digitalWrite(13, HIGH); //Establishes forward direction of Channel B
  digitalWrite(8, LOW);   //Disengage the Brake for Channel B
  analogWrite(11, 255);   //Spins the motor on Channel B at full speed


  delay(3000);


  digitalWrite(9, HIGH);  //Engage the Brake for Channel A
  digitalWrite(9, HIGH);  //Engage the Brake for Channel B


  delay(1000);

}