Tag Archives: Serial Port

Arduino Tutorial 18: Reading Numbers from the Serial Monitor

In many projects, it is very useful to get input from users. You need to be able to prompt the user for input, and then input that information into the Arduino. In most cases, users will not know how to program, so you need to have an easy way to interact with them. In earlier lessons, we showed how you can send messages or information from the Arduino using the Serial Monitor. In today’s lesson, we show you how to read input from the user through the Serial Monitor.

If you want to follow along at home, you can order the Arduino Kit we are using HERE.

Python with Arduino LESSON 4: Expanding your Virtual World

In this lesson we will expand the virtual world we created in Python with Arduino LESSON 3. We will be creating a virtual world that will track a simple scene in the real world. In this project, the virtual world will track both the position and the color of a target in the real world. This lesson requires that you have the Python software and libraries installed, which we explained in LESSON 2.

This is our circuit with the HC-SR04 ultrasonic sensor and the TCS230 Color Sensor

This Lesson will be a bit more involved, and I will take you through it step-by-step. I will need to break things into two parts. In today’s lesson we will cover the Arduino side. We will develop the software that will measure distance and color, and then send those numbers over the serial port. Then in tomorrows lesson, we will develop the Python software to create a really cool virtual graphic to display the data in a virtual world.

For this project you will need the HC-SR04 ultrasonic sensor, the TCS230 Color Sensor, the Arduino Microcontroller, and some male/female jumper wires to connect to the color sensor.

The Ultrasonic Sensor can be attached per the schematic below:

Simple Circuit for Measuring Distance

Detailed tutorial on using this sensor was described in Arduino LESSON 18, so we will not go through all the details of using the sensor here. Review that lesson if you need more help. Key point here is to connect it as seen in diagram above.

You will also need to connect up the Color Sensor.

Connecting the Color Sensor to the Arduino

Color Sensor Pin
Arduino Pin
S0 GND
S1 5V
S2 pin 7
S3 pin 8
OUT pin 4
VCC 5V
GND GND

Use of the color sensor was described in detail in Arduino LESSON 15.  You should be able to develop to write the software yourself based on earlier lessons to make measurements from both the Color Sensor, and Ultrasonic Sensor, but if you get stuck, you can glance at my code below. Again, it is important for you to write your own code and not copy and paste mine. Mine is just a reference if you get stuck.

The key point to notice with this code is the print statements, summarized below:

 Notice that we are printing  our color strengths and distance on one line separated by commas. It is important to note the order of the data. When we read this in Python, we will read it in as one line of text, and then we will parse it into its individual values. So, we must make note and remember the order the data is arranged in in this line.

Remember when you have your python program reading this data, you must have your serial monitor closed. For now though, run your program and look at the serial monitor to verify you are getting correct data in the expected format.

In the next Lesson, LESSON 5, we will build the Python program to create a virtual world from this data.

LESSON 12: Simple and Easy Way to Read Strings, Int’s and float’s Over Arduino Serial Port

One of my biggest frustrations in programming is the confusion over inputting data over the serial port. There are lots of Arduino tutorials that make it much harder than it has to be. There are many difficult and complicated ways you can read data over the serial port, but luckily there is an easy way. While we touched on this briefly in earlier videos, it is such an important topic I want to focus on it in this lesson.

In order to read data over the serial port,  you first have to determine what type of data you will be expecting. Almost all possible needs can be covered by three simple data types. Usually you can make just about any project work with Strings, Float’s, or Int’s. So, you need to know how to read these three data types over the serial port. The easiest way to read a string of text is using the Serial.readString() command. The easiest way to read a Float is with the Serial.parseFloat() command and the easiest way to read in Int with with the Serial.parseInt() command.

To practice this, lets do the following. Write a program that will promt the user for his age, for how much he weighs, and then how tall he is in feet. Then print out a nicely formatted output that relays that information back to the user.

In order to do this the first thing we must do is decide what type of variables to use for each of these three pieces of data.  For the person’s name, you naturally would use a String, since a name is a string of text. To store the weight, we would just about always round to the nearest whole number so it would make sense to use an Int for this. On height, we would not normally round to the nearest foot . . . we might want to say that someone was 5.5 feet tall. In this case we should use a float, since we want the decimal numbers. So, we would then declare these variables the suitable type and then read them in using the corresponding commands.

Also, remember when you are going to read data from the serial port you must do three things: 1) Prompt the user for data, 2) wait for him to input the data, and 3) read the data. This should be review as we have done this in several earlier lessons.

Try and write the program yourself, but if you get stuck you can look at the code below that I wrote for this problem.

OK, hopefully you were able to get this working. Now time for you to go solo and write a program on your own.

Prompt the user for his name, and his weight in pounds. Then use that to calculate his weight in Ounces, in grams, and in carats. Note, that is carats, like they measure diamonds, not carrots like you eat.

After doing these calculations send a message to the user greeting him by name and telling him is weight in pounds,  in ounces, in grams,  and in carats.

In order to do this assignment you will have to do some research to figure out how to do the conversions.

Arduino Lesson 6: Reading From the Serial Port

So far in our programming we have set the variables inside of the program, usually up at the top. In order to change the number of times the LED’s blink, we would change the lines of code that set those variables. This is OK for playing around, but you can see that if you want other people to use your programs you can not have them playing around with your code. You need to be able to get input from the user without modifying the code. We can do that using the Serial Port. Just like we can print information to the user using the Serial Port, we can also get information from the user using the serial port.

In these exercises we will continue to use the circuit created in Arduino Lesson 3. If you need help in putting the circuit together, go back and review that lesson. Here is a diagram of the circuit we are working with.

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

Also, as a reminder, here is the code we have been working with that incorporates all the things we have learned so far. I am including this code so you can look at it if you get stuck, and to serve as an example for the work you do. To learn programming though, you need to be typing in your own code, making mistakes, and then finding an correcting your mistakes. You will not learn programming if you simply go through these lessons copying and pasting my code.

OK, look over this code and review what we have learned so far. At the top of the program we declare our variables, and we assign values to them. So far we have worked with variables of type int and type String.  Then in the void loop we start our serial port, and we set our two arduino pins to OUTPUT.  In the void loop we have built two for loops .  .  . one to blink the red LED and then one to blink the yellow LED. The parameters used in the for loop like how many times to blink and how long each blink should be are all defined at the top of the program.

As we mentioned at the beginning, it is OK to start out doing things this way, but at some point you need to be getting your parameter values from the user, and not hard coding them into the program. You would like the program to ask the user how many times he would like to blink the red LED, and then ask how many times he would like to blink the yellow LED. This is really pretty easy to do, and we do it over the serial port, very similar to how we learned to print in lesson 5.

In order to get input from the user, you need to make sure that you have turned your serial port on in your void setup. You do that with a Serial.begin(9600); command, as seen in the code above. You always need to have this command in your void setup() if you are going to print to the serial port or read from it. Now, in order to get input from the user, you need to do three things:

    • Prompt the User for the Input
    • Wait for the User to Enter the Input
    • Read the information from the serial port

In the program above,  lets say that in our void loop each time through the loop we want to prompt the user for how many times he wants the red LED to blink and then after that prompt him for how many times he wants the yellow LED to blink. In this scenario, we are now getting the parameters from the user instead of hard wiring them into the program. In this case, we still have to declare our varialbes, but we do not need to assign values to them. Hence in the example above the code:

Should be taken out and replaced with:

You see, now we are only declaring our variables. We are not assigning values to them, because we will be getting the values from the user. It is important, however, that any variables that we will use still need to be declared.

Now in our void loop() this would be the code to get from the user the number of times he would like to blink the LED.

There is a lot going on in these few lines of code, and lots of new things for you to learn, so lets unpack it pretty carefully. The first line should be familiar to you. You are just printing to the serial monitor a message to the user that you are waiting for input. In this case he will see a message asking him to put in a number. Now, we have to remember that the computer can work much faster than a person, so when a computer asks a person to do something, it is important for the computer to sit and wait for the person to complete it. This is done in the second and third lines of code above. This is an example of a while loop. You can see the while loop has a clause associated with it that starts and ends with the curly brackets. The program will continue to execute what is between these curly brackets as long as the condition that is in the parenthesis is true. So a while loop is sort of like a for loop, except the while loop can continue to loop forever as long as the condition in the parenthesis is true. So, we need to understand the ‘condition’ part of the code above, which is Serial.available()==0.  This is not as confusing as it looks. Serial.available() is a function that when you call it, it returns a ‘1’ if the user has input data, and a ‘0’ if the user has not input data. So, if the user has not input data, Seraial.available() will be 0, the condition will be true, and the program will stay in the while loop.

So the bottom line is that

 will sit and loop forever waiting for the user to input the data. When the user inputs the data, it will them move on to the next lines of code. Notice that we do not do anything in the while loop because there are no commands between the two curly brackets. The purpose of this line of code is simply to get the program to stay at that point until the user inputs his data.

Now at the point the user does input the data, Serial.avaialbe() will become ‘1’, and the program moves to the next line of code. The next line of code,

is the line that actually reads the data. Serial.parseInt() reads the number the user input, and then that number is assigned to the variable numRedBlinks. When you are reading an integer from the serial port, you use Serial.parseInt(). There are different commands for reading other variable types. You can imagine that if you were reading a floating number, you would user Serial.parseFloat(). To read a string you would user Serial.readString(). The important point is that you need to use the correctly precise command for the type of data you are trying to read.

OK, so put that code into your program at the start of the void loop() and also make a similar sequence of code to get from the user the numYellowBlinks. After you have that working also get other parameters from the user like the various blink time variables. You should play around with this and get comfortable getting input from the user. When you run the code, remember to click the icon in the upper right of the arduino IDE to get the serial monitor to pop up. You should see the prompt in the Serial Monitor, and you should enter your number in the small box, and then click send, or press the enter key.