Getting started with Arduino on the Raspberry Pi

Contents




Installing the Arduino IDE

Before you can start programming your Arduino, you need to setup a development environment on your Raspberry Pi system.


To do this, you simply need to install the Arduino package for Raspberry Pi.

Open the terminal, and run the command:

You will then be asked if you want to continue. Type Y (for yes) and then press enter.

Now, simply wait for the download and installation to complete. (This may take some time.)



If the download and installation process was carried out successfully, your terminal window should look as follows:

We have now completed the installation of the Arduino Development Environment for the Raspberry Pi.


In order to launch the Arduino IDE, simply go to the menu in the top left hand corner, click the sub-menu labelled Programming, and select the Arduino IDE application.

You are now ready to begin writing code for your Arduino board!




Your First Program

Now that you have installed the Arduino IDE, you can finally begin programming.


Before we begin looking at any code, here are a few useful pages on the Arduino website which give a bit more information about the Arduino platform. This guide will explain the basics, but these resources will help give you an even deeper understanding of the Arduino system.



Of all of the links above, the Arduino language reference is probably one of the most useful. Even if you aren't going to read any of the other pages, it is recommended that you at least read this one. This will help to give you a clear understanding of the Arduino programming language, which is based on C/C++, as well as an overview of all the Arduino functions which are used to interact with the board.


Another useful resource is Paul Campbell's website for his Dunedin Arduino project, the source of your Arduino programming kits:

http://www.moonbaseotago.com/dunedin-arduino/


When you first open the Arduino IDE, you should see a window that looks similar to this:

An Arduino program is called a sketch, and is created in this editor.


Before we can do anything, we need to setup the environment for developing on the Arduino Uno. Go to the Tools menu, then to the Board sub-menu. In this menu, select the Arduino Uno board.

We also need to select the serial port that the Arduino is connected to. Firstly, ensure that your Arduino device is connected to your Raspberry Pi via its USB cable. Once this is connected, go to the Tools menu, and then to the Serial Port sub-menu. Select the port labelled /dev/ttyUSB0

Now that everything is setup, we can start looking at the basic structure of an Arduino program. Every Arduino program requires two core functions: setup() and loop(). The setup function is called once when the program is started. This is when any initialisation code should be placed. The loop function is then called repeatedly afterwards. This is where the main code of the program should be placed.

Now that we have the basic structure in place, let's get started with your first program. This program will simply make an LED blink on and off.


Firstly, let's setup the pin that the LED will be connected to. We will define a constant called PIN_LED and assign it a value of 3. This means we will be connecting our LED to pin 3 on the Arduino. In the setup function, will set this pin to be an output pin by using the pinMode() function. If we were using a device we wanted to take input from, we would set the pin to be an input. This function takes two arguments: the pin number, and the mode (INPUT or OUTPUT).

Now that we have setup the pin to be an output, we can now start writing the main code of our program that will make the LED blink on and off.


If we want to turn the LED on or off, we can use the function digitalWrite(), which takes the arguments of a pin number, and a state: HIGH (on) or LOW (off).


We can also use the function delay() which takes an argument of time in milliseconds. This function causes the program to wait for the specified amount of time.


To make the LED continuously blink on and off, we will need to turn it on, wait a while, turn it off, wait again, and then repeat this process over and over. Using the loop() function and the two functions described above, we can easily accomplish this.

loop() is called repeatedly, so the LED will blink continuously, not just once.


Now that we have finished writing the code for our simple blink program, it would be a good idea to save our project. Go to the File menu and select Save As, and select a sensible location and name to save your work under.


The final step of this process is to now build the simple circuit for our blink program. You will need the following parts from your Arduino kit: 2 wires, a large blue resistor, an LED and the prototyping board. You should also disconnect your Arduino from the Raspberry Pi.

In a prototyping board, all of the holes in each row are connected to each other. This makes it very simple to connect components together. Each of the coloured lines in the image below show different rows of holes in the board that are connected.

The circuit for this blinking LED program is very simple. If you look back at the code we wrote earlier, you will see that we defined the LED pin number as 3. This means that we need to connect our LED to pin number 3 on the Arduino board. Using a wire, make a connection between pin 3 and one side of the prototyping board. On the other side of the board, one row to the side, so that the wires are not directly opposite each other, make a connection with one of the ground pins on the Arduino.


Your circuit should now look like this:

Next, connect a resistor directly across the middle of the prototying board. It should only be connected to one of the wires at this stage.

Finally, in the rows where the resistor and wire are not in line, you need to add the LED to connect them together. The direction in which the LED is inserted matters, but if you don't know anything about this, we can easily fix it later if it's not right. The completed circuit should now look similar to this:

Now that we have finished building the ciruit, the only thing we need to do is upload the program to the Arduino device. Plug the Arduino back into your Raspberry Pi and simply click the green arrow at the top of the IDE to upload the program.

If you have done everything corectly, the LED in your circuit should start blinking on and off, repeatedly. If this is not happening, try taking the LED out and turning it around.


In the end, you should end up with something like this:




Once you have the simple blink program working, try changing the delay, or perhaps adding more LEDs to the circuit.


Something which was not mentioned earlier, but is worth noting, is that there is a red button on the Arduino that can be used to reset your program. This will cause it to terminate and restart from the beginning.




Next Steps

Learn about using inputs for the Arduino such as the switches, thermistors and light sensors that are in your kit. Paul Campbell's introduction to these components can be found on his website: http://www.moonbaseotago.com/dunedin-arduino/inputs.html


Try making some programs that combine using the input devices you have learnt about and some LEDs as outputs. Make some traffic lights that cycle through the colors when a button is pressed. Make a temperature detector that turns on a red light when it's too hot. Make a circuit that turns on an LED when it is too dark, using the light sensor. If you can think of something you can make using the components you have, give it a go!


One other thing you can do using your Arduino is to use it to interact with the I2C sensor that you were given earlier. If you want to do this, let Matthew or Zhiyi know so that we can help you connect your sensor to the Arduino.


A library for I2C is provided for the Arduino system. Go to the Sketch menu, select Include Library and then click the library named Wire. The documentation for the Wire library can be found at: https://www.arduino.cc/en/Reference/Wire. See if you can figure out how to use it yourself. It may be useful to reference the code from your Raspberry Pi IOT project. Perhaps you could do something with the data from your I2C sensor and the components in your Arduino kit. This one might get a little tricky, so don't be afraid to ask for help!