Maker Challenge: Make and Control a Servo Arm with Your Computer

Contributed by: ITL Program, College of Engineering and Applied Science, University of Colorado Boulder

A photograph shows the end of an orange industrial robot arm that is writing text with an ink pen on a roll of paper.
Industrial robots are capable of many things. This robot arm writes with a pen!
copyright
Copyright © 2008 Mirko Tobias Schaefer, Wikimedia Commons CC BY-SA 2.0 https://commons.wikimedia.org/wiki/File:Bios_robotlab_writing_robot.jpg

Maker Challenge Recap

Computer-controlled servos enable industrial robots to manufacture everything from vehicles to smartphones. For this maker challenge, students control a simple servo arm by sending commands with their computers to Arduinos using the serial communication protocol. This exercise walks students through the (sometimes) unintuitive nuances of this protocol, so by the end they can directly control the servo position with the computer. Once students master the serial protocol, they are ready to build some suggested interactive projects using the computer or “cut the cord” and get started with wireless Bluetooth or XBee communication.

More Curriculum Like This

Wirelessly Control Lights and Motors Using XBee Communication!

Students learn how to send signals (such as from buttons or sensors) from one system to another using XBee radio communication modules. By activity end, they are able to control LEDs and motors wirelessly using Arduino microcontrollers and XBee shields. Introduces the concept of the Internet of thin...

Introduction to Arduino: Getting Connected and Blinking LEDs

Students learn how to connect Arduino microcontroller boards to computers and write basic code to blink LEDs. Provided steps guide students through the connection process, troubleshooting common pitfalls and writing their first Arduino programs. Then they independently write their own code to blink ...

What Is Bluetooth?

Students learn about electrical connections, how they work and their pervasiveness in our world. They learn the Morse code rules and translate a few phrases into Morse code. Specifically, they learn about a wireless connection type known as Bluetooth that can be used to control LEGO robots remotely ...

Elementary Lesson
Control a Servo with Your Phone Using Bluetooth!

Students learn how to control an Arduino servo wirelessly using a simple phone application, Bluetooth module and an Android phone. This prepares them to wirelessly control their own projects.

Maker Materials & Supplies

  • computer, Windows or Mac
  • Arduino software; download from https://www.arduino.cc/en/main/software
  • SparkFun RedBoard or Arduino Uno microcontroller and programming cable; an Arduino Inventor’s Kit is recommended (some of its many components are the items listed below), for $99 at SparkFun
  • breadboards
  • 10k-ohm resistors
  • 180-degree servos; one comes with the Arduino Inventor’s Kit recommended above—the generic sub-micro size servo for $9 from SparkFun, or obtain higher-quality servos such as the Hitec HS-422 standard size for $10 from SparkFun
  • 5V voltage regulator for $1 from SparkFun; only needed if you plan to control more than two servos with one Arduino
  • 9V battery; only needed if you plan to control more than two servos with one Arduino
  • Popsicle sticks
  • hot glue gun and hot glue sticks

Kickoff

All around us, electrical devices are communicating with each other. For example, computers talk to printers, which talk to wireless routers. In order to use a computer to talk to devices and make creative Arduino projects, you need to learn the language—the communications protocol—that many of these devices are speaking.

What is the name of the cord that connects Arduino to a computer? It is a USB, which is short for universal serial bus, and the communication type is called the “serial” communication protocol. Having a standard protocol for electrical device communication is like having a common language among humans. Think about how difficult it can be to communicate with someone who does not speak your native language. While different devices communicate using many different protocols, serial is one of the oldest and remains one of the most popular.

Serial communication is like sending data through a single pipe, one bit—a one or a zero—at a time. Be forewarned: While serial communication on the Arduino is powerful, it has some nuances that may seem strange at first. Once you learn what is going on, you will find that it really is not that hard and an entire world of wired and wireless communication will open to you for exploration!

Resources

Maker Time

This maker challenge is all about understanding the nuances of communication between two complicated electrical devices: Arduino and a computer. Since students will use the serial monitor extensively, make sure that the computers and Arduinos are connected and students can upload code reliably.

For the first exercise, use the serial monitor to start processing user input. Have students develop a program that “reads” from the user in the serial monitor input line and outputs this back to the output window of the serial monitor. Although you are just using the serial monitor on the computer to view both the input and output, an Arduino must be connected for this to work. Once the Arduinos are connected via USB within the sketch, direct students to select the correct board and port for their Arduinos using this path: Tools > Board: Arduino/Genuine Uno, then Tools >Port: COM #.

Students will need to upload their code to the Arduino and then select the magnifier icon in the top right corner to see the serial monitor. Have students type their names in the input line of the serial monitor and note the results; if they do it correctly, the results might seem a little strange! See the code in Figure 1.

A screen capture of the serial code—a quick sketch to demonstrate the basics of serial communication. Includes void setup and void loop commands.
Figure 1. The serial code.

What is going on?

When students write their names and hit enter, the Serial.read() command processes each input as a character. Since Arduino cannot speak our language and we cannot speak its language, the system converts each letter into a number that corresponds to the character. The code that the system uses is called ASCII. Each character has a unique number and once students know the code (see the Resources), both they and Arduino can understand the user input!

Prompt: Try typing in 100. What happens? Arduino processes these numbers as numeric characters and assigns a value. You should see a 49 followed by two 48s. Again, if students are interested, the Resources section includes a link to the ASCII code chart. Now try to decipher the ASCII code for your name again!

What should students do if they want to type in 100 and see the value 100 returned to the screen? With some research into Arduino commands, they might be able to figure out the logic to do this. However, an existing Arduino command exists to make this quick and easy:

Serial.parseInt().

Using the previous code, have students try this:

1.     Replace Serial.read() with Serial.parseInt()

2.     Remove the delay.

Prompt: What happens now when you type 100 into the serial window? Answer: The number 100 is returned. Prompt: What if you type in 100.456? Answer: 100 and 456 are returned on two separate lines. Prompt: What are pitfalls to using Serial.parseInt()?

Pitfall 1: Even though the delay was removed, the update rate is still 1 second, which is pretty slow.

Pitfall 2: A zero is always returned, except for user input.

To solve Pitfall 1 and enable quicker updates, use the command Serial.setTimeout() command (see Resources). Add this to the “setup” function, just after the Serial.begin (9600) command. This command adjusts the update speed. Have students try setting this to 100, or even 50.

Solving Pitfall 2 requires another level of thinking. We need the system to respond only to user input. Try wrapping all of the loop commands in the following command string:

If (Serial.available()>0) {

//All other loop commands here

}

Serial.available() returns the number of bytes available to the serial monitor. If nothing is available to return, the loop keeps looping, looking for information. If information exists, then the loop processes the information as before. Have students try this! Does a zero still get returned? If so, make sure that you are wrapping all of the commands in the squiggle brackets of Serial.available().

With a firm knowledge of how serial communication works, let’s apply the knowledge to make something fun!

Prompt: Using these serial commands, can you wire a servo and control it with your computer?

Here is the recipe for success:

  1. Wire a servo and test it using Circuit 8 in the SIK Guide.
  2. Have Serial.parseInt code working so that it returns the value that a user types in to the serial monitor. Tip: If your code keeps returning a zero to the serial monitor, make sure that you have the Serial.available() command appropriately wrapping your code.
  3. Once you have the code working individually, combine the two programs so that the user can control a servo from the screen. Tip: Use the constrain command to make sure that the user is not able to enter commands that would break the servo!

See the code in Figure 2.

A screen capture shows the code with servo.
Figure 2. The servo code.

Once students have a working servo, have them:

  • Attach a Popsicle stick to the servo to make a rudimentary arm that you can control. 
  • Have several groups team up to make a robotic hand (see Figures 3-5), or even an arm. 
  • Create a robot that can write the letter C. What other letters can you write?

A photograph shows five servos each connected by wire to an Arduino. On each servo is hot-glued a portion of a popsicle stick, making each like a finger, and together, mimicking a human hand. For comparison, an open hand is next to the five servos.
Figure 3. A human hand next to a mechanical hand made of Popsicle sticks moving on servos controlled by an Arduino.
copyright
Copyright © 2018 Rachel Sharpe, ITL Program, College of Engineering and Applied Science, University of Colorado Boulder

A photograph shows an Arduino and breadboard hooked up to five servo motors, each with glued-on partial Popsicle stick heads, mimicking a five-fingered human hand. Same setup as Figure 3.
Figure 4. A closer view of the five servos and Popsicle stick “fingers.”
copyright
Copyright © 2018 Rachel Sharpe, ITL Program, College of Engineering and Applied Science, University of Colorado Boulder

Note: If students wish to use more than two micro servos to make a hand, they need to use a 5V voltage regulator and a 9V battery. More than two servos draw more current than the USB power cable can supply from the 5V pin on the Arduino to enable them to move and operate smoothly. Figure 5 depicts how to hook up the voltage regulator in combination with a 9V battery to power five servos. (For more details on where to purchase a voltage regulator, as well as data sheets for the correct usage, refer to the SparkFun voltage regulator resource listed in the References section.)

Two photographs show more detail of the setup partially shown in Figures 3 and 4. At left, the Arduino and breadboard hooked up to five servo motors, a 9V battery, and voltage regulator. At right, a closer photograph pf the Arduino breadboard shows its wire hook-ups to the battery and voltage regulator.
Figure 5. The complete five-servo setup with an Arduino, 9V battery and voltage regulator.
copyright
Copyright © 2018 Rachel Sharpe, ITL Program, College of Engineering and Applied Science, University of Colorado Boulder

Now that you have mastered the basics of serial communication, you are ready to “cut the cord” and move to wireless challenges! Two common ways to do this with Arduino are with Bluetooth and XBee components. More advanced maker challenges:

Wrap Up

As a class, have students reflect on what worked and what was challenging about the exercise. Did the code work right away? Did you understand how the ASCII code maps characters to numbers? What were the hang-ups? What were the challenges in combining the sets of working codes? If you had to do this exercise again, what would you change?

Servos come in all sizes, from micro servos to large industrial applications. What other projects can you think of if you had a smaller servo? What about a larger servo?

How would you use this project as a springboard to control something that you care about? If you had a big enough servo, could you control a lock? A door?

Tips

  • Important note: Real-time serial communication is difficult! See the resources for guidance and complete the Create and Control a Popsicle Stick Finger Robot maker challenge before attempting this challenge. At a minimum, students should be familiar with servos and Arduino libraries.
  • Make sure to plug the Arduino into the computer.
  • Students often forget to “attach” the servo in setup.
  • Serial.parseInt is parsing integers. Sometimes students spell this as Serial.parseLnt—using a lower-case l. Remember, we want to parse integers, not dryer lint!
  • After instituting a Serial.parseInt(), if your code keeps returning a zero to the serial monitor, make sure that you have the Serial.available() command appropriately wrapping your code. Make it the first line of code in the “loop” function.
  • If the servo is buzzing and getting hot, try constraining the angles from 10-165° instead of 0-180°.

Contributors

Daniel Godrick

Copyright

© 2017 by Regents of the University of Colorado

Supporting Program

ITL Program, College of Engineering and Applied Science, University of Colorado Boulder

Acknowledgements

This activity was developed by the Integrated Teaching and Learning Program in the College of Engineering and Applied Science at the University of Colorado Boulder.

Special thanks to Jacob Segil and SparkFun Education.

Last modified: June 15, 2018

Comments