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

  • PC - Download and Install the Arduino IDE
    Discussion:

    Before you begin using the Arduino IDE, you must first learn how to download, install, and set your preferences. This lesson is specifically tailored for those who use a PC. The next lesson will cover this same topic, but for Mac users.

    I'm currently using a Windows 8.1. You are probably running a newer version of Windows. However, the following instructions will be almost identical from one version to the next. If this should ever change, I'll update this lesson to reflect those changes.

    Find the Arduino IDE Download

    First, jump on the internet with your favorite browser, and navigate to www.arduino.cc. Look for the tab labeled "Download".

    The page on which you land might look a little different than this. It does change from time to time, but there will always be an obvious tab for Download.

    Click on that "Downloads tab". That will bring you to the "Download the Arduino Software" page.

    Once on this page, you have several options. You can download the Windows Installer, or you can download a Windows ZIP file if you are a non-administrator.

    The third option is to get the absolute latest, razor-sharp release of the Arduino IDE. This is found in the section labeled "Hourly Builds".

    If you want the most recent update, you can download from this section. But this is just as the name implies. The information here can be updated as often as hourly, and may not be as stable as an official public release.

    Therefore, in this lesson, I will demonstrate the Windows Installer. This is the best route to go in almost any case. It's rare that another option is better.

    Download and Install

    When you click on the "Windows Installer" link, it takes you to a contributions page. It's completely up to you if you want to contribute to the Arduino cause.

    It is a great idea if you do. However, since this is simply a demonstration, I'm just going to choose download.

    Next, a popup window opens so that you can direct where to save the ZIP file. It really doesn't matter where you save it.

    I'm going to save it to my downloads folder. It will then immediately start to download.

    It's not a big file. It's around 77.7 Megabytes for this current release. I have just an everyday, run of the mill cable internet connection, and yet it still doesn't take very long to download.

    Once the file is done downloading, double click the executable file. This will open a couple of popup windows.

    The first one is dependent upon whether or not you already have a older version of Arduino installed.

    If you already have an older version of Arduino installed, it will ask if you want to first uninstall the older version before continuing the download of the newer version.

    My recommendation is to select "Ok" to uninstall the older version. It will not harm anything or delete any work you have done.

    It simply gets rid of the older Arduino IDE, not your sketch folder. If you don't know what that means, it's not important. Just know that you will not lose anything if you uninstall.

    Next, it will run through the uninstallation process. Once completed, click "Ok". Then, close the uninstall box.

    Afterwards, agree to the new license, which is just like any other license that basically says you owe Arduino your children. The popup window then asks you to set a few installation options. Just leave all these checked.

    Finally, you select the install location. I personally put it into my programs folder. Once you click "Install", it will start extracting.

    That's really it for the installation. It does take a little longer than you might expect - maybe a minute or two to install the whole package.

    Set Preferences for Arduino IDE

    Once installed, open the Arduino IDE. You can either use the desktop icon or go to your programs folder to use the icon there.

    We're going to set some Preferences. Go to "File" and then "Preferences".

    Probably the most important thing to look at is the first line that comes up - the Sketchbook location.

    When we write a program in the Arduino IDE, it's called a sketch. All sketches are saved within a Sketchbook.

    The Sketchbook is a folder where you can to keep all of your work. The default location is usually under Users\The User's Name\Documents\ and then a folder created that is named Arduino.

    If yours does not look like this, or if you would prefer the Sketchbook folder be in a different place, you can browse to select a different location.

    I would definitely recommend using a folder named Arduino. This default location has always worked pretty well for me.

    The next preference to set is the editor language. You can pick from a bunch of different languages. I'm using English, obviously.

    Another setting is editor font size. The default is 12, but I prefer a larger font. Font size 18 works very well for me.

    Be aware that sometimes if you make the font too big, you'll see that one line actually gets written over top of the other. Therefore, you might have to play around with font to find your optimal size.

    There are a few other things that I think are good to set in preferences. One is whether or not to display the line numbers.

    I suggest you check the box for this. This means that each line of your program, or sketch, will be displayed on the left side of the Arduino IDE.

    Another preferences I suggest you choose is to verify the code after upload. This won't mean anything to you now, but you'll see later why this it's an excellent idea to check this feature.

    The next setting I usually choose is to check for updates on startup. This is not imperative. You can check or uncheck this box depending upon your own personal taste.

    It depends on whether or not you want to stay with the same current version the whole time. If you just want the most updated version, you can keep that box checked.

    I do recommend, however, keeping the "Update sketch files to new extension" checked.

    Basically, this means that whenever you open an Arduino file, it ensure that if the file doesn't have the proper extension, then that file will automatically be converted to .ino.

    Arduino files used to have a .pde extension. However, now they are .ino files. That's why it's handy to keep this option active.

    Finally, I think having the last preference chosen is a good idea. That setting is to save the file when you are verifying or uploading.

    Again, we'll talk about verifying and uploading in depth in later lessons, but just take my word for now that it is always good practice to save as frequently as possible.

    Going Old School

    Not all versions are the same. It could be that maybe you prefer the look and feel of a previous version. Or maybe you're just really familiar with and used to working within the framework of a certain version.

    If you prefer to download and use a previous version of Arduino, never fear. We have a solution for you that like to go old school.

    On the Arduino website for downloads, look to the bottom right panel. There is the section for previous releases.

    Clicking that will show you a list of all previous releases for each type of operating system, along with the date they were released. Click which you prefer, and from there downloading is the exact same process as before.

    Again, this lesson applies to those who own PCs. The next lesson explains this same download, installation, and preference concept, except it is for those that work on a Mac.

    8 min
  • Arduino Integrated Development Environment Version
    Discussion:

    The following videos will show you step by step how to download and install the Arduino Integrated Development Environment, or IDE for short. We'll also be setting up some preferences in the IDE.

    The phrase Integrated Development Environment may sound complicated and crazy, but it's really not. To be perfectly honest, it looks and feels a lot like a text editor, such as Microsoft Word or Notepad.

    Arduino IDE is simply a small software program that we're going to load on our computer. It's where we'll write the code that actually gets loaded onto the Arduino board.

    Consistent Look and Feel

    It is completely free to download and use. Understand, though, that it's updated all the time.

    Being kept updated is a good thing. You just need to make sure you are aware of which version you are using.

    Usually the updates are fairly minor and only affect the appearance of the IDE. Maybe the some of the menu options will be reorganized, or maybe some keyword coloring will change.

    However, generally speaking, the IDE maintains a consistent look and feel from one version to the next. I really can't stress enough that it doesn't matter which version of the Arduino IDE you're using while you follow along in the course.

    The Version for This Course

    For this course and all of the videos, I used Version 1.6.6.

    Depending on when you take the course, there may be a newer version of the Arduino IDE available. Maybe some of you already have a different version installed.

    As I said before, the lion's share of the changes from one version to the next are very trivial. The computer code and concepts taught in this course, though, always stay the same.

    Now, I understand if you want to get the exact same version that we use in the course. This will ensure that everything you see in the videos is a mirror image of what you'll see in your Arduino IDE software.

    To do this, just make sure you click on Version 1.6.6 when you install. This and all past versions of the Arduino IDE are kept on the Arduino website.

    We'll discuss this further as we walk through the next few lessons. I can't wait to get you up and running!

    3 min
  • Map of the Familiarization Module
    Discussion:

    Before we can start diving into programming the microcontroller on the Arduino, we need to have a solid understanding of the tools used to make it happen. Therefore, in this particular lesson, we're just going to do an overview of what you'll learn in this familiarization module.

    As the name implies, this module will get you familiar with the software and hardware tools used throughout this course and beyond. More specifically, this section will cover:

    • The Arduino IDE Version Used in This Course
    • How to Download, Install, and Set Preferences
    • The Arduino IDE Itself
    • The Arduino Board
    • The Arduino Toolchain

    First, however, I want to explain how best to learn the objectives of this and any other module. There are several things provided in each lesson that can help you be more successful as you move through the course.

    Obviously, you need to become familiar with the Arduino software and hardware. In addition to this, each lesson has accompanying videos, challenges, and further reading suggestions. Take advantage of these, and you'll easily rock this course!

    Get to Know Arduino Integrated Development Environment (IDE)

    In the beginning of this module, we'll discuss administrative items regarding the version of the Arduino IDE used here. Then, I'll walk you step-by-step through downloading and installing the Arduino IDE. We'll also talk about setting up some of the preferences.

    Once we have it installed, then we'll get familiar with the Arduino IDE itself - some of the buttons, tabs, and that type of thing. Afterwards, we'll take a close look at the Arduino board hardware. Finally, we'll do an overview of the Arduino toolchain.

    Even if you already have the Arduino IDE installed, and even if you have already been creating programs with it, I still say it's a great idea to watch the videos in this section. There might be a couple of things that can help expand your knowledge of the IDE and the hardware - maybe even some tricks or tips that you've not seen before.

    Doing the Challenges is the Key to Growth

    Check out the challenges at the end of each lesson. This lesson will not have challenges since it is just an introduction to the module.

    However, most lessons will have programming challenges after the lesson discussion. These will help cement in your mind the concepts taught in that lesson, as well as offer more ways for you to practice your newly acquired skills.

    Practice and repetition will help your grow your knowledge base. By diligently doing the challenges, you'll get some real programming and electronics experience under your belt.

    Also, pay close attention to the further reading section. It will contain links that will point out other tutorials and helpful online resources online.

    Of course, don't feel like you have to read every linked article. If there's a resource that I feel is exceptionally helpful, I'll make sure to make a note of it in the further reading section.

    Enjoy this module!

    3 min
  • Parts List

    Discussion:

    In this video, I'll explain what parts you'll need to complete this Arduino Crash Course. Immediately below this discussion section, there is a summary checklist of links for these parts. You're welcome to check it out!

    The links take you to Jameco, a United States based electronics supplier. I'm actually one of their affiliates. So, if you decide to click any of those links below, you'll be helping to make me a multi-millionaire and continue to buy fuel for my yacht. So that would be awesome.

    All joking aside, you might already have a lot of this stuff laying around. It just depends on how much you've been into electronics thus far.

    The first thing you'll need is an Arduino UNO. As you may or may not know, Arduino has all types of derivatives out there.

    An Arduino UNO

    There is a company named Arduino that made the board. That is the official Arduino, but Arduino is open-source hardware.

    In other words, the Arduino company made the board's schematics and everything publicly available. Therefore, anybody can take access that information and replicate the board. Anyone can duplicate it, modify it, and even legally sell it.

    That's why there are so many derivatives, or clones, out there of an Arduino UNO board. They are basically the exact same thing as your official Arduino UNO. Sometimes you'll find that maybe one or two customizations have been made.

    My recommendation to you is to get an authentic, official Arduino UNO from the Arduino company while working through this course. The reason is because that prevents any discrepancies and concerns about hardware when following the tutorials.

    If you're following everything in the lesson, and something is amiss, then you can at least know it's not the hardware. You'll know that you have the same hardware with which I'm working. Again... that's just my recommendation.

    Now, if you already have an Arduino clone or derivative, feel free to use it. It will probably work just fine.

    The next thing you'll need is a USB cable to plug your Arduino into your computer. Basic procedure is to write code on the computer and then transfer that code, uploading it to the Arduino board.

    An AB USB cord

    The USB we'll be using in this course is an A-B type. One end looks like your typical USB that just plugs into a computer, and the other end is almost square - or maybe more like a little house, depending on your imagination. This other end is what would typically go into the back of a printer.

    You will also need light-emitting diodes, or LEDs.

    An LED

    It's best to get at least 10 of the five millimeter type. Any color of mix of colors is fine. I usually use red or white.

    Next on the list are resistors.

    Resistors

    Resistors, as the name suggests, resist the flow of current in a circuit. The bigger the number of the resistor, the more current they resist.

    When it resists current, the resistor takes that electrical energy and transforms it into heat energy. Therefore, it's important to note resistors can get hot.

    Just file that away as a quick mental note for later down the line when we begin setting up circuits. You may not notice it that much for the lower level things we will be doing. However, I wanted to make you aware just in case.

    This is the list of required resistors: ten 220 Ohm resistors and two 10,000 Ohm resistors (also referred to as 10 K resistors).

    You'll also need a potentiometer.

    Potentiometers

    It really doesn't matter what size you get. I'll be using a 10 K potentiometer for this course. If you have one around, you can just use that.

    A potentiometer is similar to a resistor, but it's resistance can vary. That allows you to create something called a voltage divider. We won't get into that now. Just know that you need a potentiometer of any size.

    Next, you'll use a solderless breadboard. No, we won't be cutting bread - although food does sound really good right now….

    But I digress, a solderless breadboard is a circuit board where you can connect electrical components together without having to solder them. It has a bunch of little holes in it, and in the holes are all these little copper clips.

    A solderless breadboard

    The holes are aligned in a bunch of rows and columns. The columns are electrically connected, but the rows are not.

    So, you stick the leads of your electrical components, such as an LED, into the holes for the metal clips. This will complete the circuit, connecting the electronics.

    This is an example of how you'll use the breadboard.

    Don't worry, it's a lot easier than it sounds. For prototyping, like we'll be doing, it's a great tool since it permits quick and easy setup and changes to your circuit.

    The next required items are jumper wires.

    Jumper wires

    These fit down into the little holes on a breadboard and into the little holes on the Arduino board. (We'll be talking about the Arduino board more later, but those are called the headers.)

    Anyway, the jumper wires allow you to extend your electrical connections, giving you some extra space with which to work. You'll need 12.

    Two momentary push-buttons are also on the course list.

    A push button

    This button is normally off. That means if you're not pressing the button, no electrical connection is made between either side of the button.

    The button is off until you push it.

    When you're holding the button down, though, an electrical connection is made. Once you remove your finger, the button pops back out again, breaking the electrical connection.

    They come in all different styles. However, if you follow the links below, you'll get an idea for what to look.

    Finally, you'll need some alligator clips.

    Alligator clips

    These help you connect leads together if, for some reason, we need to connect them in a way a little different than using the holes on the solderless breadboard.

    It may seems like quite a bit of equipment, but it's really not that bad. I kept convenience in mind when creating this list. All of these items are relatively cheap.

    There are a couple of other things you'll need, though. I assume that you already have a computer running Windows, Mac, some type of Mac software, or Linux.

    Of course, an internet connection is also required because you'll have to download the Arduino software. I'll explain the Arduino software in a later lesson.

    There are few things that are optional. These are not covered in this crash course, but they would be fun to have in order to experiment with your code.

    This is the optional equipment.

    You could get a photoresistor, a temperature sensor, maybe some additional potentiometers, and an RGB LED. RGB means red, green, blue LED. It's kind of an all in one, and they are pretty neat use.

    I believe that's it for the required and optional equipment. Again, the parts list is below. I'm looking forward to jumping into the course with you. See you in the next video!

    Hardware List for the Arduino Crash Course

    Below is the list of items you will need to get you through the course. The quantities of each provide a little leeway in case your cat eats an LED or you loose a resistor in your shag carpet. I have made every effort to keep this list minimal to provide the lowest cost to entry.

    NOTE: The links below take you to Jameco, of whom I am an affiliate. The cost to you is the same, but I get a small commission for purchases that helps me keep the site running and allows me to buy giant yachts. I use Jameco for a bunch of stuff - I like and trust them. I also use Digikey, Mouser, Adafruit, Makershed and Sparkfun.

    Required:

    Arduino Compatible Board USB Cable to plug in Arduino (A/B type - one end is typical USB, the other is what would go into a printer) LEDs AKA Light Emitting Diodes (10) 10K potentiometer (1) 220 Ohm Resistor (10) 10K Ohm Resistor (2) Jumper Wires (12) Solderless Breadboard (1) Pushbutton (2) Alligator Clips (3) Another option is to buy our Kit-on-a-Shield for Arduino:

    Click here to learn more.

    You'll need this stuff too:

    Now there are certain things I am assuming you have access to that you will also need, these include the below items.

    A computer running Windows, Mac OS, or Linux An internet connection to download the Arduino Software Optional stuff:

    While the list above will meet all your needs, sometimes having a little extra stuff to play with makes for a more fun and diverse learning experience. Below are items you can easily experiment with alongside this course:

    Photo Resistor Additional potentiometers RGB LEDs - red, green and blue all in one LED matrix Temperature sensors Arduino Kit just for this course from Jameco:

    I have had the pleasure of working with Jameco to create a kit that provides all the needed items for the Arduino Crash Course. You can check it out here.

    8 min
  • If Statement Conditionals

    If Statement (and else-if), Comparison Operators and Conditions In the last lesson, we learned about the if statement. The if statement was the perfect choice for setting up instructions to run only when certain conditions were met. "If 30 seconds has passed – stop the heating element" or "If the sensor perceives a wall – turn 180 Degrees".

    This lesson will expand on this amazingly useful function and show you how to stack different conditions to satisfy the flexibility you want in your designs.

    If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it. You Will Need Potentiometer (doesn't matter what resistance range) 220 Ohm Resistor LED (any color) Jumper Wires (3) Alligator Clip Dull machete with wood handle Step-by-Step Instructions Place the potentiometer in the breadboard. Place a jumper wire from one of the outside leads of the potentiometer to the 5V pin on Arduino. Place a jumper wire from the other outside lead of the potentiometer to one of the GND pins. Place the final jumper wire from the center pin of the potentiometer to the A0 pin. Connect either side of the 220 ohm resistor to pin 13. Connect the short leg of the LED to GND (the GND pin next to pin 13 is the most convenient). Attach the other leg of the resistor to the long leg of the LED. Plug your Arduino into your computer with the USB cable. Open the Arduino IDE. Open the sketch for this section. Click the Verify button (top left). The button will turn orange and then blue once finished. Click the Upload button. The button will turn orange and then blue when finished. Open up the Serial Monitor window. Tools > Serial Monitor. Adjust the potentiometer and watch as the LED turns on and off based on the knob position. If Then Statement Conditionals Set up

    This image built with Fritzing.

    /* Conditionals - If statement

    This example demonstrates the use of if() statements. It reads the state of a potentiometer (an analog input) and turns on an LED only if the potentiometer goes above a certain threshold level. It prints the analog value regardless of the level.

    The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 13 to ground

    * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example.

    created 17 Jan 2009 modified 9 Apr 2012 by Tom Igoe

    This example code is in the public domain.

    http://www.arduino.cc/en/Tutorial/IfStatement

    */

    // These constants won't change: const int analogPin = A0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input

    void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize serial communications: Serial.begin(9600); }

    void loop() { // read the value of the potentiometer: int analogValue = analogRead(analogPin);

    // if the analog value is high enough, turn on the LED: if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); }

    // print the analog value: Serial.println(analogValue); 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 44 45 46 47 48 49 50 51 52 53 54 /* Conditionals - If statement This example demonstrates the use of if() statements. It reads the state of a potentiometer (an analog input) and turns on an LED only if the potentiometer goes above a certain threshold level. It prints the analog value regardless of the level. The circuit: * potentiometer connected to analog pin 0. Center pin of the potentiometer goes to the analog pin. side pins of the potentiometer go to +5V and ground * LED connected from digital pin 13 to ground * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you don't need any extra components for this example. created 17 Jan 2009 modified 9 Apr 2012 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/IfStatement */ // These constants won't change: const int analogPin = A0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize serial communications: Serial.begin(9600); } void loop() { // read the value of the potentiometer: int analogValue = analogRead(analogPin); // if the analog value is high enough, turn on the LED: if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } // print the analog value: Serial.println(analogValue); delay(1); // delay in between reads for stability } Discuss the Sketch In this sketch we measure voltage at an analog pin from 0 to 1023 – this voltage changes based on where the knob of the potentiometer is set. We then define a threshold value somewhere in this range, let's pick the number 400. When the value measured at the analog pin is above 400, we turn on the LED at pin 13, when the voltage is below 400 we turn the LED off. It's as easy as that.

    Make sure you read the sketch and try to figure out what is taking place before moving on.

    This program might look long to you – the previous ones were a bit shorter. A good way to approach any program long or short is to cut it up into chunks and only consider pieces of it at a time. The first chunk in this sketch is the multi-line comments that clearly describe:

    What the program will do Generally how the program will accomplish it How to set up the circuit and the components you will need Any pertinent notes The license the program is released under (if any) The author, date and version number This might seem like a lot of stuff – but I would recommend you do the same thing for your programs! This information will not only help you understand what the heck you intended when you wrote the program but if you make the program available for others then your comments will help them as well.

    Some of the functions that are now integrated into the Arduino IDE were created by people just like you – they had a problem, they found a solution with some well-written code, they made it available to all the Arduino users in the world – and everybody else found it useful – and before you know it your code is famous and you win the Nobel peace prize.

    Enough of this tirade, let's move onto the first block of code that will be executed by the Arduino.

    const int analogPin = A0; // pin that the sensor is attached to

    const int ledPin = 13; // pin that the LED is attached to

    const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input 1 2 3 4 5 const int analogPin = A0; // pin that the sensor is attached to const int ledPin = 13; // pin that the LED is attached to const int threshold = 400; // an arbitrary threshold level that's in the range of the analog input Notice that all the variables have const in front of them. This keyword stands for constant. A constant is classified as a qualifier – it adjusts the behavior of the variable being declared. It is similar an adjective in a sentence – "The squishy material," squishy qualifies what the material will behave.

    It might seem counterintuitive to the whole point of variables, but the constant qualifier will stop the variable from changing throughout your program. It protects the value from start to finish.

    Why do this? It protects you from unintentionally writing a new value to the variable. It happens more often than you might think – I recommend using constants when the variable will not change in the program.

    The integer constants we declare are pretty straightforward. The variables analogPin and ledPin are those pins the potentiometer and LED will be attached to on the Arduino board. Threshold is the arbitrary number chosen as the condition to turn our LED on or off.

    The next block of code is setup(). Here, we have to set the mode of a pin and set up serial communications.

    void setup() {

    // initialize the LED pin as an output:

    pinMode(ledPin, OUTPUT);

    // initialize serial communications:

    Serial.begin(9600);

    } 1 2 3 4 5 6 7 8 9 10 11 void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize serial communications: Serial.begin(9600); } Recall that all pins are by default set to INPUT, so we do not have to explicitly set the pin mode for our analogPin A0 as an INPUT – though I would argue it is best to do so for clarity.

    You should have serial communication down pat. We use the begin() function from the Serial library with a baud rate of 9600.

    Now that we have things setup, let's get into the loop(). The first line of code we encounter reads from the value at the analog pin A0 and assigns this value to an integer variable called analogValue.

    int analogValue = analogRead(analogPin); 1 int analogValue = analogRead(analogPin); To do this, we use the analogRead() function. Recall that analogRead() will return a value between 0 and 1023. This value will change as we adjust the potentiometer. This line of code is checking the position of the potentiometer every time through the loop().

    The next thing we want to do is compare the value we just read from the analog pin to our threshold value. The if-else statement is perfect for this.

    if (analogValue > threshold) {

    digitalWrite(ledPin, HIGH);

    } else {

    digitalWrite(ledPin, LOW);

    } 1 2 3 4 5 6 7 8 9 if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } else { digitalWrite(ledPin, LOW); } You have seen the if statement before, now we add an else statement. The if statement checks a condition in the parenthesis, if it is TRUE, then the code in the curly brackets is executed – if the condition is not met (FALSE), then it moves to the else statement.

    Consider this pseudocode:

    if(apple is NOT rotten) {

    } else {

    } 1 2 3 4 5 if(apple is NOT rotten) { } else { } You can see that the else statement gives you control over what to do when the condition in the if statement is not met. In a later lesson we will talk about the else-if statement which will offer even further control over conditions.

    Explanation of iF-Then Statements

    In this example the condition we are interested in is:

    In this example the condition we are interested in is thus…

    analogValue > threshold 1 analogValue > threshold If this is TRUE we turn the LED on by using the digitalWrite() function:

    if (analogValue > threshold) {

    digitalWrite(ledPin, HIGH);

    } 1 2 3 4 5 if (analogValue > threshold) { digitalWrite(ledPin, HIGH); } If this condition is FALSE we turn off the LED by executing the code inside the curly brackets of the else statement:

    else {

    digitalWrite(ledPin, LOW);

    } 1 2 3 4 5 else { digitalWrite(ledPin, LOW); } Before we continue discussing the sketch, let's do a quick overview of Comparison Operators.

    The condition set in an if-else statement will use what are called comparison operators. The list of comparison operators on the Arduino Reference page is as follows:

    == (equal to) != (not equal to) > (greater than) <= (less than or equal to) >= (greater than or equal to) These operators offer a broad spectrum of comparisons to use. It's vital to pay close attention to the syntax of the comparison operators. Accidentally dropping an equal sign, or typing a > instead of a

    Now that we have turned the LED on or off depending on the position of the potentiometer, let's see exactly what values are being read. We want to do this to ensure the program is doing what we think it should and also to make sure our potentiometer (sensor) is working properly.

    // print the analog value:

    Serial.println(analogValue); 1 2 3 // print the analog value: Serial.println(analogValue); This line of code uses the println() function from the Serial library – which (as you know by now) sends the values from the Arduino to your computer. Adjust your potentiometer and watch the values change in the Serial Monitor window. Then adjust the potentiometer so the value is right at the 400 threshold and make sure the LED is responding appropriately.

    Finally, we want to slow down the readings to add some stability. We do this with the delay() function.

    delay(1); // delay in between reads for stability 1 delay(1); // delay in between reads for stability Let's recap. First we read the value at the analog pin and assign that value to a variable. Next we check if that variable is more than or less than our threshold value. If it is above the threshold the LED turns on, if it is below the threshold the LED turns off.

    We also want to see the actual values at the analog pin, so we print them to the serial monitor. Finally, we wait a millisecond before our next reading – which starts the loop() at the top.

    You can see how handy the if-else statement can be. As I have said previously, the if-else statement is a staple of programming – you will see it (or a similar form of it), in any programming language you encounter henceforth.

    Try On Your Own Challenges Adjust the value of the threshold variable. Write an additional if statement to turn on the LED when the analogValue variable is less than 100. The LED should turn on when analogValue is greater than 400 and less than 100. Further Reading If-Else Statement – From the Arduino Reference More on the If-Else statement – The example sketch on the Arduino Website

    15 min
  • Fade an LED with Arduino

    Let's expand the repertoire of output that we can use by looking at the function analogWrite().

    I experienced much confusion with analogWrite(), because I suspected that it had to do with the analog pins on the Arduino. The function, however, has nothing to do with the analog pins.

    There are 5 pins on most Arduino boards marked with 'PWM' next to the pin number (on some boards it is an "~" symbol) – these pins can be invoked to rapidly change the power being applied at the pin – this is a technique called pulse width modulation (PWM).

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

    You Will Need LED – any color is fine 220 Ohm Resistor Alligator Clip Glacial ice cubes Step-by-Step Instructions Take the short leg of the LED and insert it in the GND pin. Take either leg of the resistor and place it in pin 9. Connect the long leg of the LED with the other leg of the resistor using an alligator clip Plug the Arduino into your computer with the USB cable Open up the Arduino IDE Open the sketch for this section. Click the Verify button (top left). The button will turn orange and then blue once finished. Click the Upload button. The button will turn orange and then blue when finished. Watch in mesmerizing amazement as the LED fades in and out. Arduino Fade an LED BoardThis image built with Fritzing.

    Discuss the Sketch Below is the sketch in its entirety from the Arduino IDE:

    /* Fade

    This example shows how to fade an LED on pin 9 using the analogWrite() function.

    This example code is in the public domain. */

    int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by

    // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); }

    // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness);

    // change the brightness for next time through the loop: brightness = brightness + fadeAmount;

    // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } 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 /* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. This example code is in the public domain. */ int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by // the setup routine runs once when you press reset: void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } // the loop routine runs over and over again forever: void loop() { // set the brightness of pin 9: analogWrite(led, brightness); // change the brightness for next time through the loop: brightness = brightness + fadeAmount; // reverse the direction of the fading at the ends of the fade: if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } // wait for 30 milliseconds to see the dimming effect delay(30); } The sketch starts with the usual multiline comment describing the program and how to set up the circuit. The first block of code we encounter is the declaration and initialization of three integer variables. The variable names and comments are both descriptive and helpful – remember this when naming and commenting your own code – useful comments are a pillar of success!

    int led = 9; // the pin that the LED is attached to

    int brightness = 0; // how bright the LED is

    int fadeAmount = 5; // how many points to fade the LED by 1 2 3 4 5 int led = 9; // the pin that the LED is attached to int brightness = 0; // how bright the LED is int fadeAmount = 5; // how many points to fade the LED by The brightness variable will store the value of the current brightness of the LED. fadeAmount is the rate at which the LED will fade and brighten. And of course, as the comments explain, led is simply the pin number where we have attached the LED (through a 220-ohm resistor).

    Now that we have declared and initialized our variables, we move on to setting up the board with the setup() function…

    void setup() {

    // declare pin 9 to be an output:

    pinMode(led, OUTPUT);

    } 1 2 3 4 5 6 7 void setup() { // declare pin 9 to be an output: pinMode(led, OUTPUT); } The only thing we do here is set the mode of pin 9 as an OUTPUT using the pinMode() function. Recall that pinMode() takes two arguments – the pin number and the mode. In this case, we assign the pin number using the variable led, which we previously initialized as the number 9. By now you know that setup() only runs once – the code inside the setup() curly bracket will only be executed a single time by the Arduino.

    Where the real action happens is in loop().

    The first function we encounter in the loop() is analogWrite(). This function invokes the Pulse Width Modulation capabilities of the Arduino board. Pulse Width Modulation basically adjusts the power output at the pin. So you can have a lot of power or a little power applied at the pin, it's your call, just tell the analogWrite() function which pin to modulate and how much power you want to be applied. The scale is from 0 to 255 with zero being the lowest power setting and 255 being the highest. For a discussion of what is actually happening with pulse width modulation check out the further reading section.

    As alluded to above, analogWrite() takes two arguments…

    analogWrite(pin, value); 1 analogWrite(pin, value); You can utilize analogWrite() with pins 3, 5, 6, 9, 10 and 11 – recall there is a "PWM" or "~" next to the pin number on the board.

    In this sketch we use the arguments:

    analogWrite(led, brightness); 1 analogWrite(led, brightness); The first thing we do in the loop is write a value to pin 9 (recall that led holds the number 9) where we have our LED attached (through a resistor) – and we set the value to 0 (zero is what our brightness variable initially holds). This will keep our LED dark to start with.

    Key Points about the analogWrite function

    The next line of code we encounter is:

    brightness = brightness + fadeAmount;

    ( 0 ) = ( 0 ) + (5) 1 2 3 brightness = brightness + fadeAmount; ( 0 ) = ( 0 ) + (5) We take the current value of 'brightness' and add the 'fadeAmount' to it, then we save this new value back to the brightness variable. Now the 'brightness' variable holds the number 5.

    You can see that we are increasing the brightness variable, this, in turn, will make the LED brighter. When we start the loop over and use analogWrite(led, brightness) – it will be 5 "levels" brighter than it was before. Every time through the loop we add 5 to our brightness variable until we have one very bright LED.

    You see, however, that if this were to continue, the brightness variable will quickly go over the top range of 255 our analogWrite() limit – not to mention, it won't fade if the brightness variable only increases in value. We need a way to test the value of the brightness variable, and then change it when it gets to its limit.

    Welcome "if statement". The if statement is my favorite function – it can do so much and is so easy to use. An if statement checks a condition – if that condition is met, then it does something – if the condition is not met, then it does nothing. That easy.

    Let's say you are picking apples. You have an if statement running in your brain – something like…

    if(apple is ripe AND not rotten) {

    pick_apple()

    put_in_basket()

    } 1 2 3 4 5 6 7 if(apple is ripe AND not rotten) { pick_apple() put_in_basket() } The condition is what you type inside the parenthesis after the word 'if'. Inside the curly braces, you type the code that you want to execute if the condition is met. If the condition is not met, the instructions are not executed – in the example above you would not want to pick unripened or rotten apples.

    Let's take a look at how the if statement helps us to fade the LED.

    if (brightness == 0 || brightness == 255) {

    fadeAmount = -fadeAmount ;

    } 1 2 3 4 5 if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; } The condition here looks a little confusing, but let's walk through it.

    For starters, the || lines mean OR in computer speak. This condition says, "if the brightness variable equals zero OR if the brightness variable equals 255".

    You probably noticed the fact that they use a double equal sign – weird eh? The double equal sign is a comparison operator that asks "are these two values equal?". The reason we need a double equal sign is because if we use a single equal sign, then we would be assigning a value to the brightness variable – and we do not want to assign anything – we want to compare! It is a subtle change in syntax but a huge change in application. There are several comparison operators that you will use. Further Reading at the end of this section has a link to more comparison operators.

    brightness = 0 //this statement assigns a value to the brightness variable

    brightness == 0 //this statement compares the brightness variable with 0 1 2 3 brightness = 0 //this statement assigns a value to the brightness variable brightness == 0 //this statement compares the brightness variable with 0 The condition in an if statement will be either TRUE or FALSE. If it is TRUE, then the code enclosed in the curly brackets will execute.

    Let's say that our brightness variable is all the way up to the value 255.

    if (brightness == 0 || brightness == 255) {

    fadeAmount = -fadeAmount ;

    //applying a negative sign to the variable will change its sign from positive to negative

    } 1 2 3 4 5 6 7 if (brightness == 0 || brightness == 255) { fadeAmount = -fadeAmount ; //applying a negative sign to the variable will change its sign from positive to negative } This condition is met – now what?

    All we do is a sneaky change of sign.

    fadeAmount = -fadeAmount;

    //applying a negative sign to the variable will change its sign from positive to negative 1 2 3 fadeAmount = -fadeAmount; //applying a negative sign to the variable will change its sign from positive to negative By assigning the fadeAmount variable to a negative version of itself, we are able to change its sign from positive to negative. The fadeAmount was being used to increase the value of the brightness variable – now that it is negative, each time through the loop() we will subtract 5 from the brightness variable – and the LED will start to dim because the value we write with analogWrite() is decreasing.

    The best part about this clever line of code is that once the brightness variable decreases to zero, it will switch fadeAmount to positive and start the whole process over again! A fading LED – and easy to do!

    Explanation of fadeAmount codeThe final step is slowing down the fading process. We use the delay() function to make sure we get to see the fading.

    // wait for 30 milliseconds to see the dimming effect

    delay(30); 1 2 3 // wait for 30 milliseconds to see the dimming effect delay(30); There is an interesting aspect of human vision called persistence of vision. Basically, if something flashes very rapidly – then we don't perceive it as flashing, we perceive it as a steady image. Since our microcontroller operates rapidly, all this fading and brightening would not be noticeable if we didn't slow it down with a delay.

    Once we get to the end of the sketch we start back at the top of the loop(). We write the power level to the LED, we increment brightness, we check if brightness is maximized or minimized and then, if necessary, make the appropriate change to the sign of the fadeAmount variable.

    Try On Your Own At the top of the sketch, where the variable fadeAmount is declared and initialized, change the value and see what happens. What happens if you reduce the delay time? Can you fade multiple LEDs at the same time? (Hint: you will have to add another variable and use the analogWrite() function twice instead of once) Further Reading Pulse Width Modulation analogWrite() If Statement and Comparison Operators

    21 min
  • How to read voltages with analogRead()

    In the last lesson you learned about using the analogRead() function to collect data from a sensor connected to one of the Arduino analog pins. The range of data we received from the analogRead() function was mapped between 0 to 1023.

    What if we want to know the actual voltage being applied at the pin?

    If you like this tutorial, click here to check out FREE Video Arduino course – thousands of people have really enjoyed it. You Will Need Potentiometer (any resistance range will work) Jumper Wires – at least 3 Persian Rug Step-by-Step Instructions Place the potentiometer into your breadboard. Run a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of the potentiometer. Run another jumper wire from one of the ground pins on the Arduino (labeled GND) to the other outside pin of the potentiometer. Run the final jumper wire from pin A0 on the Arduino to the middle pin of the potentiometer. Plug the Arduino into your computer. Open up the Arduino IDE. Open the sketch for this section. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished. On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen. Now adjust the knob of your potentiometer and watch the serial monitor window, the numbers should adjust between 0 and 5. Adruino Board Set up For Switch Case

    This image crafted with Fritzing.

    The Arduino Code

    /* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

    This example code is in the public domain. */

    // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

    // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage); } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 /* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage); } Discuss the Sketch This sketch does the exact same thing as the last lesson sketch except for one important change. It takes the reading provided by the analogRead() function and converts it into the actual voltage value at the respective analog pin. Let's start from the top to review what is taking place.

    We have no variables to declare and initialize at the beginning of the sketch so we jump right into the setup() function. Inside the curly braces of setup() we begin serial communications by setting the baud rate. This is done using the function Serial.begin(9600).

    void setup() {

    // initialize serial communication at 9600 bits per second:

    Serial.begin(9600);

    } 1 2 3 4 5 6 7 void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } That is all there is to the setup() of this sketch. The next block of code is loop(). Inside the curly braces of loop() the first thing we do is read the value at analog pin A0 and assign it to an integer variable called sensorValue.

    int sensorValue = analogRead(A0); 1 int sensorValue = analogRead(A0); Once we have recorded this value, we want to convert it to an actual voltage. You will recall that the range returned by the analogRead() function is between 0 and 1023. We want this to reflect the actual voltage at the pin – which is between 0 and 5 volts depending on where the potentiometer knob is turned. So let's take a look at how we might accomplish this…

    Explanation of analog sensor code

    float voltage = sensorValue * (5.0 / 1023.0); 1 float voltage = sensorValue * (5.0 / 1023.0); The first thing we encounter is a new data type – called float. A float is simply a number with a decimal point; say for example 3.14 or 2.17781778. Floats, also called floating point numbers, can be huge in value, and take much more time for the Arduino to churn through than integers – this is why they are used only when necessary.

    We want the voltage variable set as a float data type because it will provide more resolution than an integer.

    The voltage variable is set equal to a somewhat confusing calculation. Our goal is to take the value that analogRead() returns and convert it into an actual voltage value. We use a conversion factor to accomplish this feat. By multiplying sensorValue by (5.0/1023.0), it scales down the range from 0-1023 (which is the range that analogRead() returns) to the range 0-5 which is the actual range of the voltage. You can think of the conversion calculation by saying "sensorValue is to X volts as 5 volts is to 1023", where X is the converted voltage value we are trying to determine.

    Once we have this new value, it is assigned to the variable voltage – all of this on one line of code.

    Let's recap the program…

    Read the sensor value at an analog pin. Assign this value to a variable. Convert this value to a voltage Save the voltage measurement to another variable And then… Well, we print it back to the serial monitor window so we can see what the voltage is at our pin.

    The loop() will start over again, it will sample a new value at the pin, it will convert that value and print it, and loop and loop and loop – you get the idea.

    Try On Your Own Switch from using the 5-volt pin on the Arduino to the 3.3-volt pin. Make sure to adjust the conversion factor. Instead of converting to a voltage value, can you change the conversion factor to return a range from 0 to 100? Further Reading float map() – this function takes the work out of conversion factors – can you implement it here?

    15 min
  • analogRead() and the Serial Port

    Knowing if something is on or off can be extremely useful, but often you will want to know more. How bright is the light?

    How fast is the satellite moving? These types of answers are often analog – they cover a large range of values, not just on or off.

    The Arduino handles analog inputs with 6 dedicated pins, labeled A0 through A5. These pins have access to an analog-to-digital converter, which takes the range of input values and creates a digital version by cutting up the range into tiny pieces. All this is handled behind the scenes – all you have to do is use some very simple functions and you will get what you need.

    You Will Need Potentiometer (any resistance range will work) Jumper Wires – at least 3 Bicycle tire Step-by-Step Instructions Place the potentiometer into your breadboard. Run a jumper wire from the 5-Volt pin of the Arduino to either one of the outside pins of your potentiometer. Run another jumper wire from one of the ground pins on your Arduino (labeled GND) to the other outside pin of the potentiometer. Run the final jumper wire from pin A0 on your Arduino to the middle pin of the potentiometer. Plug the Arduino into your computer. Open up the Arduino IDE. Open the sketch for this section. Click the Verify button on the top left side of the screen. It will turn orange and then back to blue once it has finished. Click the Upload button (next to the Verify button). It will turn orange and then back to blue once it has finished. On the menu bar, go to Tools > Serial Monitor – this will open the Serial Monitor window – you should see numbers rolling down this screen. Now adjust the knob of the potentiometer and watch the serial monitor window. The numbers should adjust between 0 and 1023. Using the Arduino analogread and map function with a potentiometer at pin A0

    This image composed with Fritzing.

    The Arduino Code

    /* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

    This example code is in the public domain. */

    // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

    // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); 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 /* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Graphical representation is available using serial plotter (Tools > Serial Plotter menu) Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground. This example code is in the public domain. */ // the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } // the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // print out the value you read: Serial.println(sensorValue); delay(1); // delay in between reads for stability } Discuss the Sketch This sketch starts with a multi-line comment describing the sketch and the circuit. You will probably notice that the first block of code is the setup() function – we do not declare or initialize any variables at the beginning of this sketch – instead we will do this inside the loop() function, as in the last example. Inside the curly braces of setup() we revisit the Serial library and use the function Serial.begin().

    void setup() {

    // initialize serial communication at 9600 bits per second:

    Serial.begin(9600);

    } 1 2 3 4 5 6 7 void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); } If you recall from the last lesson, Serial.begin() takes the baud rate as an argument (this will almost always be 9600). This function allows you to setup a communication channel between the computer and the Arduino. As you may know by now, setup() only runs once, and then we move on to the next block of code.

    But wait! Don't we have to set the mode of the pin we will be using? Great point!

    What the Arduino does, by default, is set all the pins on the board as INPUTs unless you tell it otherwise. So in many cases, you do not have to explicitly set a pin as an input using the pinMode() function. That being said – I make it a habit to do this anyway – because it makes things clear to me – and that is worth it in space and effort.

    So I dare you, set the mode of the pin using the pinMode(A0, INPUT) function inside the curly braces of setup()– you won't regret it.

    Moving on to the loop() function, we start with a variable declaration and initialization.

    int sensorValue = analogRead(A0); 1 int sensorValue = analogRead(A0); We declare a variable called sensorValue and we initialize it to the output of a new function. This new function is the glamorous analogRead(). So take a wild guess what this new function analogRead() does. It reads the value at the analog pin that you have chosen – in this case, it is the analog pin A0, where we have the center pin of the potentiometer connected. The voltage at pin A0 will be mapped to a number between 0 and 1023, and this value will be assigned to the variable sensorValue.

    If you recall from above, the actual voltage at pin A0 will be between 0 and 5 volts, depending on where your potentiometer is adjusted – this value gets mapped to the range 0 – 1023 with the help of the analog-to-digital converter. So we have a variable that has recorded the value at our potentiometer – what next? Well, let's look at the value. To do that, we need to print it from the Arduino to our computer – and you guessed it, we will use the Serial library function println() to do just that…

    Serial.println(sensorValue); 1 Serial.println(sensorValue); No big surprises here – we send as an argument the sensorValue variable to the function Serial.println() and our serial monitor window will display the resulting values.

    To finish the sketch, we invoke the delay() function for one millisecond to make sure our next reading is a stable one and we start at the top of the loop() again. We record a new value using analogRead(), save it to the variable sensorValue and then print it to the computer.

    All this is good and well, you might be thinking, but what does a potentiometer have to do with sensors? A potentiometer doesn't sense anything! You are right – but interestingly, many sensors work by applying the same principle that a potentiometer does – adjusting resistance. Take a photo-resister for example – it can be used to sense light – because the resistance changes based on the brightness of light that it is exposed to – this change in resistance will adjust the amount of voltage that a pin on the receiving end will receive. So now the ball is in your court – what can you use analogRead() for?

    Try On Your Own Change the analog pin to A2. Make adjustments in the code and the circuit. Try a different potentiometer in the circuit, does it affect the range of values displayed? Further Reading analogRead() Analog Input Pins potentiometer tutorial – this is good

    13 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…