Lift-Off with CodeX - Mission 3: Conserve Energy!
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.
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.
Digital Resources
Classroom Materials
- ▸Laptop/computer with the Chrome browser
- ▸CodeX and connecting cable
- ▸White LED
- ▸Potentiometer and divider
- ▸Motion sensor
Real-World Applications
Extensions & Cross-Curricular
🔤
Vocabulary
▾
🐍
New Python Code
▾
📐
Standards
▾
CSTA Standards - Grades 6-8
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
- 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.
- 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
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?"
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.valuereturns True or False, and the potentiometer is analog, sopotentiometer.valuereturns 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.
Students work through the mission objectives in CodeSpace, taking notes and answering questions in their assignment doc as they go.
-
Build ConserveEnergy, setting up the LED with
exp.pwm_out(exp.PORT0). -
Add the potentiometer with
exp.analog_in(exp.PORT1)and driveled.duty_cyclefrom the knob position. -
Read the motion sensor and light the LED when
motion_sensor.valueis True. -
Use
time.ticks()to hold the LED on for a set stretch after the last motion, then shut it off. - Complete both Checks for Understanding in CodeSpace.
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?"
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.