CW>

Projects / Robot Arm

Simple Example

Here I've put together a simple example to show how to connect up just the claw part of the arm with a single servo and program it to open and close. When I started building the arm, I started with a setup something like this to test moving each bit before I put everything together. You could do this with just the servo if you haven't built anything yet and then try connecting it up to some lego later to test it out.

Single servo connected to Pi
Wires connected to GPIO pins
Video of opening and closing claw

Wiring

The three wires for the servo are connected directly to the GPIO pins on the Pi Zero using three jumper wires of the same colours. I power down the Pi when connecting up servos to the GPIO pins. The red wire is connected to the 5V power (first from left, top row as pictured). The brown wire is connected to a ground pin (third from left, top row). And the orange wire for the servo control signal is connected to GPIO pin 17 (see diagram on getting started page, or type pinout at terminal), which is the 6th pin from the left on the bottom row. I picked this one because I use it in the full arm, but you could use any of the GPIO pins shown in green in the pinout diagram. You just need to take note of the GPIO pin number as this is used in the program to move the servo. It's the number shown in green in the pinout diagram you need, not the white numbers.

Program

You can download a zip file containing the code for this example here. Unzip the file into a folder on you Raspberry Pi and follow the instructions further down to compile and run the program. Below is the C++ code for the main program (main.cpp) to move the servo and open and close the claw when you press the up and down keys on the keyboard.


#include "screen.h"

// Main program
int main() {

	// Setup screen for I/O
	Screen io = Screen();
	
	// The GPIO pin for the servo
	const int SERVO_PIN = 17;
	
	// Keep track of the position of the servo
	int position = 1000;
	
	// Check for keys pressed until ESC key
	int key = 0;
	while (key != KEY_ESC) {
	
		// Get keyboard input
		key = getch();
		
		if (key == KEY_DOWN) {
			// Down arrow key: Move servo down
			position -= 50;
			io.printLine("down: %d", position);
		}
		else if (key == KEY_UP) {
			// Up arrow key: Move servo up
			position += 50;
			io.printLine("up: %d", position);
		}
		
		// Set the servo position
		gpioServo(SERVO_PIN, position);
		
		// Sleep for a short time before the next update
		io.sleep(0.1);
	}
	
	return 0;
}

The first line includes the "screen.h" file which is the definition for the Screen class. This class contains some common code to setup things like keyboard input, terminal output and GPIO. I moved this code into a separate file to keep it out of the main program and make it more readable. The code for the Screen class is in the "screen.cpp" file which is compiled along with this main program in "main.cpp". This same Screen class is also used in the complete arm control program, explained in the Programming section. You don't really need to understand the stuff in here for now if you're new to C++ programming. You just need to know that these files are needed for it to work. The interesting stuff is in "main.cpp". One of the first things the program does is create an object of the Screen class called "io". This sets up the keyboard for input and the terminal for output and sets up the GPIO for controlling the servo. Methods of the "io" Screen object are also called later in the program.

The "main" function contains the program. This function runs when the program is started and execution ends when the code exits the main function. The program defines a constant called SERVO_PIN for the number of the GPIO pin used for the servo. I used 17, but this could be a different number, just use the GPIO pin number from the pinout diagram that matches how you connected the servo up. It also defines a variable called "position" to store the current position of the servo and a variable called "key" to read the keyboard input into. The while loop then does the following steps over and over, until the escape key is pressed to return from the main function and end the program:

Compiling and Running the Program

Before compiling and running the program to test the servo for the first time, a few steps are necessary to install and setup some components it needs to work. Type the following commands at the terminal and wait for them to complete. You may need to confirm the install by pressing Y.

The "build.sh" file is a shell script containing just the command needed to compile the program, shown below.

g++ *.cpp -o arm -lpigpio -lrt -lncurses

It just saves you typing all this in every time you change anything. Just type "sh build.sh" at the terminal to compile the program code (once you are in the correct directory/folder). This uses the g++ (GNU C++) compiler to compile the code in the .cpp files along with the pigpio and ncurses libraries that you installed above. It produces a compiled program called "arm". Then type "sudo ./arm" at the terminal to run the compiled program. When the program runs, you should be able to press the up and down keys to move the servo. If it doesn't work or you have problems with any of the steps above, go through them again from the top and check you didn't miss anything. Rebooting the Raspberry Pi can often help too, if something gets messed up. Getting this setup and working the first time is the difficult part. You can now write code to do all sorts of things with the GPIO pins on the Raspberry Pi.