element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet & Tria Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • About Us
    About the element14 Community
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Japan
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      •  Vietnam
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Arduino Tutorials
  • Products
  • Arduino
  • Arduino Tutorials
  • More
  • Cancel
Arduino Tutorials
Blog Getting to Know Arduino : Part 1 : Hello, World!
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Arduino Tutorials to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: cstanton
  • Date Created: 28 Mar 2014 9:24 AM Date Created
  • Views 11794 views
  • Likes 15 likes
  • Comments 33 comments
  • tutorial
  • min
  • 10
  • arduino uno
  • arduino-uno
  • ardunio
  • microcontrollers
  • 10minarduino
  • programming
  • getting-to-know
  • atmega328
  • tutorials
  • atmega328p
  • microcontroller
  • minute
  • 10minutearduino
  • uno
  • getting_to_know
  • getting to know
  • c++
  • arduino
  • feature tutorial
  • feature_tutorial
  • atmel
Related
Recommended

Getting to Know Arduino : Part 1 : Hello, World!

cstanton
cstanton
28 Mar 2014

You don't have permission to edit metadata of this video.
Edit media
x
image
Upload Preview
image

 

Next Guides:

Getting to Know Arduino : Part 2 : Hello, Again!

Getting to Know Arduino : Part 3 : Taking your Temperature

 

I recorded a video to introduce the Arduino UnoArduino Uno and how to get started with it. The premise was that you didn't need to add anything extra, except for an A to B USB cableA to B USB cable (always check the type you need for the Arduino you have) because although you can connect an external power supply it can be powered purely from the USB, and you could get code running with a few simple steps on Microsoft Windows to make an LED (Light Emitting Diode) start to flash on your board.

image

The Arduino I showed in the video was a revision 3 board, there have been a few versions so far, in the image (from zenbike.co.uk) you can see an earlier version, but it nicely maps out the connectors on the board. I think in the recent version the reset button has moved, but that makes little difference overall. What it doesn't note on the image is that any socket with the ~ (tilde) symbol next to it can be used as a Pulse Width Modulation output. This is a way of having a digital signal pretend that it's analogue but typically you don't need to worry about this until you've got some hardware that you need to output to which needs it.

 

The first instruction we can make the Arduino perform is to flash an LED, which is often considered the "Hello, World!" of getting started. The example code 'Blink' will do this for you, it flashes the LED marked 'L' on the board. This LED is also linked up with the breakout pin '13' like many connections on the board, some go through components and others do not, but they ultimately connect to the ATMega328 micro-controller processor on the board, which is the brains of it all.

 

We'll need to tell the Arduino what we want it to do and for that we can use the Arduino Integrated Development Environment (IDE), this environment contains a text editor and a console window which shows you any error messages and how successful the software has been when communicating with the Arduino. You can get the IDE for more than just Microsoft Windows, there are Mac OS and Linux variants. You might want to check your package manager and software repository for whether or not there is a version pre-packaged for your version of Linux. For example you can use apt-get on Ubuntu to "apt-get install arduino" from a terminal.

 

It's often best to get the latest non-beta version of the Arduino IDE, but if you have a board that is newer then usually this isn't supported in the 'stable' builds and a beta or nightly build is required.

 

After installing the Arduino IDE in Microsoft Windows, you'll need to connect your Arduino Uno if you haven't already. It may help to reconnect it so that it re-detects the device and installs the driver. Then you can run the IDE. To be able to upload code to the device you will need to know what COM port it is running on.

 

To do this, open your Start menu, navigate to Control Panel, once the window appears, open System and then navigate around the tabs/options until you find Device Manager. With this window open, click the + symbol next to "Ports (COM & LPT)" and the Arduino should be listed along with its COM and a number.

 

In the IDE, go to the Tools menu and then expand Board and choose your Arduino, then do the same, but go to Serial Port and choose the one that it stated in Device Manager.

 

Now to get the LED flashing, choose the File menu, go to Examples and then 01.Basics, under this menu navigate to Blink. To get this running on your Arduino you can now either click on the arrow next to the tick icon or choose the Sketch menu and click Verify/Compile. Now the 'L' LED on your board should be blinking!

 

Feel free to change the code, copy and paste bits to see what works and what doesn't.

 

This will open a new window with the following code in it:

 

/*
  Blink
  Turns on an LED on for one second, then off for one second, repeatedly.

  This example code is in the public domain.
*/

// Pin 13 has an LED connected on most Arduino boards.
// give it a name:
int led = 13;

// the setup routine runs once when you press reset:
void setup() {        
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}
       

 

This is what is referred to as a 'sketch' in IDE terms, however it is actually using a low level programming language called C++ which has been around for a notable amount of time, most of the software you use is written with C++ including Microsoft Windows and parts of Linux.

 

Line 01 and 06 in the code uses text that the IDE recognises as a special arrangement so that it knows to ignore anything typed between them (it denotes that whatever is between them is a comment. On line 08 it shows the line starting with a similar arrangement that means to ignore that entire line as a comment.

 

int led = 13;
       

 

This line is literally mathematical, it could be said that programming is inherently mathematical because processors are just doing calculations and moving numbers around. So this line in particular is declaring that the english word, led is a certain type of variable (kinda like a bucket that can only contain something of a particular thing and its contents can vary) and that is called an integer, which means a whole number. As opposed to a number with a decimal (or a real number). We're then making the contents of the variable (or bucket) equal to the number 13. What then terminates the line is a semicolon and this is required by the IDE to know that we're finished putting commands on the line.

 

The IDE, or specifically the part of it that changes our near-english (sketch/programming) into a language that the Arduino can understand typically operates on a line by line basis, which is like how old typewriters used to work where by you normally couldn't go back and change what you had just created/read and had to continue (probably a bad analogy).

 

void setup() { }
      

 

The setup declaration is called a function, anything within { } is processed by the IDE, 'setup' is a reserved name for the function and it means that it is ran before anything else within the sketch. Like the variable led is of a type int, the setup function is of a type void. We don't have to worry about that for now. pinMode() is another function that is referred to and you can read up about it.

 

void loop() { }
      

 

The loop function is another reserved name that means anything within { } is ran again from the first { when it reaches the last line or instruction before the final }. The speed of this is governed by how fast the microprocessor, for example the ATMega328 can get through the instructions per second. The digitalWrite() and delay() functions can also be read up about on the Reference section of the Arduino site.

 

Hopefully you have found this useful to get started with your Arduino and it has introduced you to some new concepts for programming for micro-controllers. The best way to learn is trial and error, so don't be afraid to mess around with the code or read up about it.

 

If you have any questions, problems or otherwise please leave a comment! You can also find more information in The specified item was not found. community group!

  • Sign in to reply

Top Comments

  • cstanton
    cstanton over 12 years ago in reply to e14 Contributor +5
    Yep. SPI is broken out onto the header sockets and also the ISCP header (one of the block of 6 pins). You can read more here: http://arduino.cc/en/Reference/SPI and there's a schematic for the Uno R3 here…
  • mcb1
    mcb1 over 12 years ago +4
    Nice work. I bet the newbies here don't search for it and still ask, but at least there is a good place to point them to. Mark
  • e14 Contributor
    e14 Contributor over 12 years ago in reply to cstanton +3
    Thank you for the very prompt and helpful response. I'll probably use my Rasberry Pi for experimentation and prototyping and then transfer to Arduino for a more permanent solution.
Parents
  • e14 Contributor
    e14 Contributor over 11 years ago

    That using delay(x) in a loop to do a delay that is dependant on the speed of the processor is one of the worst ways to do delays. Teaching this to a newbie encourages bad programming practice from the onset which later when they want to do real things will bring their world crumbling down.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jw0752
    jw0752 over 11 years ago in reply to e14 Contributor

    What is a better way in this situation?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Comment
  • jw0752
    jw0752 over 11 years ago in reply to e14 Contributor

    What is a better way in this situation?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
Children
  • e14 Contributor
    e14 Contributor over 11 years ago in reply to jw0752

    Hi John, with the simple tools described in this presentation not a lot, but the way I approach it (and it should be for real world applications) is to have a separate timer routine that runs independent of the main routines and does nothing else other than allocate time to things like toggling LED's. Not knowing your background and for the benefit of other watchers that may be newbies I will try to put it in layman's terms:

    There is a class of operations on almost any processor called interrupts which as the name implies interrupts the normal goings on in your program and divert the processor's functioning to execute this other program called an interrupt routine. There are a number of instigators of interrupts but nearly every processor has at the very least one that is based on time. So what you do is before you get to your main loop you configure a timer interrupt to happen say every 1 millisecond. In that interrupt routine you might load a counter with the number 1000 if the counter is zero and toggle the LED. if the counter is not zero then you would decrement it and then exit, so in effect you spend maybe 10 instruction times in the timer interrupt either decrementing or toggling the LED and setting the counter to 1000. This means that almost no matter what you do in the loop the LED ALWAYS toggles at the 1 second rate. Now what if you have no need to do anything else? Well then you can put your processor to sleep and conserve 99.9% of your power which is great if you're running on batteries. The other thing is that in the example they showed imagine your processor needs to do 50 things besides the led toggle and timing, and you decide to add 50 other things, well all of a sudden your delay timing is up the creek, where as with my methodology it continues to tick over and keep near perfect time. Now consider you want to have a second LED flashing at a third of a second rate, well with my model you just add a second counter that is loaded with 333 on the first time through and with 334 the next 2 time through (because we can't easily do 333.333333333 :-) I think you get the picture from here, and in a fashion this method of scheduling can allow you to do very complex things VERY efficiently, without going to the extent of a full blown operating system.

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • cstanton
    cstanton over 11 years ago in reply to e14 Contributor

    Do you have code examples of what you're talking about, for the Arduino (Uno) / ATMega328P ?

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • e14 Contributor
    e14 Contributor over 11 years ago in reply to cstanton

    :-) I've bought an UNO, a Pi and a BeagleBoard but haven't yet used any of them because everything that comes my way I seem to design a board from scratch to fit in the space required and only have the pins I need wired except maybe for an LED so I can "see" what my code is doing. I have a test point on the same pin so I can look with a CRO for exact timing. I do however use C so the code should run on a UNO with very little modification, so I include her a few snippits to support the explanation.

    Here is the initialisation of the Timer 1 in an ATmega88 which is a smaller version of the 328 so should match:

    // Timer/Counter 1 initialization

    // Clock source: System Clock

    // Clock value: 1000.000 kHz

    // Mode: Normal top=0xFFFF

    // OC1A output: Disconnected

    // OC1B output: Disconnected

    // Noise Canceler: On

    // Input Capture on Rising Edge

    // Timer Period: 0.52429 s

    // Timer1 Overflow Interrupt: Off

    // Input Capture Interrupt: On

    // Compare A Match Interrupt: On

    // Compare B Match Interrupt: Off

    TCCR1A=(0<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);

    TCCR1B=(1<<ICNC1) | (1<<ICES1) | (0<<WGM13) | (0<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);

    TCNT1H=0x00;

    TCNT1L=0x00;

    ICR1H=0x00;

    ICR1L=0x00;

    OCR1AH=0x03;

    OCR1AL=0xE8;

    OCR1BH=0x00;

    OCR1BL=0x00;

     

    Here is the code that runs on the timer interrupt:

     

    //**********************************************************************************

    //                        Interrupt Handler for Tick Timer

    //**********************************************************************************

    // Timer 1 output compare A interrupt service routine

    interrupt [TIM1_COMPA] void timer1_compa_isr(void)

    {

        OCR1AW += One_Msec_Value;

        _Toggle_Pin();                    // debug only toggles LED

     

    /************************ Stuff done every 1mS ********************************/

     

        if ( Timer1 )                    // RxTimeOut timer

            {

                Timer1--;

     

                if ( Timer1 == 0 )

                    RxHasTimedOut();    // RxTimeOut handler

            }

     

        if ( Timer2 )

            {

                Timer2--;

     

                if ( Timer2 == 0 )

                    MotorHasStopped();    // motor stopped handler

            }

     

        if ( Timer3 )                    // Tx reply turnaaround timer

            Timer3--;

     

        if ( Timer4 )                    // while NZ prevents autostart

            Timer4--;

     

        if ( Timer5 )                    // while NZ delays pinchange sample timer

            {

                Timer5--;

     

                if ( Timer5 == 0 )

                    DoQuadrant();        // pin change handler

            }

    }

     

    Timer1, Timer2 etc. are just byte variables declared as uchar Timer1 etc.

     

    The idea is here that somewhere else eg Timer1 is set to a number and then as long as that external task keeps the counter above zero RxHasTimedOut() doesn't get executed. I have it set up so that an incoming character on the UART loads Timer1 with 100 (100mS) so that's the time out time, but it's just as easy to have:

     

    if (Timer1 == 0)

    {

         Timer1 = 100;

         Toggle_LED();

    }

     

    This would toggle the LED 10 times per second.

     

    BTW, OCR1AW is OCR1AL and OCR1AH declared by me as a 16bit register to make the code cleaner. I just added #define OCR1W (*(unsigned int *) 0x8a) // 16 bit access in a header file. This works because I only update it in the interrupt routine.

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • Robert Peter Oakes
    Robert Peter Oakes over 11 years ago in reply to e14 Contributor

    Excellent example Max,

     

    Not good for a beginner as it would most lightly scare them away image,  but defiantly for the intermediate to advance programmer.

     

    I have used similar techniques to create a precise 16bit PWM output on a MEGA328 (You can actually select the resolution anywhere from 8 to 16 bits) as I was wanting to have more control over the brightness of some LED strip lights I implemented in my kitchen (256 levels is not enough when you need a log output to match eye sensitivity)

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • jw0752
    jw0752 over 11 years ago in reply to e14 Contributor

    Hi Max, Thanks for the insights. Your back and forth discussion with Peter was informative. As with any complex activity, learning to program is a process. When someone is at the level of Peter and you it is easy to look back and see the entire forest. However for those of us still learning, the trees are encountered and seen one at a time. It was very good that you pointed out the downside to using delay() in some instances it will make me go back and revisit the command and its alternatives. Christopher, for his intent to give a new person a positive experience, had the correct approach in using delay() for his example. It is great to have someone with your skills and knowledge on this site. I look forward to learning more from you as time goes on.

    John

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2026 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube