You must first complete TouchPad Conditional Statements before viewing this Lesson

Taking Sensor Measurements

Introduction

Building off of our usage of the lp_sensors library from the previous project, we will now be using other sensors: the light and temperature sensors. We will also be using the Brain’s screen for the first time!

Components Needed

  • Little Python Brain
  • A tablet/phone/laptop
Steps

1

Importing libraries

Get the import lp_sensors block as well as the from time import sleep blocks

2

Setting up the sensors

Take the tempSensor and lightSensor setup blocks on the Setup category

3

Creating the main loop

As with previous lessons, we’ll be making our loop from the while True block.

4

tempSensor.measure() and lightSensor.measure()

These are the primary sensor measurement blocks that we need for light and temperature. Every time these blocks are read by the loop, they take the temperature and light readings and store them into the variables Temperature and LightLevel respectively.

5

Printing the measured values

We can then print the measurements we recorded. Take a print() block and copy what’s shown. Be particular about the comma and the plus sign as both of these are very important. Failing to include these might result in an error!

6

Adding sleep

Finally, we’ll be adding a sleep(1) block at the end of our loop. Run the code!

The Complete Code

Every time the code loops, the temperature measurement and the light level measurement are stored inside the variables Temperature and LightLevel. 

Afterwards, we use the print() function to display these. 

After a 1 second wait, the loop repeats.

Back to: Beginner > Weather Station

CONTACT US

    Log in with your credentials

    or    

    Forgot your details?

    Create Account