AP CSP CodeX - Mission 12: Night Light

Mission 12 Lesson Plan

Mission 12: Night Light

Students read the CodeX light sensor and use it to control pixel brightness, building a smart night light.

⏰ 45 min 🎯 Grades 9-12 💻 CodeSpace 🎮 CodeX 🐍 Python
View Lesson Outline
📋

Overview

During this mission, students build a smart night light. They read the CodeX light sensor, decide when it is dark, and convert the reading into a brightness level for the pixels. They also review the difference between analog and digital from Mission 5. After Objective 5, students define a constant and create a function with a parameter to convert the light reading into a brightness level.

🎯 Mission Goal: Students will use the light sensor to make a smart night light.

🎯

Learning Targets

  • I can explain the differences between analog and digital I/O.
  • I can use code to read the light sensor.
  • I can convert a light sensor reading to a brightness level.
  • I can use the light sensor reading to control the pixel brightness.
💡

Key Concepts

  • The photocell helps convert light level into an electrical voltage level.
  • Analog means infinite variation from dark to light, cold to hot, and so on. The CodeX's analog to digital converter (ADC) gives a digital approximation of the photocell's analog reading.
  • I/O pins can be read with read_digital() and read_analog() functions.
✅

Assessment Opportunities

☑️

Success Criteria

  • Read data from the light sensor
  • Use the data to control the pixel light
  • Convert data to a brightness level
  • Use the converted data to set the pixels' brightness level
  • Create and call a function with a parameter
📁

Digital Resources

🧰

Classroom Materials

  • ▸CodeX
  • ▸USB-C cable
  • ▸Computer
🌍

Real-World Applications

💡Common uses for a light sensor:
  • Your phone has a tiny ambient light sensor near the front camera
  • Automatic street lights turn on at dusk and off at sunrise
  • Solar panels use light sensors to find where the sun is brightest
  • Pulse oximeters (pulse detection) use optical and light sensors to check your heart rate and blood oxygen
🚀

Extensions & Cross-Curricular

ExtensionAdd a "kill switch" so a button press ends the program.
ExtensionShow an image on the display, such as a sun or a moon, based on the light level. Students can get creative with how they use images in the program.
ExtensionChange the pixel color as well as the brightness, such as a warm color when it is very dark.
MathConvert the raw light reading into a percentage of the maximum. Work out what percent your classroom reading is.
ScienceResearch how a photocell changes its electrical resistance as more light falls on it.
Lang ArtsWrite a short persuasive paragraph for your school board on how daylight harvesting could save energy in your school.
🔤

Vocabulary

▾
Light sensor - A sensitive electronic device that measures the amount of light falling on it.
Analog - Infinite variation in something, like hot to cold or light to dark; smooth and continuous signals that represent a quantity, like sound waves.
Digital - A numerical representation of an analog signal, represented in increments.
ADC - Analog to digital conversion.
🐍

New Python Code

▾
value = light.read()Read from the light sensor.
pixels.fill(WHITE) # turns all pixels on pixels.fill(BLACK) # turns all pixels offSet all pixels the same color using a single function.
pixels.fill(WHITE, brightness=20) pixels.fill(WHITE, brightness=level)Adjust the brightness of the pixels using the keyword parameter.
📐

Standards

▾

CSTA Standards - Grades 9-10

3A-CS-01 3A-CS-03 3A-DA-11 3A-DA-12 3A-AP-13 3A-AP-16 3A-AP-17 3A-AP-18 3A-AP-21

CSTA Standards - Grades 11-12

3B-CS-02 3B-DA-06 3B-AP-10 3B-AP-14 3B-AP-16 3B-AP-22 3B-AP-23

AP CSP Framework

DAT-2.A - Describe what information can be extracted from data.
Computational Thinking Practice 3.A - Generalize data sources through variables.
Computational Thinking Practice 4.C - Identify and correct errors in algorithms and programs, including error discovery through testing.
📝
Preparing for the Lesson
  • The assignment is best completed digitally. Prepare the assignment to give through your LMS.
  • Have the Analog and Digital slides from Unit 1 Mission 5 ready to review after Objective 1.
  • If you have a word wall, or another form of vocabulary presentation, prepare the new terms.

🧑‍🏫
Teacher Notes
  • Encourage the students to do as much code on their own as they can, and use CodeTrek to check their work, or as a hint when needed.
  • The assignment adds an extra step to complete after Objective 5, before turning in the program.
  • If you have time at the end of the lesson, use the Mission 12 Kahoot! Review.
  • Refer to the lesson plans in the Python with CodeX Mission Pack Curriculum for additional challenges, cross-curricular suggestions, and additional information.
🗺️

Lesson Outline

This outline gives the narration for one way to present the lesson.

🗣️Warm-up

In Mission 5 you learned about analog and digital sounds. What do you remember about analog and digital? Besides sounds, what else can be analog? Hold a short discussion. In today's lesson, students will use analog light and convert it to digital information, then use it in a program to control the CodeX pixels.

💻Activity - Mission 12

Randomly group students into pairs for pair programming (or they can work individually).

Students log in to one computer. Two computers can be used if they want to have the assignment open on one computer and CodeSpace on the other computer.

Students go to make.firialabs.com and should be at the beginning of Mission 12.

Teaching tip - Objective 1: Students start a new file, and then locate the light sensor. They need to close the Objective Panel to see the CodeX in the simulator (3D view).
Teaching tip - After Objective 1: Use the Analog and Digital slides from Unit 1 Mission 5 to review analog and digital. Students answer questions on their assignment.
Teaching tip - Objective 2: Students will use the light sensor in code. They will need to experiment a little and come up with their own value for darkness. The suggested value is 2000, which is pretty dark. The number is changed in the next objective.
Teaching tip - Objective 3: Students will learn a new way to turn on all the pixels. They can compare this single command to using a list.
Teaching tip - Objective 4: The code will require some math here. Students should use the hints and CodeTrek to guide them. They will be adding the math inside the if statement. This is a logical place to create a function, which isn't in CodeTrek but can be encouraged.
Teaching tip - Objective 5: Students will modify the math to include brightness.
🧩Activity - After Objective 5
Teaching tip - After Objective 5: The students define a constant for the max percent and create a function with a parameter for converting the light sensor reading into a brightness level. Instructions are on the assignment document.
Teaching tip - Challenge: If students have time, have them try the challenge. This gives them an opportunity to apply their knowledge in a new situation. A code solution for the challenge is available, but hopefully students will be creative in how they incorporate images in the program.

✅ The assignment is complete and ready to turn in.

🧑‍🤝‍🧑Wrap-Up

Discussion: This project introduced students to an area with lots of potential for improving the world! Light sensors and LED lights controlled with code can reduce energy consumed and make lighting more awesome!

This code can enable and enhance many real-world applications. Have students brainstorm their own applications of using a light sensor. Here are some ideas:

  • Outdoor lighting: street lights, parking lots, home lighting
  • Stadium lights
  • Even controlling the light color so it looks better on camera
  • Indoor lighting: sensing daylight from windows and skylights is called Daylight Harvesting, and it saves energy

That is exactly what your last NightLight code was doing!

Optional. Use a formative assessment for the wrap-up.

Formative assessment:

  • Daily reflection journal
  • Mission 12 Kahoot! Review (in class or individual)
  • Exit ticket on analog and digital devices or measurements