How To: The Hardware Hacker's Introduction to Microcontrollers, Part Two: How Does Arduino Think?

The Hardware Hacker's Introduction to Microcontrollers, Part Two: How Does Arduino Think?

In this article, I'll be continuing my series on microcontrollers. If you haven't read part one, I'd recommend heading over there and reading it!

So How Does Arduino Think?

Arduino is programmed with ones and zeros, but hopefully that's not how we programmers have to write the code. Most Arduino programs are born through the use of C++ and C, two very common computer languages.

Now, you may be wondering what a computer language even is! Simply put, a computer language is a tool used to develop a set of instructions a computer is capable of understanding. In this case, the computer is an Arduino, and the set of instructions are quite simple (at least in this article!).

A Look into the Code

A very simple Arduino script usually starts out with an initialization of a variable. To create an integer variable (meaning the variable can represent integers), 'int' is used. For example:

int led = 13;

This line of code creates a integer variable called 'led', and assigns the value of 13. Now, we need to do something with this allocated variable. The next step is to create a function in which we will use the data stored in 'led' to turn on a physical LED!

void setup() {

Basically what this does is create a function from which no value is returned, hence 'void'. The function, in the context of this Arduino program, gives you the opportunity to configure any physical "pin" inputs or outputs, and which pin to use.

pinMode(led, OUTPUT);
}

This assigns the pin number to 13, and a digital setting of "output" to that pin. 'pinMode' lets Arduino know we're about to change the settings on a pin. Since 'led' means '13' in the Arduino's mind, we can simply put 'led'. This also makes it much easier to change the pin in the future, as we can just change the variable to a different integer!

Pin 13 on the Arduino happens to be an LED already installed, so it's quite easy to try out this program without any extra hardware apart from the device itself. You can clearly see the LED circled in the image below:

Image via ladyada.net

Or, if you have the hardware available, you can add your own LED like so:

Image via arduino.cc

There is still one part missing, the actual instructions!

At the moment, the Arduino knows what the digital setting of pin 13 is, but hasn't any idea what to do with it. What we move into now is another function, called a 'loop'. Commands in this function will be repeated in order until otherwise noted.

For example, if we had a simple function of:

void loop() {
led = 1;
led = 2;
led = 3;
}

Our function will now loop through these instructions, setting 'led' first to the integer 1, then to 2, then to 3. Once the program reaches the '}' notifier, it starts at the beginning again.

For our program, we want to blink the light every second. To do this, we need four commands. An 'on' notifier, a delay, an 'off' notifier, and finally another delay.

void loop() {
digitalWrite(led, HIGH);
delay(1000);
digitalWrite(led, LOW);
delay(1000);
}

The command 'digitalWrite' lets Arduino know we are about to send a pre-configured pin some information changing its current state.

In this case, we first define the pin as 'led' (meaning 13), and then set its state to 'HIGH', meaning full voltage on. Next is the delay command, which is in the format of milliseconds, and takes integers. We then turn the LED off, and wait another second before repeating the loop.

The Final Step: Compiling!

Now that you have written the code, the final step is to format it and send it off to the Arduino via USB. By "format", I mean compile.

Compiling basically turns your C++ and C into hexadecimal, which is one hop skip ahead of binary (ones and zeros). If you're curious, check out this article here explaining it.

You can upload your code by going to File, and then to "Upload to I/O board", like so:

Image via ladyada.net

Once the Arduino receives this special hexadecimal code, it begins to execute your program. You'll see the light blink! If not, check your code and make sure there are no mistakes.

Stay tuned for part three...more complicated programs!

Of course, be sure to check out some of my other articles, such as Making Electromagnetic Weapons, Shocking People with Your Fingertips, and Electron Spirographs with an Old TV!

Just updated your iPhone? You'll find new emoji, enhanced security, podcast transcripts, Apple Cash virtual numbers, and other useful features. There are even new additions hidden within Safari. Find out what's new and changed on your iPhone with the iOS 17.4 update.

Cover photo by Gene Lu

Be the First to Comment

Share Your Thoughts

  • Hot
  • Latest