Programming
ARDUINO
With Python for Robots
(2020 Edition)
A Beginner to Advanced Reference Guide to Arduino Microcontroller Processing and Robotics
Ted Humphrey
Copyright
Copyright2020 Ted Humphrey
All rights reserved. No part of this book may be reproduced or used in any manner without the prior written permission of the copyright owner, except for the use of brief quotations in a book review.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Printed on acid-free paper.
Table of Contents
CHAPTER ONE
CREATING THE PROGRAMMING ENVIRONMENT FOR PYTHON AND ARDUINO
This chapter is essential if you are a beginner who doesnt understand much about python and creating an enabling environment for Arduino on your computer. It doesnt matter which category of operating system you are running on your computer as this chapter will take you through the python and Arduino programming environment.
Getting Started with Python
In 1991, Guido Van Rossum brought the python programming language which was at the time a basic language of instruction for learners and students of programming. Today, the python programming language is among the most used and user-friendly programming language. The programming language works on all platforms, such as Windows (all versions of Windows), Linux OS, and Apple OS. The python programming language, recently, has gone through some minor modifications when newer versions have been developed to replace the older versions. The switching between python v2 to python v3 was met with huge applause. One main difference between python v2 and the newer versions (python v3) is that the v3 can handle both English and Non-English characters while the v2 can only work with English characters. Also, the python v3 is a bit more compatible and smaller than the v2. Nonetheless, the python v2 is still being used to teach the basis of python programming today.
Installing python on your computer
As it has been said above, the python programming language has two versions which are the v2 and the v3 versions. Most operating systems, such as Linux distributions and Macs OS, have their default python which has been pre-installed on them. The python V2 is the default python on most operating systems. The python 2.7 is the last version of the v2.x series (x here represents unknown v2 series) while the python 3.8.5 is the latest version of the V 3.8 series as at the time of writing this guide. The python 3.8 will be used as a guide in this book because it is user-friendly and it is easy to understand the basics. The instructions in this guide will work on all versions of the python 2.x and the 3.8 series. The pythons installation procedures for three of the most common operating systems will be discussed.
Linux distributions: Installing python on Linux distributions (such as Ubuntu and Fedora) is quite easy unlike the Windows operating systems. This is because the Linux distributions come default with python that has already been installed. Your job will be to check, using some sets of basic instructions, if you are running the right version of python. If you want to check the version of python that has been pre-installed on your computer, enter the command line $ python V . If you execute the command line properly, you will get a window displaying the version of the python you are using. Endeavor to use a capital letter V in the command prompt. If the version on your Linux OS is the latest version, that is good. You are on the path to getting started with python. However, if the version on your computer is below the version 3.8, you will have to upgrade the version to the V3.8 series.
If you are using the Ubuntu operating system, you should have a pre-installed python latest version. If you dont have the python latest installed, you can upgrade to the latest version by upgrading with the command line: $ sudo apt-get update && sudo apt-get --only-upgrade install python.
Installing python on Windows
Installing your python on the Window OS is not as simple as the other operating systems. The first thing is to go to http://www.python.org/getit to download the python version 3.8. which is the latest version of python To get it right, navigate to the system property of your computer to confirm the type of operating system you are running. You will be able to see whether you are running the 32bits OS or the 64bits OS. Once the python file has been downloaded successfully, you can then run the file for full installation. As soon as the installation is complete, you can explore the python command line tool and IDLE from the start menu. If you want to make the python accessible from the Windows command prompt, you need to set the path in the environment variable . The path is the most used environment variable in Windows is the path. Follow these steps to add the python path for easy execution;
- Scroll to the bottom pane of your Window interface, and search Advanced system settings in the type here to search box.
- The system properties window will be prompted which is usually open to the advanced tab by default. Scroll to the bottom and select environmental variables.
- From the environmental variable window, you will get two sets of variables: the system variable and the user variable. Both of the variables have the path variable and you can decide to edit any of the paths (either from user variable or the system variable). You can choose to edit the path for the user variable section if you are planning to be using the command for only your user account on the computer. You will choose the system variable if you need the change to be effective across all users sharing the computer with you.
- Tap on path and then scroll to the right section to choose edit .
- Add a new path by simply tapping on New and a new line will be added at the bottom of the list where you can either type or paste the path of the python that you downloaded. You can also choose the browse option which will enable you to browse your files and copy the program file.
- Tapping on the PATH option as shown in the screenshot above will bring an Edit System Variable window. Add C:\Python27 or the full path of the custom Python installation directory at the end of the existing PATH variable. You should put a semicolon (;) right before the installation path.
Installing python on Mac OS: The Mac OS comes preinstalled with a copy of python, only that the rate of updates for the default version is not really fast. The version of Mac OS called the 10.9 Maverick comes default with python 2.7.5.
Installing python Setup tools
The python Setup tools is a library featuring an assemblage of utilities that users can use for building and distributing Python packages. The most used tool from this assemblage is called easy_install . It enables you to access the PyPI, the de facto python repository that features the highest number of Python packages is the PyPI ( http://pypi.python.org ). The PyPI also gives easy ways to install as many packages as possible on your operating system. With the PyPI, you will be able to install any package by name on your python. The easy_install utility will be able to automatically download, build, install, and manage your packages for you as a user. Many of the packages that will be used for the python and the Arduino project in this book will be installed by the easy_install. It is necessary to state here that the python experts all over the world have been, of recent, using another tool called pip to replace the easy_install utility. This is because the easy_install utility fails to carry out some basic tasks such as support for uninstalling packages, tracking actions and support for other versions control systems. The easy_install and pip utility use the same python repository; they can both be used to install any python package of your choice.