PLAY PODCASTS
Learn Programming and Electronics with Arduino

Learn Programming and Electronics with Arduino

61 episodes — Page 2 of 2

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

Mar 10, 201721 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 us

Mar 9, 201714 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.

Mar 8, 201711 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

Mar 7, 201710 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.

Mar 6, 20178 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.

Mar 5, 20175 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.

Mar 4, 20178 min

Introduction to the Arduino Crash Course

Mar 3, 20175 min

What can you do with Arduino? Let's get pumped!

I want to get you excited about the possibilities when you learn some basic programming and electronics that drive the Arduino. It is literally amazing what you can do.

Mar 3, 20173 min

Prelude to the Arduino Crash Course

The premise of our course is that Arduino is a tool to assist in prototyping your imagination.

Mar 2, 20171 min

Arduino Lessons from Programming Electronics Academy

This is the introduction for the following lessons on the programming and electronics of Arduino. Michael talks about the following lessons, the Arduino Crash Course, and the premium Arduino Course for Absolute Beginners.

Mar 1, 20172 min