Quick Look
Grade Level: High school; also scalable for middle school
Time Required: 2 hours (wild guess!)
Subject Areas: Computer Science, Physical Science, Physics, Problem Solving, Science and Technology
Maker Challenge Recap
Computer-controlled servos enable industrial robots to manufacture everything from vehicles to smartphones. For this maker challenge, students work through the engineering design process as they 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.
Maker Materials & Supplies
- computer, Windows or Mac
- Arduino software; download from https://www.arduino.cc/en/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), 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 from SparkFun, or obtain higher-quality servos such as the Hitec HS-422 standard size from SparkFun
- 5V voltage regulator 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
Worksheets and Attachments
Visit [www.teachengineering.org/makerchallenges/view/cub-2216-servo-arm-arduino-serial-communication-protocol] to print or download.Subscribe
Get the inside scoop on all things TeachEngineering such as new site features, curriculum updates, video releases, and more by signing up for our newsletter!More Curriculum Like This
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...
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
- Refer to the Engineering Design Process hub on TeachEngineering to guide your students through the challenge. Utilize the Engineering Design Process Notebook for design process documentation.
- In the SparkFun SIK Guide, see Circuit 8 for the servo: https://cdn.sparkfun.com/datasheets/Kits/RedBoard_SIK_3.2.pdf
- To get connected with Arduino: Introduction to Arduino: Getting Connected and Blinking LEDs maker challenge
- To use sensors with Arduino: Build Your Own Night-Light with Arduino maker challenge
- Servo Library reference by Arduino: https://www.arduino.cc/en/Reference/Servo
- Hobby Servo tutorial (more detailed) by SparkFun: https://learn.sparkfun.com/tutorials/hobby-servo-tutorial?_ga=2.84742297.1452592132.1497891440-1988227511.1497891440
- ASCII Chart reference by Arduino: https://www.arduino.cc/en/Reference/ASCIIchart
- Serial.setTimeout() reference by Arduino: https://www.arduino.cc/en/Serial/SetTimeout
- Serial.available() reference by Arduino: https://www.arduino.cc/en/Serial/Available
- Constrain Command reference by Arduino: https://www.arduino.cc/reference/en/language/functions/math/constrain/
- Map Command reference by Arduino: https://www.arduino.cc/reference/en/language/functions/math/map/
- Serial Communication reference by Arduino: https://www.arduino.cc/en/Reference/Serial
- Serial Communication tutorial by SparkFun: https://learn.sparkfun.com/tutorials/serial-communication
- Voltage Regulator Resource at SparkFun https://www.sparkfun.com/products/107
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.
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:
- Wire a servo and test it using Circuit 8 in the SIK Guide.
- 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.
- 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.
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?
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.)
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°.
Copyright
© 2017 by Regents of the University of ColoradoContributors
Daniel GodrickSupporting Program
ITL Program, College of Engineering and Applied Science, University of Colorado BoulderAcknowledgements
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: May 24, 2022
User Comments & Tips