If you live on the east coast of the US then you know that this week was the week of "Snowmageddon." For those not in the know, an intense storm which was supposed to blow through causing massive disturbances on a scale not seen for decades. But as usual, a lot of the news reports ended up being hype however, this doesn't mean to say that we didn't get snow and that there were not delays. Specifically, for the 2nd time since I started blogging about my lessons utilizing the Raspberry Pi school was delayed for 2 hours which typically results in my lessons being cut short 10-15 minutes according to an adapted schedule. No worries though because I felt confident that I could adapt my lesson plan accordingly. Click the link to view the lesson I had originally planned and keep in mind that I am building upon 4 successive lessons leading up to this one:




If you'd like to have some insight into what was covered earlier then please reference my blog posts written prior to this. But in a nutshell, I've been developing a robot which draws shapes and have alluded this to the Python turtle library. The class I am teaching is called Beginning Programming so I decided that the most basic topic to cover, if I was to introduce someone to the Python programming language, is drawing shapes. Then again, it is more engaging in interesting to then use these commands to get something to move. If any justification is needed then that is why I've trotted down this route of developing a robot rather than continue to focus on drawing endless polygons on a screen.


Here is a sneak peak at what we did during this lesson:

FullSizeRender (20).jpg

So, this final lesson is about connecting an Arduino microcontroller to a Rasberry Pi. Why would you want to do this, you're probably thinking? Well, it's because the lesson provides a valuable thought experiment for interfacing the functionality of different hardware and software. What this does is provide valuable realizations into the nature of computers and touches upon the concept of serial communication; the idea of transferring data back and forth between electronic interfaces. If you read about the background on the Raspberry Pi, the intentions of the developers was not necessarily to use the device as a means for teaching programming (as I have) but more so, as a tool for teaching the basics of what a computer is; the parts, the functions among other structural components which truely make the Raspberry Pi a computer rather than what the Arduino is. Let me highlight at this point- the primary difference resides in the fact that an Arduino Uno does not have a software interface embedded within it (Windows, Linux or Mavericks style) but is a blank tool which can simply tap into the frequencies of digital/analog output signals. In the end, it must be a "slave" to a higher order controller- either a laptop computer or a Raspberry Pi; if it is to be programmed or if data is to be passed back and forth- unpacked and parsed apart for some type of communicative function.


You can read about tonnes of ways to connect and Arduino to a Raspberry Pi and you may come across articles such as this one which talk about slaving an Arduino to the Raspberry Pi using 12C pins:

Connecting an Arduino to a Raspberry PI using I2C | Peter Mount's Blog


Fair enough, and well done Peter, but there are also simpler ways to do this. Like plugging in the Arduino to the Raspberry Pi using the traditional method of a USB A-B cable:



Connected to the Arduino like so (ignore those lights in the picture):

arduino USB cable.jpg

The USB end plugged into the Raspberry Pi:


There are 4 of those USB ports so you'll still have room for your Wi-fi, USB memory stick or whatever accessories you want to "sprinkle" into the holes of your Raspberry Pi.

Once you've connected the two you are ready to enact Serial communication between a Raspberry Pi and an Arduino: even more interesting, this will occur through combining communication between two different programming languages- Python on the Raspberry Pi and Arduino (C++).


How to introduce these topics to my student though....with so little time and with so much that can be talked about regarding the topic of interfacing and creating a collage of different types of hardware. Well, I decided to start out with giving a demonstration of how the serial monitor on my laptop can be used to communicate with the Arduino to turn an LED on and off. I took the example found within the Arduino IDE called "Physical Pixel" to show this and this saved me time because I didn't have to write anything at all. The set up for the Physical Pixel sketch is as follows:


The code for turning the LED on and off can be found in the drop down example list within the Arduino IDE under the subheading "Communication":

const int ledPin = 13;
int incomingByte;   
void setup() {
  pinMode(ledPin, OUTPUT);

void loop() {
  if (Serial.available() > 0) {
    incomingByte = Serial.read();
    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);
    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);


In this code, you send the letter character 'H' through the serial monitor to turn the LED on pin 13 on, you send the letter character 'L' through the serial monitor to turn the LED off. Very basic and an interesting point of study to look at to begin the lesson.


The objective for this lesson was to get a servo motor working in an up and down function so it then became necessary to relate the LED example to making a servo motor go up and down. Justification for why we are controlling the servo motor through an Arduino rather than the Raspberry Pi is because the pins of the Raspberry Pi have a much harder time regulating the PWM signals to get the servo to function properly. It becomes simpler to use a micro-controller which is better and controlling hardware to do this task. Here is the sketch that I wrote to demonstrate the similarities between controlling the LED in the serial monitor and then the servo motor:


#include <Servo.h>

Servo myservo;
int pos = 0; 
const int ledPin = 13;
int incomingByte;  

void setup()


  pinMode(ledPin, OUTPUT);

void loop()

  if (Serial.available() > 0) {

    incomingByte = Serial.read();

    if (incomingByte == 'H') {
      digitalWrite(ledPin, HIGH);

    if (incomingByte == 'L') {
      digitalWrite(ledPin, LOW);



I had my student, William, compare and contrast the two. At this point he doesn't need to know much about the Arduino syntax; we will examine that during a later lesson; just the basics of how the Arduino is recieving a command serially and then does this depending on if the letter sent is "H" or "L":



This was then related to the Python sketch William and I wrote earlier, where a function for a button calls for our Raspberry Pi "Turtle Bot"to move in a particular way. Now we had to adapt our sketch so the buttons send "letter character" through the serial monitor. To show William how to this I made a photocopy of the following code and we discussed the example. The code creates a Tkinter button for turning an LED on and OFF. If we had more time I would have loaded this up and showed William it's function but at this point we had to keep the ball rolling. The Python code is as follows:

import serial
from Tkinter import *
arduino = serial.Serial('/dev/ttyACM0', 9600)

def ledOn(): # this is how you define functions in Python. ledOn() is executed if onButton is pressed
  arduino.write('h') # sends 'h' to the Arduino
def ledOff(): # this is how you define functions in Python. ledOff() is executed if offButton is pressed
  arduino.write('l') # sends 'l' to the Arduino

appWindow = Tk() #creates the application window
appWindow.wm_title("LED Control") #displays title at the top left

var = IntVar()

onButton = Radiobutton(appWindow, text="LED ON", variable = var, value = 1, command=ledOn)
onButton.pack( anchor = W )
offButton = Radiobutton(appWindow, text="LED OFF", variable = var, value = 2, command=ledOff)
offButton.pack( anchor = W )

appWindow.mainloop() # main loop. From here, the GUI starts updating and responding to user inputs.


Key points to highlight are:


1. This is the sketch that will be loaded into the Raspberry Pi. Notice how there is mention in line 3 of "arduino = serial.Serial('/dev/ttyACMO', 9600)"

/dev/ttyACMO is the name of the serial port when the Arduino is plugged into the Raspberry Pi. This is the same as when the Arduino is plugged into my laptop computer and for example "COM3" comes up as the name for what the serial port between the two is.


2. That we need to "import serial" into the python library to call this function into effect


3. That within the button notice how we have a command= ledOn which is related to the function for "led On" in line 6


All these things being considered I asked William to adapt this, sketch (which he had written previously, noticing that the buttons he has made earlier also have a command sent upon a mouse click which calls into effect a type of motion for the robot:

import serial
import time
from Tkinter import*
import RPi.GPIO as GPIO
import picamera

from rrb2 import*

def Square():

def Forward():

def Stop():

def Left():

def Right():

def Reverse():

def Camera():
    with picamera.PiCamera() as camera:
appWindow = Tk()
appWindow.wm_title("Robot Square")
appWindow.config(bg = "lawngreen")

controlFrame = Frame(appWindow, width=1000, height = 1000, bg="red2",highlightthickness=2, highlightbackground="#111")
controlFrame.grid() #did not have this last lesson

btnFrame = Frame(controlFrame, width=1000, height = 1000, bg="blue")

upBtn = Button(btnFrame, text="FORWARD", command=Forward, bg="green")
upBtn.grid(row=2, column=2, padx=5, pady=5)

stopBtn = Button(btnFrame,text="STOP",command=Stop, bg="purple")

LeftBtn = Button(btnFrame, text="LEFT", command=Left, bg="orange")
LeftBtn.grid(row=3, column=2, padx=5, pady=5)

RightBtn = Button(btnFrame, text="RIGHT", command=Right, bg="yellow")
RightBtn.grid(row=3, column=4, padx=5, pady=5)

ReverseBtn = Button(btnFrame, text="REVERSE", command=Reverse,bg="black")
ReverseBtn.grid(row=4, column=2, padx=5, pady=5)

SquareBtn = Button(btnFrame, text="SQUARE", command=Square, bg="magenta")
SquareBtn.grid(row=4, column=4, padx=5, pady=5)

CameraBtn = Button(btnFrame, text="CAMERA", command=Camera, bg="brown")
CameraBtn.grid(row=5, column=2, padx=5, pady=5)



Look, for example, at the similarities between line 61 and then line 22 (the function which it calls into effect). The only difference now is that when a button is pressed, it must call a function which sends a letter character through the serial monitor "H" or "L" which then corresponds to what the sketch on the Arduino reads with "H" or "L": Up or Down. Williams code looked like something like this in the end:


import serial # import the serial library
import time # import the time library
from Tkinter import * #import Tkinter GUI library
import RPi.GPIO as GPIO
arduino = serial.Serial('/dev/ttyACM0', 9600)

from rrb2 import*
rr =RRB2()

def Forward():

def Reverse():

def Left():

def Right():

def Gear1():

def Gear2():

def Gear3():

def Gear4():

def Gear5():

def Stop():

def Auto():

appWindow = Tk() # creates the application window (you can use any name)
appWindow.wm_title("Raspi Robot!") #Makes the title that will appear in the top left
appWindow.config(bg = "lawngreen")

#Control Frame and its contents
controlFrame = Frame(appWindow, width=10000, height = 10000, bg="red2", highlightthickness=2, highlightbackground="#111") #defines the control frame
controlFrame.grid() #positions the control frame with the corresponding parameters

btnFrame = Frame(controlFrame, width=10000, height = 10000, bg="black")# defines the button frame with these characteristics
btnFrame.grid() #positions the button frame inside which control buttons will reside

upBtn = Button(btnFrame, text="FORWARD", command=Forward, bg="green2") #defines the UP button
upBtn.grid(row=2, column=2, padx=10, pady=10) #positions the UP button within the button frame

downBtn = Button(btnFrame, text="REVERSE", command=Reverse, bg="deepskyblue") #defines the DOWN button
downBtn.grid(row=4, column=2, padx=10, pady=10) #positions the UP button within the button frame

leftBtn = Button(btnFrame, text="LEFT", command=Left, bg="orange2") #defines the LEFT button
leftBtn.grid(row=3, column=0, padx=10, pady=10) #positions the LEFT button within the button frame

rightBtn = Button(btnFrame, text="RIGHT", command=Right, bg="khaki") #defines the RIGHT button
rightBtn.grid(row=3, column=3, padx=10, pady=10) #positions the RIGHT button within the button frame

stopBtn = Button(btnFrame, text="STOP", command=Stop, bg="red") #defines the STOP button
stopBtn.grid(row=3, column=2, padx=10, pady=10) #positions the STOP button within the button frame

autoBtn = Button(btnFrame, text="AUTO", command=Auto, bg="spring green") #defines the STOP button
autoBtn.grid(row=5, column=2, padx=10, pady=10) #positions the STOP button within the button frame

cameraBtn = Button(btnFrame, text="GEAR 5", command=Gear5, bg="red2") #defines the STOP button
cameraBtn.grid(row=1, column=6, padx=10, pady=10) #positions the STOP button within the button frame

cameraBtn = Button(btnFrame, text="GEAR 4", command=Gear4, bg="DarkOrange1") #defines the STOP button
cameraBtn.grid(row=2, column=6, padx=10, pady=10) #positions the STOP button within the button frame

cameraBtn = Button(btnFrame, text="GEAR 3", command=Gear3, bg="tan1") #defines the STOP button
cameraBtn.grid(row=3, column=6, padx=10, pady=10) #positions the STOP button within the button frame

cameraBtn = Button(btnFrame, text="GEAR 2", command=Gear2, bg="yellow2") #defines the STOP button
cameraBtn.grid(row=4, column=6, padx=10, pady=10) #positions the STOP button within the button frame

cameraBtn = Button(btnFrame, text="GEAR 1", command=Gear1, bg="yellow green") #defines the STOP button
cameraBtn.grid(row=5, column=6, padx=10, pady=10) #positions the STOP button within the button frame

appWindow.mainloop()# begins main loop


Notice how there are multiple "arduino.write" commands being called into the sketch to write a letter character to the serial monitor. William was able to understand this and write the buttons correctly into his sketch however, he did rush a bit when writting his code so we got this mass of red error messages when we ran the sketch:

FullSizeRender (18).jpg


This is where the lesson ended. But I monitored his progress and I knew he was on the write track. After William left I spent some time looking at his code and noticed that he had a few errors mainly spelling "arduino" as "arduina" at one point. And also writing the serial port name as /dev/ttyACM0; using the number "0" rather than the letter "O." It's those small things that will get you. Later I emailed William to inform him of where the erros were, making note is his code to show him next time. When everything is cleaned up though this is what executing the Python code produces:


That wonderful control interface, now fresh with a "pen up" and "pen down" function controls this very special robot:

FullSizeRender (13).jpg

FullSizeRender (15).jpg

Ah! -The classic red pen and Scotch tape likens this drawing robot to something found out of a crypt from the Valley of the Kings.It was functional though- and provided an excellent; albeit rushed and short introduction to serial communication and the concept of interfacing different types of hardware and software to produce a given function. Please take a look at the following video again to see the end result:


Now that my Element 14 Road Test project is complete it is time for me to reflect on the experience as whole.

What I realized is that the Raspberry Pi is a great tool for bringing programming to life. However, I think it's potential is limited by the experience of the instructor and also how much time he/she wants to put into preparing the device for use. Specifically, you need to load in programs from the terminal which do not come pre-installed on the Raspberry Pi and if you choose to follow my wrote of teaching programming through Python sketches you may find yourself having to create files with example Python sketches on the Rasbian desktop for your student to refer too.

Teachers could make an interesting lesson out of setting up and configuring a Raspberry Pi however, this would take much more time than I had.

The camera module sent to me by Element 14 did not really have a valuable use for my project but there are ways you can make interesting use of it if you wish to build upon what I have done. Online, you can find a wealth of information about turning the camera module into a facial detection device. Can you image how cool it would be for my drawing turtle bot to draw certain shapes if it recognizes certain faces? This would be in place of creating buttons to produce remote movement.


Another idea is to create many drawing robots to examine swarm behavior. The camera could be used by the robot to recognize other devices and then behave in a particular way as a result.


In the end, this project ended up being great for getting my student, William, excited and engaged with programming during very short lessons. The skills which I tried to build upon at the very end was a type of lateral thinking- generate through processes where a student can think creatively about combining and creating with seemingly different technologies. It's this type of innovate thinking ability that we need more of in our societies. Artists who are also creators with technology- the ultimate result being to push the limits of what can be achieved though mixing and matching the devices at our disposal. The Raspberry Pi is a means for achieving this purpose, but only if lessons are designed to make use of it's functions apart from it's basic form as a single board computer; I am grateful to Element 14 for sending me the materials and giving me the opportunity to push my instructional style into incorporating this undoubtedly valuable tool.