Chapter 2. Measure Data Using Your Raspberry Pi Zero Board

In the first chapter of this book, we worked on setting up your Raspberry Pi board so you can use it in your projects and realize all the projects you'll find in this book.

In this chapter, we are going to make our first project using the Zero board: measuring data using your board. We are going to learn how to connect a very simple temperature and humidity digital sensor to your Pi, and how to write software to read data from it.

From there, we'll look at some very basic applications using this sensor that can be really useful inside a smart home: how to log data on the Pi itself, how to access the measurements remotely, and finally, how to display past data on a nice plot.

Hardware and software requirements

We have already discussed most of the requirements for this project in the first chapter of this book. Here, you will simply need an additional component: a DHT11 sensor (https://www.adafruit.com/products/386). The following image shows the sensor:

Hardware and software requirements

You can of course use other similar sensors, for example the DHT22, which is more precise. To use a DHT22, you will only need to change one thing inside the code we'll see later.

You will also need a 4.7k Ohm resistor to make the sensor work, as well as jumper wires and a breadboard.

Hardware configuration

Let's now look at how to configure the hardware for this project; basically, how to connect the sensor to the Pi Zero board.

The following figure is a schematic to help you out:

Hardware configuration

As it's the first project we are actually building using the Raspberry Pi Zero, there is something important I wanted to point out here. To connect the board to components like this sensor here, we have two options. You can either use jumper wires directly (as shown on the schematic), or use a cobbler kit to connect all the pins of the Pi to the breadboard, as shown in the following image:

Hardware configuration

This is up to you, and to be clear, I'll always show only the individual wires on the schematics, but use a cobbler kit to actually build the projects.

Here, you simply need to place the DHT11 on the breadboard, and then connect the resistor between the VCC and the data pins. Then, connect the VCC to the 3.3V pin of the Raspberry Pi, GND to GND, and finally, connect the data pin of the sensor to pin 4 of the Raspberry Pi board.

Software configuration

Now we are going to install additional software on your Pi to make sure we can read data from the sensor.

Following the instructions from Chapter 1, Configuring Your Raspberry Pi Zero Board log into your Pi via SSH, or just use it with an external screen with mouse and keyboard.

  1. Inside a terminal, type the following:
    wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.50.tar.gz
    
  2. Wait for the download to complete and then type the following:
    tar zxvf bcm2835-1.50.tar.gz 
    
  3. Next, type the following:
    cd bcm2835-1.50
    
  4. Now, configure the software you just downloaded with the following:
    ./configure
    
  5. Build this software with the following:
    make 
    
  6. Now, verify that everything is okay with the following:
    sudo make check 
    
  7. If there are no errors, you can then install the software on your Pi with the following:
    sudo make install
    

After that last step, you can now move on to the projects of this chapter!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset