Brain DeCoded with CodeX - Mission 1, Objective 6: Plug In…more!

Mission 1 · Objective 6 Lesson Plan

Plug In…more!

Students wire up a servo and a potentiometer, then write a program that turns the servo by turning the knob.

⏱ 20-40 min 🎯 Grades 8-12+ 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

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

ExtensionStudents can add additional 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.
ExtensionTurn the pixels a color in each branch as a visual clue to what the reading is.
SciencePhysical science: have a lesson on electronics and PWM.
MathLook at the chart in the Hints that shows the duty cycle for the servo. Make a graph from the chart.
Lang ArtsHave students write about one of the topics covered in this objective.
🔤

Vocabulary

180 Positional Servo:a motor that can rotate 180 degrees in each direction, but does not spin.
Potentiometer:a knob that turns and varies the voltage; is used to control other peripherals.
🐍

New Python Code

potentiometer = exp.analog_in(exp.PORT2)Set up the potentiometer.
PERIOD = 20
CYCLE = 2**16 // PERIOD
servo = exp.pwm_out(exp.PORT3, frequency=PERIOD)
Set up the 180 servo.
def set_servo(percent):
    return CYCLE * percent // 100
Function that determines the position of the servo, determined by the percent.
reading = potentiometer.valueRead the potentiometer's value.
📐

Standards

Computer Science - Grades 9-12

9-12.AP.16
📝
Preparing for the Lesson

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.

🧑‍🏫
Teacher Notes
  • 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

🗣️Warm-up / Hook

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?"
Teaching tip: Volume knobs, dimmer switches and game controller triggers all land here. Use whichever answer you get to make the input/output split concrete before any wiring starts.
📖Introduce the Objective

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.
Teaching tip: Say up front that the PERIOD and CYCLE lines are setup for PWM and students do not have to understand them today. That keeps the class moving instead of stalling on the math.
💻Student Work Time

Students work through Objective 6 in CodeSpace and build servo_fun.

  1. Connect the servo and the potentiometer, with the divider, using the connecting wires. Check the wiring before running anything.
  2. Follow the CodeTrek steps to set up both peripherals and write the set_servo() function.
  3. Read the potentiometer with potentiometer.value and use the branches to set the servo position.
  4. Run the program, turn the knob, and confirm the servo moves and holds each position.
Teaching tip: Servo not moving and no error on screen? Check the wires first, then check that the divider is in place on the potentiometer. Remind students to pull plugs straight out, the CodeX ports bend easily.
✏️Wrap-up & Review

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?
Teaching tip: Students who finish early can add their own 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.