-
Type “myservo.attach(9);” between the curly brackets ({ and }) of the setup function.
-
Observe the Arduino board and notice the tilde symbol (~) before pin 9 and other pins.
STEM Connections
There are two types of pins in the digital row of the Arduino Uno Board. Pins with a tilde symbol (~) can send signals through electric current using a technique known as pulse width modulation, or PWM. The other pins (1, 2, 4, 7, 8, 12 & 13) cannot.
By typing “myservo.attach(9);” you are telling the Arduino board to send a signal to the Servo from pin 9. You are using pin 9 because it is a pin that can send signals through electric current. It is necessary to use a PWM pin, like pin 9, to control Servos.
Be sure that you are typing this line of code inside of the curly brackets for the setup function. Remember, code that is written inside the void setup curly brackets runs just once at the beginning of the program. So, you are adding the “attach” function to the setup section of the code because pin 9 only needs to be set up once.
Notice the ''//'' before the gray text. As we saw in the LED Blink Activity, these are comments meant for humans and do not change the program.
At the Thinkabit lab, we explain how pins work and ensure that each student has an opportunity to both write code and check the code.
-
Remind students that the Servo is controlled by a certain shaped signal but only certain pins on the Arduino are physically capable of generating the PWM signal.
-
Ask students to identify which numbers on the digital side can generate the signal. Hint: these all have a squiggly line next to them.
-
Students should answer: 3, 5, 6, 9, 10, 11
-
Explain that the squiggly line (a tilde) indicates that those pins can create a PWM signal.
-
Explain that in the void setup, one of the pins (listed above) will be assigned to control the Servo. We happen to choose 9 in this activity. Since there are 6 PWM pins, we could potentially control up to 6 Servos at the same time using one Arduino Uno.
-
Explain the following analogy. We can think of the PWM pins as bilingual. All the digital pins speak the language called digital, where we have 2 choices: on or off, yes or no, 0 or 1. The PWM pins also speak a language called analog, where there are many choices or a range of values. The digital signal is like a light switch; the light is either on or off. Analog is like a light dimmer. The lights can be very bright, not as bright, just a little light, or off.
-
Before transitioning to the next activity, ask students to switch roles, so that each student can write code and check the code.
If you are using the Thinkabit Lab Notebook:
Have students fill in the next blank on page 15:
myservo.attach(9); attach myservo to pin 9
Alternatively, you can write the entire code on the computer and fill in the notebook at the end.
‘attach’ didn’t turn orange
-
Check the spelling of the word “attach” and make sure that no letters are capitalized.
-
Make sure there is a period between “myservo" and "attach”.
-
Be sure there is a semicolon after "myservo.attach(9)".
-
If it changed colors to a different color, the Arduino software may have been updated. As long as it is colored, the code should still work properly.