Page 1 of 1

Arduino Part II: Programming

#1 erik.price   User is offline

  • D.I.C Lover
  • member icon

Reputation: 486
  • View blog
  • Posts: 2,690
  • Joined: 18-December 08

Posted 28 November 2009 - 11:48 AM

Alright everyone, welcome back to the world of Arduino! The last tutorial was just downloading and installing everything, and a little bit of background knowledge of what Arduino is. So, don't worry if you missed the first bit.

For the purposes of this tutorial, my only assumptions are that you have the software installed, and you have some form of Arduino (although you can still go through this tutorial without actually testing anything out).


Arduino uses a special dialect of AVR C (which itself is just stripped down C with some special functions and constants), which doesn't really have an official name, besides "The Arduino Language"
*If you know C or Java, most of this tutorial will be basic for you, and you may want to consider checking out the official reference

The Basics
Every Arduino program has two functions that must be used for the program to compile: setup() and loop().

What they do is pretty self-explanatory, setup() is called once, at the beginning of the program's execution, and initiates variables and sets up external things like LEDs (don't worry, we'll get to this)

The next section, loop() repeats infinitely until the power supply is removed. Here is where you would want to put everything that your Arduino will need to do (detect light, move a motor, read a value, etc.)

I'm just trying to do a crash course in C here, not the entirety of the language, if you have no programming experience whatsoever, I recommend reading a good tutorial before continuing.

I'm going to give you a quick little example of an Arduino program. Don't worry if you don't understand everything just yet.

Let's say you type this into the Arduino editor and hit "Upload to Board":
int led = 13;
void setup () {
	//this sets up pin 13 as an output
	pinMode (led, OUTPUT);
void loop () {
	//this continuously turns the LED on
	digitalWrite (led, HIGH);

Arduino then takes your code, and translates it into true AVR C, which is put into a temporary file, and then compiled:
#include "WProgram.h"

int led = 13;
void setup () {
	pinMode (led, OUTPUT);
void loop () {
	digitalWrite (led, HIGH);  // turn on the LED
int main(void)
	init();	//defined in WProgram.h  
	setup(); //call setup() one time 
	for(;;)	//infinite loop
	return 0;

Easy stuff, right? Well, if it's not, I'll try and clear it all up now.

Let's dissect this program line by line, starting off with this:
int led = 13;
There are three parts to this statement:
  • int led declares the type and name of the variable we want to create
  • = this is an assignment operator, it assigns the value of the expression to its right to the variable on its left, in this case, led is set to 13
  • ; end of statement marker. With the exception of conditionals, loops, and functions, the semicolon goes at the end of every line (since whitespace is just ignored)
void setup () {/*code here*/
This is a function, Java and lots of other languages call it a method, but either way, it is the same thing.
  • void this is the return type. It specifies what the function will return back when it finishes. Since this is just initializing variables, it has no return type (it returns void, the lack of a return)
  • setup() this is the name of the function. From here on in the code, anywhere setup() is used, this function will be called
  • {/*code*/} everything between the brackets is the body of the function, which is the code to be executed when the function is called. /*code*/ is a comment. Everything within /* and */ will be ignored. You could also do line comments, like so //comment!

If you still aren't grasping this completely (and I don't blame you, I didn't go very deeply in-depth) feel free to leave a comment or PM me, and I'll try to point you in the right direction :)

Commonly Used Functions

Arduino provides a lot of built in functions to do a wide variety of things. I'll just cover the ones I feel are the most important here. (You can find the full list here)

pinMode(pin, mode)
This function is used to set up a pin for either output or input. 'pin' is the pin number attached, and mode is either OUTPUT or INPUT (both must be uppercase) which, not surprisingly, mean the pin is used for output or input, respectively.

A function which pauses the execution of the program for ms milliseconds. This can be useful if you are blinking a LED, because an LED that has no delay in between blinks just looks like it's turned on.

digitalWrite(pin, value)
Writes to a pin that has been setup for output a value of either HIGH or LOW, which mean, in essence, On or Off.

Reads the pin at 'pin' and returns an integer, either HIGH, or LOW, representing, like with digitalWrite(), On or Off

analogWrite(pin, value)
Writes value (between 0-255) to pin. 0 being off, and 255 being on. Everything in between is like using a dimmer switch compared to a regular light switch. (Not exactly, if you wanted to get technical about it, check out how PWM waves work ;))

Returns a value from pin in the ranges of 0-1023. Something to think about when using this, if you have a sensor which returns 0-1023, and want to change that to 0-255 (analogWrite, possibly?).
You can solve this by using the map(value, fromLow, fromHigh, toLow, toHigh)
Consider the following snippet.
int val = analogRead(0);
 val = map(val, 0, 1023, 0, 255);

Okay, so in the next tutorial we'll start building (I promise) but for now, I want to finish this tutorial up.

Feel free to comment and watch out for Part III - coming to a theater near you! :pirate:

Is This A Good Question/Topic? 3
  • +

Page 1 of 1