Book Image

Raspberry Pi Sensors

By : Rushi Gajjar
Book Image

Raspberry Pi Sensors

By: Rushi Gajjar

Overview of this book

Table of Contents (16 chapters)
Raspberry Pi Sensors
Credits
About the Author
Acknowledgements
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

Raspberry Pi is a single-board, credit-card-sized, computer packed with many opportunities to explore and invent. It is really amazing to see kids start coding Python from scratch, and build a bird box that streams live video on the Internet to check whether a bird has got its meal. I remember that when I was a kid, I used to play with Lego toys attached to DC motors and batteries, which was engaging. At that time, I could not imagine the logic that went into coding, and did not get any chance to code my projects and control the movement of those Lego blocks using a mobile phone. But I am lucky enough now to get an opportunity to explain such projects and provide a launchpad for young creators who really have a passion to create something and change the world around us.

The world is moving towards a new era. Technology is revolutionizing daily needs and habits and making them available on a simple interface, which gave me motivation to write a book on Raspberry Pi sensors. It's a world of creativity, and the I believe that creativity comes when you start understanding and appreciating the fundamentals and start applying logic to it. A lot of information and projects on Raspberry Pi are floating on various webpages, and one wishes to achieve as much as he/she can. I feel that the information on webpages is often observed as incomplete. It gives us a quick start to build projects but does not explain what is behind them.

It is known that without actually diving too deep into electronic devices and communication protocols, you can start coding on Raspberry Pi and craft amazing projects. I have colleagues around me who often need to code and wire the sensors on the Raspberry Pi platform for their experiments. They can develop Python code on artificial neural networks in a short span of time, but when it comes to wiring something, they look around. I believe that a basic understanding of electronics is a plus for such prodigies out there, who want to develop code on such platforms. In the opposite scenario, hardware developers can wire sensors, ensures proper voltage levels on device pins, but when it is time to code, they need help.

The most interesting thing that I find with the Raspberry Pi is that I can still play with the hardware components and soldering iron, and code my hardware to make it live. This book provides a kick start for such creators, who really want to know how things work together, and want a direction for starting projects on sensor interfacing and the Internet of Things with Raspberry Pi. There is tremendous growth in technology when we look towards the connected array of everything around us.

Internet of Things opens up a new world for collecting data and analyzing it for better user experience. A lot of data from the array of sensors has been generated from several different sensor nodes. In this context, the Raspberry Pi provides us with the opportunity to start with simple projects, such as uploading data to the Internet from a developed sensor station, as described in the chapters of this book. This will be your first step to building an Internet of Things project. Another interesting thing is that with the rise of Raspberry Pi 2 model B, developers have got enough processing power to perform computation-intensive algorithms on the Raspberry Pi. Therefore, image processing has been included in one of the chapters. It would have been very difficult to try to explain image processing to beginners, but I have at least tried to offer a simple start for readers so that they begin image processing on their own.

This book explores five different projects, any of which can be a startup for different ways of building electronics projects. The approach I have followed while preparing the projects is quite interesting. This is the methodology I often follow to develop complex hardware designs. Although I do not rely on breadboards (as I am more into high-speed circuit designs), small project prototypes, some of which are covered in this book, can easily be wired on breadboards. The first approach should be to purchase the best hardware components (preferably through hole for breadboard testing), on which you can rely when the code is not working or not giving the proper results. Prepare a block diagram and consider each issue that may occur during hardware and firmware design. Second, read datasheets of components used and ensure every single entity meets the design requirements. Thirdly, wire the components to the breadboard and check it thoroughly. Finally, when the hardware is built robustly, write the code (or firmware), and rewrite it to make it more perfect. Remove the unnecessary variables and unreachable code or loops, handle interrupts, define the sleep time and watchdogs of a processor, and manage proper memory segments to avoid crashes. However, this book has followed mostly simple code that does not go that deep into managing embedded programming. Installing useful coding libraries on the Raspberry Pi takes care of the faults often created by a programmer. Just call a function and it does all the embedded calls in the background. Thanks to the developers of the Raspberry Pi libraries, with which we build more robust code (whether knowingly or unknowingly). When you prepare a sample of code, I advise you to break it down into pieces.

You may face some difficulty when building the project for getting data from temperature, humidity and light sensors. First, get it done for temperature and humidity, and then code for the light sensor. Whenever both pieces of code give you the desired values, recode them. Then you can combine them by managing the function calls.

When writing the chapters, I have followed a common theme across the book: first the setup, then the purpose of the project, and finally describing the hardware with complete details. In some of the chapters, the software has been divided into components, and then they have been merged so as to avoid monotony for you. I apologize to you for being lengthy in the theory portion in some parts of this book, but I am sure that you will love to read and learn a lot from it, and you can get the most out of it.

Any questions, improvements, and suggestions are welcome, and should probably take place in the GitHub issues for the book at https://github.com/rushigajjar/raspberrypisensors so that everybody can take part. Besides that, anybody can contact me on LinkedIn at https://in.linkedin.com/in/rushigajjar, and send messages regarding their interesting projects and startups. I would really love to hear about it. Or you can send tweets by sharing temperature and luminance values at @rushigajjar once you get your air conditioner and lights automated!

What this book covers

Chapter 1, Meeting Your Buddy – the Raspberry Pi, gives an introduction to all the models of Raspberry Pi available in the market, including the recent Raspberry Pi 2 model B. A method of installing the operating system and interesting ways to share the Internet with the Raspberry Pi are discussed. Then we perform some hands-on coding in Linux terminal, Linux shell scripting, Python, and C on the Raspberry Pi.

Chapter 2, Meeting the World of Electronics, explains the fundamentals of electronics and communication protocols by which electronic devices communicate. Experiments with GPIO are more interesting to perform with the shell, Python, and C languages.

Chapter 3, Measuring Distance Using Ultrasonic Sensors, shows you how to set up an ultrasonic sensor with the Raspberry Pi and learn to take care of different voltage levels across the devices. We prepare a code to get our ultrasonic sensor running, and develop an aid for a visually impaired person with an obstacle avoidance system.

Chapter 4, Monitoring the Atmosphere Using Sensors, develops your skills in choosing a sensor from many that are available in the market. We then implement the hardware and software required for temperature, humidity, and light sensors to automate our home appliances.

Chapter 5, Using an ADC to Interface any Analog Sensor with the Raspberry Pi, explains interfacing of analog-to-digital convertors with an array of sensors. We build a sensor station for the Raspberry Pi using serial protocols to use the generic software function built, to get data from any sensor interfaced with it. Finally, the data can be stored in a log file for analysis.

Chapter 6, Uploading Data Online – Spreadsheets, Mobile, and E-mails, takes a dive into the Internet of Things and sensor nodes. With the help of the sensor station developed in the previous chapter, we upload the data to online spreadsheets and observe a real-time graph. We also get emergency e-mails on our e-mail platforms. Once you get your project done, you can send your sensor values to .

Chapter 7, Creating an Image Sensor Using a Camera and OpenCV, covers the basics of image processing and how an installation of the OpenCV library can be performed successfully. Using a camera, we will develop an IP camera to install in your backyard to observe live streaming of activities. Further, we will build a piece of motion detection code in OpenCV to detect human movement in a particular area and capture an image for an immediate alert.

Appendix, Shopping List, includes the list of the hardware components that need to be purchased in order to perform the hands-on tasks described in the book. From chapter 2 onwards, these components will be required to test our codes. You can directly take this list to the electronics distributors near you and come home with a filled shopping bag!

What you need for this book

There are no special demands for implementing the projects of this book on the Raspberry Pi, except the essential hardware components! You can use a personal computer with Linux, Windows, or Mac OS X to connect the Raspberry Pi to it. Any of the Raspberry Pi models available in the market (Raspberry Pi 1 models A+, B, or B+, or Raspberry Pi 2 model B) should be okay for performing experiments on the code. You can refer to the Appendix, Shopping List, for the requirement of hardware before getting into Chapter 3, Measuring Distance Using Ultrasonic Sensors, and the later chapters.

Who this book is for

This book is perfect for hardware enthusiasts who want to create a variety of projects using the Raspberry Pi. It is for people who have prior programming experience, especially in Linux, C, and Python, but it's not limited to them. Those who do not have programing knowledge can get the essentials from the book and start developing the projects instantly. In any case, this book will get you ready with all the latest electronics concepts that are required for hardware programing with the Raspberry Pi.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "There are two functions in the spi-dev library for sending the data to SPI slave devices."

A block of code is set as follows:

import RPi.GPIO as GPIO
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
Print "All Set in Python! Let's Blink"

Any command-line input or output is written as follows:

sudo nano /etc/modprobe.d/raspi-blacklist.conf

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Select the Enable Camera option in the configuration settings using the keyboard."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.