SPIKE™ Prime with Python

Making Moves with Motors

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

45 min
Beginner
Years 7-9 OR Key Stage 3

Questions to investigate

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

Prepare

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 GETTING STARTED section of the Knowledge Base in the SPIKE App. Here students can access 4. Controlling Motors. This Getting Started lesson provides students with an early experience in building and coding with SPIKE Prime.
To investigate the programming of motors in more depth, 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 A of the hub.

Run Single Motor for Degrees
Running a motor for degrees allows you to set how far the motor will run.

Brainstorm with students on ways 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
  • Turn motor on
  • Move clockwise for 2 rotations

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 4: Controlling the Motors section).

import motor
from hub import port
import runloop

async def main():
    # Run a motor on port A for 360 degrees at 720 degrees per second.
    await motor.run_for_degrees(port.A, 360, 720)
    
runloop.run(main())

Troubleshooting Tip: Ensure students have the motor plugged into the A port or change port A to the correct port.

Discuss what the 360 and 720 represent in the parenthesis in the last line of code. The code comment provides a hint.

  • Prompt students to put different numbers in for these two 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.

Run Multiple Motors

Prompt students to add an additional medium motor 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 about what they need to add to their program to allow for two motors to run. The task is to make the two motors run (one in port A and the second 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 motors run at the same time.

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

import motor
from hub import port
import runloop

async def main():
    # Run a motor on port A for 360 degrees at 720 degrees per second.
    await motor.run_for_degrees(port.A, 360, 720)
    # Run a motor on port B for 360 degrees at 720 degrees per second.
    await motor.run_for_degrees(port.B, 360, 720)
    
runloop.run(main())

3. Explain

Discuss with students how the program worked. Did students notice that the motors turned in the opposite direction? Did students notice that the first motor ran and then the second ran? This is because we added an await on an awaitable.

Ask students to modify the code by removing the await that is before each motor.run_for_degrees(port.B, 360, 720) and run the program again. Did students notice that the motors now ran at the same time? That’s how an awaitable works.
You can find more about awaitables in the Knowledge Base in the Getting Started, 4. Controlling Motors.

What different combinations of the degrees and speed did students choose?

Ask students questions like:
• Explain the difference between importing the library for only one motor and two motors.
• What parameters 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?

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

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?

Self-Assessment:
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 students 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

CSTA
2-CS-02
Design projects that combine hardware and software components to collect and exchange data.

2-AP-10
Use flowcharts and/or pseudocode to address complex problems as algorithms

2-AP-16
Incorporate existing code, media, and libraries into original programs, and give attribution.

2-AP-17
Systematically test and refine programs using a range of test cases.

2-AP-19
Document programs in order to make them easier to follow, test, and debug.

CCSS ELA
SL.8.1
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

SL.8.4
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

RST.6-8.3
Follow precisely a multistep procedure when carrying out experiments, taking measurements, or performing technical tasks

L.8.6
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

Vocabulary
Pseudocode
Initialize
Motor
Speed