Learn Arduino #1 – Blink!

Image result for led lighting

I wanted to start this tutorial series with a basic project to get going with – writing a program to get a LED to blink. While this may sound easy for anyone to do, it involves using many key Arduino programming principles, which I will introduce to you in this post (woohoo!). All you will need for this project is an LED and an Arduino board.

In This Tutorial

  1. How Arduino Boards Work
  2. How LEDs work
  3. Wiring the LED
  4. Programming with the basic ‘Blink’ sketch.

How Arduino Boards Work

Before we get started, you should know what different pins on an Arduino board are used for:

Untitled Sketch_bb.png

ARDUINO BOARDS ARE MADE TO DO TWO THINGS – READ/DETECT A VOLTAGE GOING INTO THEM, AND SEND OUT A VOLTAGE FROM THEM TO A COMPONENT/OTHER DEVICE.

  • The Digital Pins at the top have two functions –  they can send out a voltage to an electronic component/module (OUTPUTS), or detect/read a voltage going into that pin (INPUTS).
  • Analog Input Pins are used to read a voltage from an analog sensor (more about digital/analog sensors in future posts)
  • The power pins send out a continuous voltage for anything you need to power, such as a sensor.
  • The USB port allows you to program the Arduino from a computer
  • The Power supply allows you to take the Arduino on-the-go by attaching  a battery to it.

Don’t worry about anything else on the board – it’s not too important for now.

How LEDs work

LEDs, standing for Light Emitting Diodes, emit light when voltage is applied to them. Basically they are very small light bulbs. If you’ve ever had a Christmas jumper with lights all over it or RGB lighting behind your TV, they are made up of LEDs.

Pictured on the left is a red LED, however there are many different LEDs of different colours and sizes. These are very commonly used in Arduino projects so it is useful to know how to program them.

Image result for led

LEDs have two legs, two metal wires sticking out the bottom which the voltage will travel along to reach the top. The longer leg is known as the ‘anode’ and the shorter leg is known as the ‘cathode’. You apply positive voltage along the anode and negative along the cathode.

 

Wiring the LED

Untitled Sketch_bb

Wiring the LED couldn’t be simpler – just plug the LED into digital pin 13 and ground, with the longer leg (the anode) in digital pin 13, and the shorter leg (the cathode) in GND.

Programming the LED – key programming principles

Ok, now we’re on to the main event – the programming. Download the Arduino IDE or use the Arduino Web Editor. These instructions are for the Arduino Web Editor, because I prefer it over the Arduino IDE, as it does not require a download and has a nicer user interface.

Here is the first part of the code, explained below.

void setup() {
pinMode(13, OUTPUT);
}

 

Void Setup vs Void Loop

When programming Arduino, you generally write your code in two different sections, in void setup or in void loop:

  • You write any code in void setup if you want this code to run one time only, at the beginning of your program.
  • You write any code in void loop that you want to run repeatedly; this is where you generally put the majority of your code.

We only want this code to run once, so it goes in a void setup function. Void setup functions look like this:

void setup(){

}

As you can see, the code goes between the two curley braces.

pinMode

In the void setup function above, pinMode is used. As said earlier, digital pins can be used as inputs or outputs. You use pinMode to tell it whether to be an input (detect voltage going into the pin), or be an output (send a voltage out of that pin). They look like this:

pinMode([enter digital pin], [enter INPUT or OUTPUT];

 

This will set the pin up to be an input or an output for the program. In this case, we want it to send out a voltage to light up the LED, so it will have to be set as an output.

THE SEMI-COLON

You may be wondering why there is a semi-colon at the end of that line. It's not a mistake, it's simply there to tell the Arduino to move on to the next line of code. It goes at the end of practically every line.

Here is the second part of the code

void loop() {
digitalWrite(13, HIGH); 
delay(1000); 
digitalWrite(13, LOW);
delay(1000); 
}
//HELLO :D

This code in in the void loop function, because we want it to run again and again repeatedly.

digitalWrite

Digitalwrite is used to send out a voltage out of a digital pin. In this case, we start off by sending a voltage out of digital pin 13, the pin the LED is attached to, in order to light it up. Digitalwrite looks like this:

digitalWrite([enter pin number], [enter HIGH or LOW]);

  • You put HIGH if you want to send out a voltage
  • You put LOW if you do not want to send out a voltage.

delay

Delay is used to stop the program for a certain period of time, and is done in milliseconds. It looks like this:

delay([enter milliseconds to delay for]);

In this case, delay is used to give a 1 second blink.

The LED is powered on, wait one second, the LED is powered off, wait one second.

COMMENTS

Sometimes when writing long pieces of code, you may forget what something does later on down the line. How do you stop this? You comment.

By putting two forward slashes (//), the Arduino will ignore that line of code, meaning you can write whatever the hell you like.

Summary of the Code

Untitled Sketch_bb.png

Conclusion

If it has worked, you will have a nice blinking LED on your Arduino board. If it hasn't, check:

  • The LED is the correct way round with the longer leg in digital pin 13.
  • The LED is not broken.
  • The code is correct and you haven't misspelt anything or forgotten a semi colon.

If you have problems with this project, or have any questions, email piandchipsblog@gmail.com.

New posts for this tutorial series are scheduled to come out on the Tuesday of every week for ten weeks.

Thanks for reading, please like this post if it has been helpful.

 

Advertisements

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 )

Google+ photo

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

Connecting to %s