Programming the Arduino – Basics / Part 1

Since we post up lots of Arduino related content on the website, it only makes sense that we have some sort of tutorial series to follow. This will start from the basics to the more advanced subjects, hopefully expanding your knowledge of the Arduino.

The Requirements

  1. 1x Arduino Compatible Board (Uno, Duemilanove, Leonardo, Mega etc.)
  2. Windows Environment (We’ll be using Win 7 / 8)
  3. Knowledge of basic programming (e.g. knowing what a variable, function etc. is)

All of the program code will be compatible with all versions of the Arduino; all code will run within the Uno specification, as that is what we will be using. The tutorial series will include the use of some switches, LEDs, sensors etc. which will be mentioned in either the title of the tutorial or in the requirements section.

What Part 1 will be covering

  1. Installation and Setup of the Arduino IDE.
  2. Uploading and Running your first Arduino Sketch.
  3. Explanation of basic programming functions.

Let’s get started with Part 1.

The Arduino programming language is based on Wiring, this is an open-source programming language for microcontrollers. You can write C/C++ for the Arduino, however you only need two functions defined for the program to run – It is programmed using the Arduino IDE (which is based on Processing)

The Arduino IDE is a free development tool for writing Arduino code; this will allow you to write code and upload it directly to a USB connected Arduino device. Head over to http://arduino.cc/en/Main/Software  and download the latest stable version (at the time of writing this, it is 1.0.5)

We would recommend downloading the Windows Installer, which will give you start menu short-cuts and an un-installation option if you wish to remove it at a later date.

After a short installation wizard, you’ll have Arduino short-cuts in your Start Menu and Desktop. Load up the Arduino IDE by clicking on one of these icons.

arduinoide

This is the Arduino IDE. There isn’t much help from here other that writing in the large white box. Luckily, the Arduino is packed full of example code which is tucked away under the file menu.

The first thing we would recommend you look at is the Examples > Basics and BareMinimum example.

The code in this example is exactly what it says, it’s the bare minimum that will compile and run on an Arduino, it is made up of two functions a setup() section and a loop() section, none of these take any arguments and are a requirement for running your Arduino program.

 

The setup routine is what’s called at the start of the Arduino code; it gets executed before anything else and only gets executed once (on it’s own, you could call it again if you wish) this makes it a perfect place for initialising things for the first time, hence why it’s called setup.

The loop routine lives up to it’s name; it repeats over and over in a never-ending loop. This is the main program cycle and your code should be entered here.

What does the Arduino look like?

There is a series of high-quality PDFs that a user on the Arduino forums created. These used to be available at pighixxx.com but it has been down for quite a while. We have uploaded the relevant PDF below, but we will go back to linking to the site once it is back up.

pinout

 Click here to download the above image as PDF

Basic Arduino Functions

We’ll be covering the following functions in this tutorial:

  • digitalWrite(pin) – Write to a digital pin.
  • digitalRead(pin) – Read a value from a Digital pin.
  • delay(ms) – Delay the program by x milliseconds.
  • pinMode(pin) – Set pins as either INPUT’s or OUTPUT’s
  • Serial.println() – Print a line

You’ll be needing to interface with the pins on the Arduino to make anything useful; this is done with a couple of functions. The following is a description of digitalWrite() from the Arduino.cc website:

Write a HIGH or a LOW value to a digital pin.

If the pin has been configured as an OUTPUT with pinMode(), its voltage will be set to the corresponding value: 5V (or 3.3V on 3.3V boards) for HIGH, 0V (ground) for LOW.

If the pin is configured as an INPUT, digitalWrite() will enable (HIGH) or disable (LOW) the internal pullup on the input pin. It is recommended to set the pinMode() to INPUT_PULLUP to enable the internal pull-up resistor. See the digital pins tutorial for more information.

NOTE: If you do not set the pinMode() to OUTPUT, and connect an LED to a pin, when calling digitalWrite(HIGH), the LED may appear dim. Without explicitly setting pinMode(), digitalWrite() will have enabled the internal pull-up resistor, which acts like a large current-limiting resistor.

The first thing you have to do is set a pinMode on a selected pin; for example if you are flashing an LED you would want the pin mode to be an output (pinMode(1, OUTPUT))

You can then use the digitalWrite function to write a HIGH or LOW value to the pin – This is essentially a 1 or 0, On or Off value. Because you’ll be doing this inside of the loop() function this will get repeated constantly and have the effect of the LED staying on.

What you need to do is turn the pin to HIGH, delay by [x] milliseconds (let’s use 1000ms for 1 second) and then turn the pin back to LOW, then insert another delay(1000). This would show a flashing LED that turns on/off every second. The code would look like the following:

Also take note that we are using Pin 13 without anything connected to it; this is using the on-board LED on the Arduino.

This is very similar to the Examples > Basics > Blink example, except it isn’t using a variable for the LED pin number. Using inputs with digitalRead is used in the same manner; you can use digitalRead to either return a HIGH or LOW of a selected pin number; for example:

The above line would read the status of pin 5 and insert a HIGH or LOW value into the value integer variable, you can then program accordingly based on this value. Example uses of using digitalRead would be reading switches and detecting when a button is pressed.

The only other function we will be mentioning in this first tutorial is the Serial.println() – Description from Arduino.cc is the following:

Prints data to the serial port as human-readable ASCII text followed by a carriage return character (ASCII 13, or ‘\r’) and a newline character (ASCII 10, or ‘\n’). This command takes the same forms as Serial.print().

This is extremely handy for debugging purposes; this will allow us to print variables, input from switches and other information directly to the serial monitor. To access the Serial Monitor, it is under the Tools menu on the Arduino IDE.

Anywhere in the code, you can print to the Serial Monitor as follows:

Everytime you go through the loop to this Serial.println statement it will appear in the Serial Monitor – You can also interface the serial data other ways, such as Processing, PHP or .NET languages (more on this later)

That about covers the basics of this tutorial; by now you should have learned how to load up the Arduino development environment and write a basic program, as well as interact with pins on the Arduino. It is worth looking at the language reference page: http://arduino.cc/en/Reference/HomePage

Stay tuned for Part 2 of this series.

 

Email this to someoneShare on Google+Pin on PinterestTweet about this on TwitterShare on RedditShare on Facebook

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">