Introduction

Arduino overview

Arduino is a simple Atmel microprocessor with an easy to use electronic and programming interface. An Arduino board includes the Atmel processor and combines it with a USB serial converter, 5 volt voltage regulator, and breakouts for the all of the pins. There’s more to it, but that’s the important take away here.

WhyArduino?

Arduino provides and easy entry into both the physical electronics and programming aspects of robotics. Because most of the work is done when it comes to interfacing with the microprocessor it makes it very easy to create simple mobile robots. With a little practice and creativity it is not much more difficult to leverage an Arduino for more advanced applications.

For example, in one of my larger projects I am using an Arduino Mega as, what I’m calling, the nerve center of the robot. Most of the actuators and sensors are connected to the Arduino which is then connected via USB to a laptop running Linux and the Robot Operating System (ROS). The laptop does the heavy lifting in terms of processing and decision making while the Arduino separates the task of managing sensors and motors from the main computer. As more functionality is added to the robot I can build each in a modular format which can then be added to the robot as needed. Each module can use its own Arduino, abstracting the functionality. This is similar to how your home printer works. The computer sends an instruction to print a document, the printer manages the performance of this task, and then notifies the PC of progress or errors.

What you need:

  • Arduino Uno or clone (Amazon,  Sparkfun)
  • The appropriate USB cable for your selected Arduino
  • Continuous rotation servo (Amazon, ServoCity, Sparkfun)
  • Mini breadboard (Amazon, Sparkfun)
  • Battery holder for 4 AA or AAA batteries. The type that lines them up flat is better (Amazon, Sparkfun)
  • Computer for programming the Arduino
  • Male to male jumper or connection wires (Amazon)

While you’re ordering or shopping for bits, you will probably want to get the parts for Part 2 also:

  • Second continuous rotation servo
  • Infrared or sonic sensor. Parallax makes a very nice 3 pin sonic ping sensor which makes integrating the sensor easier, but it’s not cheap at around $30. The class is designed around an SR-04 sonic sensor available on-line for around $5 (Amazon)
  • Male to female jumper or connection wires (Amazon)

Opening the box

Arduinos generally include the board and little else. Some clones will include the USB cable and occasionally the pin diagram. But I wouldn’t count on it.

Install software

The software can be downloaded from http://arduino.cc/en/Main/Software. Download and install the appropriate version for your OS. Once downloaded and installed we’ll be ready to go. If it’s not already open, open the Arduino interface.

Connecting the Arduino

Plug the Arduino into your computer using the appropriate USB cable. Different manufacturers use different USB connectors, so make sure you have the right one for your board. When you do this the computer will do its new hardware detected thing and install the needed drivers. Once this is done you’ll need to tell the programming interface where the Arduino is.

Go to tools/serial port and take note of which ports are listed. Then close the interface and restart it. Once again go to tools/serial port and select the new one that was added. This is where your Arduino should be. Now that it’s been assigned that port number it will pretty much always be there.

Programming

Hardware “Hello World”

Like so many other endeavors in the technology world we will be starting with the hardware version of a “Hello World” program. To get you familiar with the parts of the interface and the process of creating a program (called a sketch in Arduino land) we’ll simply get an on-board LED to blink at us.

Parts of a sketch

A sketch consists of 2 parts and must always have these 2 parts if it’s actually going to do anything.

Setup

This is where we will put code we only want to process once. Things like giving names to values to use later or telling the Arduino what pin we’ll use for what will go here.

By the way, giving names to values is generally referred to as setting variables and telling the Arduino what pin to use is generally referred to as initializing.

Loop

This is where most of the work happens. The loop does exactly that; it loops. This code is executed over and over and over again until the Arduino is shut off. So, because of this, you will learn to use a special kind of loop called a “while”. This will let us control the conditions under which our code will run and a way to stop it if we need to do. And by stop it I mean not run the chunk of code in the while loop. The Arduino will keep running its loop. But we’ll go over this later.

Digital output

Digital output is super easy. It’s simply turning something on or off. In electronics parlance it’s setting a pin high or low. There’s not a lot to it. Think of it as turning a switch on or off. When the pin is high, the switch is on. When the pin is low the switch is off. Later we’ll talk about analog output which is more like using dimmer… and is a lie, but we’ll go over that later.

Blinky sketch

Before we can get the light to blink we need to tell Arduino where it is. Actually, Arduino doesn’t care where the light is but we do. The Arduino comes with an LED attached to pin 13. What does that mean? The pin Arduino calls 13 has an LED on it. Really, it’s that easy. But, this pin is also available for general use so we can add other things to it later if we want. For now, though we’re just going to use the attached light. So, what we really need to do is tell Arduino, “hey, there’s something on your pin 13 I want to do something digital with.” But we don’t need to tell Arduino this more than once. So we’ll put it in the Setup block. And, while we’re doing that, we’ll make it easier for us to read and use a variable. In the Arduino interface type this:


const int LED = 13;

void setup() {
pinmode(LED, OUTPUT);
}

So, what did we do here. Well, we told Arduino that the word LED will always be the numeric value of 13 in this program. It can never change, it’s constant. To break it out a touch more technically we told Arduino, “The term LED will be the constant integer 13.” Then, in the one-time code block we told the Arduino to make the pin we call LED ready for output.

So now let’s make the light blink. To do this we will turn it on, wait 1 second, then turn it off. But we want to do this over and over again and we want a 1 second to pass between blinks. Because we want this to repeat we’ll put these instructions in the part that loops. Add this to your code:


void loop() {
digitalWrite(LED, HIGH);
delay(1000);
digitalWrite(LED, LOW);
delay(1000);
}

This part is pretty straight forward. We did exactly what we said we were going to do. The loop section will execute these instructions over and over again. The first digitalWrite tells Arduino to set pin LED (which means 13) to high, turning it on. We then tell it to wait for 1000 milliseconds, then set the same pin to low, then wait another 1000 milliseconds. Arduino will keep doing this until you unplug it.

 

Programming the Arduino

Great! You’ve got a program. Now you have to get it into the Arduino. Connect the Arduino to the PC with your USB cable. Click the verify button to make sure your code is good. It looks like a check mark. Once that is done and everything turns out alright, click the upload button. It’s next to the verify button and looks like an arrow.

There will be some flashy-flashy lights on the Arduino and then the LED on-board will begin to blink.

Now you can make adjustments to the code. In this case you may want to play with the delay settings. Once you make a change, verify and upload your code. It’s really that easy. In a few minutes we’ll make the program more complicated, but the verifying and loading will remain this simple.

Analog output

We talked about digital input, on and off. What if we want to do more than just on and off? Signals that have a range of levels is generally called analog (ok, gross generalization, but go with it). But, with a digital system you can’t truly have an analog signal. Instead what you can do is turn the digital pin on and off really fast and vary the time between pulses. If you do it fast enough the human eye will see that as a dimming effect on lights or a slowing effect on motors. This is called Pulse Width Modulation or PWM. This is how we simulate analog signals. The pins capable of handling PWM on an Arduino are marked with a tilde, “~”.

Why is that important? Well, the signal we use to tell the servo what to do is controlled by this signal. The cool thing is, whereas we interpret it as dimming, other electronic devices can read the differing time between pulses as a numeric value or instruction. It’s this property the servo will use.

Servo control

Servos

Servos are simply a small DC motor with some gears and a small control circuit. The output shaft of the servo is connected to a potentiometer, a variable resistor. The circuit takes the signal you provide it converts it into something it can use, and tries to match the signal from the potentiometer to that. In doing so it moves the attached arm, called a horn, to an appropriate position. That’s a standard servo. We’ll be using a full rotation servo. In a full rotation servo the potentiometer is removed as are the little plastic tabs that keep the horn from turning too far. The signal then is translated to a speed rather than a position.

The servo will have 3 wires; black, red, and something else (usually yellow, but not always). The black and red wires are your power. The yellow wire is for the control signal. In a continuous rotation servo we will be sending values between 1000 and 2000. 1000 means full counter-clockwise, 2000 means full clockwise, and 1500 means stop. With a standard servo we would send values that represent the angle we want the servo to turn, with 90 being in the middle.

There are a couple reasons we’ll be using servos for this project. First, we don’t have to worry about other parts and making a motor controller. It’s not difficult, but it is outside the scope of this lesson. Second, most commercial motor controllers use the same signal you’d send to a servo to control the motors. So, by learning the servo thing now you’ll be ready to move up to bigger things later.

Hooking it up

At this point it’s time to move to something bigger. Earlier we used the LED on the Arduino board. This time we’ll be connecting a servo motor through a breadboard. Why the breadboard? Glad you asked. The Arduino only delivered a little bit of power to whatever you connect to it. If the thing you’re connecting pulls more power than the Arduino can supply it can damage the board. So, we are going to use a small breadboard to connect 4 AAA batteries to supply both the Arduino and the servo. The servo we’re using could probably run from the Arduino’s 5v supply, but you need to be in the habit of making this separation.

Power supply

Our power is coming from 4 AAA batteries. Each battery provides 1.5 volts (approximately). 4 of them will give us 6 volts which is what we need. Well, we need 5 volts, but the extra volt won’t hurt the servo and the voltage regulator on the Arduino will turn it into the 5 volts it needs.

If you are using a breadboard with power rails you’ll connect the black lead from the batteries to a black or blue hole on the breadboard. Doesn’t matter which hole, as long as it has a blue line. The red wire from the batteries will go to a red hole next to the black one. Most boards with this set up will have 2 or more sets of red and blue marked holes on either side of the board. One side is not connected to the other so make sure you use holes on the same side of the board.

If you are using a mini board you may not have these power rails. Instead you’ll hook the batteries up like the diagram below.

Connect the black wire from the servo to a hole that aligns with the black wire from the batteries and a the red wire to a hole that aligns with the red wire from the batteries. This will provide power to the servo.

Signal

The servo will be receiving its signal from the Arduino on the yellow wire. So, connect that wire to the hole on the Arduino marked “~11.” This is pin 11 and the “~” indicates it is a PWM pin. We talked about PWM earlier in the lie that was analog output.

Arduino

Next, connect a red wire from one of the holes lined up with your red battery wire to the hole on the Arduino marked “vin”. This is voltage in and will power the Arduino. Also run a black wire from a hole lined up with the black wire from the batteries to a hole on the Arduino marked “gnd.” This is the ground and is needed to complete the circuit.

That completes the wiring. Now, disconnect the red wire from the Arduino since it shouldn’t be connected during programming.

Basic Servo Connection

Program

This code was tested on an Arduino Uno with a Hitech HSR 1425CR servo. I bring this up because some servos will have different ranges for the values. This particular servo has a larger dead zone, the range of values at which the servo stops spinning. In this case, 1500 to 1600. Your servo may be different so play with the values in the second part.

Like before we’ll start by telling the Arduino what pin we want to use to control the servo. Start a new sketch and enter the following:


#include <Servo.h>

const int servo1Pin = 11;
Servo servo1;

void setup() {
servo1.attach(servo1Pin);
servo1.writeMicroseconds(1500);
}

Ok, we’ve done a lot more here than before. That first line, “#include <Servo.h>” tells the program to bring in some more code than what’s included in the basic set. This is how you will add all kinds of fun functionality. In this case we’re pulling in the servo library. This will make working with our motors much easier. We created the constant we called servo1Pin and made it mean 11. Next, we created an instance of the servo object and named it servo1. Alright, what does that mean? Basically, that code we told the program to include has in it a block of instructions that abstractly represents our physical servo. This abstraction of a thing or concept is called an object. It’s like a blueprint of a thing. When we create an instance of an object it’s like making that thing from the blueprint. Now that we have our object we can do something with it.

What we’re going to do is spin the servo. You should have a continuous rotation servo. If not we’ll use an alternative program to sweep the servo from side to side.

In the setup function we do 2 things. First we tell the Arduino we will be connecting the servo’s signal wire to the pin we call servo1Pin. Then we tell the Arduino to send the value 1500 to the servo. For a continuous rotation servo this actually means stop.

But let’s start with the speed for the continuous rotation servo. Add the following to your code:


void loop() {

servo1.writeMicroseconds(1400);
delay(1000);
servo1.writeMicroseconds(1200);
delay(1000);
servo1.writeMicroseconds(1400);
delay(1000);
servo1.writeMicroseconds(1500);
delay(1000);

servo1.writeMicroseconds(1700);
delay(1000);
servo1.writeMicroseconds(1900);
delay(1000);
servo1.writeMicroseconds(1700);
delay(1000);
servo1.writeMicroseconds(1500);
delay(1000);
}

This program will move the servo counter-clockwise at half speed for 1 second, then full speed for 1 second, then half speed again for 1 second and then stop. It will repeat this in the clockwise direction as well. It will continue to do this until you disconnect the power.

Speaking of disconnecting the power… Make sure the power is disconnected from the Arduino board before you plug the USB cable to it for programming. Once the program is loaded and the USB cable is detached you will connect the power and watch what happens.

In the second part of this series we will be covering some simple logic, input, the serial monitor (so we can see what’s happening), and getting an ultra sonic range sensor hooked up. By the end of part 2 you will have enough information to build a basic robot.

In part three we will put everything together in the basic robot chassis.