Learn Programming and Electronics with Arduino

Learn Programming and Electronics with Arduino

By Programming Electronics AcademyTechnologyEducationCourses
Download on the App Store

Learn Programming and Electronics with Arduino episodes

  • digitalRead() and the Serial Port

    As simple as it may seem, knowing when something is either on or off can be a great tool for designing something useful.

    This lesson will answer the following questions:

    Is a button being pressed? Has a switch been turned on? What is the on/off sensor status? When you can answer questions like these, you can implement actions based on the current status – if the button is pressed do this – otherwise, do that. If the sensor is HIGH take this action, otherwise do nothing. You get the gist. But before we can implement the actions, we have to be able to track the status and the changes of the digital pins.

    If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.

    You Will Need A momentary push button – this is a button that is spring-loaded, i.e. it never stays in a down position unless it's held down. Jumper wires (3) A 10,000 Ohm resistor more commonly referred to as a 10K resistor A very ripe banana, (1) – not completely useful, but nutritious Step-by-Step Instructions Connect the pushbutton to the breadboard. Connect one side of the pushbutton to the 5-volt pin on the Arduino board using a jumper wire. Connect one side of the 10K resistor to the other side of the pushbutton. Connect the other side of the resistor to the ground pin on the Arduino. You may have to use a jumper wire to make it reach. On the same side, the resistor is connected to the pushbutton, connect a jumper wire and run it to pin 2 on the Arduino board. Connect the Arduino to your computer with the USB cable. Open the sketch for this section. Click the Verify button in the top left corner of the IDE. The Verify button will turn orange and then back to blue when it has completed compiling. Click the Upload Button (located to the immediate right of the Verify button). This button will also turn orange and then back to blue once the sketch is uploaded to the Arduino board. Now go to the menu bar at the top and select Tools > Serial Monitor. Or you could use the shortcut key, Shift + Control + M. The serial monitor window will open and will be spouting off numbers. It should be a bunch of zeros. Press and hold the pushbutton – watch the serial monitor window, the numbers should now be ones. If the numbers are not scrolling, make sure you click Autoscroll at the bottom left of the serial monitor window. digitalRead Serial Port Circuit

    This image built with Fritzing.

    The Arduino Code

    /*

    DigitalReadSerial

    Reads a digital input on pin 2, prints the result to the serial monitor

    This example code is in the public domain.

    */

    // digital pin 2 has a pushbutton attached to it. Give it a name:

    int pushButton = 2;

    // the setup routine runs once:

    void setup() {

    // initialize serial communication at 9600 bits per second:

    Serial.begin(9600);

    // make the pushbutton's pin an input:

    pinMode(pushButton, INPUT);

    }

    // the loop routine runs over and over again forever:

    void loop() {

    // read the input pin:

    int buttonState = digitalRead(pushButton);

    // print out the state of the button:

    Serial.println(buttonState);

    delay(1); // delay in between reads for stability

    } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 /* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor This example code is in the public domain. */ // digital pin 2 has a pushbutton attached to it. Give it a name: int pushButton = 2; // the setup routine runs once: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); // make the pushbutton's pin an input: pinMode(pushButton, INPUT); } // the loop routine runs over and over again forever: void loop() { // read the input pin: int buttonState = digitalRead(pushButton); // print out the state of the button: Serial.println(buttonState); delay(1); // delay in between reads for stability } Discuss the Sketch This sketch opens with a multi-line comment containing a short description of the program and circuit. The first block of code following the comment is where we declare and initialize variables. From the last lesson, we are familiar with the integer data type.

    int pushButton = 2; //This is the pin that our push button is connected to. 1 int pushButton = 2; //This is the pin that our push button is connected to. Notice how the variable pushButton is declared and initialized all on the same line. Also, notice the descriptive name of the variable – pushButton – the variable name implies its use within the program – this is a good example to follow.

    Let's consider what we have done so far – we have made a variable that will store the pin number that our pushbutton is connected to.

    The next block of code we come to is the setup(). Inside these wily curly brackets there are two functions – a familiar one, pinMode() and another which we will learn to love – Serial.begin().

    Serial.begin() is part of a family of functions referred to as a library. The name of the library is the Serial library. A library is just a group of functions that work toward a similar purpose. If you had a Circus library, it might contain the functions juggle(), balance() and flamingCircleOfDeath(). To access the functions in a library, you write the name of the library followed by the name of the function in the library, with a period in between.

    Circus.juggle(); 1 Circus.juggle(); Arduino has many preinstalled libraries. There are also many community-contributed libraries that you can add. You can view all of the preinstalled libraries and some of the contributed libraries at http://arduino.cc/en/Reference/Libraries.

    So what does the Serial library do?

    The Serial library helps establish communication between your computer and the Arduino board. If you ever go to marriage counseling, you will learn that communication involves sending and receiving. Data can flow both ways. If we want to establish this communication, we use the begin() function from the Serial library.

    Serial.begin(9600); 1 Serial.begin(9600); The begin() function takes one argument – the baud rate. What is the baud rate you ask? It is the rate at which data will flow between your computer and the Arduino. For most Arduino sketches a baud rate of 9600 is used as the argument.

    That's all you really need to know about the baud rate to get started with serial monitoring. But I have a feeling you want to know more, so if you check out the further reading section at the end of this tutorial, there will be some links to tempt your insatiable desire for acquiring an understanding of all things in the universe.

    The next function after Serial.begin() is the pinMode() function. We want to set the mode of a pin and what is cool about pinMode() this time around is that we are changing the arguments we pass to the function. Instead of being an OUTPUT (as in the Blink sketch), we want our pin to be an INPUT because we want to read voltage at this pin, not provide it.

    pinMode(pushButton, INPUT) ; 1 pinMode(pushButton, INPUT) ; Here we use the variable pushButton to let the function know we are setting the mode at pin 2. Then we use the keyword INPUT, to say which mode we want.

    Those are the only two functions in the setup() curly braces – and just as a reminder – setup() only runs once.

    The next block of code is the function loop(). What do we see inside the curly braces of the loop()?

    int buttonState = digitalRead(pushButton); 1 int buttonState = digitalRead(pushButton); Whoa! What the heck is this? It looks like the programmer is declaring a variable! I thought variables were declared at the top of the sketch. While variables are often declared before the setup() function, you can actually declare and initialize a variable just about anywhere in a sketch. Soon you will see why this placement is the way to go.

    Let's break down this statement. First, look at the data type and the name. We declare an integer and name it buttonState. Notice the variable name buttonState is indicative of its purpose, as we will see this variable is assigned the position of the button.

    To initialize the variable, we see something altogether new – the variable is set equal to the output of a function called digitalRead(). This is going to take a little recall power on your part. Do you remember the reason for the word void in front of the loop() function? We had to write void because the function loop() does not return a value. But that is not the case for the function digitalRead().

    The digitalRead() function returns an integer – either 1 or 0. This value is then assigned to the variable buttonState.

    If it is 1, the voltage at the pin is HIGH, if the value is 0, the voltage at the pin is LOW. What pin you ask? Well the pin you pass as an argument in the digitalRead() function. In this case, we send the variable pushButton, because we want to read the state of pin 2 (if you recall pushButton was initialized to equal 2).

    All of this is in the following line of code:

    int buttonState = digitalRead(pushButton); 1 int buttonState = digitalRead(pushButton); This is why Arduino rocks – one line of code and you are on your way to dominating the world.

    Now the state of our pushbutton will be either HIGH (pressed) or LOW (not-pressed). HIGH will be reported as a 1, and LOW will be reported as 0. When we press the pushbutton, pin 2 is exposed to the 5-volts from the Arduino board, this is considered HIGH, and the digitalRead() function will return 1. If the button is not pressed, then all that pin 2 is exposed to is the ground voltage which is 0 and digitalRead() will return 0.

    Explanation of Digital Read Code

    In the next line of code we return to the Serial library for another function called println().

    Serial.println(buttonState); 1 Serial.println(buttonState); The Serial.println() function returns the value of whatever variable you stick in as an argument. It will report this value to the serial monitor window on your Arduino IDE. To open up the serial monitor window all you have to do is click Tools > Serial Monitor (or SHIFT + CONTROL + M). This is one way to retrieve the data on your Arduino board.

    Keep in mind, that when you unplug your Arduino and use some batteries to charge it, the Serial.println() function won't do you much good. But while you are creating the circuit and the sketch, there is no better way to troubleshoot than use the println() and print() functions from the Serial library.

    So let's cover what we have done so far in the loop. First we read the state of digital pin 2 and save the state in a variable. Then we display the state in the serial monitor window.

    Finally, we use the delay() function and wait one millisecond – this allows the reading at the pin to stabilize.

    Once this is done, the loop starts from the top. We read another value at pin 2 – we are checking every time whether the button is pressed or not pressed – this value gets assigned to our buttonState variable, then we display the newly recorded value to serial monitor window – again. And we do this over and over – hundreds of times per second.

    So go ahead, press that button, watch the serial monitor window – I think you are already brewing applications for these functions…

    Try On Your Own Challenge Change the function Serial.println() to Serial.print(). What happens to the output in the serial monitor window? Can you tell the difference between the two functions? Change the pin that you are reading to pin 3. Make the circuit change and the changes to the sketch.

    Further Reading Baud Rate Serial Library Serial.begin() Serial.println() Serial.print()

    22 min
  • How to Blink an LED with Arduino

    The first program you usually write when learning a new programming language is called, "Hello World". Its only function is to display the words "Hello World" on the computer monitor.

    When learning to program microcontrollers such as the Arduino, the equivalent of "Hello World" is a program that blinks an LED. Guess what it is called – Blink.

    You Will Need An LED (any color works fine) A 220 Ohm Resistor An alligator clip (not essential but makes the circuit easier) Fourteen small and smooth rocks from the a western pacific island (not essential but adds an esoteric feel) NOTE: On most Arduino boards there is an LED soldered right by pin 13 – it is actually connected to pin 13 – so if you do not have an LED laying around (or a resistor for that matter), you can use the board mounted LED – it will blink with the same sketch.

    Step-by-Step Instructions Insert the short leg of the LED into the GND pin on your Arduino (use the GND pin closest to pin 13). Connect the 220 Ohm resistor to pin 13 on the Arduino. It doesn't matter which way you connect the resistor. Use the alligator clip to connect the long leg of the LED to the other leg of the resistor. If you do not have an alligator clip, twist the two leads together as best you can to get a steady electrical connection. Plug the Arduino board into your computer with a USB cable. Open up the Arduino IDE. Open the sketch for this section. Click the Verify button on the top left. It should turn orange and then back to blue. Click the Upload button. It will also turn orange and then blue once the sketch has finished uploading to your Arduino board. Now monitor the Arduino board – the LED should be blinking.

    This image was made using Fritzing.

    The Arduino Code

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 /* Blink Turns on an LED on for one second, then off for one second, repeatedly. Most Arduinos have an on-board LED you can control. On the Uno and Leonardo, it is attached to digital pin 13. If you're unsure what pin the on-board LED is connected to on your Arduino model, check the documentation at http://www.arduino.cc This example code is in the public domain. modified 8 May 2014 by Scott Fitzgerald */ // the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(13, OUTPUT); } // the loop function runs over and over again forever void loop() { digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }

    Discuss the Sketch On the previous page, you can see the Arduino code that constitutes the Blink program – make sure to read each line, even if doesn't make any sense yet.

    Notice the comments at the top of the program. Note the use of the multi-line comments syntax /* */. It is always a good idea to take time and see what the programmer has to say about the sketch they wrote. The comments will likely be concise, describing how the program works or what it should accomplish. A few may even tell you how to connect the circuit.

    The next block of code you encounter in the Blink sketch is…

    1 2 3 4 void setup( ) { // initialize the digital pin as an output. pinMode(led, OUTPUT); } Recall that the setup() function is in almost every Arduino sketch you encounter. Inside the curly braces is code that will only be run once by the Arduino. For this sketch notice the function pinMode() is inside the curly braces of the setup() function.

    Let me start by saying that pinMode() is a wonderful function. If you recall, functions can take arguments. The pinMode() function takes two arguments – it wants a pin number and a mode for that pin. The pin number is easy, 0 to 13 for digital pins, and A0 to A5 for analog pins.

    The mode is an easy designation as well, you want the pin to be either an INPUT (good for reading a sensor) or an OUTPUT (good for powering an LED).

    When a pin is set as an INPUT, it prepares the pin to read voltages that will be applied at the pin. When a pin is set as an OUTPUT, it prepares the pin to output voltage – more on this later.

    In this example, we want to light an LED, this requires that voltage is applied at pin 13. Therefore, we need the mode of pin 13 set as an OUTPUT. Keep in mind that setting the mode of the pin to OUTPUT does not apply a voltage, it enables the pin to supply a voltage once it is programmed to do so.

    Moving on to the final block of code, we come to our favorite and ubiquitous function void loop()…

    1 2 3 4 5 6 void loop( ) { digitalWrite(led,HIGH);// turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led,LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } You may recall that void loop() runs over and over again. In this loop, we see two functions: digitalWrite() and delay().

    The function digitalWrite() is used to apply either HIGH or LOW voltage to a pin. HIGH will apply 5 volts to the pin you designate and LOW will apply 0 volts. If you apply 5 volts to a pin that is connected through an LED to ground, then the LED will light up.

    There is a voltage difference between the pin and ground, thus current is able to flow through the LED. If you apply 0 volts to the same pin the LED will not light up because no current is being "pushed" through the circuit – the voltage at ground and at the pin are both zero.

    1 2 digitalWrite() takes two arguments, the pin number and the level of voltage, either HIGH or LOW, as we have discussed above. digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) We want to start the loop by applying HIGH voltage to pin 13, where our LED is attached. In the digitalWrite() function we use the variable name 'led' to refer back to the value 13 – which was previously assigned.

    Notice that there is no need to explicitly write 13 in the digitalWrite() function, instead we are able to use the variable 'led'. Variables are awesome like that – they can take the place of numbers and are much easier to change and track. Every time we use the 'led' variable, the Arduino will see 13. If we later decide to move our LED to pin 10, then all we have to do is change the value of 'led' once, and all the instances of 'led' are changed too.

    Once digitalWrite() function has been executed, the LED will get bright – we just applied 5 volts, so hey, that makes sense. The next thing we do is delay the Arduino sketch to enjoy the bright glow of our LED. To do this, we use the delay() function.

    The delay() function takes one argument – the number of milliseconds you want the program to delay. In this case, we want 1000 milliseconds, which equals one second.

    First we said, "apply high voltage" and now we say – wait one second. Our LED will glow for exactly one second. But that gets old, and we have to stay true to the name of the sketch, so next we tell the Arduino to write a LOW voltage to pin 13 – to do this we use the same function as before, namely digitalWrite(), but, this time, we want LOW voltage instead of HIGH.

    Now the LED goes dark because no current is flowing. In order to sustain that darkness we use the delay() function again for one second. Now the LED is dark for one second.

    We are at the end of the loop. We turned the LED on for a second, then we turned it off for a second – what next? Once the Arduino completes the loop, it starts at the top of the loop again and repeats like a broken record.

    Once again, the LED will light up, delay a second and then go dark for one second. And repeat – now you have a blinking LED – pretty cool for just a couple lines of code!

    Try on Your Own Challenge Change the value of the delay functions. What happens? Change the number of the led variable to 12 and move the long leg of your LED to pin 12. See what happens. Further Reading int pinMode() digitalWrite() delay()

    15 min
  • Understanding Variables

    Let's have a discussion about a powerful and semi-confusing programming topic – variables. Arduino code variables are like buckets. You choose what types of stuff you want in the bucket and can change the contents as often as you like. When you declare a variable, you are telling the program two things, firstly – what types of things you plan to put in the bucket, and secondly, what the name of the bucket is so you can refer to it later.

    If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it. If you tell the program you will be putting fluids in the bucket, then you can go all day filling it with beer, water, and iced tea – but the second you try to fill it with rocks, the compiler will call you out on your discrepancy. Only fluids go in a bucket declared for fluids. To declare a variable, you write the type of contents it will hold followed by the name:

    fluid bucketVariable; 1 fluid bucketVariable; Notice in the above declaration statement that the word fluid is a different color – that is because Arduino knows variable data types – and they get a special color to reduce confusion and of course because they are cool.

    There are several types of variable data types you can declare. In this lesson, we will discuss the integer data type.

    You probably know that an integer is a whole number (no decimals). For Arduino, an integer is a number from -32,768 to 32,767. If you try to put a number bigger than that into an integer variable, the value will roll over to the opposite side like a game of Pac-Man. If you add 5 to 32,767, you would get –32,764. If you subtracted 5 from -32,768 you would get 32,763.

    Integer is abbreviated int. Since an integer is an Arduino data type, it will change color to an orange.

    int led;// an integer variable called led. 1 int led;// an integer variable called led. The name of the variable can be whatever you want with certain restrictions. There are also a couple good conventions to follow…

    The variable name should be descriptive of its function, for example, the ledPin variable could be the pin number that you put your LED into on your Arduino board. By convention, most variables start lowercase. Variable names cannot be the same as keyword names. Now, what if we want to put something in the bucket? Well, we assign a value to the variable. When we first assign the value, it is called initialization, and we use the equal sign to do so. It looks like this.

    intled; //first we declare the variable

    led = 13; //now we initialize the variable 1 2 3 intled; //first we declare the variable led = 13; //now we initialize the variable Or, we can initialize and declare a variable at the same time…

    int led = 13; //declare and initialize a variable with a single statement 1 int led = 13; //declare and initialize a variable with a single statement Now if you are going to initialize a variable (that is assign it a value to hold) before the setup() function, then you must do it all in one line like this:

    int led = 13;

    void setup(){

    } 1 2 3 4 5 int led = 13; void setup(){ } Or you can do it like this:

    int led;

    void setup(){

    led = 13;

    } 1 2 3 4 5 6 7 int led; void setup(){ led = 13; } Well, that's all we will talk about variables for now. I hope you have a basic idea of how they are declared and initialized.

    12 min
  • Understanding Arduino Syntax

    Arduino Code & Syntax Overview As you learned in Module 01, IDE stands for Integrated Development Environment. Pretty fancy sounding, and should make you feel smart any time you use it. The IDE is a text editor-like program that allows you to write Arduino code.

    When you open the Arduino program, you are opening the IDE. It is intentionally streamlined to keep things as simple and straightforward as possible. When you save a file in Arduino, the file is called a sketch – a sketch is where you save the computer code you have written.

    The coding language that Arduino uses is very much like C++ ("see plus plus"), which is a common language in the world of computing. The code you learn to write for Arduino will be very similar to the code you write in any other computer language – all the basic concepts remain the same – it is just a matter of learning a new dialect should you pursue other programming languages.

    If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it.

    The code you write is "human readable", that is, it will make sense to you (sometimes), and will be organized for a human to follow. Part of the job of the IDE is to take the human readable code and translate it into machine-readable code to be executed by the Arduino. This process is called compiling.

    The process of compiling is seamless to the user. All you have to do is press a button. If you have errors in your computer code, the compiler will display an error message at the bottom of the IDE and highlight the line of code that seems to be the issue. The error message is meant to help you identify what you might have done wrong – sometimes the message is very explicit, like saying, "Hey – you forget a semicolon", sometimes the error message is vague.

    Why be concerned with a semicolon you ask? A semicolon is part of the Arduino language syntax, the rules that govern how the code is written. It is like grammar in writing. Say for example we didn't use periods when we wrote – everyone would have a heck of a time trying to figure out when sentences started and ended. Or if we didn't employ the comma, how would we convey a dramatic pause to the reader?

    And let me tell you, if you ever had an English teacher with an overactive red pen, the compiler is ten times worse. In fact – your programs WILL NOT compile without perfect syntax. This might drive you crazy at first because it is very natural to forget syntax. As you gain experience programming you will learn to be assiduous about coding grammar.

    Let's get our hands dirty and introduce some syntax.

    PIcture of Arduino Code Syntax

    The Semicolon ; A semicolon needs to follow every statement written in the Arduino programming language. For example…

    int LEDpin = 9; 1 int LEDpin = 9; In this statement, I am assigning a value to an integer variable (we will cover this later), notice the semicolon at the end. This tells the compiler that you have finished a chunk of code and are moving on to the next piece. A semicolon is to Arduino code, as a period is to a sentence. It signifies a complete statement.

    The Double Backslash for Single Line Comments //

    //When you type a double backslash all the text that follows on the same line will be grayed out 1 //When you type a double backslash all the text that follows on the same line will be grayed out Comments are what you use to annotate code. Good code is commented well. Comments are meant to inform you and anyone else who might stumble across your code, what the heck you were thinking when you wrote it. A good comment would be something like this…

    //This is the pin on the Arduino that the LED is plugged into int LEDpin = 9 1 2 //This is the pin on the Arduino that the LED is plugged into int LEDpin = 9 Now, in 3 months when I review this program, I know where to stick my LED.

    Comments will be ignored by the compiler – so you can write whatever you like in them. If you have a lot you need to explain, you can use a multi-line comment, shown below…

    /* The multi-line comment opens with a single backslash followed by an asterisk. Everything that follows is grayed out and will be ignored by the compiler, until you close the comment using first an asterisk and then a backslash like so */ 1 /* The multi-line comment opens with a single backslash followed by an asterisk. Everything that follows is grayed out and will be ignored by the compiler, until you close the comment using first an asterisk and then a backslash like so */ Comments are like the footnotes of code, except far more prevalent and not at the bottom of the page.

    Curly Braces { } Curly braces are used to enclose further instructions carried out by a function (we discuss functions next). There is always an opening curly bracket and a closing curly bracket. If you forget to close a curly bracket, the compiler will not like it and throw an error code.

    void loop() { //this curly brace opens //way cool program here } //this curly brace closes 1 2 3 void loop() { //this curly brace opens //way cool program here } //this curly brace closes Remember – no curly brace may go unclosed!

    Functions ( ) Let's switch gears a bit and talk about functions.

    Functions are pieces of code that are used so often that they are encapsulated in certain keywords so that you can use them more easily. For example, a function could be the following set of instructions…

    Wash Dog 1. Get a bucket 2. Fill it with water 3. Add soap 4. Find dog 5. Lather dog 6. Wash dog 7. Rinse dog 8. Dry dog 9. Put away bucket 1 2 3 4 5 6 7 8 9 10 Wash Dog 1. Get a bucket 2. Fill it with water 3. Add soap 4. Find dog 5. Lather dog 6. Wash dog 7. Rinse dog 8. Dry dog 9. Put away bucket This set of simple instructions could be encapsulated in a function that we call WashDog. Every time we want to carry out all those instructions we just type WashDog and voila – all the instructions are carried out.

    In Arduino, there are certain functions that are used so often they have been built into the IDE. When you type them, the name of the function will appear orange. The function pinMode(), for example, is a common function used to designate the mode of an Arduino pin.

    What's the deal with the parentheses following the function pinMode? Many functions require arguments to work. An argument is information the function uses when it runs.

    For our WashDog function, the arguments might be dog name and soap type, or temperature and size of the bucket.

    pinMode(13, OUTPUT); //Sets the mode of an Arduino pin 1 2 pinMode(13, OUTPUT); //Sets the mode of an Arduino pin The argument 13 refers to pin 13, and OUTPUT is the mode in which you want the pin to operate. When you enter these arguments the terminology is called passing. You pass the necessary information to the functions. Not all functions require arguments, but opening and closing parentheses will stay regardless though empty.

    millis( ); //Retrieves the length of time in milliseconds that the Arduino has been running 1 2 millis( ); //Retrieves the length of time in milliseconds that the Arduino has been running Notice that the word OUTPUT is blue. There are certain keywords in Arduino that are used frequently and the color blue helps identify them. The IDE turns them blue automatically.

    Now we won't get into it here, but you can easily make your own functions in Arduino, and you can even get the IDE to color them for you.

    We will, however, talk about the two functions used in nearly EVERY Arduino program.

    void setup ( ) The function, setup(), as the name implies, is used to set up the Arduino board. The Arduino executes all the code that is contained between the curly braces of setup() only once. Typical things that happen in setup() are setting the modes of pins, starting

    void setup( ) { //the code between the curly braces is only run once for setup() 1 void setup( ) { //the code between the curly braces is only run once for setup() You might be wondering what void means before the function setup(). Void means that the function does not return information.

    Some functions do return values – our DogWash function might return the number of buckets it required to clean the dog. The function analogRead() returns an integer value between 0-1023. If this seems a bit odd now, don't worry as we will cover every common Arduino function in depth as we continue the course.

    Let us review a couple things you should know about setup()…

    1. setup() only runs once.

    2. setup() needs to be the first function in your Arduino sketch.

    3. setup() must have opening and closing curly braces.

    void loop( ) You have to love the Arduino developers because the function names are so telling. As the name implies, all the code between the curly braces in loop() is repeated over and over again – in a loop. The loop() function is where the body of your program will reside.

    As with setup(), the function loop() does not return any values, therefore the word void precedes it.

    void loop( ) { //whatever code you put here is executed over and over 1 void loop( ) { //whatever code you put here is executed over and over Does it seem odd to you that the code runs in one big loop? This apparent lack of variation is an illusion. Most of your code will have specific conditions laying in wait which will trigger new actions.

    If you have a temperature sensor connected to your Arduino for example, then when the temperature gets to a predefined threshold you might have a fan kick on. The looping code is constantly checking the temperature waiting to trigger the fan. So even though the code loops over and over, not every piece of the code will be executed every iteration of the loop.

    Try On Your Own This course is based around the example sketches provided with the Arduino IDE. Open up your Arduino IDE and go to File > Example > 01.Basics and open up three different sketches. Identify the following syntax and functions that you find in the sketches:

    ; semi-colons // single line comments /* */ multi-line comments { } open and closing curly braces ( ) parenthesis void setup() – identify the opening and closing curly braces void loop() – identify the opening and closing curly braces some blue keywords like OUTPUT or INPUT Further Reading Go to the Arduino Reference page and check out all the syntax under Further Syntax that we talked about. You will find some further useful info on each of these elements.

    11 min
  • Arduino IDE and Sketch Overview

    IDE stands for Integrated Development Environment. Pretty fancy sounding, and should make you feel smart anytime you use it. The IDE is a text editor like program that allows you to write computer code for your Arduino board.

    When you open up the Arduino program, you are opening the IDE. It is intentionally stream lined to keep things as simple and straightforward as possible. When you save a file in Arduino, the file is called a sketch - a sketch is where you save all the computer code that you have written.

    Lets take a look at some of the buttons on the IDE.

    The button that looks like a checkmark is called "verify". When you press this, your code will be compiled and any errors will be displayed in the window at the bottom. The short cut key for verify is Control + R.

    The button in the shape of an arrow pointing right is the upload button. When you press this, the sketch will be uploaded to your Arduino board. The short cut key for upload is Control + U.

    The button on the far right side of the screen is the serial monitor button. When you use the serial monitor functions, you can send and receive information from your Arduino board while it is running. We will talk much more about serial monitor later, but I just wanted to point it out now. The shortcut key for serial monitor is Shift + Control + M.

    *Try On Your Own:

    This course is based around the example sketches provided with the Arduino IDE. Open up your Arduino IDE and go to File - Example - 01.Basics and open up three different sketches. Try loading the sketches to your Arduino using the mouse and the key board short cuts.

    *Further Reading:*

    Check out this Arduino.cc webpage and see if you can identify a couple things you learned and pick up on other things I did not cover.

    9 min
  • Download & Install the Arduino IDE

    One of the absolute best things about the Arduino IDE and platform is how easy it is to get started. The software that is installed on your computer is completely free and designed specifically for ease of use. The program is called an Integrated Development Environment, or IDE. The fancy name might intimidate you, but it runs just like a text editing program.

    As with any software install, you may have some peculiar things working on your computer that can hinder a smooth installation. I have loaded the Arduino several times on different operating systems and have had few problems. Once or twice I had to re-download the zip file that contains the Arduino IDE because somehow or other it got messed up during the download process. You may have to install drivers yourself for your Arduino board – this turns out to be fairly easy and is clearly explained on the Arduino.cc website.

    To provide you with the most accurate and up to date information about downloading and installing the Arduino IDE, I defer to the Arduino "Getting Started" page. They have done such a clear and concise job of describing the process, I see no reason to repeat it here.

    6 min
  • Tutorial 01: Hardware Overview

    What are all the components on that aesthetically pleasing blue Arduino circuit board? What does GND stand for, and what is with the "~" mark next to those plastic lifted holes mean?

    This tutorial covers the hardware on the Arduino board that you will likely use as you work on projects. It is by no means a comprehensive study of the physical layout, but enough to make you familiar with the parts you will be using.

    On an aside - isn't the packaging great too? Clean, compact and appealing - overall fun to open.

    9 min

About Learn Programming and Electronics with Arduino

From the publisher's feed

Video lessons on learning programming and electronics with Arduino. This is part of our Arduino Crash Course and Arduino Course for Absolute Beginners. It's designed to take someone with little or…