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

Getting started with the Arduino IDE


The Arduino IDE is a cross-platform application developed in Java that can be used to develop, compile, and upload programs to the Arduino board. On launching the Arduino IDE, you will find an interface similar to the one displayed in the following screenshot. The IDE contains a text editor for coding, a menu bar to access the IDE components, a toolbar to easily access the most common functions, and a text console to check the compiler outputs. A status bar at the bottom shows the selected Arduino board and the port name that it is connected to, as shown here:

What is an Arduino sketch?

An Arduino program that is developed using the IDE is called a sketch. Sketches are coded in Arduino language, which is based on a custom version of C/C++. Once you are done with writing the code in the built-in text editor, you can save it using the.ino extension. When you save these sketch files, the IDE automatically creates a folder to store them. If you are using any other supporting files for a sketch, such as header files or library files, they are all stored at this location (which is also called a sketchbook).

To open a new sketchbook, open the Arduino IDE and select New from the File menu, as shown in the following screenshot:

You will be prompted with an empty text editor. The text editor supports standard features (that is, copy/paste, select, find/replace, and so on). Before we go ahead with an Arduino program, let's explore the other tools provided by the IDE.

Note

The Arduino IDE version prior to 1.0 used the .pde extension to save sketchbooks. Starting from 1.0, they are saved with the .ino extension. You can still open files with the .pde extension in the latest IDE. Later, the IDE will convert it to the .ino extension when you save them.

Working with libraries

The Arduino IDE uses libraries to extend the functionalities of existing sketches. Libraries are a set of functions combined to perform tasks around a specific component or concept. The majority of the built-in Arduino libraries provide methods to start working with external hardware components. You can import any library by navigating to Sketch | Import Library…, as shown in the following screenshot:

You can also use a library for your sketch by just specifying the library with the #include statement at the beginning of the sketch, that is, #include <Wire.h>.

The Arduino IDE also provides the capability to add an external library that supports a specific hardware or provides additional features. In the upcoming chapters, we will be dealing with some of these external libraries, and we will go through the process of importing them at that time.

You can learn more about built-in Arduino libraries from http://arduino.cc/en/Reference/Libraries.

Using Arduino examples

The Arduino IDE contains a large number of built-in example sketches. These examples are designed to get the user familiar with basic Arduino concepts and built-in Arduino libraries. The examples are well maintained by the Arduino community since they have comprehensive support for each example through the Arduino website (http://arduino.cc/en/Tutorial/HomePage). In the Arduino IDE, you can access these examples by navigating to File | Examples, as shown in the following screenshot:

Let's start with a simple in-built example. Open the Blink example by navigating to File | Examples | 01.Basics | Blink. The IDE will open a new window containing code that is similar to the code in the following program:

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
 */

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

This Arduino sketch is designed to blink an LED on digital pin 13. You must be wondering why we didn't discuss or ask you to bring any hardware. That's because the Arduino Uno board is equipped with an on-board LED that is connected to digital pin 13. Now, instead of diving deeper into the Arduino code, we are going to focus on the process of dealing with the Arduino board through the IDE.

Compiling and uploading sketches

Once you have your code opened in the IDE, the first thing you need to do is to select the type of Arduino board on which you are going to upload your sketch. The Arduino IDE needs to know the type of board in order to compile the program for the appropriate microcontroller, as different Arduino boards can have different Atmel microcontrollers. Therefore, you need to perform this step before you go ahead with the compiling or uploading of the program to the board.

You can select the Arduino board by navigating to Tools | Board, as displayed in the following screenshot:

Select Arduino Uno from the list of boards, unless you are using a different Arduino board. Once you have selected the board, you can go ahead and compile the sketch. You can compile the sketch by navigating to Sketch | Verify / Compile from the menu bar or by using the keyboard shortcut Ctrl + R. If everything is set up well, you should be able to compile the code without any error.

After successfully compiling the sketch, it is time to upload the compiled code to the Arduino board. To do this, you need to make sure that your Arduino IDE is properly connected to your computer. If it is not already connected, connect your Arduino board to your computer using a USB port. Now, it is time to let your IDE know the serial port on which the board is connected. Navigate to Tools | Serial Ports and select the appropriate serial port.

Note

In the case of some Linux distributions, you may not be able to see or upload the Arduino program to the board due to permission restriction(s) on the serial port. Running the following command on the terminal should solve that problem:

$ sudo usermod -a -G uucp, dialout, lock <username>

You can now upload the compiled sketch to your Arduino board by navigating to File | Upload. This process will use the serial connection to burn the compiled firmware in the microcontroller. Please wait for some time or until the LEDs (Tx and Rx LEDs) on the board stop flashing. Now, you have your Arduino board ready with your first sketch. You can observe the performance of the blinking LED near digital pin 13.

Using the Serial Monitor window

In the previous process, we used a Universal Serial Bus (USB) cable to connect your Arduino board to a USB port of your computer. The USB port is an industrial standard to provide an interface for connecting various electronic components to a computer using the serial interface. When you connect an Arduino board using USB, the computer actually interfaces it as a serial peripheral device. Throughout the book, we are going to refer to the connections made using a USB as serial connections. The Serial Monitor window is a built-in utility of the Arduino IDE. The Serial Monitor window can be accessed by navigating to Tools | Serial Monitor or by using the Ctrl + Shift + M keyboard shortcut. It can be configured to observe data that is being sent or received on the serial port that is used to connect the Arduino board to the computer. You can also set the baud rate for the serial communication using the drop-down menu option. This utility is going to be very useful (further on in the book) when testing your prototypes and their performances.