t r o n i x s t u f f

fun and learning with electronics

Getting started with Arduino! – Chapter Zero

Hello world!

Please join with us as we learn about electronics and the Arduino! Together through this series of articles I would like to share with you a journey of learning, exploration and fun with the Arduino system, and make some enjoyable, useful, interesting, useless and practical things. These posts will be published on a regular basis, on top of the other non-micro controller posts. Whew! So much to read… and so much fun to have! The index of articles is to your right. Furthermore, if there is something you would like to see covered, don’t be afraid to ask!

During the first few posts, we will refer to the Arduino website, and the book:

and also assume a basic knowledge of electronics. I can recommend the following book for such information:

For support or to have your questions answered, post your enquiry in our Google Group – a friendly place for such discussions :)

First of all, let’s breakdown the whole system into the basic parts. From there we can build an understanding of what Arduino is all about.

Arduino is an open source physical computing platform based on a simple input/output board and a development environment that implements the Processing language (www.processing.org). Arduino can be used to develop standalone interactive objects, or be connected to software on your computer. [1]

So, we have hardware and software. Our hardware will be a personal computer that can run the Arduino IDE (integrated development environment) software; and the Arduino itself and the electronics to go with it.

Our software is the IDE – software very similar to a word-processor, but can send the Arduino program (or “sketch”) to the micro controller. These sketches are programs that are written in the processing language – which is similar to C. These are interpreted by a boot loader – software in the chip that allows it to understand your sketch. As the Arduino system is open source, anyone can purchase a blank micro controller and put the boot loader on it, or even write their own boot loader or modify the original one.

Now for the Arduino itself. But what do we mean by that? Let’s have a look…

What we have is a microcontroller installed into a circuit board with a USB interface, a DC power socket, and many input and output lines (more about them later). Plus some LEDs for status reports, and other miscellaneous components. This board is the Uno , and uses the ATMega328 micro controller.

There are also larger, smaller, older and in the future newer boards – each different by their physical size, interface type, and available sketch and data memory. A very good improvement on the Arduino boards are available from Freetronics. The purpose of the board is to simplify to a great degree access to the micro controller, and allow you to easily interface with inputs, outputs, add power supply, connect to a PC for programming, and talk to other circuits. However the board is more for your convenience, you can actually use the programmed micro controller in your own designs without the board.

To summarise at this point – with an Arduino you can connect various forms of input and output, and create a program to process and respond to the inputs and outputs. For example, you could create a temperature alarm – when your room temperature rises above a set amount, the Arduino could sound an alarm.

Forms of input can include: buttons, switches, movement sensors, temperature sensors, light sensors, gas sensors (!), dials that you can turn (e.g. like a volume knob), wireless data receivers, etc. Forms of output can include: lights, noise-makers, motors, pumps, other circuits, liquid-crystal displays, etc. Basically anything that can be switched on or off, or controlled by electricity, can be controlled by an Arduino.

To make things easier, you can buy or make what is called a shield. This is an interface board that sits on top of your Arduino, and has various types of circuitry you can interface with. For example, an ethernet network connection, a joystick for games or controlling a robot, or an LCD.

So that means with the right sketch, and the right hardware interface, and maybe a shield, you can do almost anything. Great!

Thankfully that’s about all we need to learn at the moment. Now it is time to actually do something. You are going to need three things:

  • A personal computer running Linux, MacOS or Windows with a USB port, and able to access the internet;
  • a USB cable that matches your board socket;
  • an Arduino or compatible board. Most boards should come with a USB cable, check before buying.

And now for the initial preparation – please install your Arduino IDE software using the instructions here. In this series we will be using Arduino v23 – however the tutorials are currently being updated for v1.0 and greater. . If you are running Ubuntu, here is an excellent installation guide.

How did you go? Did your LED blink? Were you mesmerised by it, staring blankly at all the blinky goodness? It’s ok, you can admit it – we all were the first time.

At this point I hope you realised that the Arduino can be powered by your computer’s USB port. However, if you want to use your programmed Arduino away from the computer, you will need a nice regulated plugpack or another source of power.

There is some really basic things to get started, however with respect to the Arduino creators, please refer to the book Getting Started with Arduino book until the end of page 38. You should be able to get that LED to blink on your own!

Exercise 0.1

Now let’s have some more blinky fun. Do you remember Knight Rider with David Hasselhoff? The ‘hoff drove a tricked-up Trans Am with some cool lights in the bonnet, a horizontal row with each light blinking in sequence from left to right to left…

Your mission, is to simply recreate this with your Arduino, using not one but eight LEDs. Blink them in an endless loop in the sequence 1-2-3-4-5-6-7-8-7-6-5-4-3-2-1-2-3-… with a delay of one second.

You will need:

  • Your standard Arduino setup (computer, cable, Uno or compatible)
  • 8 light emitting diodes (LEDs). Anything as long as they draw less than 40mA.
  • 8 560 ohm 0.25 W resistors. They are to reduce the current to protect the LEDs
  • a breadboard and some connecting wire
  • a camera (optional) – to document your success!

Hint – make the delay a variable so you can alter it easily.

From your original flashing LED project in the book, the digital pin 13 was used. This time, you can use digital pins 2 to 9. The hardware side is easy – run a wire from each digital output pin to the anode of an LED, then put a 560 ohm resistor from the cathode back to ground. See the diagram below:

And this is what the result should hopefully look like:

And of course, here it is in action:

Now it is your turn to do something – write the code! But before you do that, plan what you want to do first (some old-schoolers would call the plan an algorithm). For example, for this exercise you might write something like…

exercise 0.1 plan

set all the pins I want to use as outputs
create a variable to store the delay duration in milliseconds
start an infinite loop
turn on pin 2, wait for delay duration, turn it off
turn on pin 3, wait for delay duration, turn it off
repeat for pins 4 to 9
then do the same thing but backwards down to pin 3
end of infinite loop

So how did you go? Did it work first time? It’s ok if it didn’t, as you learn by doing and fixing your mistakes. Remember – if you have any questions, leave a comment at the bottom of this post and I will get back to you. But to save you time, here is the sketch:

/*
exercise 0.1 - KITT emulator!
Created 02/04/2010
By John Boxall

http://tronixstuff.wordpress.com...
http://wp.me/pQmjR-3i
Blinks LEDs from output pin 2~9 in a forwards<>backward motion
The circuit:
an LED is connected to each output pin from 2 to 8, thence to a 560 ohm resistor, then to ground (pin 4 on the bottom left of the Arduino Duemilanove).
based on an orginal by H. Barragan for the Wiring i/o board
*/
// The setup() method runs once, when the sketch starts
int del=100; // sets a default delay time, 1000 milliseconds (one second)
void setup()
{
// initialize the digital pins as outputs:
// later on there will be easier ways to do this
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
}
// the loop() method repeats indefinitely until you turn off the power
void loop()
{
digitalWrite(2, HIGH);   // turn on LED on pin 2
delay(del);              // wait (length determined by value of 'del')
digitalWrite(2, LOW);    // turn it off
digitalWrite(3, HIGH);   // turn on LED on pin 3
delay(del);              // wait
digitalWrite(3, LOW);    // turn it off
digitalWrite(4, HIGH);   // turn on LED on pin 4
delay(del);              // wait
digitalWrite(4, LOW);    // turn it off
digitalWrite(5, HIGH);   // turn on LED on pin 5
delay(del);              // wait
digitalWrite(5, LOW);    // turn it off
digitalWrite(6, HIGH);   // turn on LED on pin 6
delay(del);              // wait
digitalWrite(6, LOW);    // turn it off
digitalWrite(7, HIGH);   // turn on LED on pin 7
delay(del);              // wait
digitalWrite(7, LOW);    // turn it off
digitalWrite(8, HIGH);   // turn on LED on pin 8
delay(del);              // wait
digitalWrite(8, LOW);    // turn it off
digitalWrite(9, HIGH);   // turn on LED on pin 9
delay(del);              // wait
digitalWrite(9, LOW);    // turn it off
digitalWrite(8, HIGH);   // turn on LED on pin 8
delay(del);              // wait
digitalWrite(8, LOW);    // turn it off
digitalWrite(7, HIGH);   // turn on LED on pin 7
delay(del);              // wait
digitalWrite(7, LOW);    // turn it off
digitalWrite(6, HIGH);   // turn on LED on pin 6
delay(del);              // wait
digitalWrite(6, LOW);    // turn it off
digitalWrite(5, HIGH);   // turn on LED on pin 5
delay(del);              // wait
digitalWrite(5, LOW);    // turn it off
digitalWrite(4, HIGH);   // turn on LED on pin 4
delay(del);              // wait
digitalWrite(4, LOW);    // turn it off
digitalWrite(3, HIGH);   // turn on LED on pin 3
delay(del);              // wait
digitalWrite(3, LOW);    // turn it off
}

So there you have it. Today you have learned how to set up a computer to program your Arduino, and written some sketches to create outputs based on your design. Although it wasn’t that much, we have to start somewhere… but great things will follow. Like chapter one! 

Have fun and keep checking into tronixstuff.com. Why not follow things on twitterGoogle+, subscribe  for email updates or RSS using the links on the right-hand column, or join our Google Group – dedicated to the projects and related items on this website. Sign up – it’s free, helpful to each other –  and we can all learn something.

Notes:

[1] from “Getting Started with Arduino” by Massimo Banzi (O’Reilly).

April 4, 2010 - Posted by | arduino, microcontrollers | , , , , , , , , , , , ,

51 Comments »

  1. Just had a quick read through the Arduino tutorials – really good. I’ll be looking over those again at some point. There’s a little bit of Arduino content on my blog – I’ve left it alone for a while due to other commitments, but will soon be getting back to it along with your tutorials.

    Cheers
    Jon

    Comment by Jon | May 3, 2010 | Reply

    • Thank you for dropping by, looking forward to seeing what comes in the future.

      Comment by John Boxall | May 3, 2010 | Reply

    • hello sir,
      i uploaded this code, this is third code im uploading!
      but its showing .. avrdude: stk500_getsync(): not in sync: resp=0×00
      only rx led is glowing .. what to do ,kindly help ..tried uploading many times..

      Comment by victor | February 22, 2012 | Reply

      • There can be many causes for the error you describe. Some common causes are:
        Make sure the correct board type is selected
        Check the drivers are installed correctly (I have found reinstalling them helps anyway)
        Check that you don’t have anything connected to D0 or D1 while uploading

        Comment by scourist | February 23, 2012

  2. Cool little project. I added a potentiometer to control the speed of the light.

    Comment by John Cudd | May 22, 2010 | Reply

    • Excellent, I am glad you enjoyed it! Cheers, John

      Comment by John Boxall | May 22, 2010 | Reply

  3. [...] the Arduino. I’m thinking of giving it a try, and found a great series of Arduino tutorials here thanks to [...]

    Pingback by Thomas M. Coyle » Blog Archive » Great Arduino tutorial | July 10, 2010 | Reply

  4. I think your series are going to be a great help for me in the coming months/years as I get into the Arduino craze – having written software for PCs for the past few years, I want to be able to build things that aren’t just displayed on a screen, but actually do things.
    One question about your code: could you use a loop? I see a lot of repetitive code there that could be pared down by a fair few lines, is all. Of course, it might not actually make a difference once it’s complied and sent to the board.

    Comment by Stu | July 14, 2010 | Reply

    • Sorry, ignore this. I just went on to read the NEXT chapter where you describe exactly this. My bad.

      Comment by Stu | July 14, 2010 | Reply

      • Hello
        It’s all good, I hope you enjoy the series of articles. If you have any questions don’t hesitate to ask :)
        Cheers,
        John

        Comment by John Boxall | July 13, 2010

  5. [...] Started with Arduino – [Link] Tags: Arduino Filed in Arduino | 1 views No Comments [...]

    Pingback by Electronics-Lab.com Blog » Blog Archive » Getting Started with Arduino | August 1, 2010 | Reply

  6. This nice project to climb Arduino ladder.
    Why 8 resistors. One is enough between gnd and negative of LED until you plan to glow two LEDs in one go.

    Comment by Akshay | August 23, 2010 | Reply

    • Yes, that is true. However many people would quickly modify it to display 1 to 8 inclusive, so I don’t want them to fall into the habit of not using the correct circuitry.
      thanks
      john

      Comment by John Boxall | August 23, 2010 | Reply

  7. Hello John,

    In chapter zero exercise 0.1 there appears to be a typing error in line 9 and 11 ‘Blinks LEDs from output pin 2-8 in a forwardsbackwards motion’. It needs to be pin 2-9.

    In chapter one exercise 1.1 LEDs 2 to 9 bliks correctly but LEDs 8 to 3 does not work. I have tried on Arduino/Sanguino boards and have the same problem. Can you give some guideline.

    Thanks

    B.K.Chaturvedi

    Comment by B.K.Chaturvedi | August 23, 2010 | Reply

    • Thanks for your information. With exercise 1.1, I am not sure what you are saying. If LEDs 2 to 9 work correctly then 8 to 3 should work as well.
      cheers
      john

      Comment by John Boxall | August 23, 2010 | Reply

  8. [...] Boxall – A tutorial on the Arduino universe. The first chapter is here, the complete series is [...]

    Pingback by Moving Forward with Arduino – Chapter 16 – Ethernet « Hey it’s my blog… | September 12, 2010 | Reply

  9. Hello john,
    I studied chapter 14 but i am confused that can i use the XBee wireless transceiver for both data and video transmission and receiving? and at what frquency? please help me out. waitng for your quick response..

    Thanx

    Best,
    Halar

    Comment by Halar | September 25, 2010 | Reply

  10. What is the difference between learning Arduino and another type of development board. Because I already have one with ATmega 8535 embedded inside it.

    Comment by Darwin | October 8, 2010 | Reply

    • That is a very broad question.
      You would need to compare the microcontrollers’ features (such as I/O types [UART/I2C etc], speed, flash memory, etc) as well as the development environments available for them.
      However the Arduino ecosystem (and PicAXE) IMO offers one of the easiest methods of making interactivity come to life.
      cheers
      john

      Comment by John Boxall | October 8, 2010 | Reply

  11. I’m new to this and this is great stuff. However. I’ve tried loading the VirtualWire library and Arduino fails to recognise it. I copied the folder to arduino/hardware/library.

    Any hints please?
    Thanks
    Richard

    Comment by Richard | November 12, 2010 | Reply

    • Hello
      Your library folder should be called ‘libraries’ if you have a default installation. Have a look in your arduino folder, you might have ended up with a ‘libraries’ and a ‘library’ folder. You only need the former. Copy the VirtualWire folder into it, restart arduino and try again.
      cheers
      john

      Comment by John Boxall | November 13, 2010 | Reply

  12. ‘The best’ content and explanation for using Arduino right from basics!!! I dont think there is a better explanation than this on the internet. Great work!!

    Comment by Saurabh | December 17, 2010 | Reply

    • Absolutely! Thank you for your comment
      John

      Comment by John Boxall | December 18, 2010 | Reply

  13. What’s the idea about using the 560 ohm resistors? Wouldn’t 150-220 ohm give much “brighter leds = more fun” and still be well below 40 mA?

    Comment by astroboy | December 18, 2010 | Reply

    • True, however this article is aimed at total beginners, who may have LEDs with all sorts of current values. So the 560 is the “better safe than sorry” value.
      cheers
      john

      Comment by John Boxall | December 18, 2010 | Reply

  14. Excellent tutorial, I can not stop of reading it!
    I made a little simple change on the loop() function for this sample, setting BAN=1; and i=2; at the begining of the sketch… and it worked =)

    void loop(){
    digitalWrite(i, HIGH);
    delay(del);
    digitalWrite(i, LOW);

    if(BAN == 1){
    i++;
    }else{
    i–;
    }

    if(i == 9){
    BAN = 0;
    }else if(i == 2){
    BAN = 1;
    }

    Regards

    Comment by JorgeVS | January 25, 2011 | Reply

    • Great to hear, thank you for letting me know.
      Cheers
      john

      Comment by John Boxall | January 25, 2011 | Reply

  15. Hi John B,

    First of all thanks for this tutorials, i really love it. I suggest you to make a chapter with the list of the parts we need to do every example in each chapter from 0 to 28,

    Thanks

    Comment by Joan | February 11, 2011 | Reply

  16. Thank you ! this is really something i wanna learn, and this will help allot i think :P

    Enough to read, and well documented i’ve read only chapter 0,1 yet

    Comment by Jeremy | May 9, 2011 | Reply

    • Hello Jeremy
      Thanks for your comment and feedback, I really appreciate it.
      There’s plenty to read so enjoy yourself!
      cheers
      john

      Comment by John Boxall | May 9, 2011 | Reply

  17. love the tutorials, thank you so much for posting it. just did the kitt light and took about 5 mins to double the leds and make a dual scanner, that starts in the middle and works it’s way to the outside. Looking forward to reading and trying the rest.

    Comment by brad velfling | May 30, 2011 | Reply

    • Hello Brad
      Great to hear you are having fun and success. Thank you for letting me know, I really appreciate it.
      Have fun
      john

      Comment by John Boxall | May 30, 2011 | Reply

  18. is it possible to use Shifting bit instead of doing off and on every time?

    Comment by kudrot-e-Elahi | August 2, 2011 | Reply

  19. If it’s near C++ and TTL or CMOS compatible so really good for me.

    Comment by psdsen | November 2, 2011 | Reply

  20. Why do the resistors go after the LED? Dont resistors usually go before the LED to protect it from too much current? Thanks for the tutorial!

    Comment by Matt | November 3, 2011 | Reply

    • They work either way – a resistor slows the flow of current along the whole section.
      cheers
      john

      Comment by John Boxall | November 3, 2011 | Reply

  21. Well, I’m just totally awesome. I haven’t programmed since I played with Apple Basic with my Apple II. I looked at your “problem,” and I purposefully avoided looking at the solution, so I could figure it out myself. I remembered the concept of the “loop,” and I realized that this was a prime candidate (to avoid separate lines for each LED). I figured out I needed the “for” statement, then how to set the initial LED to “2,” then how to “increment” the loop from 2 to 9. I then set up another loop to “decrement” the loop from 9 to 2.

    After battling with about a bazillion syntax errors, it finally uploaded, and it works! I even set a variable to make it easy to change the delay time.

    Using the loop, I greatly reduced the amount of code (and the tedium of writing it). I then went to look at your “solution.” I see you didn’t use a loop, but I see in the comments that a lesson in loops is in my future. Looks like I just accidently self-taught myself. :)

    Thanks for the lessons!

    Comment by Brad | November 4, 2011 | Reply

    • Hello Brad
      Thank you for your comments and feedback. It is great to hear you are learning and finding success.
      I hope you enjoy the forthcoming chapters in your journey.
      cheers
      john

      Comment by John Boxall | November 4, 2011 | Reply

  22. great, i’ll get my arduino soon, and this is really good to start, great page. I have a question, let suppose that i want that circuit in a board, but instead the arduino board i wanna use the ATMega328 (it’s microcontroler), to program it, can i put it in the arduino board, load the sketch and then use it in the final board?

    Comment by Dario | December 21, 2011 | Reply

  23. John, have just discovered Arduino CAN BUS shield and want to use it to “listen in” on my car’s CAN Bus. Can you suggest best reference for a tutorial? (Have downloaded sketches but can’t get them to work – still learning about the C++ coding but was looking for a “fast track” to get going. Had a look thru your tutorial list but did not see anything that mentioned CAN Bus … Thanks

    Comment by Con | January 6, 2012 | Reply

    • Not something I have looked into at all (I don’t drive a car). Perhaps ask in the arduino forum?
      cheers
      john

      Comment by John Boxall | January 7, 2012 | Reply

  24. hi…..i want to control any gsm mobile phone sothat i can send a single sms to many people using arduino UNO R-3…i don’t want to use any GSM shield..USB shield may b allowed but it’ll better if i not use any shield..is it possible?

    Comment by srideep nayak | March 5, 2012 | Reply

    • You can use older GSM handsets like Siemens C-series from memory to do this. Might be worth you looking into.

      Comment by John Boxall | March 5, 2012 | Reply

  25. i dont have much knowledge about Siemens c-seties…so please help with little bit more details

    Comment by srideep nayak | March 6, 2012 | Reply

    • Old Siemens C35 GSM phones have a serial TX/RX connection you can tap into and control the phone with. I have seen someone do this a few years ago. Search around with Google and see what you come up with.

      Comment by John Boxall | March 6, 2012 | Reply


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 2,588 other followers