Raspberry Pi Home Automation with Arduino(Second Edition)
上QQ阅读APP看书,第一时间看更新

Arduino

Our first task is to install the Arduino IDE. The Arduino IDE is where we will write the code and upload it to the microcontroller.

Note

This piece of software can be found at http://arduino.cc/en/main/software.

Here, you will find the installation instructions for your operating system, including Linux, Windows, and Mac OS X. It is also possible to install the Arduino IDE directly on the Raspberry Pi. The following instructions will provide an overview of this process.

Installing the IDE on your Raspberry Pi

If you wish to install the IDE directly on your Raspberry Pi, you can do this via the command line. This will be necessary if you don't have a second computer on which you can install the Arduino IDE.

Once you have the Terminal window open, type the following command:

sudo apt-get install arduino

Accept any prompt that appears on the screen. The installation takes up about 81 MB of space.

Now that the installation is complete, you will be able to open the Arduino IDE on your Raspberry Pi and connect your microcontroller directly to the Raspberry Pi's USB drive.

A quick guide to the Arduino IDE

The Arduino IDE is a graphical user interface that allows you to develop Arduino code and then upload it directly to the microcontroller.

When you launch the program, you will be presented with a sketch. This is where you will write your Arduino code:

  1. You will find a number of options in the top menu. These include the examples that come bundled with the Arduino IDE, which can be found by navigating to File | Examples.
  2. Try selecting this option 0.1 Basics | BareMinimum.
  3. To upload an example to your microcontroller, ensure that it is plugged in. Next you will need to set the board. You can do this by navigating to Tools | Board. Here, you will find a list of the Arduino microcontrollers, and then you can select the model you have purchased, for example, Arduino Uno.
  4. Next, we need to select the USB port that the board is plugged into so that the IDE knows where to upload the sketch code.

    Tip

    If your Arduino IDE does not detect the USB port, you may need to run the application as root via sudo. StackExchange provides some suggestions on fixing this issue, at http://arduino.stackexchange.com/questions/739/arduino-program-only-works-when-run-as-root.

    The USB port list can be found by navigating to Tools | Serial Port.

    Once you have the serial port correctly configured, you can upload the code to the microcontroller.

  5. Click on the play button icon in the sketch. If everything was configured successfully, your code should now be running on the Arduino. The sketch we uploaded does not do anything. However, it is a good way of verifying that our setup is correct. In Chapter 3, Central Air and Heating Thermostat, we will dive into Arduino in more detail.

Next, let's quickly look at the Arduino to Raspberry Pi connection bridge shield.

Using the Arduino to Raspberry Pi connection bridge

The Arduino to Raspberry Pi connection bridge hardware provides a convenient method to connect our Raspberry Pi to some of the Arduino shields that are available. Thus, we can reuse hardware between our two devices.

The shield's I/O (Input/Output) options include:

  • A socket for wireless modules
  • RX/TX pins
  • i2C pins (SDA and SCL)
  • SPI pins (SCK, MISO, MOSI, and CS), which can also be used as GPIO.
  • An 8-channel analog to digital converter.
  • Switch to enable external power supply

You might remember seeing these in the diagram in the previous chapter.

Note

For more information on their functionality, you can go to the Cooking Hacks website, at http://www.cooking-hacks.com/documentation/tutorials/raspberry-pi-to-arduino-shields-connection-bridge.

In Chapter 6, Curtain Automation – Open and Close the Curtains Based on the Ambient Light, we will download the arduPi library in the Raspberry Pi. The arduPi library provides us with a software library that allows us to write Arduino-style C++ code in the Raspberry Pi that looks similar to Arduino sketches. This code can then control the bridge shield I/O pins and any Arduino shields or electronics connected to them.

There are several releases of the library available, depending on which model of the Raspberry Pi you purchased. You may be interested in checking out which version you have and making a note of this now.

Note

Cooking Hacks provides a guide about this connection bridge, at http://www.cooking-hacks.com/documentation/tutorials/raspberry-pi-to-arduino-shields-connection-bridge#step3.

This overview covers the last of our hardware components. We are now ready and can start using these devices. First, let's quickly recap what we covered.