Lift-Off with CodeX - Mission 3: Conserve Energy!

Mission 3 Lesson Plan

Conserve Energy

Students wire up three peripherals at once, a motion sensor, a potentiometer, and an LED, then write code that turns the light on when it senses movement and holds it on with a timer.

⏱ 45-60 min 🎯 Grades 6-12 💻 CodeSpace 📱 CodeX 🔌 Peripherals Kit 🐍 Python
View Lesson Outline
📋

Overview

It's a long way to Mars. Maybe we should plan. Mars is a hike! Power is not something you can top off along the way, so this mission puts students in charge of a lighting system that only runs when it is actually needed.

Students work with three different peripherals to detect motion, simulate a light source, and control the activation and brightness of the light after motion has been detected. Along the way the LED gets promoted from a simple on/off device to an analog one using PWM, and students see the difference between reading a digital sensor and reading an analog one.

🎯 Mission Goal: Students write code to control the lighting system by detecting motion.

🎯

Learning Targets

  • I can use the potentiometer to control the level of brightness on an LED.
  • I can use the motion sensor to detect movement.
  • I can use a timer to keep the LED on after motion has been detected.
💡

Key Concepts

  • Reading analog input (the potentiometer) is different from reading digital input (the motion sensor). One returns a range of numbers, the other returns True or False.
  • The LED is first set up as a digital output peripheral, but it gets changed to analog using PWM, pulse-width modulation.
  • PWM requires a duty cycle and a frequency.

Assessment Opportunities

  • Check for Understanding in CodeSpace (2)
  • Identify digital and analog inputs around them
  • Mission 3 Assignment
  • Exit ticket, explain PWM
  • Submit and/or check the ConserveEnergy program
  • Journal entry on their learning experience
☑️

Success Criteria

  • Set up the potentiometer as an analog input and the motion sensor as a digital input.
  • Control the brightness of an LED with the potentiometer.
  • Keep the LED on when motion is detected using a timing technique.
🧰

Classroom Materials

  • Laptop/computer with the Chrome browser
  • CodeX and connecting cable
  • White LED
  • Potentiometer and divider
  • Motion sensor
LED peripheral board for the CodeX, showing the latching cable connector and the G, V, S labels Potentiometer peripheral board for the CodeX with a knob and the G, V, S labels Green divider board labeled CodeX and Sensor with numbered ports 1 and 2, used to split one CodeX port between two peripherals Motion sensor peripheral board for the CodeX with a white domed PIR lens
🌍

Real-World Applications

💡Lights that switch on or off by themselves. Walk past the refrigerator case at the grocery store and the case lights up for you.
🏠Motion sensors and built-in timers show up all over the house, from porch lights to thermostats to air conditioning that backs off when nobody is home.
🌎Conserving energy is a real engineering constraint. Different energy sources have real trade-offs, and only some of them work in outer space.
🚀

Extensions & Cross-Curricular

ChallengeUse the CodeX to add an indicator for energy-saving mode and energy-wasting mode. Add sounds, change pixel colors, or display an image or color on the screen.
ChallengeThe CodeX also has a built-in light sensor. Use another motion detector to control that sensor.
Cross CurrHave students plan a road trip. Use a map for geography, calculate distance using the map scale, and budget gas money, mileage, and stops. Then have them find the distance to Mars and rework the plan. If conserving energy does not come up on its own, ask a question that forces them to consider it.
ScienceExplore distance, rate and time, conserving energy, artificial vs natural light, and how volts and the divider work.
DiscussionLead a discussion or lesson on energy conservation. What are the different energy sources, what are the pros and cons of each, and which ones might work in outer space?
🔤

Vocabulary

Analog - A peripheral with a range of integer values, from 0 (off) to 2^16 - 1 (full power).
Digital - A binary peripheral with two states, True or False.
ADC - Analog to digital converter. It turns an analog measurement into a finite digital value. The CodeX is a 16-bit microcontroller, so those values run from 0 to 2^16 - 1 (65,535).
Pulse-Width Modulation - An analog measurement where on/off pulses are sent at a constant rate, set by the duty cycle and the frequency (or analog period).
🐍

New Python Code

exp.pwm_out(exp.PORT0)Used to set up a peripheral with PWM. It requires a duty cycle and a frequency.
exp.analog_in(exp.PORT1)Used to set up an analog input peripheral, such as the potentiometer.
led.duty_cycleDetermines the power going to the LED. A higher integer means a brighter light.
sleep_ms()Delays program execution in milliseconds.
time.ticks()Returns the current clock time, the elapsed time since the last reboot.
motion_sensor.valueReturns the motion sensor value: True (motion detected) or False (no motion).
potentiometer.valueReturns an integer for the position of the potentiometer knob.
📐

Standards

CSTA Standards - Grades 6-8

2-CS-02 2-CS-03 2-AP-10 2-AP-11 2-AP-12 2-AP-13 2-AP-14 2-AP-19

CSTA Standards - Grades 9-10

3A-CS-02 3A-AP-13 3A-AP-15 3A-AP-16 3A-AP-17 3A-AP-18 3A-IC-26

CSTA Standards - Grades 11-12

3B-CS-02 3B-AP-10 3B-AP-14 3B-AP-15 3B-AP-16
📝
Preparing for the Lesson
  • Students need a computer or laptop with the Chrome web browser.
  • Make sure students can successfully log in to make.firialabs.com.
  • Set up the LED, potentiometer with its divider, and motion sensor yourself once before class. Three peripherals at the same time is the part that slows students down.
  • Have the "Flowcharts" slide deck ready if you plan to use it.

🧑‍🏫
Teacher Notes
  • One of the more challenging concepts in this project is the combined use of digital and analog sensors. It helps to have students talk through the difference between the two types of devices and review the data each one returns before they start coding.
  • The "Flowcharts" slide deck teaches the basics of flowchart symbols and pulls its examples from Mission 2 and Mission 3.
  • Extensions and cross-curricular projects are included to enhance the concepts in the mission. Use the extensions to stretch students' programming experience. A remix is not explicitly planned here, but you can add one as an option for extra learning. A planned remix follows Mission 3.
🗺️

Lesson Outline

🗣️Warm-up / Hook

Students access prior knowledge by answering the pre-mission questions in the assignment doc.

  • Ask: "Where have you seen a light turn itself on when you walked up to it? How do you think it knew you were there?"
  • Ask: "A dimmer switch and a regular light switch both control a light. What is different about the information each one gives?"
Teaching tip: Frame the mission with the trip to Mars. There are no charging stations on the way, so every watt the lighting system wastes is a watt the crew does not get back.
📖Introduce the Mission

Front-load the analog vs digital distinction and the new setup calls before students start typing.

  • Have students sort devices around the room into digital (two states) and analog (a range of values). This doubles as an assessment.
  • Show that the motion sensor is digital, so motion_sensor.value returns True or False, and the potentiometer is analog, so potentiometer.value returns an integer.
  • Explain that the LED starts as a digital output, then gets switched to analog with exp.pwm_out(), which needs a duty cycle and a frequency.
  • Point out the divider, and why the potentiometer needs it.
Teaching tip: A dimmer switch is the cleanest analogy for PWM. The light looks steady, but it is actually flicking on and off very fast, and the duty cycle is how much of that time it spends on.
💻Coding Time

Students work through the mission objectives in CodeSpace, taking notes and answering questions in their assignment doc as they go.

  1. Build ConserveEnergy, setting up the LED with exp.pwm_out(exp.PORT0).
  2. Add the potentiometer with exp.analog_in(exp.PORT1) and drive led.duty_cycle from the knob position.
  3. Read the motion sensor and light the LED when motion_sensor.value is True.
  4. Use time.ticks() to hold the LED on for a set stretch after the last motion, then shut it off.
  5. Complete both Checks for Understanding in CodeSpace.
Teaching tip: If the LED will not respond, check which port each peripheral is plugged into before debugging the code. Three peripherals means three chances to swap two ports.
Teaching tip: Fast finishers can start the challenges, adding an energy-saving vs energy-wasting indicator with sounds, pixel colors, or a screen image.
🧑‍🤝‍🧑Class Debrief

Bring the class back together to lock in analog vs digital and the timing technique.

  • Ask: "What does the potentiometer return, and what does the motion sensor return? Why do they need different setup code?"
  • Ask: "How does the timer keep the light on after you stop moving, and why is that better than just leaving it on?"
  • Ask: "What would you change if this system had to run for nine months on a spacecraft?"
Teaching tip: Use the exit ticket on PWM here. If students can explain duty cycle in their own words, they have it.
✏️Wrap-up & Review

Students answer the reflection question in the assignment doc, add a journal entry about their learning experience, and submit.

Use the Mission 3 Review Questions or the Mission 3 Review Kahoot! through whichever method you prefer, class discussion, Kahoot!, or an LMS quiz.

Teaching tip: A remix and assessment follows Mission 3, so have students keep their ConserveEnergy code handy. They will build on it.