Book Image

Python Programming for Arduino

Book Image

Python Programming for Arduino

Overview of this book

Table of Contents (18 chapters)
Python Programming for Arduino
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Introduction to Arduino


Any electronic product that needs computation or interfacing with other computers first requires a quick prototyping of the concept using simple tools. Arduino is an open source hardware prototyping platform designed around a popular microcontroller family, and it includes a simple software development environment. Besides prototyping, you can also use Arduino for the development of your own do-it-yourself (DIY) projects. Arduino bridges the computational world with the physical world by letting you simply connect the sensors and actuators with a computer. Basically, you can write code to monitor and control various electronic components in your daily life by using Arduino's input/output pins and microcontroller. Examples of these components include motors, thermostats, lights, switches, and many more.

History

In 2005, Massimo Banzi, the Italian cofounder of Arduino, developed the technology for his students at Interaction Design Institute Ivrea (IDII). Since then, Arduino has developed into one of the largest open source hardware platforms. All software components and schematics of the Arduino design are open source, and you can buy the hardware at a very low cost—approximately 30 dollars—or you can even make it yourself.

Why Arduino?

The major goal of the Arduino community is to continuously improve the Arduino platform with the following objectives in mind:

  • The Arduino platform should be an affordable platform

  • It should be easy to use and easy to code

  • It should be an open source and extensible software platform

  • It should be an open source and extensible hardware platform

  • It should have community-supported DIY projects

These simple but powerful objectives have made Arduino a popular and widely used prototyping platform. Arduino uses Atmel's ATmega series of microcontrollers that are based on the popular hardware architecture of AVR. The huge support that is available for AVR architecture also makes Arduino a hardware platform of choice. The following image shows the basic version of the Arduino board, which is called Arduino Uno (Uno means one in Italian):

Arduino variants

Like any other project, hardware requirements are driven by project specifications. If you are developing a project that requires you to interface with a large number of external components, you need a prototyping platform that has a sufficient number of input/output (I/O) pins for interfacing. If you are working on a project that needs to perform a huge amount of complex calculations, you require a platform with more computation capability.

Fortunately, the Arduino board exists in 16 different official versions, and each version of Arduino differs from the others in terms of form factor, computational power, I/O pins, and other on-board features. Arduino Uno is the basic and most popular version, which is sufficient enough for simple DIY projects. For the majority of exercises in this book, we will be using the Arduino Uno board. You can also use another popular variant called Arduino Mega, which is a larger board with extra pins and a powerful microcontroller. The following table shows the comparison of some of the more popular and active variants of the Arduino board:

Name

Processor

Processor frequency

Digital I/O

Digital I/O with PWM

Analog I/O

Arduino Uno

ATmega328

16 MHz

14

6

6

Arduino Leonardo

ATmega32u4

16 MHz

14

6

12

Arduino Mega

ATmega2560

16 MHz

54

14

16

Arduino Nano

ATmega328

16 MHz

14

6

8

Arduino Due

AT91SAM3X8E

84 MHz

54

12

12

LilyPad Arduino

ATmega168v or ATmega328v

8 MHz

14

6

6

Any of these variants can be programmed using a common integrated development environment called Arduino IDE, which is described in the upcoming section. You can select any one of these Arduino boards according to your project requirements, and the Arduino IDE should be able to compile and download the program to the board.

The Arduino Uno board

As Uno is going to be the de facto board for the majority of the projects in this book, let's get ourselves familiar with the board. The latest revision of the Uno board is based on Atmel's ATmega328 microcontroller. The board extends the I/O pins of the microcontroller to the peripheral, which can then be utilized to interface components using wires. The board has a total of 20 pins to interface, out of which 14 are digital I/O pins and 6 are analog input pins. From the 14 digital I/O pins, 6 pins also support pulse-width modulation (PWM), which supports the controlled delivery of power to connected components.

The board operates on 5V. The maximum current rating of the digital I/O pins is 40 mA, which is sufficient to drive most of the DIY electronic components, excluding motors with high current requirements.

While the previous image provided an overview of the Uno board, the following diagram describes the pins on the Uno board. As you can see, the digital pins are located on one side of the board while the analog pins are on the opposite side. The board also has a couple of power pins that can be used to provide 5V and 3.3V of power to external components. The board contains ground pins on both sides of the board as well. We will be extensively using 5V of power and ground pins for our projects. Digital pins D0 and D1 support serial interfacing through the Tx (transmission) and Rx (receiver) interfaces respectively. The USB port on the board can be used to connect Arduino with a computer.

Now that we are familiar with the Arduino hardware, let's move on to programming the Arduino board.

Installing the Arduino IDE

The first step to start getting familiar with Arduino is to install the Arduino integrated development environment (IDE). According to the operating system that you selected at the beginning of the Python installation section, follow the appropriate subsection to install the correct IDE.

Linux

The installation of the Arduino IDE is really simple in Ubuntu. The Ubuntu repository already includes the Arduino IDE with the required dependencies.

For Ubuntu 12.04 or a newer version, execute the following command in the terminal to install Arduino:

$ sudo apt-get update && sudo apt-get install arduino arduino-core

The latest version of the Arduino IDE in the Ubuntu repository is 1.0.3. You can obtain more information regarding other Ubuntu-related questions at http://playground.arduino.cc/Linux/Ubuntu.

For Fedora 17 or a newer version of Red Hat Linux, execute the following script in the terminal:

$ sudo yum install arduino

Answers to additional installation questions for Fedora can be obtained at http://playground.arduino.cc/Linux/Fedora.

Mac OS X

To install the Arduino IDE on Mac OS X (10.7 or newer), perform the following steps:

  1. From http://arduino.cc/en/Main/Software, download the latest version of the Arduino IDE for Mac OS X, which was 1.0.5 when this book was being written.

  2. Unzip and drag Arduino to the application folder.

The Arduino IDE is built in Java and requires that your computer is equipped with the appropriate version of Java. Open the IDE from your applications. If you don't have Java installed on your Mac, the program will prompt you with a pop-up window and ask you to install Java SE 6 runtime. Go ahead and install Java (as per the request) as the OS X will automatically install it for you.

Windows

Installation of Arduino for Windows is very simple. Download the setup file from http://arduino.cc/en/Main/Software. Select the most recent version of the Arduino IDE, that is, 1.0.x or a newer version.

Make sure you download the appropriate version of the Arduino IDE according to your operating system, that is, 32 bit or 64 bit. Install the IDE to the default location as specified in the installation wizard. Once installed, you can open the IDE by navigating to Start | Programs.