The Arduino LED Blink

Updated 22 April 2022

The practice of making and editing sketches is essential to get anywhere with the Arduino platform. We are going to step you through the basics when it comes to writing a sketch, and the most basic of all is the LED Blink. We will be telling our Arduino that a LED is connected to it and that we want to turn that LED on and off.

Yep, it is as simple as it sounds. Let's dive into it. 

Grab these components if you have them handy, if not take a look at our fantastic kits that come with a range of components for this project and future projects:

LED Blink Fritzing Sketch

Take a look at the Fritzing diagram provided, it represents how your hardware should be connected. Note that the LED component is polarized, meaning you will need the side of the LED with the flat surface to be connected toward the GND Pin (Black Wire). The resistor is not polarized so its orientation is not important as long as it is connected to the flat side of the LED and ground.

The basics of a sketch

Functions in programming refer to groupings of instructions. Every sketch must have the following 2 functions if it does not contain them it will simply not compile. We can also create extra functions to make our sketches run smoothly, but that comes later!

  • Void setup () { } - This is boot up the function of your sketch, this is where we will tell our Arduino what pins we are going to use and how they will be used. It will be run every time you reset or power up your Arduino.
  • Void loop() { } – This is our main construct, the set of instructions we give inside of the curly brackets here will be repeated as long as power is connected to your board. 

Designing your sketch from scratch

Arduino IDE with Blink Sketch loaded screenshot

  1. Load up your Arduino IDE and select new sketch from the file menu.

  2. Remember we said that we want to turn a LED on and off. At the moment, your Arduino is essentially ready for instruction but has no idea what you want. So let’s tell it that there is going to be a pin connected to a LED. 

  3. We use the pinMode() command to define what the pin will be used for. This command requires you to set two inputs, what pin we are talking about and how it will be used, separated by a comma. 

  4. As we see in our diagram, our pin is Pin 8. Set that as the first input inside the brackets. Then set the mode to OUTPUT, by entering output after a comma. This essentially means that we have told the Arduino to write a signal to that pin from this point onward. With DC voltages that signal will be either HIGH or LOW. A high signal will turn the LED on and a Low signal will turn the LED off.

  5. We are going to turn the LED on, make the program wait for a set amount of time, turn the LED off and make the program wait again

  6. To turn our LED on we need to write a HIGH output to it. digitalWrite is the command we use for this. Similarly to pinMode, we separate the pin number and the signal we wish to write with a comma. Terminate this line with a semi-colon. Set the LED on using digitalWrite(8, HIGH);.

  7. To make the LED stay lit for a set amount of time we use the Delay(); command. The number in the brackets represents how many milliseconds we pause the sequence for.

  8. To turn the LED off we use digital write, however, this time we send a LOW signal.

  9. Delay the program once again to hold the LED off for a period of time. 

That's it for the coding required to make our LED blink, but a few more things are needed to get it happening on our Arduino. We need to verify that we have coded our sketch up to the set guidelines for an Arduino sketch. It's a good habit to manually check your code before verifying it. Look for missing semi-colons, capitals and check that every bracket you open also closes. I tend to do this from right to left, bottom to top. Protip: mouseover brackets to highlight their corresponding closing bracket. 

If you think all is good, click the verify button. Your program should check your sketch and return white text in the debug console if you have done everything correctly. If not, you are going to have to read the debug console and fix what the error was. Usually, it will be a missing semi-colon in these beginner sketches. When all is up to scratch with our sketch, hit the Upload button to send that set of instructions to your Arduino. 

You should receive a upload successful message in the debug console, and your LED will begin blinking. If so, it means you have taken your first step into the wonderful world of Arduino. If not, you still took a step, but maybe in a slightly offset direction. Congratulations either way!

If this was too easy for you and you want to extend your understanding of this idea, try changing your LED control to a different Pin. You'll have to alter your code to reflect any changes. Or you could add more LEDs and experiment blinking them in a sequence with the delay command. 

Where to from here? Grab one of our recommended Arduino kits and jump into the more challenging projects they come with. 

Have a question? Ask the Author of this guide today!

Please enter minimum 20 characters

Your comment will be posted (automatically) on our Support Forum which is publicly accessible. Don't enter private information, such as your phone number.

Expect a quick reply during business hours, many of us check-in over the weekend as well.

Comments


Loading...
Feedback

Please continue if you would like to leave feedback for any of these topics:

  • Website features/issues
  • Content errors/improvements
  • Missing products/categories
  • Product assignments to categories
  • Search results relevance

For all other inquiries (orders status, stock levels, etc), please contact our support team for quick assistance.

Note: click continue and a draft email will be opened to edit. If you don't have an email client on your device, then send a message via the chat icon on the bottom left of our website.

Makers love reviews as much as you do, please follow this link to review the products you have purchased.