Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Raspberry Pi Sensors

You're reading from   Raspberry Pi Sensors Integrate sensors into your Raspberry Pi projects and let your powerful microcomputer interact with the physical world

Arrow left icon
Product type Paperback
Published in Apr 2015
Publisher
ISBN-13 9781784393618
Length 192 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Rushi Gajjar Rushi Gajjar
Author Profile Icon Rushi Gajjar
Rushi Gajjar
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Meeting Your Buddy – the Raspberry Pi FREE CHAPTER 2. Meeting the World of Electronics 3. Measuring Distance Using Ultrasonic Sensors 4. Monitoring the Atmosphere Using Sensors 5. Using an ADC to Interface any Analog Sensor with the Raspberry Pi 6. Uploading Data Online – Spreadsheets, Mobile, and E-mails 7. Creating an Image Sensor Using a Camera and OpenCV A. Shopping List Index

Be ready with Python and C

We'll use Python because it is a very simple, yet powerful, language and is easy to write and read because of its indentation and standard English keywords.

Note

There are two major versions available and there is a current debate on Python 2 versus Python 3. You can read it at https://wiki.python.org/moin/Python2orPython3.

This book will mostly use Python 2.7.x. If you are beginner and want to learn Python, I advise you to go with Python 3; there is not much difference between the two, but there are noticeable differences and you will observe them.

The C programming language offers ample benefits when developing the projects using already available libraries, such as wiringPi, which can give you full control of GPIO pins. If you have previously developed a project on C, you can integrate the wiringPi functions and get the same functionalities as your previous project. Also, you can simultaneously use GPIO.

Let's play around with both the languages; this will not give you the whole idea of the programming, but it will give you a good start and will create interest. We will see both the languages one by one.

Writing and executing the Python program

When you use the RasPi, the Python is already an installed component. In the Linux CLI, you can just type python and the Python CLI will wait for you to enter the commands. Just type print "this is my first program in Python" and press Enter. Voilà! You have executed a command of Python. This will not allow you to write full-length code directly now, so what to do if you want to write a long code? There is a better way than this, and we will use that throughout the book.

Type sudo nano example1.py and you will observe the nano text editor on the screen. Then type the following code:

name = "World"
name = "Hello " + name
print name
for i in range(3):
    print "Whoa"
import this

Now press Ctrl + X and then press Y to save the changes. You will be back to the Linux CLI. Now type python example1.py. The Python program will be compiled and the output will be displayed in the same window. One thing you should notice is that indentation is very necessary in Python. Remove the indentation before the print "whoa" script and then execute the program; you will find an error of indentation. In the loops, special care for inserting indentation should be taken while writing the code. This makes the programs easy to read for people other than programmers.

Writing and executing the C program

You should know that the most powerful language existing today is C, and it allows us to fulfil all our coding needs. The C language is very common and is an essential language. Let's go through the procedure of executing a C program, which is almost similar to executing Python programs.

Type sudo nano example2.c in the LXTerminal or PuTTY. Then you can type any C code you know, or on a beginner basis, you can try this code:

#include<stdio.h>
int i;
int main()
{
for(i=0;i<3;i++)
printf("Harder you work, Luckier you get");
return 0;
}

Press Ctrl + X and press Y to save the changes. Now it's our turn to compile and execute the program. The compiler for C programs is always included in the Linux distribution, which can be cc or gcc. Type this command to compile the C program:

gcc -o example2 example2.c

In -o example2, the example2 part will be the name of the output file and the example2.c part is the file we saved after writing the C program. Press Enter and check the errors. Correct it by typing sudo nano example2.c and solve it (if any error occurs). Once it is successfully compiled, type the ls command to check whether the output file has been created. The output filename will be example2. You can now type ./example2 to execute the compiled code.

These processes are really helpful in creating sensor projects, and once you practice more codes, it will be easy for you to understand the process.

You have been reading a chapter from
Raspberry Pi Sensors
Published in: Apr 2015
Publisher:
ISBN-13: 9781784393618
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 $19.99/month. Cancel anytime