Tag Archives: Bread board

Arduino LESSON 4: Printing Over the Serial Port

In Lesson 3 you learned about for loops, and how for loops can make your life as a programmer much simpler.  You wrote a program that would blink a red LED and then a yellow LED the number of times indicated by the program. In this lesson, we will be using the same 2-LED circuit. If you don’s already have it put together, you should go ahead and do it now. This is a schematic of the circuit:

LED Schematic
This circuit will allow you to independently control two Light Emitting Diodes from the arduino microcontroller

In order to independently blink the two LED’s you were probably using code similar to this:

As you learned in Lesson 3 for loops make your program much simpler to write and much simpler to modify. In the code above, to change how many times the LED’s blink, you just have to simply change two lines of code:

By changing either of these variables at the top of your program, you effectively adjust how many times each LED will blink in each cycle.

Now besides just blinking the LED, it would be nice to provide some information directly to the user. It would be nice to be able to have the arduino ‘print’ information to your computer screen. You can do this by working with the Serial Port and the Serial Monitor.

In order to use the Serial Port, the first thing you have to do is turn it on inside your program. Since this is something you would only need to do once, you do it in the void setup(). The code below, when placed inside the void setup() will turn your serial port on:

You will want to keep whatever else you already have going on in your void setup(), and just add the line of code above to it. Note that this line of code tells the arduino to turn the serial port on. The ‘9600’ tells the arduino to communicate at 9600 baud, which basically is just the speed you will be working at. The higher the number the faster data will be sent and received over the serial port. This can be set to different numbers, but the important thing is that everyone is talking and listening at the same speed. So, if you tell the arduino to run at 9600 baud, when you open your serial monitor later, you need to make sure it is set to the same speed.

OK, now that you have started your serial monitor you can start sending and receiving data over it. The first and easiest thing is to send data to it. We do this using Serial.print and Serial.println commands. These commands send data to the arduino serial monitor. We will show you how to open the serial monitor in a minute, but for now, lets add some print statements to our program.

Notice that the loop above is our loop for blinking the red LED. We have added a new line to the start of the loop . . . Serial.println(j). After you add the line of code, download the code to the arduino, and then pop open your serial monitor. You do this by clicking on the magnifier icon at the upper right corner of the arduino IDE.

Arduino Serial Monitor
Click on Magnifier to Open the Arduino Serial Monitor

When you click on the icon you should see the serial monitor pop open, and you should see the values of j being printed out. It should look like this:

Arduino Serial Monitor
Serial Monitor shows what you are printing

You should see the numbers printing out as your program goes through the for loop. Make sure that you have the baud rate set in the lower right corner of the serial monitor to the same value you specified in the program. 9600 baud is usually a good choice.

In the example above, we are printing the variable j each time the program loops through the for loop for the red LED. We can also print out a string of text. To print out a string of text, you put the string in quotes.  To print a string you would do something like Serial.println(“Blinking Red LED”). It will print the words between the quotes. Lets add this to our code, but lets add it before the for loop, so it just prints it once each cycle. Your code for the red LED should look like this:

Now download and run the code and look at your serial monitor. You should see those words printing each time before the Red LED blinks. Now go ahead and add similar code to your Yellow LED loop so that it will announce which LED is blinking and then the blink counter. Your code should look like this for the two for loops:

 So this code will announce which LED is blinking, and will then give a count of which blink you are on. Pretty cool! Your Serial Monitor should now be looking something like this as it tells the user which LED is blinking and which blink you are on:

Arduino Serial Port
Serial Monitor shows what values the arduino is printing

So we can see that we can print both variables, like j, and strings. When printing strings, it is important to include the text in quotes, so that the arduino knows you are printing the string of text, and not a variable.

Notice that each time you use Serial.println it goes to the next line. If you want to print to the same line, without advancing to the next line, you should use the command Serial.print. In the example above, lets say instead of just printing the number j, you want text in front of the number that says ‘you are on blink number ‘ and then the number. You can modify your code and add another Serial.print command to the for loops. I will do it for you on the red loop, but you need to figure out the yellow for yourself:

You should get something that looks like this:

Arduino Serial Port Example
Nicely formatted Arduino Serial Monitor

Make sure to play around with blank spaces when you print strings so that your output is neat and readable.  Notice on mine that by using blank spaces I get an indent which makes the scrolling text more readable.

So, we need to carefully consider when it suitable to use Serial.print vs. when we should use Serial.println.

Arduino Lesson 2: Using a Breadboard

Breadboard for prototyping your Arduino Circuits

In this lesson we will begin to build our first circuits that will be controlled by the Arduino. We will start out with simple circuits and build from there. It is important for you to learn the basics before moving on, and one of the most important basics is how to use a Breadboard.  In this lesson we will learn the ins and outs of breadboards, and by the end of the lesson you will have your first circuit built, and it will be controlled by the arduino.  Watch the video below for in depth description of how the breadboard works.

In order to build a circuit, you have to connect circuit elements together. You could run a bunch of wires to connect components, but you find that very quickly you end up with a rats nest of wires, and it becomes impossible to debug. In order to keep your circuit organized you need to use a breadboard, pictured above. The breadboard allows you to connect components together by plugging them into the little holes. The key is to understand how the holes are connected. As you can see in the diagram, the holes in a column (when oriented as shown in the picture) are connected together.  So to connect components together you need to plug the leads you want connected into the same column. Note that the columns are not connected across the “trench” in the center of the board. Also notice that as the long rows at the top and bottom are connected together. These are typically used to create “rails”.  These are typically used for grounds and supply voltages you might need to connect many components to. Notice some rows are marked (+) and some(-). These are just markings. The row will be set at whatever voltage YOU connect to it.

LED Circuit
Simple LED Circuit for blinking an LED

So, lets look at a real example. In this circuit we have a voltage supply connected to an LED through a resistor. We will need to take this circuit schematic and figure out how to connect it up in the real world. As mentioned above this should be done using a breadboard. You can see that you need to connect the voltage supply to one leg of the resistor. The other leg of the resistor is connected to the LED. Note that the LED is directional, meaning it has to be connected in a certain orientation. You must connect the Cathode to the positive voltage. The Cathode is typically the longer of the two leads on the LED. If you put the LED in upside down, it will not light up. The other leg of the LED needs to be connected to the negative terminal of the voltage supply. For this project we will supply the voltage from the Arduino microcontroller. That way, we can turn the LED on and off from our program.

Look at the diagram at the top of this post to see how the holes are connected in the breadboard, and figure out a way you could connect the circuit up using the PC board. There are many ways to hook it up, but one that will work is shown here.

Arduino LED Circuit
This shows how you can hook an LED to the arduino through a current limiting resistor

When you hook your circuit up in real life, it should look something like this:

LED Arduino Circuit
Photograph of the circuit. Your LED will not come on until you write your program

Remember that it is the long leg of the LED that is connected to the resistor and the short leg to the black wire going to ground.

Now, if you follow along and develop the code as outlined in the video you should be able to do amazing things with the LED.

RESOURCES: On all these lessons I will include resources on where you can purchase the items mentioned in the lecture.

Arduino Microcontroller: You can get a good deal on the arduino on Amazon. Arduinos are open source and many are cheap chinese knockoffs, so you want to make sure you get an “official” version, which you can at the link above.

Arduino Beginner’s Kit: While the bare arduino will get you started, I really suggest getting the complete Beginner’s Kit. The projects I will feature in this tutorial set will use the components in this kit, and it is probably cheaper to go ahead and get the kit than to buy the individual components as you go along. The kit is under $100 on Amazon.