Brain DeCoded with CodeX - Mission 1, Objective 6: Plug In…more!
Plug In…more!
Students wire up a servo and a potentiometer, then write a program that turns the servo by turning the knob.
Overview
Objective 5 added one output peripheral. This objective adds two peripherals at once, and they work together. The potentiometer is an input, a knob that varies the voltage going into the CodeX. The 180 degree positional servo is an output, a motor that turns to a position and holds it. Students write servo_fun so that turning the knob moves the servo.
The servo is driven with PWM, so the code sets a period and a cycle and converts a percent into a servo position. Students do not need to understand PWM to finish the program, but the setup code is a good hook if you or your class want to dig into the electronics. The potentiometer also needs the divider board to reach its full range of voltage, which is the other new piece of hardware here.
🎯 Project Goal: Students will write a Python program to control a servo using the potentiometer.
Learning Targets
- I can connect a servo to the CodeX.
- I can connect a potentiometer to the CodeX.
- I can control the servo by turning the potentiometer knob.
Key Concepts
- The servo and potentiometer are connected to the CodeX using connecting wires.
- The potentiometer needs the divider board to reach its full range of voltage.
- Attaching a horn to the servo helps you see its position and which way it is turning.
Assessment Opportunities
- Complete the program servo_fun.
- Exit ticket: explain the purpose of the divider board.
Success Criteria
- Complete the CodeTrek steps
- Servo and potentiometer connected correctly to the CodeX
- Program runs correctly without errors
Digital Resources
- ▸CodeX Peripherals Guide, wiring reference for the servo, the potentiometer and the divider
- ▸Project the Hints from CodeSpace while students wire up. The duty cycle chart for the servo is there, and it is the source for the math extension
Classroom Materials
- ▸180 degree positional servo, with a horn attached
- ▸Potentiometer
- ▸Divider board
- ▸Connecting wires
- ▸CodeX and USB cable
- ▸Laptop/computer with Chrome browser
Extensions & Cross-Curricular
elif statements to the code with their own potentiometer values and percents. Make sure to stay with the order of the statement, going from smallest value to largest.
🔤
Vocabulary
▾
🐍
New Python Code
▾
CYCLE = 2**16 // PERIOD
servo = exp.pwm_out(exp.PORT3, frequency=PERIOD)Set up the 180 servo.
return CYCLE * percent // 100Function that determines the position of the servo, determined by the percent.
📐
Standards
▾
Computer Science - Grades 9-12
See the CodeX Peripherals Guide for wiring help before you hand out the servos.
- Wire up a servo and a potentiometer yourself and run servo_fun, so you know what a working setup looks like and can spot a reversed wire quickly.
- Attach the horns to the servos ahead of time, or plan a minute for students to do it. Without a horn it is hard to tell the servo is moving at all.
- Count out the servos, potentiometers, dividers and wire sets, and decide whether students work solo or in pairs.
- Read through the Hints. The duty cycle chart and the PWM explanation are there if you want to go deeper.
- The 180 servo uses PWM, and there is a lot of code and information about cycles and frequency. Students do not need to fully understand this to complete the goal or the mission pack. It is an opportunity to dig deeper into electronics if you or your students are interested.
- Make sure the peripherals are connected to the CodeX correctly. If the wires are reversed, they will not work properly.
- Students need to be gentle with the connections on the CodeX. The ports can bend.
- The potentiometer needs the divider board to reach its full range of voltage. Without it, students will see a smaller range of readings and the servo will not move through its full travel.
- Extensions and cross-curricular projects are included to enhance the concepts in the objective. You can use the extensions to extend students' programming experience.
Lesson Outline
Hold up a servo with a horn on it and a potentiometer, and open with two questions.
- Ask: "Last objective the peripheral only put light out. Which one of these two do you think sends information in to the CodeX?"
- Ask: "Where have you turned a knob to control something, and what was it controlling?"
Introduce the two peripherals and the divider, then show how they connect.
- Potentiometer: an input. A knob that varies the voltage going into the CodeX.
- 180 positional servo: an output. A motor that turns 180 degrees in each direction and holds a position, it does not spin freely.
- Divider: the potentiometer needs it to reach its full range of voltage.
- Connecting: both peripherals use connecting wires. Reversed wires are the number one reason nothing works.
-
The code: the program reads the potentiometer value and calls
set_servo()with a percent to position the servo.
Students work through Objective 6 in CodeSpace and build servo_fun.
- Connect the servo and the potentiometer, with the divider, using the connecting wires. Check the wiring before running anything.
- Follow the CodeTrek steps to set up both peripherals and write the
set_servo()function. - Read the potentiometer with
potentiometer.valueand use the branches to set the servo position. - Run the program, turn the knob, and confirm the servo moves and holds each position.
Collect the peripherals, then close with the exit ticket.
- Exit ticket: explain the purpose of the divider board.
- Quick share: which peripheral is the input and which is the output, and how do you know?
elif branches with new values and percents, smallest value to largest, or light a pixel color in each branch so the reading is visible without watching the servo.