Program an Arduino UNO with your Raspberry Pi

By Russell Barnes. Posted

In this feature we look at to connect an Arduino UNO to a Raspberry Pi and program Arduino IDE in Raspbian. We’re using an Arduino UNO with a Raspberry Pi 3 for this guide, but the steps are similar for all models.

Get a free Pi Zero W, Official Case, and Accessories with your 12-month subscription to The MagPi. Click here for more info.

Install Arduino IDE on your Raspberry Pi

The first step in programming an Arduino board with a Raspberry Pi is to install the Arduino IDE (integrated development environment) on your Raspberry Pi. This program checks code and loads it onto the Arduino. Install the latest version of Arduino IDE using apt:

sudo apt-get update && sudo apt-get upgrade
sudo apt-get install arduino

Alternatively, open Chrome on your Raspberry Pi, head to magpi.cc/2tPw8ht, and click the Linux ARM link under ‘Download the IDE’. Extract the file to your /opt directory , then open a Terminal and run the install.sh script to install.

cd Downloads/
tar -xf arduino-1.8.3-linuxarm.tar.xz
sudo mv arduino-1.8.3 /opt
sudo /opt/arduino-1.8.3/install.sh

You will find Arduino IDE under Menu > Programming. Open the app to start programming your Arduino board.

Program Arduino IDE on a Raspberry Pi

Arduino programs are called ‘sketches’, and are based on the C programming language. Open the IDE and you’ll see a blank sketch, with the two basic areas for code: void setup() and void loop(). If you see an empty script, choose File > Examples > 01.Basics > BareMinimum.

As the comments explain, void setup() is for code that runs once to set up your electronics properly. The code you type into void loop() will repeat from the first line to the last in an endless loop.

To demonstrate, we’ve written a basic sketch that flashes two LEDs when you move your hand over a PIR sensor. Connect the two LEDs and PIR sensor to the Arduino, as shown in the circuit diagram.

Once you’ve wired up the Arduino to the circuit, attach it to one of the Pi’s USB ports. This provides power to the Arduino, as well as a data connection between your Raspberry Pi and the Arduino board. Do not use the Arduino’s barrel-jack power input – a Pi 2 or 3 can supply enough power for an Arduino board over USB.

Blink a PIR with Arduino IDE

Enter the code from PIRBlink.ino and save it as PIRBlink using File > Save. It will be saved inside the Arduino directory in your home directory.

/*
 * Blinks two LEDs when motion detected
 */

//variables
int led1 = 5;
int led2 = 6;
int motion = 3;
int wait = 500; //500ms = 1/2s

void setup() {
  // put your setup code here, to run once:
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(motion,INPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
  if (digitalRead(motion) == HIGH){
    digitalWrite(led1,HIGH);
    digitalWrite(led2,HIGH);
    delay(wait);
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    delay(wait);
    }
}

We use the pinMode instruction to tell the Arduino which GPIO pins the LEDs are attached to (pins 5 and 6) and that each pin should be treated as an output.

In the void loop() section, we tell the Arduino to raise the voltage on these pins to HIGH (5 V) with the digitalWrite instruction, then to pause for half a second, and then to turn off the LEDs.

Before uploading your sketch to the Arduino, tell the IDE which Arduino board you’re using by opening Tools > Board. Then tell the IDE which port to use to upload your code by opening Tools > Serial Port and selecting /dev/ttyACM0. On version 1.8.3 of the IDE, you need to select the version marked ‘(Arduino/Genuino Uno)’.

Now click Upload to program the Arduino with your sketch. After a few seconds of code-checking and uploading, you should see your LEDs flashing whenever you move your hand over the sensor.

Stop the program

Unlike a Raspberry Pi, the Arduino will keep running the same program, even if you unplug or reset the board. The easiest way to stop it running is to open a new blank program and upload it to the board.

Arduino IDE etiquette

While not necessary, it’s also good Arduino sketch etiquette to include a description at the start.
At the top of your sketch, type /* and hit ENTER: the IDE will add two more lines. Anything written between the /* and */ will be ignored by the Arduino, but can be read by any humans wanting to understand what your sketch does and how it works.
We’ve used variables for our LEDs, meaning that if we change the GPIO pins they’re attached to, we’ll have fewer edits to make.

We tell the Arduino IDE the type of variable, then the name, and the value. So our LED pins are int led1=5;. By replacing all instances of 5 with led1, any changes to our build require only one change to our code.

Add motion

We’ve also added a PIR (passive infrared) motion sensor, as shown in the circuit diagram (Figure 1). We therefore declared the variable int motion = 3 and added this line to void setup():

pinMode(motion, INPUT);

Finally, we added some code to make the motion sensor trigger the LED flashes:

if (digitalRead (motion) == HIGH) {

As with the if statement, this is part of the void loop() section that will run continuously, meaning the motion sensor will be read – or polled – repeatedly for signs of motion.
A final note on coding with Arduino: every line of code must end with either a semicolon or a curly bracket. The IDE will highlight any line of code that suffers an error, and typically it’ll be because the previous line lacks its semicolon terminator.

If you are unfamiliar with the C programming language, there are lots of resources you can find online to learn how to use it.

Learn to Code with C

Arduino uses the C programming language. To get up to speed with C, read our Essentials Guide: Learn to Code with C.

 Learn to code with C in our Essentials book!

From The MagPi store

Subscribe

Subscribe to the newsletter

Get every issue delivered directly to your inbox and keep up to date with the latest news, offers, events, and more.