SPIKE™ Prime with Python

Making Moves with Motors

Students will work with individual motors to create a dance party.

45 min
Years 7-9 OR Key Stage 3

Questions to investigate

How can engineers and programmers work together to make something move?


Ensure SPIKE Prime hubs are charged, especially if connecting through Bluetooth.

1. Engage

Engage students in thinking about how motors move. Have students stand to act as motors. Have students create a line and ask them to walk forward 5 steps. Have them try this a couple of times. Ask students why everyone does not move the same distance.

  • Ask students to raise their right arm when you say “Raise”. Wait a couple of seconds and say “Raise.” Why didn’t everyone move at the exact same time?
  • Have them do this a couple of times. Ask students why everyone does not move the same way.
  • Ask students to clap when you say “clap.” Wait a couple of seconds and say clap. Have them try this a couple of times. Ask students why everyone does not clap at the same time.
  • Discuss how a group of robots programmed with the same code would react given the same command simultaneously.
  • Discuss how a robot moves and why movement for a given command would be the same every time. Ask students when this would be useful or helpful.

2. Explore

Students will explore working with motors and ways to code movement.

Direct students to the START section in the SPIKE App. Here students can access 2. Motors and Sensors. This Getting Started lesson provides students with an early experience in building and coding with SPIKE Prime.

Use this lesson to guide students in building the motor modules. Students should complete the building instructions through step 19 only. You will not need to complete the rest of this lesson. Direct students to leave the Motors and Sensors lesson.

Open a new project in the Python programming canvas. Ask students to erase any code that is already in the programming area. Students should connect their hub.

Plug the large motor into port C of the hub.

Before students can use motors, the motors must be initialized. That means you are naming the motors in a way that clarifies for you and the program which motor(s) you want to use and where they are located in the hub. Without this step, the motor will not work.

For example,
A motor in Port A would be initialized as follows:

motor = Motor('A')

Run Single Motor for Time
Running a motor for time allows you to set how long the motor will run.

Brainstorm with students how to program the motor to move. Discuss what information the software needs to run the hardware correctly.

Write a pseudocode program for making the motor run for 2 seconds with students. Pseudocode is writing in words what you want the program to do.

An example could be:

  • Import motor
  • Initialize motor
  • Turn motor on
  • Move clockwise for time 2 seconds

Note: The code written in steps does not need to match exactly what will be put in the program. Writing pseudocode helps students determine what is needed.

Provide students with the sample code below to move the motor. Have students type the code into the programming canvas. (Students can copy and paste the sample code from the Knowledge Base Getting Started Part 2: Controlling the Motors section under Run Single Motor for Time).

from spike import Motor

# Initialize the motor
motor = Motor('C')

# Rotate clockwise for 2 seconds at 75% speed
motor.run_for_seconds(2.0, 75)

Troubleshooting Tip: Ensure students have the motor plugged into the C port or change the Motor(‘C’) to the correct port.

Discuss what the 2.0 and 75 represent in the last line of code. The code comment provides a hint.

  • Prompt students to put different numbers in for these to values to investigate how it changes how the motor moves.
  • Allow students to share the new examples they create and discuss ways to program the motor.

Remove the large motor from the hub.

Run Multiple Motors

Prompt students to add the two medium motors to their hub. Having multiple motors attached will allow students to run more than one motor at a time. Ask students to consider what type of robots they could create that would need more than one motor. What function could multiple motors serve?

Brainstorm with students what they need to add to their program to allow for two motors to run. The task is to make one motor run (in port C) and then the second motor run (in port B).

Remind students that they can plug the motors into any port. However, the program needs to match the chosen port, which might not match the sample program.

Ask students to add to the pseudocode used previously. Add wording to make the second motor turn on after the first motor.

Have students use the pseudocode as inspiration to help write their new program that will run both motors.

from spike import Motor

# Initialize the motor
Motor_c = Motor('C')
Motor_b = Motor ('B')

# Rotate clockwise for 2 seconds at 75% speed
Motor_c.run_for_seconds(2.0, 75)
Motor_b.run_for_seconds(2.0, 75)

3. Explain

Discuss with students how the program worked. What different combinations of the variables time and speed did students choose?

Ask students questions like:

  • Explain the difference between importing the library for the medium motor and the large motor.
  • What is required when initializing more than one motor? How do you differentiate between them?
  • What variables for the motor can you change to make it move in different ways?
  • How did the two motors move? Did they move at the same time? Why or why not?
  • How can you troubleshoot errors that occur when working with motors?

Note: Explain to students that the number specified for seconds is a “float.” A float type parameter can be a decimal number.

Explain to students that the number specified for speed is an integer type input. An integer type input can be a positive or negative whole number ranging from -100% to 100%.

4. Elaborate

Dance Party

Challenge students to create a short program to run the 2 medium motors to create a dance party. Play some music to get kids creative.

For example, students could have both motors moving in one direction or have each motor moving in opposite directions.

  • Allow students to add additional bricks to their motors to create something new.
  • Prompt students to add sounds and hub lights to their program to complete the dance party.
  • Remind them to think about which libraries need to be imported to support their programs and where to find help in the Knowledge Base as needed.

Students should share their dancing robots and explain their programs.

Disconnect the motors and return all parts to the correct bin location.

5. Evaluate

Teacher Observation:
Discuss the program with students.

Ask students questions like:

  • What happened on your hub when you ran the program?
  • What libraries did you use to run this program?
  • How can engineers and computer programmers work together to create a way to communicate ideas to others?
  • How can engineers and programmers work together to make something move?

Have students answer the following in their journals:

  • How do you determine which motor you are programming to move if multiple motors are attached to the hub?
  • What characteristics of a good teammate did I display today?
  • Ask students to rate themselves on a scale of 1-3, on their time management today.
  • Ask student to rate themselves on a scale of 1-3, on their materials (parts) management today.

Teacher Support

Students will:

  • Program motors to turn individually using parameters of time and speed
  • Create a robot dance party
  • SPIKE Prime sets ready for student use.
  • Devices with the SPIKE App installed.
  • Student journals

Design projects that combine hardware and software components to collect and exchange data.

Use flowcharts and/or pseudocode to address complex problems as algorithms

Incorporate existing code, media, and libraries into original programs, and give attribution.

Systematically test and refine programs using a range of test cases.

Document programs in order to make them easier to follow, test, and debug.

Engage effectively in a range of collaborative discussions (one-on-one, in groups, and teacher-led) with diverse partners on grade 8 topics, texts, and issues, building on others' ideas and expressing their own clearly

Present claims and findings, emphasizing salient points in a focused, coherent manner with relevant evidence, sound valid reasoning, and well-chosen details; use appropriate eye contact, adequate volume, and clear pronunciation

Follow precisely a multistep procedure when carrying out experiments, taking measurements, or performing technical tasks

Acquire and use accurately grade-appropriate general academic and domain-specific words and phrases; gather vocabulary knowledge when considering a word or phrase important to comprehension or expression