Lesson 7: Analog Input

Overview

In this lesson, students explore how the three analog sensors (sound, light, and temperature) can be used to write programs that respond to changes in the environment. The use of these sensors marks a transition in terms of how users interact with a program. By using sensors as an input, the user of an app doesn't have to directly interact with it at all, or may interact without actually realizing they are doing so.

Purpose

This lesson builds on the previous by introducing analog sensors as a new form of input. These sensors all perform analog-to-digital conversion, allowing programs to sense things as represented by a 10 bit number (0-1023).

Agenda

Warm Up (10 min)

Activity (40 min)

Wrap Up (5 min)

View on Code Studio

Objectives

Students will be able to:

  • Develop programs that respond to analog input
  • Scale a range of numbers to meet a specific need
  • Represent a sensor value in a variety of ways

Preparation

Links

Heads Up! Please make a copy of any documents you plan to share with students.

For the Students

Vocabulary

  • Analog - Any continuously changing signal that is not restricted to finite set of values. For example, the wave forms of spoken words are an analog signal.
  • Digital - Data or signals represented by a finite number of values. Analog signals (which can have infinite values) must be converted to digital in order to be computed with.

Introduced Code

Teaching Guide

Warm Up (10 min)

Analog and Digital

Discussion Goal

The goal here is to get students thinking about how an natural (or analog) value can be converted into a computable format. Encourage students to consider what the upper and lower bounds of each value might be (for example, is there a minimum or maximum amount of sound?).

Think, Pair, Share: How would you quantify each of your five senses (touch, taste, sight, smell, and hearing). Think back to the previous unit and the various ways you came up with to encode data for a computer.

Video: Today we're going to write programs that have "senses" of their own! Show Difference between Analog and Digital Signals - Video

Activity (40 min)

Analog Inputs

Distribute: Pass out Circuit Playgrounds.

Transition: Send students to Code Studio. Let them know that today they will be experimenting with some sensors that detect analog signals and convert them to digital values that can be used by the computer.

Wrap Up (5 min)

Sensors All Around

Journal: Considering all of the computing devices that you interact with on a regular basis, identify as many potential analog sensors as you can. Where do your computing devices take a continuously changing signal and convert it into digital data?

View on Code Studio

Sensor Experiment

Run the program to the right and experiment with your board. Try interacting with your board in many different ways to figure out what each sensor might be. As you experiment with each sensor, discuss with a neighbor:

  • What does this sensor measure?
  • What is its maximum value?
  • What is its minimum value?
View on Code Studio

Student Instructions

Reading the Sound Sensor

The soundSensor.value block allows you to get the current sound sensor reading

Do This

In this app we've already added a text label with the id "sound_value". Using the soundSensor.value block, display the value of the sound sensor.

Tip: You can drag the soundSensor.value block directly into any other block where you could type a value instead.

View on Code Studio

Student Instructions

Light Sensor Updates

This app is very similar to the last, but we've added a button with the ID "update_button". Write a program that displays the current value of the light sensor every time the update button is clicked.

Do This

Use an event handler to update the text of "light_value" using the lightSensor.value block.

View on Code Studio

Student Instructions

Make a Thermometer App

While the light and sound sensors have just a raw value with the value property, the temperature sensor is a little bit smarter. Instead of tempSensor.value, there are two properties tempSensor.F and tempSensor.C which convert the raw input value to either Fahrenheit or Celsius.

Do This

Make a simple thermometer app that reads in values from the temperature sensor. Your app can display the temperature in either Fahrenheit or Celsius (or both!)

  • Using Design Mode, create a button for updating and a label to display the temperature
  • Add an event handler to respond to your update button being clicked
  • Add code to your event handler to read the temperature sensor and display the value on screen
View on Code Studio

Student Instructions

Make a Prediction

Read the code in this program and make a prediction below. What do you think this program will do when you run it? How is the user intended to interact with the board? You may need to blow onto the board to see the full range of this app.

  • Sensor Events
  • 9
  • 10
  • 11
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Data Event

Each of these analog sensors emits a special event called "data" which occurs every time new data comes in from the sensor. Using this event, we can write programs that continually update.

Do This

This is the first program you worked on in this lesson, let's see if we can make it continually update the sound reading using the "data" event.

  • Create an event handler to respond to sound sensor's "data" event
  • Move the existing code into your event handler
View on Code Studio

Student Instructions

Change Event

The data event is fine if you don't mind your code running constantly, but sometimes that's not the most efficient solution to your problem. The sensor "change" event only fires if the sensor value has changed since the last reading, which can make sure your program isn't running code when it's not necessary.

Do This

Using the provided event handler, write a program that buzzes the buzzer and updates "temp_value" whenever the temperature sensor reading changes. Place your thumb over the temperature sensor to get it to change and see how your program responds.

View on Code Studio

Student Instructions

Change Threshold

By default a sensor's "change" event fires every time the sensor value changes at all, even by one. The light and sound sensors are constantly fluctuating, so you probably want the "change" event to be a little less responsive. The threshold property allows you to set how much a sensor should change before considering it a "change" event - so setting soundSensor.threshold = 100 would cause the sound sensor's "change" event only to trigger if its value increased or decreased by at least 100.

Do This

This app changes the background color of the screen whenever the sound sensor value changes, but we want to only respond to big changes in volume (like a loud noise). Use the soundSensor.threshold block to fix it.

  • Add a soundSensor.threshold block before the event handler
  • Test out which how different threshold values impact the "change" event
  • Choose a threshold value that makes the screen background change only when a loud noise is sensed.

Tip: Your threshold depends on how much ambient noise there is in the room, and how much it changes. A threshold that works in a quiet room may not work in a louder classroom.

  • Sensors to Colors
  • 13
  • 14
  • 15
  • (click tabs to see student view)
View on Code Studio

Student Instructions

Displaying Sensor Readings in Color

In the earlier prediction level you saw how the setScale() method can convert the full range of sensor readings (0-1023) into a range that can be used for RGB values (0-255). Let's build on that idea to make an app that can communicate the current value of all three analog sensors with a single color.

Do This

To start, we just need to set the sensor scales correctly - don't worry about actually setting the color of the background yet.

  • Using the sensor.setScale() block, set each of light and sound sensors to the right scale for an RGB color channel.
  • Create a variable for each of the three colors and assign each one to the value of a different sensor.
  • Use console.log() to test that your variables are in the correct range.

Hint: Order matters. You need to set the scale of a sensor before checking its value

Hint: Remember that the temperature sensor can be read in both Celsius and Fahrenheit - either way it should already give you a value that's within the necessary range for RGB

View on Code Studio

Student Instructions

Putting Color on the Screen

Now that your sensor data is in a scale that works for RGB, you can go about setting the screen color.

Do This

  • Add a setProperty() block and set it to change the screen's background color.
  • Add an rgb() block to the last parameter of setProperty().
  • Use the three color variables you created as inputs to the rgb() block.
View on Code Studio

Student Instructions

Continuous Updates

This sensor-to-color app isn't super useful if it only sets the color once when you start the app. You can use the sensors' "data" event to make your app continually update the background color every time the value changes.

Move your setProperty() block into a an event handler for one of the sensors to make the app update continually.

View on Code Studio

Challenge

Check with your teacher before pursuing this challenge

Now that you've practiced taking input from the various analog sensors and changing their scale to meet your needs, can you come up with an interesting use for sensor data? It's easy to display the raw numbers coming out of a sensor, but far more interesting to present that data in different ways.

Consider the following ways you might communicate sensor data:

  • Use the board outputs
  • Change the width, height, or position of a UI element
  • Use conditionals to display different images based on sensor values

Standards Alignment

View full course alignment

CSTA K-12 Computer Science Standards (2017)

AP - Algorithms & Programming
  • 2-AP-11 - Create clearly named variables that represent different data types and perform operations on their values.
  • 2-AP-12 - Design and iteratively develop programs that combine control structures, including nested loops and compound conditionals.
  • 2-AP-13 - Decompose problems and subproblems into parts to facilitate the design, implementation, and review of programs.
  • 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.
CS - Computing Systems
  • 2-CS-01 - Recommend improvements to the design of computing devices, based on an analysis of how users interact with the devices.
  • 2-CS-02 - Design projects that combine hardware and software components to collect and exchange data.
  • 2-CS-03 - Systematically identify and fix problems with computing devices and their components.