Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Raspberry Pi Zero Cookbook

You're reading from  Raspberry Pi Zero Cookbook

Product type Book
Published in Mar 2017
Publisher Packt
ISBN-13 9781786463852
Pages 422 pages
Edition 1st Edition
Languages

Table of Contents (17) Chapters

Raspberry Pi Zero Cookbook
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface
Kick-Start Your Raspberry Pi Zero Setting Up Physical and Wireless Connections Programming with Linux Programming with Python Getting Your Hands Dirty Using the GPIO Header Controlling the LEDs and Displays Controlling the Hardware Taking Digital Inputs to the Raspberry Pi Zero Interfacing Sensors with the Raspberry Pi Zero Cooking up Projects to Amaze the World!

Chapter 9.  Interfacing Sensors with the Raspberry Pi Zero

In this chapter, we will cover the following recipes:

  • Interfacing any resistive sensor on GPIO

  • Interfacing an LDO with the RPZ

  • Interfacing an ultrasonic sensor with the RPZ

  • Interfacing an analog-to-digital convertor to interface any analog sensor with the RPZ

  • Interfacing an infrared receiver to read remote controls and control a TV with the RPZ

  • Interfacing a motion sensor

  • Interfacing a temperature humidity sensor using bit-banging

  • Interfacing a gyroscope and accelerometer with the RPZ

  • Pulling it all together with the Pi Sense Hat

Interfacing any resistive sensor on GPIO


Resistive sensors are devices whose resistance changes when the sensor's input is altered. A photoresistor will change based on the amount of light, and a thermistor's resistance changes with temperature. With a simple circuit, you can configure your GPIO to read changes in a resistive sensor's values.

Getting ready

For this recipe, I used a photoresistor as they are the easiest ones to change values on. Any two-lead resistive sensor will work for this recipe. You'll also need a 1uF electrolytic capacitor.

How to do it...

  1. Configure the following circuit and wire it to the Raspberry Pi:

  2. Using RPi.GPIO again, we can put together a simple program that tracks our sensor. Create photoresitor.py in the ch9 directory with the following code:

        #!/usr/bin/env python 
        # Raspberry Pi Zero Cookbook 
        # Chapter 9 - Analog Sensors 
        import RPi.GPIO as GPIO 
        import sys 
        import time 
        import os 
        #GPIO.setwarnings(False) 
        GPIO.setmode(GPIO...

Interfacing an LDO with the RPZ


Analog resistance isn't the only thing you can calculate with the Raspberry Pi Zero GPIO. You can also capture the voltage levels from something like a low dropout voltage regulator. These regulators will work even when the input voltage is very close to the output voltage. By reading the sensor, we can read values of things such as rechargeable battery packs.

Getting ready

We can demonstrate this concept with only a variable resistor and a 1 uF capacitor. I used the 10K variable resistor included in the Elego kit.

How to do it...

  1. With the following circuit, we can control the voltage divider from the 3.3 volt Raspberry Pi Zero output and read its value. As you can see, it is just like the previous recipe on resistive sensors. This time, we will track the voltage instead.

  2. If you were measuring higher voltages, you would use a higher capacitor value to be able to more accurately track the discharge time. It's how we determine the voltage value this time, but timing...

Interfacing an ultrasonic sensor with the RPZ


Ultrasonic sensors can give you an accurate distance measurement. The transmitting side of the sensor sends out an ultrasonic pulse, which will bounce back off the target, to be caught by the receiving side of the transmitter. Knowing that the pulse travels at the speed of sound, we can determine how far away an object is by capturing the time between sending and receiving.

You can use your Raspberry Pi Zero to understand the ultrasonic sensor's data and report to you how far away an object is. Let's take a look!

Getting ready

For this recipe, we will use the following components

  • One 1 KOhm resistor

  • One 2 KOhm resistor

  • One tactile pushbutton switch

  • The Elego HC-SR04 Ultrasonic sensor (though any four-pin ultrasonic sensor should work)

How to do it...

  1. The following diagram shows how to connect the switch, resistors, and sensor to the Raspberry Pi Zero:

  2. The Python code for setting up this recipe is quite simple. Enter the following code as ultrasonic...

Interfacing an analog-to-digital convertor to interface any analog sensor with the RPZ


The Raspberry Pi Zero GPIO is a great interface, but it does lack the analog ports you would find on something like an Arduino. We can use PWM to utilize analog devices over the digital ports, like we did with the first photoresistor recipe, or we can add an analog-to-digital convertor (ADC) to our circuit. As you can probably guess, the ADC will read the voltage and convert it into a digital signal that the Raspberry Pi understands natively. The MCP3008 analog-to-digital convertor will handle up to eight analog signals and report back over the SPI interface. Let's get started!

Getting ready

We will use the MCP3008 ADC. Along with that, you'll need the following:

  • One photoresistor (the one used in the earlier recipe is fine)

  • One thermistor

  • Two 10K resistors

Note

You can add more analog sensors to this recipe as you'd like, up to eight of them! I used two different sensors to give you an idea. The MCP3008 is a...

Interfacing an infrared receiver to read remote controls and control a TV with the RPZ


Infrared sensors are used everywhere for remote operation. We can add an IR sensor to our Raspberry Pi to be able to control it remotely.

Note

The latest Raspbian version supports infrared receivers, though it was a little tricky to get working perfectly. I'll cover all of the steps it took, though it is possible future versions of Raspbian will make it a little easier to get going quickly.

Getting ready

I used the infrared sensor available in the Elego Kit. This includes both the sensor and a remote control. There are a lot of infrared sensors available online, and you can really use any remote control, including one you are already using, such as your TV remote. That can be a lot of fun: if your TV and Raspberry Pi both see the remote signal, your TV's power button can both turn on the TV and turn off the lights!

You'll also want to make sure that the infrared service is installed and up to date:

sudo apt...

Interfacing a motion sensor


A motion sensor will report a signal when it detects any kind of motion within its range. We can incorporate this into the Raspberry Pi Zero to trigger any kind of event, take a picture, send an e-mail, or turn on a light or motor. Here, we will have our Raspberry Pi Zero understand the incoming signals from a motion sensor.

Getting ready

All you'll need for this recipe besides your Raspberry Pi Zero is an infrared sensor board. The Elego Kit includes one and they are available at many Internet of Things online suppliers.

How to do it...

  1. Configure the sensor using the following circuit:

  2. The code is just as easy as the circuit. Create the motionsensor.py file with the following code:

        #!/usr/bin/env python 
        # Raspberry Pi Zero Cookbook 
        # Chapter 9 
        # Motion Detection 
        import time 
        import RPi.GPIO as GPIO 
        GPIO.setmode(GPIO.BCM) 
        GPIO.setwarnings(False) 
     
        motionpin=17 
        GPIO.setup(motionpin,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)...

Interfacing a temperature humidity sensor using bit-banging


Digital temperature and humidity sensors are quite common in the Internet of Things world. They are rather inexpensive and provide an easy way to record accurate temperature and humidity readings. Using a technique called "bit-banging," we can retrieve a digital signal that will tell our Raspberry Pi Zero both the temperature and relative humidity.

Getting ready

All you'll need is a digital temperature and humidity sensor and a 10 KOhm resistor. There is one included in the Elego kit, and most of the ones you'll find online operate the same way.

To test the device, I used the pydht2 tool in the Python library, installed using sudo pip install pydht2.

How to do it...

  1. Connect your sensor to the Raspberry Pi Zero, as shown here:

    Note

    A lot of Digital Sensors expect and provide 5 Volts for input and output, respectively. Check your sensor's data sheet to find out what voltages are supported. If you have a 5V sensor, you can easily integrate...

Interfacing a gyroscope and accelerometer with the RPZ


Gyroscopes and accelerometers are chips that detect their relative position and movement. They are the core component of things such as quadcopters that need to make fast motor adjustments for the drone to hold a position or move in a particular direction. Let's take a look at how easy it is to integrate a gyroscope with your Raspberry Pi Zero. Could you make your own quadcopter? Here's how you might start!

Getting ready

The Elego kit includes an analog gyroscope/accelerometer, which is easy to use with an ADC such as the MCP3008 used earlier in this chapter. The Raspberry Pi Zero, MCP3008, and gyroscope are all you'll need.

How to do it...

  1. If you've kept your MCP3008 connected from the earlier recipe with resistive sensors, you are almost done. Here is the circuit you'll need to read your gyroscope:

  2. Using the ADC, the gyroscope is easy to read. Create the gyro.py file in the ch9 folder:

        #!/usr/bin/env python 
        # Raspberry Pi Zero...

Pulling it all together with the Pi Sense Hat


I don't go over many shields in the book, but there are a few that combine a lot of useful features for a low cost and are readily available on the Internet. One of those shields is the Sense Hat: for only $30 US, it combines eight sensors with a joystick and 8x8 RGB LED matrix. This is a quick and easy way to track several sensors with just a little hardware.

Getting ready

The Raspberry Pi Sense

Hat is available through most retailers that sell the Raspberry Pi. The Raspberry Pi Foundation website (www.raspberrypi.org) has links to the retailers for its products.

Make sure you have the latest version of the Python libraries installed with sudo apt-get install sense-hat python-sense-hat.

Note

If you don't have a Sense Hat, Raspberry Pi has an emulator you can use instead. Just install the following: sudo apt-get install sense-hat-emu sudo apt-get install python-sense-emu For the following code, instead of importing sense_hat, you will import sense_emu...

lock icon The rest of the chapter is locked
You have been reading a chapter from
Raspberry Pi Zero Cookbook
Published in: Mar 2017 Publisher: Packt ISBN-13: 9781786463852
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}