Arduino Hello World program


Contents

  1. Creating your first circuit
  2. Communicating Between Your Arduino and Your PC
  3. Creating your first code
  4. References

Creating your first circuit


To create your first circuit you will need a toolkit with the following things:

  • 1x Arduino board
  • 1x LED
  • 1x 220 ohm resistor

As a first project, let’s connect an LED to your Arduino board. This diode only allows electricity to flow through it one way, so if you hook it up backwards it won’t work.

To build your first circuit, attach a 220-ohm resistor to pin 13. Then attach the long leg of an LED (the positive leg, called the anode) to the resistor.
Attach the short leg (the negative leg, called the cathode) to ground (GND). Then plug your Arduino board into your computer, start the Arduino program, and enter the code below.
Most Arduino boards already have an LED attached to pin 13 on the board itself. If you run this example with no hardware attached, you should see that LED blink.

example arduino circuit
Figure 1 - Example Arduino circuit

fcircuit schematic
Figure 2 - FCircuit schematic

If you have followed the guide properly the LED should light up. If the LED do not lights unplug power from the Arduino board. Check that the wires are connected properly and make sure you have not plugged the LED in backwards. Then try to power it again.

Communicating Between Your Arduino and Your PC


The first program will communicte with the PC via the Serial port (the USB cord). To set this up, you need to add the following line to your setup() method:

Serial.begin(9600); - 9600 is the baud rate (it essentially means the number of signal changes made per second, and merely ensures that the PC and the Arduino are on the same page in regards to this).
Whenever you would like to write something to the serial port, simply use the Serial.print or Serial.println function, as so: Serial.print("Hello world!");
Note that you can also read data from the PC, but for this first application it is not discussed.

Creating your first code


Now let’s write a program to light up the LED. Each Arudino program must contain at least two functions. A function is a series of programming statements that can be called by name. The two functions of a program must contain the followings:

  1. setup() which is called once when the program starts.
  2. loop() which is called repetitively over and over again as long as the Arduino has power.
const int
kPinLed = 13;

This defines a constant that can be used throughout the program instead of its value. I recommend this for all pins as it makes it easy to change your software if you change your circuit. By convention, constants are named starting with the letter k. You don’t have to do this, but it makes it easier when you look through your code to know what is a constant.

Lighting up the LED

void setup()
{
pinMode(kPinLed, OUTPUT); 
}

This sets up the pin that our LED is connected to as an OUTPUT pin. (Meaning that the Arduino is controlling “writing” to a pin instead of reading from it.)

void loop()
{
digitalWrite(kPinLed, HIGH);
delay(500);
digitalWrite(kPinLed, LOW);
delay(500);
}

These lines where the commands are flowing. Let’s start by writing HIGH out on the pin connected to the LED which will turn the LED on. (HIGH means putting 5V out on the pin. The other choice is LOW which means putting 0V out on the pin.) We then call delay() which delays the number of milliseconds sent to it. Since we send the number 500, it will delay for 0.5 second. We then turn the LED off by writing LOW out on the pin.We delay for 500 milliseconds.

This will continue until power is removed from the Arduino. Before we go any further, try this on your Arduino and make sure it works. You will know this works because this blinks the LED twice as fast as the original program that is on your Arduino. If it blinks once a second, thenyou have not successfully sent your new program to the Arduino.

More information