Writing your First Code - LED Blink


Arduino programming language is easy and it is based on C/C++ language.  We will start by opening a new sketch, a window will show up like this one (Using Arduino IDE 1.8.2 as of this writing):






A window with a code in arduino IDE is called a Sketch. This is the bare-bone structure for any arduino code. Believe it or not, this is a ready to go code, you can upload it to the arduino but the code will do nothing at the moment. Still a good way to check the arduino connection, but we'll tackle that later.

The main thing to notice here is the void setup() and the void loop(), you will notice that they have an enclosing brackets after them, that's where code is written.

Both  void setup() and void loop() will execute their code, but there are key differences:
            void setup() Always starts executing it code FIRST, and only runs the code inside it ONCE when you start/reset the arduino.
            void loop() Starts executing after void setup(), and runs it code repeatedly. Every time void loop() reaches the end of it's code, it repeats the code inside the brackets again, and again, and again forever... or until the battery dies!


But why?

Well.. these 2 blocks of code are made for different purposes (as can be read from their names):
        The void setup() is used to setup the arduino like setting up the pin modes or starting a serial communication.
        The void loop() is used to constantly repeat the code so that the arduino is always up, running and doing what it needs to do.

It is worth mentioning that the code doesn't differentiate between void setup() and void loop(), but some pieces of code need to be ran FIRST and only ONCE and other pieces need to run continuously, so you need to place them in their correct blocks in order for the code to work properly.

Now that we know the different parts, let's write a simple code. The goal here is to learn how code is written and how the arduino will behave, we'll go step by step until we reach the final desired arduino behavior.

We will write a code that blinks an LED ON and OFF. You do not need any extra electronic components since arduino comes with a builtin LED on pin 13. What we want to do is turn the LED ON and then OFF repeatedly, since it is repeated, that part of the code goes in the void loop() part of the sketch. But, in order for the LED to work, we have to set it pin to OUTPUT so the arduino supply sufficient power to it. pinMode() function will do that for us. That part will be added to the void setup() section of the code as follows:



The code is CASE SENSITIVE so be sure to get the proper cases for the letters. Now the arduino will set pin 13 to output when it starts running. The next step is turning the LED ON and OFF on pin 13, ON means we give it voltage higher than ground (usually 3.3v or 5v), which ever the value is, this is called HIGH digital logic voltage. And to turn it off we give it ground level voltage which is 0v, this is called LOW digital logic voltage. To set the voltage level on a digital pin we use digitalWrite() function as follows:




This will turn ON the LED near pin 13. Now we need to turn it off using the same function digitalWrite():


That should be enough!

Well... not exactly. The arduino can run this piece of code over a million times in a second. Microcontrollers are extremely fast for the human eye to catch up on whats going on, so if we upload this piece of code to the arduino you won't see the LED going ON and OFF, you will see it light up a little. That's because the LED is ON half the time and OFF the other half, and it's doing that a million times a second, since your eye is not that fast, it can only interrupt the "average" so to speak, and the average in this case is an ON LED 50% of the time, hence the dim light.

So, how do we fix this? 

We can tell the arduino to chill a bit so we can see the result, to make the arduino wait a curtain period of time, we need to use the function called delay(). This function will stop the arduino code a set period of time in milliseconds (ms), there are 1000 milliseconds in 1 second.

Now, what we want to do is to tell the arduino to pause a little bit, say 1 second, so we can see the LED go ON. Since we want to see the LED ON and OFF, we have to add a delay after each of those lines of code, so let's add a 1 second delay after the ON and OFF code for the LED. Remember that 1 second is 1000 milliseconds:


This'll do the trick, this is a fully completed that can be uploaded to the arduino, you can verify the code for errors by clicking the "check mark" button under the "File" menu button. The IDE will ask you to save the file, which is always a good idea, I'll save my file as "MyBlinkingLED" but feel free to name it anything you like. Once saved, your code will be verified, if there are no errors, u will see a "Done compiling" message at the blue bar bottom of your code screen :


If you see this, it means your code is clear of syntax errors and may be uploaded to Arduino. 

There are a few settings we need to set before uploading though. In order to upload the code to the arduino we need to Connect the arduino to the USB cable and we need to choose the arduino Board we're using as well as the Port, you will find these settings in the Tools menu and then Board (I'm using arduino Uno so I'll pick that from the list):



Now for the Port, you need the drivers installed in order for the port to show up. You can check our guide on Driver Installation if you haven't yet.
Note: The new arduino IDE (version 1.8.2 as of this writing) installs the driver along with the IDE so you don't have to do it manually.

To set the Port, it is also in the Tools menu just under the Board we set previously, if the drivers are installed correctly, you should see the name of the board next to the port COM number:



Now the code can be uploaded. To upload the code press the button with the arrow point to the right. If all goes well, you should see "Done uploading." message:

You should be able to see your arduino LED blinking, you can play around with the delay values to get different effects. The two delay values do not have to be the same. Be sure to upload your code every time you make a change so the arduino is updated.

Congratulations on your first code. And if your code did not work (which is a very common occurrence) please post a comment in the discussion section below and we'll reply as soon as we can. Most of the errors that happen on arduino are already solved online by the Arduino community, just type in your error on google and off you go.

Good Luck!

We ship to the GCC and MENA regions.

Reliable and fast shipping to your door, quality assured.

More about shipping

Find us on
Instagram

Just type @idea_link

Visit our profile