Gather Your Parts
Our Goldilocks Device will require two components: a temperature probe, and an LED display gauge. We’ll build these two components separately, so try not to mix up the parts!
For the temperature probe. You will need:
For the LED gauge, you will need:
Build a Temperature Probe
Let’s start building! Wire up your temperature sensor to the Pi, using the following diagrams as guidance.
Build an LED Display Gauge
Now, let’s build the component which will tell us whether the temperature is “too cold”; “too hot; or “just right”!
Once again, use the following diagrams to help you assemble the LED gauge.
Once you are done, the Goldilocks Device should look like this:
Code the Goldilocks Finder
Start with a Main Loop
To start, we’re going to create a simple loop.
Find a Start block from Logic, and bring it into the programming area. Then, attach a repeat forever block, found in Loops, to the Start block.
Record Temperature as a Variable
Now, we are going to capture the output of the temperature sensor in a variable.
Create and name a new variable - temperature - using the Variables pane. Take a set temperature to block from Variables and add it to your loop.
Then, find a Temperature (F) block from Expansions, and use it to set the value for temperature.
Set Up Conditional Logic
Under Logic, find an If do else block with the “=” inequality as shown. Select the settings gear for this block and choose an else if and an else block. Drag these under the if block as shown.
The final statement should be “if do, else, else if, else, do”. This sets up a conditional that means three different things can happen depending on the input values from the sensor.
Add a Comment
Click on the question mark part of this block to add a comment so that you know why you decided to create this conditional block.
Tidy Your Coding Space
Make sure your coding space stays neat and tidy by dragging your comment to the side of your code. Use the example below to guide you.
Prepare a “Hot!” Mode
Next, you will want to create a range in your code that signals something being measured by the temperature sensor is “too hot.'' Label the “=” block as shown.
Define “Hot!” Conditions
Add a temperature block found under Variables and add it to the equality as shown. Change the equality to a greater than or “>” symbol to ensure that temperatures above the set temperature result in the desired outcome. Add an integer variable (“0”) found under the variables tab. Change the integer to a value that you believe is “too hot” in Fahrenheit. The example below shows 82 degrees.
Turn On the “Hot!” LED
Your code should tell the red LED to turn on when something is “too hot” or greater than (>) 82 degrees Fahrenheit. Drag a turn on pin block into the do part of your conditional as shown. Set the pin number to 40.
(Note: you may want to check your wiring to make sure the red LED is wired up to pin 40 on the Raspberry Pi. You can use the diagrams at the beginning of this tutorial to help you!)
Create a “Cold!” Mode
Next, you will label the else if section of your conditional to make the “cold” mode of your Goldilocks device. Duplicate the inequality statement and label it as shown. Drag the inequality statement into the else if part of the conditional.
Define “Cold!” Mode
Set the inequality to less than or “<” to ensure that temperatures colder than the value will result in the blue LED lighting up. Choose a temperature that you deem as “too cold” For example, we have chosen 72 degrees (which is room temperature). Duplicate the Turn Pin on block and add it to the next do section of the conditional. Set the pin number to 37.
Finishing with a “Just Right” Mode
Finally, time for the “just right” temperature which will light up a yellow LED. Duplicate the Turn Pin on block and add it to the final else part of the conditional. Set the pin number to 38. This is a good time to check your wiring to ensure the pin numbers in your code match the pins that are wired to the LEDs.
If the temperature read by the sensor is outside of the cold or hot range, then the yellow LED will light to signal the temperature is “just right!”
Turn Off the Lights
You will need to ensure that you set all pins to off during each loop, so only one LED remains on at any moment. To do this, grab a “Turn all pins” block under chip and drag it to the top of the loop (below repeat forever). Set it to OFF.
Ready to test? Find objects or places around you that are at different temperatures and measure them with your temperature sensor- see which ones are “just right”!
ATTENTION: Do not place your Piper Temperature Sensor in liquids or on hot metal surfaces.