Quantcast
Viewing all articles
Browse latest Browse all 45

BLINKING AN LED USING AN ARDUINO UNO (EXPLAINED)

Image may be NSFW.
Clik here to view.

So you’re here because you want to start learning some of the Arduino Basics… So let’s start with THE most basics… no this "tutorial" is actually a little more basic than what you have in mind. It only requires that you:

  • Have ANY Arduino Board.
  • Have installed the drivers and required software for the board that you are using.
  • Have a breadboard.
  • Have one LED.
  • Have one resistor (preferably 220 Ohms).
  • And some jumper wires (M-M)1.



In fact this is ALL you will need for my second tutorial as well. Just follow the pictures and schematics below and there is no way that you will get lost along the way. In C… this is the language that you will be coding in, you need to remember:

  • HIGH=ON
  • LOW=OFF

OK! So now we are ready to get started with the HIGH/LOW tutorial.


In my tutorials you will always find that I use Black for Gnd and Red for +ve – just so that you do not get confused – ANYWHERE you see Black Jumpers in my tutorials, that is Gnd and Red is +V.

STEP 1: "Breadboard Circuit"

Have a look at the schematic below on how I am connecting this on the breadboard.

Image may be NSFW.
Clik here to view.

Now what is left is to connect the Arduino to the + and Gnd, and then program it!

Step 2: "Connecting to the Board"

This step can be a little tricky, so pay attention. On the Duemilanove (the one that I am using for this demonstration) are – avoiding the analogue in and power slots-16 pins where you can connect your LED. So you have Pins 0-13 (that’s 14 pins including 0), one Gnd Pin, and Aref. For now we will be using the Gnd Pin (the second from last on your board), and one pin between 1-13. It is important to remember what pin you are using, because you need to define it when you program the ATmega chip on the board.
For this demonstration, I have chosen pin 5 for my + (RED jumper), and the Gnd Pin (BLACK/Gnd Jumper), and I have connected them as such:

Image may be NSFW.
Clik here to view.

With the wires being connected to your board, it is now time for the final and most important step of “tutorial 0”, programming.

Step 3: "PROGRAMMING"

Now that we have connected everything, lets fire it up! The first thing you need to do (rather than recheck if you are about to short circuit something), is connect the board to your PC. Once you have connected the board you will see the fixed led on pin 9 start blinking for a few seconds. This means the board is working properly and you are ready to start the Arduino IDE.

Open up the Arduino IDE and you are ready to start programming. Don't forget that this is C you are coding in, so before I start shooting commands, you should remember: CASE SENSITIVITY IS VERY IMPORTANT!

Even though other programming languages may give you some error messages, this editor is much more complicated. As a matter of a fact, it is not as user friendly as you may imagine, and if you are like me –that is being interested more in the results rather than the details- you may have a hard time understanding the errors that you may or will receive.

So let's have a look at something really interesting that the Arduino programmers have installed into their editor in order to make it easier for people like us to know when we are making mistakes. If you type a command that is not recognised by the system, the command stays in the usual text colour. That is what you are reading now – BLACK. If the command that you have entered is correct, it immediately turns yellow. You do not need to hit enter or anything for this to happen. If the command that you are typing in is correct, as soon as you are done with the cmd input, it turns yellow. While this is very true, it is actually tricky, so use this to CORRECT yourself and DO NOT rely on it.

Shown below are two examples showing correct and wrong commands. Before you actually read below and realise what mistake has been intentionally made on the commands, you sould try and find it yourself. So look closely.

Image may be NSFW.
Clik here to view.

In the second command– Void loop () – the command should be void loop, instead of Void with a capital V. That's how easy it is to make mistakes in the Editor. So pay attention. And remember good programming principals make good programs, especially if you are working on a rather large program for your projects.

So now we know how to avoid a few basic errors, we have picked up some really good programming principles, and we know that the FIRST two commands that we have talked about are:

void setup ()
{
}
 
void loop ()
{
}

These are the 2 MOST basic commands that need to be present with or without anything inside them, or else your program will not run.


Ok, so we have talked about the setup and loop commands, now let’s get some current flowing through our LED. Remember, we have set the RED wire to pin 5 and the BLACK to Gnd.

The first thing that we need to do is define where the LED is. To do that, we use the integer ( >int) cmd as follows:

Image may be NSFW.
Clik here to view.


Now let’s analyse this a little bit.
So we have >int ledPin = 5:
Two things to note here:

  • "ledPin" can be changed to whatever name you want as long as you remember it, and as long as 5 is the pin you have connected the RED jumper to.
  • And the ";" (Semicolon). Now this is also important. Just remember that at the end of every line INSIDE A COMMAND, you need to add a ";". There are exceptions to this and you will learn these as you go.


Now we have defined the pin where we want the current to flow. So what comes next?


Now we need to define what kind of MODE the pin will be running. There are 2 kinds of modes in the Arduino environment, INPUT/OUTPUT, both of which need to be in CAPS! In order to define the Modes we use the command, >pinMode(ledPin, OUTPUT).

We use OUTPUT because we are trying to control an LED. An LED is an input device so the pin needs to be an output. If we were trying to sense a button, an output device, we would use INPUT in the secound field of the pinMode.


The command ends up looking like this:

Image may be NSFW.
Clik here to view.

VERY IMPORTANT
Note how the pinMode command is situated inside the {} for the void setup command? >pinMode cmd always needs to be in there, since you are defining the setup for the sketch/program you are about to upload.

Now that we are done with defining the mode of the pin, let’s start seeing some results. Let’s light the LED.
In order to light the LED, we need some kind of a switch. Something that will trigger the LED to turn on (HIGH or ON). Since the pin that we are using is a DIGITAL pin we need to use the command digitalWrite, whose syntax is:

Image may be NSFW.
Clik here to view.

Let’s try and upload this to the board now and see what happens! To upload, make sure you have the correct board and COM port selected under tools, then hit the arrow button next to the check mark.

The LED should switch on (if it does not, check your wiring). Now in order to switch it off, you need only add LOW where HIGH is.

Step 4:

Now I will introduce a new command that will allow you to make some pretty cool stuff, if you can look a little outside the box and become creative, delay. So basically what delay does, is exactly what it says. It delays an action that you have already defined. So for instance, in the sketch that we have composed above, if we add a delay of, let’s say, 500ms (milliseconds) and then switch the LED from HIGH to LOW with another delay of the same duration, i.e.:

Image may be NSFW.
Clik here to view.

What we have done inside the loop here is:

  1. We have switched the LED on using HIGH,
  2. We are allowing a delay of 500ms (1/2 s),
  3. We have switched the LED off using LOW,
  4. We are allowing a delay of 500ms (1/2s),

Question: What would happen if the above was NOT situated inside the void loop cmd7?

If the above code was not situated inside the loop then it would have only lasted for 1 second = 1000milliseconds. The program would have switched the LED on once and then off and then it would have kept it off. ;) try it!

Having read this very basic tutorial, I hope that you have understood how to use the loop, delay, and HIGH and LOW commands, as well as the integer and digitalWrite cmds. A video of the blinking LED tutorial may soon be uploaded onto www.jayconsystems.com, so have a look there and any questions that you may have, please feel free to contact me on tsappasis@hotmail.com.

The next tutorial that I will be compiling for you is a very interesting project that I am working on at the moment that consists of ONLY these commands that you have just learned.

Mike Tsappas


Viewing all articles
Browse latest Browse all 45

Trending Articles