Arduino. Hello World !

I've always been intimidated by the Arduino, because it always felt like the people who dabbled in it belonged to an exclusive club or clique, and for some reason this exclusivity was beyond my reach. This may seem weird because as a Software Engineer, I've been able to confidently acquire new skills  over time. The Arduino though, involved a mix of software AND hardware, and it was this hardware element that I found intimidating.

A few years back, I was involved in a project at work, that involved experimenting with an Arduino. I was merely a bystander in that project, but I had invested impulse bought a $20 Arduino micro in the hopes of tinkering with it. I never did. The microcontroller sat in its box on the floor of my basement for a couple of years.

A few days ago though, I though of a cool concept that would require the use of an Aruduino. At a very high level, I could confidently say that this microcontroller would help me achieve my goals. But, I didn't really know how to use it.

Learning

My method of learning something new is usually by employing brute force. I attempt to learn the most complicated part of a problem and work backwards towards the fundamentals. This time around, I decide to learn the basics first. I enrolled in a course on Udemy from hackster.io 

This is how I wrote "Hello World" on an Arduino

Hello World

Most traditional Programming languages have a print command that let you write "Hello World" to the output or terminal. The Arduino does not have a display, but it has an led. Therefore the Hello World for an Arduino is "Blinking the LED"

An LED, is a light source, and we know that most light sources have two states ON and OFF. In reality, a light source can also be dimmed depending on the amount of electrical current flowing through it. The two extreme states are therefore called HIGH (on) and LOW (off)

An Arduino has multiple pins that can be used either to receive input or send output. Input and Output in this case refers to electrical current. In the photo below, Pin 13 routes through an LED on the board into the microcontroller. Therefore, if the microcontroller sends or receives I/O (electrical current) to or from pin 13, that LED with light up.

So, the goal of the program is to send output to Pin 13, in a way that the LED blinks. This is the equivalent of Hello World.

Code Anatomy and Lifecycle

Arduino code has two main functions that a part of it's lifecycle

  • Setup - This is where you write code that you want to run only once at set up
  • Loop - This is code that runs continuously

Setup

In the setup function we specify that we want to use pin 13 to send output. This is done by calling the pinMode function

Loop

In the loop function we are trying to get the LED to blink. This involves turning the LED "on" and "off" with some delay between those operations. The digitalWrite function, directs current to the specified pin with the specified amount of voltage. The delay function pauses the execution for the specified amount of time.

Uploading Code to the Arduino

Arduinos come in different models but all of them connect to a PC or MAC via a USB cable. I connected mine via a micro USB cable. Once connected, the Arduino can be located in the Tools / Port section of the menu. Selecting the Serial Port lets you upload the code and receive output from any running code

You also have to select the correct board, so that the IDE can compile your code correctly. This can be found in the Tools / Board section of the menu.

You can now upload the code (which also compiles) it by clicking on the upload arrow icon. 



Success !

If all goes well, after upload you should see a blinking LED