Book Image

Scratch 2.0 Beginner's Guide: Second Edition

By : Michael Badger
Book Image

Scratch 2.0 Beginner's Guide: Second Edition

By: Michael Badger

Overview of this book

Table of Contents (18 chapters)
Scratch 2.0 Beginner's Guide Second Edition
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Time for action – recording the resistance of a thermistor over time


The following exercise walks us through creating a script that records resistance values based on an interval. If you don't have a PicoBoard or a thermistor, you could alter the script to record loudness. We're going to be setting up this project as a way to conduct experiments with resistance, and because we're using the thermistor, we can draw some conclusions about temperature.

We'll create the framework to do the monitoring, and at the end of this exercise, you'll have a thermistor measuring the resistance at room temperature. (Non-PicoBoard users can set this framework up with the loudness block to capture sound.)

We'll discuss ways to apply our environmental monitor in other interesting ways after we get the framework finished up. We create the framework as follows:

  1. Let's set up our project. Create a new project, and replace the Scratch cat with a small circle sprite that you create in the paint editor. You may name it circle sprite.

  2. Add a new list called measurements as a place to store the collected results.

  3. Make sure the PicoBoard is connected to the computer and has one of the alligator clips plugged into one of the ports.

  4. Connect one lead of the alligator clip to one lead of the thermistor. Attach the second clip to the other lead of the thermistor.

  5. Next, add the when flag clicked block to the Scripts area. Attach the following blocks to initialize the project:

    • hide

    • delete (all) of (measurements)

    • add (resistance-B) sensor value to (measurements)

  6. Add a forever block to the script so we can continually run the following blocks:

    • wait (60 secs)

    • add (resistance-B) sensor value to (measurements)

  7. The following screenshot shows the script. Note that this script will run forever. We'll review why I chose this script in the upcoming discussion.

What just happened?

If we examine the values that we recorded in the measurements list, they likely do not vary significantly at room temperature. The key control point of this script is the interval at which we collect information. Basically, we collect a sample every 60 seconds. We actually record a measurement before we start the forever loop, which gives us a reading at minute zero that which would correspond to the start of the experiment, assuming we use this script as the basis for the experiment.

The script, as written, is an infinite loop. I'm okay with that. The real question is, are you? The reason I chose not to provide an exit for the data collection is that it may be difficult to know how long the script should run, and depending on what you choose to measure, you will be present to manually stop the script and the collection. If you measure the resistance of boiling water, you'd be present. If you measure the warming of water from solid ice to room temperature, you're not sure how long that would take. So, you could set the script and come back several hours later to check on it.

In other words, you pressing the stop button in the project is the condition that stops the data collection. For example, if you wanted to ensure that you had an exact number of measurements, you could replace the forever block with a repeat () block. Your choice!

The data we collect is dependent on using the alligator clips to form a circuit using the thermistor. As I alluded, thermistors can be used to calculate a temperature based on the measured resistance. A widely used formula called the Steinhart-Hart equation (http://en.wikipedia.org/wiki/Steinhart%E2%80%93Hart_equation) can be used to calculate a temperature in Kelvin based on resistance values that could then be converted to Fahrenheit or Celcius.

Scratch interprets resistance on a scale from 0 to 100, which limits our ability to perform the actual temperature calculation based on the measured resistance. For now, the recorded resistance values could be used to make observations about temperature; the actual temperature calculation is not a goal of our exercises.

Completing a circuit

When we connect the alligator clips together, we basically replicate a switch, like the one that turns the light in the room on and off. Connect the clips to complete the circuit (that is, turn it on). Remove one clip to break the circuit (that is, turn it off). In Scratch, a resistance value of 100 is equivalent to off; no current passes through the circuit. A resistance value of 0 means that current is flowing freeing through the circuit without any resistance. Values between 0 and 100 measure the amount in the circuit.

The following screenshot illustrates the resistance values. The value in resistance-A is 100.0 because the alligator clips are not connected. The 53.8 value for resistance-B is the resistance value recorded by a thermistor at room temperature (approximately 70 degrees Fahrenheit), and the resistance-C value of 0.0 is a circuit with no resistance from two alligator clips connected together.

The loudness reporter block is working in a similar way. A loudness value of 0 means there is no detected sound, while a value of 100 indicates maximum loudness (as measured by Scratch).

Tip

You can only create a circuit using the alligator clips attached to a single sensor. In other words, connecting the clips on sensor A to sensor B will not create a circuit and the values for sensors A and B will remain 100.

When we measure electrical resistance, we want to know by how much the material impedes an electrical current. In the case of our PicoBoard sensor, the higher the value, the more the material impedes the current. How we use that value is left to our imagination and the needs of our project.