while loops

Arduino – While Loop

While there are a few choices for loops to do while coding in Arduino(Puns intended), you will find a lot of different situations to use each of these loops. We took a look at the for loop in this post, and discovered some of the uses for that. Now we’re going to take a look at the while loop, which is a little different, and I’ll explain to you why and when to use this loop.

while loops

What is it?

The while loop is a block of code that is run while the condition between the parenthesis is true. This is useful when you’re waiting for something, or you have to send a serial command somewhere repeatedly until the desired outcome is returned. It will stay in this loop until the pre-determined requirements are met. This is different from the for loop, because the for loop loops through and increments a variable until it hits a certain number. The while loop can also be written to do this, but that’s not what it’s designed to do.

Uses

One of our examples is going to be a listener, we’ll have the while loop wait until it sees that there is serial data available to read, then it will take action on the serial data that is received.

Another example is a temperature monitor. You can have it sit in the while loop and only do a temperature reading until it changes, then when it changes you can have it send a text message, write to a database, or do any other task imaginable(except split atoms, Arduino is not designed to perform that).

Usage

The following is they syntax from Arduino.cc:

while(condition){
  // statement(s)
}

Let’s break that down, it’s a very simple piece of code, right? In the first line we tell the compiler that we’re using a while statement, by stating “while”. Immediately after that we have a set of parenthesis, inside these parenthesis will be the condition that you are testing. If the condition immediately returns false, your code will never be run, otherwise it runs continuously until it returns false.

You can also use “break” to break out of the loop. This would effectively add a second condition to the while loop, if you use it inside an if statement. For example your while loop tests for sensor a to change, and your if statement tests for sensor b to change. When sensor a changes you have it return false to break out of the loop, when sensor b changes you have it return true to break out of the loop(because an if statement only reads the code inside the curly braces if the condition is true).

 

You can run forever loops by doing while(true). This is not recommended, but it can be done. An example of a forever loop is the “loop()” function that Arduino uses to loop the code you write. This is a loop that is always run, unless you use a break somewhere to remove it from the loop. You can get stuck in this loop indefinitely, so it’s not recommended to use forever loops. Just mentioning it today, so you do know that it’s possible, and that it’s also a risk.

Examples

A great example for this loop, is the post I did about serial communication via the serial monitor. The following is the code from that post:

String data;//initialize string variable

void setup() {
 Serial.begin(9600);//initialize serial
}

void loop() {
 while (Serial.available()){//loop while data is available
 Serial.print("You typed: ");
 data = Serial.readString();//read the data
 Serial.println(data);//print the data to serial monitor
 }
}

If you look through this code, you’ll see in the first line we set up a string named data. In the setup function we instantiate serial at 9600 baud rate. In this case the loop will not run until serial data is available, which would mean that something was sent from the serial monitor to the Arduino. To trigger this, you just have to type something in the serial monitor. Then it does the loop until no more serial data is available, and waits until next time it is available. You can learn more about this process in this post.

Hello World Tutorial

Sending Serial Data From PC to Arduino

Today we’re going to learn how to talk to the Arduino. We have already established how to listen. Listening is cool and everything, but we all like to be able to talk sometimes too. This can be very useful when learning how to send commands to your Arduino. You can send text, and have the Arduino act based on what you sent. That’s not what we’re doing today, we’re just going to do a simple send from the serial monitor, and have the Arduino tell us what we said. Do I hear an echo?

What is it?

I always like to start with a what is it. What is it to send serial data from the PC to the Arduino? We all know what serial is, it’s a protocol which literally communicates in 0’s and 1’s, or highs and lows – very dang fast. The serial monitor makes it very easy to send things to the Arduino. There’s a send box built right in to it! All you do, is type in what you want to send, click send and it goes. The part we need to figure out is how to receive that data with the Arduino.

Uses

One of the common uses for this, is as a debugging tool. You can create smaller sections of a large program, and test them using this method. If you’re planning on acting based on received data from another device, you could simulate that data by hand in the serial monitor. Then you can see when and how it acts based off of your input.

Another use is for triggering functions. You may use this as your primary method of controlling the Arduino. We’ll get in to an example of that in a later post, in that post we’ll control an led using text.

Usage

This is the syntax from Arduino.cc:

Serial.readString() 

That’s it! Super complicated, huh? No arguments, nothing! This function simply reads the entirety of what you’ve entered, and you can add it to a variable.

Example

Here’s an example of the usage of Serial.readString().

String data;//initialize string variable

void setup() {
 Serial.begin(9600);//initialize serial
}

void loop() {
 while (Serial.available()){//loop while data is available
 Serial.print("You typed: ");
 data = Serial.readString();//read the data
 Serial.println(data);//print the data to serial monitor
 }
}

This code does nothing, until it sees serial data is available on the usb receive pin. It will simply loop and wait for serial data. Once it sees that there is serial data, with Serial.available(), it will parse that data. The next line you see is simple serial.print stating “you typed:”. Followed by data = Serial.readString();, this will assign the data to a string for later use. The final line prints out the data read from the string. Very simple!

Let’s upload, and run it!

Now, we can open the serial monitor, the output will be blank. In the top of the serial monitor, there’s a text box where you can type. Next to it is a send button.

In here, we can type what ever it is that you’d like to send for your test.

Let’s give it a whirl, and click send!

Now we’re well equipped for some serious conversation with our Arduino! Some of us aren’t able to make friends as quickly as others, so you can obviously see how this would be beneficial in the future.

for loops

Arduino – For Loop

Arduino’s language is packed full of goodies that make things quite a bit easier to accomplish. Without loops, we would rewrite code over and over again, and it would be the ugliest code you’ve ever seen.

It takes a while before some people even start using loops, you’re in the right place if that’s you. Using loops will change the way you structure your sketches completely. You’ll find this is a huge time saver. Some times a little effort goes a long way!

for loops

What is it?

A for loop is a loop that runs a set amount of times. It loops until a predetermined condition is met, you set this condition at the beginning of the loop. Once the condition is met, it breaks out of the loop, and continues on with the code.

Most of the time, it’s good practice to keep the loops in a function or an if statement. Doing this avoids slowing down your sketch, by running through the loop every single time your main loop starts over.

Uses

One of my favorite uses, is using for loops to cycle through each value in an array. One example of a for loop that I used recently, was in a weather station. I designed a weather station that had 16 underground sensors. Instead of running a function 16 times, I ran a for loop that counted up to 16, and ran the function to read from each one individually. This took much less code than it would have otherwise.

Usage

This is the syntax from Arduino.cc:

for (initialization; condition; increment) {
 //statement(s);
}

Let’s pick that apart.

You of course state that you’re using the for loop by starting with “for”. The arguments to the for loop can be tricky at first, but they are fairly easy to understand. You start with initialization. I prefer to start with “int i”. You initialize an int here generally, because you’re going to use that int to count with. This is the variable that you’ll use to determine when the loop is done.

The condition that you’re setting here is what needs to be true for this loop to be done. You could put i<=10 and it will run until it is equal to 10.

The last portion here is increment. Normally I’d simply put i++ here. This is where you’ll add to the int that was defined.

An important note: If you use this to count things in an array, arrays start with an index of 0. So if you programmatically have your sketch determining the length of an array, you need to make sure you’re calling array[i-1]. This would start your index at 0, and make sure you don’t go over the amount of values that’s in the array.

Example

This example will count to 10, waiting 1 second in between each number. You can speed this up by changing the delay at the end of the loop:

void setup() {
Serial.begin(9600); //Initialize serial output
}

void loop() {
Serial.println("For loop starting");

for (int i=0; i <= 10; i++) { //initialize the for loop, it will count i up to 10, then be done
 Serial.print("Counting:");//Write the text counting
 Serial.println(i);//Print out the value of i and start a new line after
 delay(1000);//Wait 1 second
}

Serial.println("For loop complete");

}

Let’s see what the output gives us on the serial monitor.

There you have it! Now we can count to 10, without having to use our brain!

 

Arduino Statements

Arduino – Switch Statements

The switch statement, in Arduino programming, is very similar to the if statement that I wrote about previously. The main similarity is that you’re testing a statement, if it equals true, certain code is ran.

The difference is versatility and ease of use. When looking at a switch statement, it appears to be much more complicated than an if statement. It’s not! Actually it is much more simple to use, and produces a lot less code.

You will thank yourself later for making the switch, the switch statement switch.

What is it?

Without showing you any code right away, I’ll explain what it is in human language.

A switch statement takes a variable as an argument, and evaluates it. The actual switch part comes after. Now that it knows what it’s dealing with, it looks at the possible matches that you’ve preselected. When it finds a match, it runs the code until it comes across the word “break”. The break makes it skip the whole rest of the switch statement.

If there is no match, it runs the default code. The default code can be blank, and doesn’t have to contain anything juicy. A lot of the time, my default code just outputs to serial saying there was no match.

Uses

An example, where you would use a switch statement in the real world, could be in actual smart home light switches. Maybe you have a function that toggles them off and on when your cell phone app communicates with the arduino.

You could have the communication set a variable based on what button was pushed. Then the variable gets passed in to the switch statement. The switch statement sees the variables value of “living room” and associates it with what it knows, which may be switch number 3. Then inside your statement it can set variables and call functions based on switch 3.

And then you have light on, light off. This statement can reduce a lot of code, where you may otherwise have to write a function or if statement multiple times.

Or you may decide to put the switch statement inside the function, which would produce the same result as the function being inside the switch.

Usage

This begins with a variable that you’re going to test. This variable can be anything, an int, a string, char, etc. You start the switch statement with “switch (var)”. The “var” is where you’d place the name of your variable.

Much like other things you come across while programming for Arduino, you place the code to be run in curly braces. { and }

The difference here, from the other statements, is that the conditions that you are testing reside inside the curly braces. Normally your arguments would go inside the parenthesis, not today!

The way you compare what your variables value is, looks like this: “case condition1:”. You’d replace condition1 with the value you’re testing against your variable. For example, if you’re trying to see if a string says “taco”, you’d replace condition1 with the word taco. It would say “case taco:”.

Ending each code block after you define your case is very important. You end it with break;. If you do not break out of your code, either the compile will fail, or you’ll end up with unexpected results. This is the only time you do want to break your code, instead of fixing it.

 

The last part of this is the default portion. You define the default code to be run by calling “default:”. Any code between the “default:” and the “break;” is run if none of the other things you defined turned out to be true. Sometimes there are errors that cause the variable not to be set right, sometimes you planned on using default for multiple cases. Whatever the case is that you’re using default, you will know. I’ll say again, that you do not have to use the default section. It does need to be there, but it can be simply “default: break;” if you want your switch ignored when nothing is true.

Examples

This example will be assuming the ability to read based on the age of the person. We’ll start by defining the age variable:

int age = 0;

Then we’ll initialize Serial 0, so we can output to the pc:

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

In the loop we tell switch what variable we’re testing, and what each condition will do. In this case we’re just outputting different words to the serial monitor. After the switch statement is over we do age++, this simply increments the int by 1. You can increment any int this way, and if you have it being done in loop, it’ll just keep on adding. After we have a 5 second delay, before the loop starts over again.

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

I’ll paste the entire code now, if you’re copying and pasting it, you can use this one:

int age = 0;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

switch(age) {
 case 1:
 Serial.println("You're only 1, how can you read this!");
 break;
 case 2:
 Serial.println("You're only 2, you can talk, but surely still can't read.");
 break;
 case 3:
 Serial.println("You are 3, I'd be pretty impressed if you could read.");
 break;
 case 4:
 Serial.println("You're 4, you may be able to read, but I still don't believe you.");
 break;
 case 5:
 Serial.println("You're 5, I guess it's time to start reading.");
 break;
 default:
 Serial.println("You are either older than 5, or younger than 1. Do whatever you want, I guess...");
 break;
}
age++;
delay(5000);//wait 5 seconds
}

Let’s see what happens when we run the code!

switch output

Exactly what we were hoping for! It counts from zero to a bazillion, and correctly chooses what switch condition to use.

Let me know if you have any questions below! More than happy to answer them.

Arduino Statements

Arduino – If Statements

Welcome to If Statements! This is where you will learn to argue with your Arduino about whether or not a statement is true. If statements are very commonly used, get to know them well. Disclaimer: You will not win an argument with your Arduino, it always knows if a statement is true or not. This is similar to marriage, you may find yourself upset if you choose to argue with your Arduino.

What you need:

What is it?

An If Statement is a block of code that gets run depending on an statement you supply to it. If the statement is true, the block of code is run. If the statement is not true, it skips the block of code.

If you supply an else if statement, it will test that statement to verify if it’s true.

If that else if statement is false as well, and you supply an else statement, it will skip to the else statement and run that code block.

You don’t have to supply an else if statement, if your first if statement is false, it will go directly to else. You also don’t have to supply an else statement, if the first if statement is false, it would just not run the code at all without an else statement.

Uses

A good example of an if statement would be in a weather monitoring application. You could have a device monitoring the temperature, and if the temperature is below 32 degrees Fahrenheit, it will tell you ice is possible on the roads. An else statement for this could be an output stating that the roads should not have ice on them.

Another example would be when using an RFID tag. You could have a list of acceptable RFID tags, and if the one that is scanned belongs to the list, the door opens. An else statement for this would be a red blinking LED, that indicates a RFID tag that is not allowed access.

Usage

Lets look at the anatomy of an if statement.

The syntax from Arduino.cc looks like this:

if (condition)
{
  //statement(s)
}

Statement to be tested

Declaring the statement starts with “if ()”, you must have parenthesis here. In between the parenthesis, you put the condition that you’re testing. An example would be “if (x > 10)”. If x is 11, it would run the code. If x is 1, it would not run the code.

Code to be run

The second part of this is the curly braces { and }. This is where you put the code to be run. Anything outside these braces will be run, regardless of whether the condition is true. Inside the braces is code to be run if the statement is true.

If the If gets run, the others are ignored

The main thing to remember here, is that if any if statement equals true, the code gets run and any other part of the if statement is ignored. This means that it skips any else if, or else statements.

Examples

Simple predefined variable:

This example uses a predefined variable, which is an integer named age. This variable is declared right at the top of the following code. Here we’re trying to figure out if the person is old enough to drink. In this case the person is 14, so it should determine that the persons age is under 21, and run the else statement.

int age = 14;

void setup() {
 
Serial.begin(9600);//Initialize pc communication

}

void loop() {

if (age < 21) {
 Serial.println("You are not old enough to drink!");
}
else {
 Serial.println("You are 21 or older, you may have a drink.");
}
delay(5000);//wait 5 seconds
}

Let’s run the code, and see what we come up with. If you’re unsure how to upload code, check out my hello world post. Here is the output you should expect from the above code:

Success! It determined that we are unable to drink.

Let’s change the age variable to 24 now, and see what we get.

int age = 24;

The Arduino wins again! If statements are very useful when writing Arduino sketches. You’ll find yourself using them quite often. Let me know in the comments if you have any questions, and please subscribe for additional Arduino tips.

If you’re looking for something a little more versatile, you may try Arduino Switch Statements.

Key features for Arduino IDE

How to use the arduino ide interface

In this post, I’m providing you with an introduction to the arduino ide interface. I was writing sketches for months, before I realized I was missing out on some of the key features of this awesome tool.

I will outline some of the most important features you’ll use when writing sketches.

 

All of these features are critical to making the most of arduino development, and are often overlooked by beginner developers.

Board selection

Here you select the board you’re using, this is important for when you’re compiling your sketch. If you pick the wrong board, your sketch may not run properly. The sketch is built by the compiler, specifically for the board you select, and some boards do not contain the same features or configuration as others.

Com port selection

This is where you select the port assigned to your arduino, by the computer. Without setting this, the ide doesn’t know where to upload your compiled sketch. If you own multiple boards, they will each be assigned to a different com port.

Tabs

It was a long time before I found out that tabs were a thing in the arduino ide. The significance of tabs is that it not only allows editing multiple files at once, but these files will build together as one sketch when compiled.

This feature allows you to separate the pre-set up, setup, loop, and any other functions or variable definitions in other tabs. It’s extremely useful for readability of long sketches. You can separate things, so when you’re looking for a certain function, it’s easy to find where the function starts and ends.

Libraries

Libraries are an amazingly helpful tool. Third party libraries provide additional functionality, that is not otherwise included in the arduinos built in library set. A good example of this is the esp8266. This is a wifi device, that would take weeks to set up without an available library to help drive it.

The arduino ide makes it very easy to manage libraries with searching and quick links to the source of these libraries.

Examples

Example code is provided with most libraries, and can be accessed very easily within the arduino ide. You don’t have to navigate to wherever it is that your libraries are stored, and find the example code. They are built right in to the interface, for easy access. This is another one I didn’t notice until months after I started writing arduino code.

Auto format

Auto format is another must for code readability. This feature automatically makes sure your curly braces are lined up, to help find beginning and ends of enclosed code. It also automatically indents code within the curly braces further than the curly braces are already invented. This extra indent ensures that the curly braces don’t get lost within the other code.

Verify code vs upload

A common question you may ask yourself when writing a sketch is, am I doing this right? Or, my code makes sense to me, will it make sense to the device? Or you may be writing a sketch, and your arduino isn’t near by to upload to.

That’s where the verify option comes in handy. It will compile your code and error check it for you, without the need for an arduino device to be plugged in. Then you can debug your code before trying to actually run it.

Output console

It may not be long after you start coding, that you find yourself constantly staring at the output console. This area will display tons of important information, about what ever action you’re currently performing.

If you’re opening serial monitor, it may let you know it couldn’t bind to the port. Then you might have to ask yourself, is my arduino plugged in?

If you’re opening serial monitor, it may let you know it couldn’t bind to the port. Then you might have to ask yourself, is my arduino plugged in?

Imagine coding without this. Would you sit and read through a thousand lines of code, to realize you forgot a semicolon on line 753? I sure hope not. I would give up pretty quickly.

Serial monitor

This is where you can have your arduino talk back to you. This is infinitely useful when debugging, or verifying that your functions are returning the right results. They made it very easy to output anything you’d like right to your computer.

You could, of course, use a different serial monitor program. It’s much easier to use the arduino ide as an all in one tool.

Baud rate

Your baud rate is what calibrates communication speed between the device and your computer. Without these 2 devices matching, you would get nothing at all, or a jumbled mess of nothing that makes sense.

Baud rate is a very important, and easily forgotten setting that is extremely critical. A lot of peripheral devices for your arduino will also require a specific baud rate. Each device you connect can run at a different rate, so you’re not limited to just one.

Serial send

Serial send is an amazing feature. This allows you to send data to your device, typed right in to the serial monitor. You can code your arduino device to interpret this data, and relay the message to other devices. This allows you to test functionality of devices, and determine how you’re going to communicate with them.

Questions?

Feel free to ask questions, or provide tips for others in the comments. I’m very open to questions and suggestions, that’s what I’m here for.

Maybe now you’re ready for the Serial Hello World Tutorial. This is a good place to start coding.

Hello World Tutorial

Arduino Serial Hello World

Hey guys, this time I’m going to introduce a hello world example outputting to the serial monitor on your Arduino IDE. You’ll learn how to write a very simple code that sets your first variable, and output that variable on to your computer screen.

What you need:

Pre-Setup:

This is where we create our variable. Our variable is a character array, more commonly known as a string. Arduino sends data via serial byte by byte, meaning 8 bits at a time. Later, you’ll find that you need to define the length of what you’re sending, when communicating with other devices some times.

The following code is our simple initialization of our character array. The key here is starting with “char”, this tells the IDE what type of variable we’re trying to set up. Next we name the variable “myString”, this is the second part of setting your variable. Then we set the char type to array by adding “[]” to the end of the name. Finally we set the value of our variable to “Hello World!”, and close the line out with a semi-colon. You’ll notice that every line has to be closed out with a semi-colon, this tells the compiler that you’re done with the current statement.

Setup:

Next we’re going to start the setup function. Setup is only run once, right when the program is started on your device. This is a good place to start initializing things that need to be started prior to reading or writing. Here we will initialize the serial port, the first serial port is always linked to USB. It can be used for other things than USB, but I prefer to leave it that way.

You’ll see our complete code below, this is a very simple one-liner setup. We start by calling “void setup()”, this tells the compiler that we’re defining the setup function. You can define custom functions as well, but we’ll get to that later. To create a box for your setup to reside in, you need to enclose your code in curly braces, “{” and “}”. These curly braces tell the compiler what code belongs to the function you’ve created, which in this case is setup.

Now that Our setup function is defined we can call “Serial.begin”, capital letters do matter here – Serial needs to be capitalized. If we take this call apart, “Serial” is the object that we’re telling the compiler that we’re working with, and “begin” is the function we’re calling. Some functions have arguments that need to be passed to them, that is done inside the parentheses – “(9600)”. What we’re doing here is setting the baud rate to 9600. The baud rate is important, because it synchronizes the speed of data flow between your computer and the Arduino device.

Loop:

Here’s the juicy part, finally the loop! The loop is where your main code is run, this code is run over and over and over until the end of time, or until the device is unplugged/reset. This is a very simple loop that prints the text we defined earlier in a variable, waits 1 second, then repeats.

We have 2 functions being called here. The first is “Serial.println(myString);”, this tells the Serial object to print a new line using the variable named “myString”. There is another function to Serial that is simply “Serial.print”, which would print everything on the same line. That is useful if you have a lot of code determining what to print, and you don’t want to do it on a new line yet. The second function is delay, note that it’s lower case. Most built in Arduino functions are lower case, and the objects are upper case. The argument we’re passing to the “delay(1000)” function is amount of time in milliseconds. So 1000 = 1 second.

The final product:

//Hello world example code
 //This will be an introduction to Arduino, via serial communication
 char myString[] = "Hello World!";

void setup()                    // This only runs once, right when you start the sketch
 {
 Serial.begin(9600);           // Instantiate serial with a buad rate of 9600 bps

}

void loop()                       // Runs forever
 {
 Serial.println(myString);  // Prints Hello World! via the string that we created pre-setup
 delay(1000); // Pauses for 1 second
 }

 

Now let’s run the code! Go on to the next post to put your code to work!

Arduino USB Serial

Uploading Code to Arduino and Using Serial Monitor

This post is assuming you’ve read my previous post, Arduino Serial Hello World. There you learned how to write the “Hello World” code, in preparation for output to the Serial Monitor. By now you should also understand the Basics of Arduino. These 2 are the prerequisites of this project. You could, of course, just copy my code and upload it.

Let’s get started!

I’ll be using the Arduino Uno, this is the most commonly known device that is used for Arduino programming. If you’ve read the Arduino Serial Hello World post, you’ll already have the code in your IDE.  If not, you can copy the following code and paste it in the Arduino IDE.

//Hello world example code
//This will be an introduction to Arduino, via serial communication
char myString[] = "Hello World!";

void setup() // This only runs once, right when you start the sketch
{
 Serial.begin(9600); // Instantiate serial with a buad rate of 9600 bps
 
}

void loop() // Runs forever
{
 Serial.println(myString); // Prints Hello World! via the string that we created pre-setup
 delay(1000); // Pauses for 1 second
}

Let’s get connected

If you haven’t already, you may want to configure your IDE for connection with the board. Following the configuration process allows the IDE to properly communicate with the board. It’s a very simple process, so it shouldn’t take too long.

After you do your one time configuration process, you’re ready to start uploading! Let’s plug USB in to the Arduino, and the other side into your computer.

You may notice I’m not using the default USB that came with the Arduino Uno. I choose to use a longer USB, because it helps keep my work area clutter free. Once you start more complex projects, you may run out of room in your work area.

Let’s upload

Now that you’re connected, you may see that the LED’s came on when plugging in your device. It’s alive! If you have it configured properly, you’re ready to upload. Click the arrow that points to the right, at the top left of your screen.

Now that you’re uploading, you can watch the progress in the black area at the bottom of your screen. There’s also a small progress bar that shows how it’s going.

This process should be very fast, on such a small sketch. When you start diving in to libraries, and using more complex code, it’ll take a little longer.

Where’s my output?

Now you’ll open the serial monitor. This is where the Arduino can send messages back to your computer. This is a very good area to try to use for debugging things. Click the icon that looks like a magnifying glass, at the top right of your screen.

If all is going well,  you’ll see the Arduino communicating with the Serial Monitor. It should say “Hello World!”. You’d think it would have more to say than that, but it really only talks about the things we tell it to. If you’re not talkative, neither is your Arduino, unfortunately. Don’t expect your Arduino to be the conversation starter, you’re the one in control here.

Thanks!

I hope you found this easy to follow, let me know in the comments if you have any questions. I’m here to help you, and I’d be more than happy to do so. Feel free to Subscribe below, you’ll find that I’m going to slowly work you in to more complex development. I feel you’d benefit from it a lot, if you follow along.

arduino uno rev3

Intro to Arduino

The intent of this intro to arduino, is to explain the basics of Arduino. This is a great place to start, if you’re just starting out with Arduino programming.

What is an Arduino?

Arduino is actually the language running on your device. What people know as an Arduino is a microcontroller, which runs whatever code you’ve uploaded to the device. Arduino was created to simplify electronics prototyping, but it can be used in more permanent situations. The following is the most popular board, the Arduino UNO. You can get one here.

What is an IDE?

IDE stands for Integrated Development Environment. There are a lot of programming languages that provide an IDE for you to work in, compile your program for execution, and deploy the finished product to your Arduino. Download the IDE here.

What is compiling?

When your program is compiled, it is being built. What this does, is converts your more “people readable” code to a language that your Arduino can understand.

How does Arduino work?

There are 4 parts of Arduino code that you need to understand. They are very simple, and easy to follow. Arduino is a dumbed-down version of the C programming language. C has been around for a long time, and will be for years to come. The following are the 4 parts to Arduino code, the images are going to be from the code in my “Hello World:Serial” tutorial:

Pre-Setup

  • You define any libraries you’d like to include in your project, libraries will be explained in another post. You don’t need to know about these yet
  • You define any global variables you’d like to set before running the main part of your code.

Setup

  • This is a run-once part of your code that prepares things, some sensors need to be fired up, or initialized, before you can begin reading from them.
  • Some times you need to do things like connect to wifi, that may only get done once.

Loop

  • This is your main code, this loop will run forever until the world ends, or until you disconnect power from the device.

Post-Loop

  • I’m just calling this post loop, it’s actually an area where you can define functions for your code.
  • You’ll learn that functions are very handy when your code gets long. Some of my Loop statements are only 4 lines long, because they are looping through functions.
  • Anything you put here, is to simplify repetitive tasks, and/or clean up your code.

Is there code that isn’t run?

Yes, you can comment out lines of code. Commenting allows you to “hide” code from the compiler, if you’d like to test your code without a statement or have a reference inside your code for later. Commenting is also a great way to add a “bookmark” to your code, so you can identify sections of code easier, and remember what they are for. Adding a comment is as simple as putting // at the beginning of the line you’re commenting out.

You can keep reading, to find out how to communicate with your Arduino via a Hello World example.

Thanks!

I hope you enjoyed my introduction to Arduino. Let me know if you have any questions, I’d be happy to answer them! Please subscribe below to get the latest posts by Anarchy Development.