Now go ahead and connect a USB cable up to your Arduino board and plug it into your computer. Some LED lights will flash while it initialises and it will run whatever program is currently stored on its flash memory. If it's a brand new Arduino you know, then no program will run as there is nothing on it.
We talked earlier about microcontrollers containing flash memory which is where the code is stored. Flash memory is non-volatile memory which means that it retains information after power has been disconnected or lost. This is important as it means you don't have to re-upload your code every time you unplug your board. Things that happen such as variables and pin states are not stored on the flash however and reset whenever the board is disconnected from power.
So let's upload our first program to the board. We're going to turn the on-board LED on and off to make it blink. It's the hello world of electronics and whilst blinking an LED is a fairly simple and seemingly trivial project, it provides huge potential to use that same code to do more powerful things. For example instead of an LED the output device could be activating a much larger system or launching a model rocket or a relay, whatever it may be. The code remains exactly the same and is only limited by what you connect up to your board.
Now go ahead and open up your Arduino IDE and copy the following code into the text area of the window and we'll talk through it quickly to find out what it does. So we're going to cover the specifics of coding more in the next chapter and we'll go from the ground up to create our code. However we're going to take a quick walk through our hello world code and get a bit of a feel of how we can get the code onto our board. So don't worry if you don't understand everything we'll cover that further on, however let's take a look.
So the first thing that comes up is void setup. Now this is the first thing that will run when your microcontroller is turned on and it runs once and only once so it's for all the things that you would like to initialise and you can see here I'm using the function pin mode. Now we only want this to run once because what it does is it takes a pin and configures it as either an input or an output and there are cases where you may want to do this multiple times throughout your program but generally you want to set that pin as an output and leave it that way. So I'm configuring pin 13 which is the pin with the on board LED connected to it and I'm configuring that as an output using a semicolon to finish the line.
Now the next section is void loop and this is a function which runs directly after void setup and will run over and over and over again until you tell it to stop or you unplug power so it loops continuously, it's called an infinite loop. Now there's only four lines of coding here and they're very simple. We've configured pin 13, our on board LED, as an output so now we have to tell it to turn on and turn off. So the first thing we're going to use is a function called digital write and it outputs either a 1 or a 0, 5 volts or 0 volts to that particular pin and we'll cover the electronics and voltage and current and all the rest in the next chapter however we can really simply say that when it's high we're turning the pin on and when it's low we're turning the pin off because that's how our LED is connected up to it. So digital write, we're writing to pin 13 and we're writing it high so we're turning the LED on. Then we're going to wait for half a second so that our eye can perceive that it's on. Then we'll turn it off using the same digital write function, again targeting pin 13 and turning it low. Then we'll put another delay in for 500 milliseconds, 500 is half a second, and then it'll loop and repeat over and over again which gives the effect of it blinking. So now that we have our code ready we need to tell the computer which device we would like to upload that code to. Go to the tools menu and select the correct board type, it should be Arduino slash Genuino UNO.
Then select the corresponding com port or for Mac OS users it should be slash device. If it's not displayed there ensure that your Arduino is connected via USB to the computer and that the power light on the Arduino is on. Now you can click the upload button located in the top left hand corner which will first compile your code and check it for any errors and then upload it to your board. While it's going if everything went smoothly you'll see a success message in white, however error messages will be displayed in orange.
If you get an error message it will explain what went wrong and where, it could be incorrect syntax in your code or a communication error with the board. Go back through the steps and double check every detail to make sure you haven't forgotten anything.
We'll look at the specifics of writing code in the next chapter, however you've now created your first program and uploaded it to your Arduino board. Congratulations, that's a big step! In the next chapter we're going to look at writing programs from the ground up and building circuits using a breadboard so that we can write code that interacts with the world in new and exciting ways."
Makers love reviews as much as you do, please follow this link to review the products you have purchased.