Pinterest Stumbleupon Whatsapp
Ads by Google

arduino circuit boardLast time I left you having set up Getting Started With Your Arduino Starter Kit - Installing Drivers & Setting Up The Board & Port Getting Started With Your Arduino Starter Kit - Installing Drivers & Setting Up The Board & Port So, you’ve bought yourself an Arduino starter kit, and possibly some other random cool components - now what? How do you actually get started with programming this Arduino thing? How do you set it up... Read More your Arduino to work with Mac or Windows, and having uploaded a simple test app that blinked the on-board LED. Today I’m going to explain the code you uploaded, the structure of Arduino software, and a little more about the electronic bits on the board itself.

This article is part of an introduction to the Arduino series. The other articles in the series so far are:

The Hardware

Let’s take a closer look at what the Arduino Uno has in terms of bits on the circuit board.

Here’s an enlarged diagram to refer to:

arduino circuit board

  • Along the top, there are 14 digital Input/Output pins (numbered 0-13). These are the most versatile pins on your Arduino and can function as either input or output, and will form the core of your projects. Digital means that the signal these pins can write or read will be on or off.
  • 6 of those digital pins, which are marked by the tilde sign ~ are capable of doing what it is called Pulse Width Modulation. I’m not an electrical engineer so I won’t embarrass myself by explaining the science behind this, but to you and I it means we can provide a range of output levels – for instance, dimming an LED or driving a motor at varying speeds.
  • Pin 13 is special in that it has a built-in LED. This is for convenience and testing purposes only really. You can use that on-board LED, as you did in the Blink example app, by simply outputting to pin 13 – or it can be used as a standard I/O pin.
  • On the bottom right are 6 analog input pins. These will read the value of analog sensors such a light-meter or variable resistors.
  • On the bottom left next to the analog input pins are power pins. The only ones you really need to worry about are the ground pins (GND), 3.3v, and 5v power lines.
  • Finally, the only switch found on the Arduino is a reset switch. This will restart whatever program it has in its memory.
  • The Arduino has a set amount of memory, and if your program goes too large, the compiler will give you an error.

The Structure Of An Arduino Program

Every Arduino program is made up of at least two functions (if you don’t know what a function is, be sure to read my basic programming tutorial, part 2 – function and control statements The Absolute Basics Of Programming For Beginners (Part 2) The Absolute Basics Of Programming For Beginners (Part 2) In part 2 of our absolute beginners guide to programming, I'll be covering the basics of functions, return values, loops and conditionals. Make sure you’ve read part 1 before tackling this, where I explained the... Read More , and part 1 where we discussed variables The Basics Of Computer Programming 101 - Variables And DataTypes The Basics Of Computer Programming 101 - Variables And DataTypes Having introduced and talked a little about Object Oriented Programming before and where its namesake comes from, I thought it's time we go through the absolute basics of programming in a non-language specific way. This... Read More before continuing).

Ads by Google

The first is the setup function. This is run initially – once only – and is used to tell the Arduino what is connected and where, as well as initialising any variables you might need in your program.

Second is the loop. This is the core of every Arduino program. When the Arduino is running, after the setup function has completed, the loop will run through all the code, then do the whole thing again – until either either the power is lost or the reset switch is pressed. The length of time it takes to complete one full loop depends upon the code contained. You may write some code that says “wait 6 hours”, in which case the loop isn’t going to be repeating very often.

Here’s a quick state diagram to illustrate:

arduino circuit board

Examining The Blink Program

Take a look back at the Blink program code and identify the setup and loop functions.

Here’s the setup:


void setup() {
// initialize the digital pin as an output.
// Pin 13 has an LED connected on most Arduino boards:
pinMode(13, OUTPUT);
}

The lines that begin with // are simply comments to explain the code to a human reader, and they don’t get uploaded to the Arduino. So in fact, there’s only one line of setup code in this particular Arduino app. That line is saying “Set pin 13 to output mode”. 13, remember, is the built-in LED.

Then there is the loop:


void loop() {
digitalWrite(13, HIGH); // set the LED on
delay(1000); // wait for a second
digitalWrite(13, LOW); // set the LED off
delay(1000); // wait for a second
}

The comments at the end of each line of code explain their function quite well. HIGH and LOW refer to the ON and OFF state of a digital output – in our case the LED. You could actually write ON or OFF in the code too, both are synonymous (as is 0 and 1 also). Delay tells the Arduino to wait for a bit, in this case 1000 milliseconds (or 1 second).

Finally, a note about the programming language used here. Notice that both setup and loop functions have the word void before them. This is a special word for nothing, because the function returns nothing when it is called – it simply runs the code contained within. For now, let’s leave it at that by saying that the function’s block of code is enclosed by curly braces { }, and that each line of code must end with a ; semi-colon.

Try altering the basic program somehow by changing the precise delay values to something larger or smaller. See how small you can get it down to before the flashing is no longer noticeable. Work out which value to change to get it to stay on for longer, or to stay off for longer. Try adding some more digitalWrite and delay statements into the loop function to create a more complex flashing pattern like the morse code for SOS. If you have a buzzer, try connecting it to pins 13 and GND too (hint: the red wire goes to 13, black to ground).

That’s all for today. Next time we’ll add in some more LEDs and write our own application from scratch. As ever, comments and shares much appreciated. I can’t imagine you’d have any problems with the code referred to today, but if you’ve tried adjusting the code slightly and are running into errors or unexpected behaviour, feel free to post it in the comments and we’ll see if we can work through it together.

  1. Deekshith Allamaneni
    November 28, 2011 at 3:02 pm

    Nice articles on Arduino these days on this blog. Keep going.
    I expect more electronics related articles here like this. Thank you.

  2. Espen Meidell
    November 28, 2011 at 12:39 pm

    Great article, could you recommend a place to buy an Arduino kit?

  3. Dom
    November 27, 2011 at 6:19 pm

    I'm really enjoying this series!
    Thanks

Leave a Reply

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