ME170C/ECE181C: Arduino and Hardware


As you compare and look at the pictures on the Arduino website, you’ll notice that a substantial amount of hardware has been added on to the Arduino boards you are using.  These add-ons are called ‘shields’.  All together the Arduino and shields are called a ‘stack’.  Your Adruino stack has a Lithium Backpack shield, which supplies the Arduino board with power, a motor shield, which allows the Arduino to control motors and servos, and a prototyping shield, which offers a good way to connect sensors and additional circuitry to the Arduino.  Additionally, an LCD screen with supporting hardware can be connected to the board through a serial connection and an external power pack is available for powering sensors and motors.   For many of these added functions a library will be required.  If you are getting errors when you are uploading code, make sure that you have the necessary libraries downloaded and present in your Arduino/hardware/libraries/  directory and that you have the appropriate ‘#include <XXXXX.h>’ line at the beginning of your code.  Pictures , descriptions, and reference materials for each of these add-ons can be found below:

Lithium Backpack:

The shield below the Arduino contains a lithium ion battery.  This battery supplies power to the Arduino.  There isn’t much to this shield, it’s already been permanently attached and should not be removed.  There is a small switch that is labeled ‘batt’(battery power) and ‘chrg’(charge).  When the switch is set to charge, the battery is charged from the Arduino’s USB port, the included mini-USB port, or possibly other external power connections.  If there is no external power connection, then switching to charge will turn off the Arduino.  More information can be found on the manufacturer’s website:

Power Pack:

To satisfy all of your robot’s power needs, aside from those of the Arduino and motor shield circuitry, a custom power source is provided.  The power is provided by 5 rechargeable NiMH AA battery cells that together nominally provide 6V (1.2 V x 5 cells) and can be removed to be recharged.  Power from the batteries flows first through a fuse and then into toggle switch mounted on the power pack.  When the switch is in its ‘off’ position (down), battery power will be cut off from all connections.  This switch should be turned off whenever any work is being done on your robot and whenever possible to conserve battery power.  After the toggle switch, power is split into two wires.  One wire carries power to the motor shield’s external power terminal that powers motors, servos, etc. The other wire is regulated down to 5V and connected to the prototyping shield by a red wire; this 5V supply is used to power sensors and other circuitry.  The wire taking power to the motor shield has an in-line switch that can be used to cut power to the motor shield without interrupting the 5V supply from the power pack to the prototyping shield. 

Remember, always turn off the main toggle switch on the power pack before doing any work on your robot and whenever possible to conserve power.  Also, keep in mind that there are two separate power sources to keep charged.

Power Pack


Motor Shield:

On top of the Arduino sits the motor shield.  This addition is what allows the Arduino to drive and control all the motion of your robot.  To make you use of the motor shield, you will need to download the AFMotor library, put it in your Arduino/hardware/libraries folder, and then add the line ‘#include <AFMotor.h>’ to the beginning of your code.  This download and additional information about the motor shield can be found here: .   

To protect the Arduino and provide more consistent behavior, a separate power source is used to power the servos, motors, and anything else controlled by this shield.  This power pack will be discussed later, but it is important to note that the polarity is critical when you connect this supply - be certain that you are connecting the positive lead to the positive terminal and the negative lead to the negative terminal of the external power terminal block.  Do not trust color coding and double check with a multi-meter before you make the connection to the terminals.  You shouldn’t ever need to even mess with this connection, since a switch is in-line to cut off power.

The capabilities of this shield are impressive, but they do come at a cost- the motor shield uses up a lot of the Arduino’s available pins, but it’s not quite as bad as it looks.  Below is a table summarizing what pins are always used, sometimes used, and never used by the motor shield.


Pins:  4, 7, 8, 12

Required by motor shield


Pins:0- 2, 13, 14-19 (A.K.A.  Analog Pins 0-5)

Not used by motor shield


Pin 11

Available if motor port 1 is unused

Pin 3

Available if motor port 2 is unused

Pin 5

Available if motor port 3 is unused

Pin 6

Available if motor port 4 is unused

Pin 9

Available if servo port 1 is unused

Pin 10

Available if servo port 2 is unused


It is recommended that, if possible, motor port 2 be left unused since pin 3 is one of two pins (2&3) with a hardware interrupt option, making it very useful for other purposes.

Prototyping Shield:

This shield offers pin connections to all of the Arduino pins and a small breadboard for connecting sensors and custom circuitry.  A regulated 5-volt supply is available from the Arduino board; however, the 5V supply coming from the external power pack should be used instead to avoid damaging the Arduino board.  There is a lot of design freedom for you here on this shield, but there are some very important guidelines that must be followed:

Use consistent color coding of wires.  For our class, red wires should ALWAYS and ONLY denote a regulated 5V supply and black wires should always and only be connected to ground.  These wire colors should only be used for these purposes- there are many other colors available for you to use for other purposes.

Use header pins to connect external electronics to the prototyping board.  Plugging wires directly into the board can result in the end of the wires breaking off in the sockets- effectively making the socket useless, as it is impossible to get the broken end back out.  It is acceptable for you to plug wires into the breadboard portion of the proto shield if they are simply making connections between different areas of the breadboard and will not be plugged and unplugged often.

Use asymmetric header connections when appropriate.  Many of the sensors can be destroyed if they are plugged in wrong, so whenever possible make it difficult make this mistake.  See the figure below for an example.

Think carefully about what you are doing before plugging in wires.   There are red 5V wires coming into the prototyping board from the power pack and a 5V pin from the Arduino, if any of these are connected or simply touched to ground they will short the Arduino or the power pack and blow fuses or damage hardware.  The 5V also has the potential to destroy sensors, LCD circuitry, etc. so be very careful when plugging anything into the breadboard or whenever connecting red wires.  If you are ever unsure of a wire or pins state, test it with a multi-meter.  ALWAYS SWITCH OFF THE POWER PACK BEFORE CONNECTING OR DISCONNECTING ANY WIRES ON THE PROTO SHIELD.

Wiring Connectors: The configuration on the left is good, it can only be plugged-in one way and if it is plugged in wrong, no connection will be made to the 5V power (red).  The configuration on the right is bad; it could easily be plugged-in the wrong way and in doing so would reverse the polarity of the voltage supplied to the sensor.


Maintain a diagram of your proto shield configuration.  As your robots increase in complexity, it will become increasingly important to keep track of what sensors you are plugging in where.  Do not be so foolish as to think that you will remember what goes where or to assume you will never have to unplug sensors and remember where they were connected.  Keeping a sketch is the best way to avoid damaging equipment and to avoid troubleshooting inconsistencies between pin assignments in your code and on the hardware.


LCD Screen:

The LCD screen comes packaged with hardware that allows the Arduino to control the LCD display through a serial interface.  This means we can control the entire LCD through a single pin on the Arduino, thus keeping other pins available for other uses.  Information on using this hardware can be found at the manufacture’s website, at the bottom of this page:  under the heading ‘Files and Links’.  Here you will find the command summary and other tools/information which will help you to do everything from printing text to creating graphics and animations.  Be sure to check out their example code.  A simple example of printing text to the LCD is shown below:

(Note:  for this specific code to work, the LCD should be supplied with power (red to 5V, black to ground) and connected to the Arduino through the white wire at pin 13.  Do not cut the wires from the LCD, simply plug header pins into the sockets at the end of the wire coming from the LCD.)

 //Load necessary library

#include <SoftwareSerial.h>

//Setup Communications with LCD Screen...

#define rxPin 4  // rxPin - not used since Arduino just sends without listening- just make this an unused Arduino pin number

#define txPin 13 // Pin LCD (white wire) is connected to. (can be any available pin)

// mySerial is connected to the TX pin so mySerial.print commands are used

SoftwareSerial mySerial =  SoftwareSerial(rxPin, txPin);

void setup(){

   pinMode(txPin, OUTPUT);    // Set selected pin to be used as an output

   mySerial.begin(9600);          // 9600 baud is LCD controller chip comm speed

   mySerial.print("?f");             // clear the LCD

   delay(10);                              // allow time for LCD to clear


void loop(){

   mySerial.print("?f");                   // clear the LCD

   mySerial.print("?x00?y0");       // move cursor to first character of line 0

   mySerial.print("Hello!");           //print something

   mySerial.print("?x07?y0");       // move cursor

   delay(1000);                               //pause briefly

   mySerial.print("This message is longer...");  //Print something else



Attaching Sensors:

As your robot’s tasks increase in difficulty and complexity you will establish the detection needs of your robot and will need to select appropriate sensors.  There are several sensor types available to you, be sure to spend some time looking over the data sheets and familiarizing yourself with their capabilities.  Once you have a sensor you will need to determine how to best attach it to your Arduino (i.e. power requirements, additional circuitry (if any), type of signal, etc.).  To do this well you will need information about the sensors and also some basic understanding of the capabilities available to you on the Arduino.  Below you will find brief introductions to some key concepts, there are many great resources online that will give far better and more complete explanations, the following explanations are only meant to introduce the topics and some vocabulary to help direct your search for more information elsewhere.

Analog and Digital Signals – The Arduino receives information for sensors in the form of voltages.  A digital input interprets a voltage signal in two ways, either low or high (0 or 1).  A voltage signal at a pin configured for digital input will be interpreted as a 0 if it is below some voltage, typically 0.8 V or less, and interpreted as a 1 if it is above some voltage, typically 2.2 V or more.  As you can see, there is a grey area (0.8V- 2.2V) where a 0 or 1 could be assigned, we will want to avoid this uncertain region and will discuss ways of doing this later.  A voltage signal of 0-5V at a pin configured for analog to digital conversion will be converted from an analog signal into a number proportional to the signal’s voltage as a fraction of 5V that depends on the resolution of the converter.  The Arduino has 10-bit A/D converters so a voltage from 0-5V will be converted to a number between 0 and 1023; with 0V being a 0, 2.5V being 511, 5V being 1023, etc.  The Arduino has analog to digital pins labeled Analog 0-5, however, when coding they are known as pins 16-21 when reading from them, and pins 14-19 when enabling/disabling the pull-up resistors.

Pull-up Resistors – In introducing digital signals, we identified a need to ensure that we keep the voltage we are reading out of the ‘grey area’ where it is unclear whether it will be interpreted as a 0 or 1, that is, we want our signal to be as close to 0V or 5V as possible.  The ‘grey area’ problem typically arises from leaving a pin ‘floating’ (i.e. without a voltage applied to it or being connected to ground).  For a demonstration of this, turn on a multi-meter and set it to measure voltage.  When the leads are not touching each other or measuring a voltage you will discover that the voltage measurement bounces around.  This can be caused by many things, but is especially common with long unshielded wires since they act like antennae. 

So, to eliminate this problem we want to make sure that our pin is either connected to a voltage (preferably close to 5V) or to ground.  The most common way of ensuring this is to use a pull-up resistor.  In a pull-up resistor configuration, a large resistance is placed between a 5V source and the pin and then a sensor (in this case a simple switch) between the resistor and the pins.  The result is that when the switch is open, the pin is brought up to 5V.  When the switch is closed (connecting the wire to ground), the voltage at the pin is brought down to ground (0V).  The resistor is important because it prevents a large amount of current from flowing from the 5V source, wasting precious power, or worse, shorting the board when the switch is closed.  The diagram below shows what the circuit would look like.

Example of pull-up resistor usage.


To help illustrate this idea, imagine that you want to keep a sink either full of water or empty.  One way to ensure this would be to keep the faucet running at a slow trickle, if you open the drain the sink will empty, if you close it, the sink will fill with water.  If we assume the transitions are very fast, like in electric circuits, the sink would effectively be either full or empty at any given time.

Conveniently, the Arduino has built-in 20-50k pull-up resistors available on all of its pins; these pins can be enabled or disabled through the software.  The next section will discuss how to do this.

Configuring Pins on the Arduino – From the picture above, or on the Arduino itself, you can see that the digital pins are located along one side of the board and numbered from 0 to 13.  On the other side, there are six analog pins, they are labeled 0-5; however, in the software they are referred to as pins 14-19.  All of these pins (including the analog pins) can be set to act as digital input pins, that is, they can read a digital signal; or can be set to act as digital output pins, that is, they can be set to output 5V or 0V.  To set a pin to input or output mode, use the commands pinMode(#,INPUT) or pinMode(#,OUTPUT) where # is the pin number you want to change.  When in output mode, the commands digitalWrite(#,HIGH) and digitalWrite(#,LOW) will set the pin to output a high (5V) or low (0V) signal, respectively.  When in input mode, the same commands will enable or disable the pull-up resistor, respectively.  To read from a pin use the command digitalRead(#), this can also be used to check what value the pin is set to.  On the analog pins, the command analogRead(#) will convert the analog signal at pin # to a value between 0 and 1023, as discussed earlier in the analog and digital signal section.  Use caution when reading analog signals from analog pins that have recently been switched from a digital mode, as the reading can be affected briefly after switching back.  It is also important to be mindful of the pull-up resistor setting of the pin, since switching a port from output to input will not change the pull-up resistor setting.

Hardware Interrupts – The Arduino has two pins (2&3) with available hardware interrupts.  Hardware interrupts allow for a voltage signal to interrupt the microprocessor in the middle of whatever it is doing and have it perform some task before it returns to its previous task.  .  Essentially, the processor doesn’t have to do any checking up on the sensor, since the sensor can alert the processor when something has changed.  This is extremely useful if you want to know when something has changed at a sensor, without having to write software that constantly polls the sensor.  Some ways in which this feature can be useful to your robot is in implementation of a ‘kill switch’ to pause your code at any point or of an encoder that would count wheel revolutions.  To use hardware interrupts you will use the command attachInterrupt(); read more about this command in the Arduino reference information.

Pulse-width Modulation (PWM) – The Arduino has six pins that offer pulse-width modulation.  This is essentially an ability to switch on and off the output voltage at the pin at relatively high frequencies.  This can be used to drive a piezo-transducer to produce audio signals, a motor at different speeds, etc.  Driving the piezo-transducer is pretty straight-forward, simply hook up the transducer and apply voltages at the desired frequency by switching the PWM on and off with the appropriate delays in between.  Varying the speed of the motor works similarly, but the motor shield and the accompanying software libraries take care of this for us.  However, it is useful to understand the basic concept.  DC motors typically operate as either on or off, PWM allows them to be operated somewhere in between.  This is done by powering the motors with a square wave signal and then changing the width of the pulses.  As the ‘on’ pulse width is increased relative to the ‘off’, the signal will approach a full signal and thus power the motor at full speed.  Shorter pulses will yield a slower speed.  The higher the frequency of the square wave signal, the smoother the operation of the motor.  To perhaps illustrate this better, imagine that you are riding a bicycle but can only pedal at one speed or not at all.  To go slower than full speed, you would simply spend more time coasting than pedaling until you have the speed you desired.