Getting Started with Sensors by Kimmo Karvinen and Tero Karvinen

Getting Started with Sensors by Kimmo Karvinen and Tero Karvinen

Author:Kimmo Karvinen and Tero Karvinen
Language: eng
Format: mobi, epub
Tags: COMPUTERS / Programming / Open Source
ISBN: 9781449367039
Publisher: Maker Media, Inc
Published: 2014-08-25T04:00:00+00:00


In the following projects, we’ll show you how to use the GPIO pins from a Python program. All of the sensor projects in this book use Python.

Python is one of the easiest languages to learn. When we code for fun, most often it’s in Python. Before we get into programming GPIOs with Python, let’s get you up and running with the language.

Hello, Python World

Traditionally, the first program to write in any language is “Hello, world.” It lets you test to confirm that everything is working. If you are a beginner, you’ll also get assurance that you know how to write a Python program!

Whenever you start programming, you should always start with a “Hello, world” — on any language, on any platform.

You’ll write your Python program just like any text file. The best place to store your program files is in your home directory (/home/pi). If you’re not already in your home directory, you can change to it with the terminal (LXTerminal) command cd:

$ cd /home/pi

Start editing a new file with nano, a command-line text editor for editing plain-text files. Its purpose is similar to other plain-text editors such as Notepad, gedit, vi, and Emacs. Type this command:

$ nano hello.py

The file needs only one line. This is Python for you: easy things are so easy, but hard things are still possible. Add this line to the file:

print("Hello, world!")

The code has just one line. print() is a command, but it behaves like a function. It gets one argument, the string “Hello, world!” As you probably guessed, you are asking the computer to print the string to the terminal.

Now save your work by pressing Ctrl-X (exit). When prompted about whether you want to save, type “y” for yes. Finally, confirm the name, “hello.py”, by pressing Enter or Return.

Now it’s time to run your Python program. This is a big moment if this is your first step in Python. Type the command shown after the $ prompt. You’ll see the output as soon as you press Enter:

$ python hello.py Hello, world!

Your Python program printed the string “Hello, world!” Now you know you can write Python, and that your Python environment works.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.