- Python Programming for Arduino
- Pratik Desai
- 927字
- 2025-04-04 21:08:18
Connecting the Arduino board
As mentioned in the previous chapter, this book supports all major operating systems, and this section will provide you with steps to connect and configure the Arduino board for these operating systems. In the previous chapter, we utilized example code to get started with the Arduino IDE. If you were unable to successfully communicate with Arduino by following the information given in the previous chapter, follow the instructions provided in this section to establish a connection between your computer and your Arduino. First, connect your Arduino board to your computer's USB port using a USB cable and follow the steps according to your operating system.
Linux
If you are using the latest version of Ubuntu Linux, once you connect the Arduino board and open the Arduino IDE, you will be asked to add your username to the dailout group, as displayed in the following screenshot. Click on the Add button and log out from the system. You don't need to restart the computer for the changes to take effect. Log in with the same username and open the Arduino IDE.

If you don't see this dialog box, check whether you can see the Serial Port option in the Tools menu of the Arduino IDE. It is possible that the installation of other programs might have added your username to the dailout group already. If you don't get the dialog box and don't have any options to select in Serial Port, execute the following script in the terminal, where <username>
is your Linux username:
$ sudo usermod -a -G dialout <username>
This script will add your username to the dialout group, and it should also work for other Linux versions. In Linux, the Arduino board mostly gets connected as /dev/ttyACMx
, where x
is the integer value and depends on your physical port address. If you are using any other distribution of Linux other than Ubuntu, you might want to check out the proper groups associated with the Arduino serial port from the Linux installation page (http://playground.arduino.cc/Learning/Linux) of the Arduino website.
Mac OS X
In Mac OS X, when you connect your Arduino through a serial port, the OS configures it as a network interface. In OS X Mavericks, once the Arduino board is connected, open Network from System Preferences. A dialog box should appear that states that a new network interface has been detected. Click on OK for Thunderbolt Bridge and then click on Apply. The following screenshot displays the dialog box to add a new network interface:

For OS X Lion or later versions, on connecting the Arduino board, a dialog box will appear that will ask you to add a new network interface. In this case, you will not have to navigate to your network preferences. If you see the network interface with the status Not connected and highlighted in red, don't worry about it as it should work just fine.
Open the Arduino IDE and navigate to Serial Port from the Tools menu. You should be able to see options similar to those displayed in the following screenshot. The serial port on which the Arduino board is connected might vary according to your OS X version and the physical port to which it is connected. Make sure that you select a tty
interface for a USB modem. As displayed in the following screenshot, the Arduino board is connected to the serial port /dev/tty.usbmodemfd121
:

Windows
The configuration of the Arduino serial port is very straightforward if you are using Windows. When you connect your Arduino board the very first time, the operating system will automatically install the necessary drivers by itself. Once this process is complete, select an appropriate COM port from the Serial Port option in the menu bar. From the main menu, navigate to Tools | Serial Port and select the COM port.
Troubleshooting
Even after following the steps mentioned earlier, if you still don't see the highlighted Serial Port option as displayed in the following screenshot, then you have got a problem. There can be two main reasons for this: the serial port is being used by another program or the Arduino USB drivers are not installed properly.
If any program other than the Arduino IDE is using the specific serial port, terminate that program and restart the Arduino IDE. Sometimes in Linux, the brltty
library conflicts with the Arduino serial interface. Remove this library, log out, and log back in:
$ sudo apt-get remove brltty
In Windows, reinstalling the Arduino IDE also works, as this process installs and configures the Arduino USB driver again.

Tip
The Arduino board can be used by only one program at a time. It is very import to make sure that any previously used program or other services are not using the serial port or Arduino when you try to use the Arduino IDE. This check will become very important when we start using multiple programs to control Arduino in the next section.
Assuming that you can now select the serial port in the Arduino IDE, we can go ahead with compiling and uploading sketches to your Arduino board. The Arduino IDE ships with preinstalled example sketches with which you can play around. However, before we go ahead and start playing with complex examples, let's go through the next section, which explains the Firmata protocol and also guides you through step-by-step instructions to compile and upload a sketch.