PiperCode: Temperature

PiperCode: Temperature

 

Goldilocks Machine

BUILD A DEVICE TO MEASURE TEMPERATURE

30-45 MINUTES

 

At the table in the kitchen, there were three bowls of porridge.

 

Goldilocks was hungry.  She tasted the porridge from the first bowl…

"This porridge is too hot!" she exclaimed. So, she tasted the porridge from the second bowl.

 

"This porridge is too cold," she said. So, she tasted the last bowl of porridge.

"Ahhh, this porridge is just right," she said happily and she ate it all up.

 

 

In this project, we’ll build a “Goldilocks Device”. This device can measure the temperature, and tell us if somewhere is “just right”! 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!

 

 

 

STEP ONE:

For the temperature probe, you will need:

 

  • 1 Temperature Sensor
  • 1 Mini Breadboard
  • 1 4-pin diode jumper cable
  • 4 Jumper wires

 

 

 

 

 

STEP TWO:

For the LED gauge, you will need:

 

  • 1 Mini Breadboard
  • 3 LED's
  • 4 Jumper wires

 

 

 

 

 

STEP THREE:

Let’s start building! Wire up your temperature sensor to the Pi, using the following diagrams as guidance.

 

 

 

 

 

 

 

 

STEP FOUR:

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:

 

 

 

 

STEP FIVE:

Let's start with a Main Loop. Go to the main menu of PiperCode and open Settings. Unlock all projects in order to start a new one. 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.

 

 

 

 

STEP SIX:

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.

 

 

 

 

 

 

 

STEP SEVEN:

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.

 

 

 

 

 

 

 

 

 

 

STEP EIGHT:

Right 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.

 

 

 

 

 

 

STEP NINE:

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.

 

 

 

 

 

STEP TEN:

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.

 

 

 

 

 

 

STEP ELEVEN:

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.

 

 

 

 

 

 

 

 

 

STEP TWELVE:

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!

 

 

 

STEP THIRTEEN:

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.

 

 

 

 

 

STEP FOURTEEN:

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.

 

 

 

 

 

 

 

STEP FIFTEEN:

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!”

 

 

 

 

STEP SIXTEEN:

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.

 

 

 

 

 

 

YOUR TURN:

Find objects or places around you that are at different temperatures and measure them with your temperature sensor- see which ones are “just right”!

 

NOTE:

Do not place your Piper Temperature Sensor in liquids or on hot metal surfaces.