Arduino Serial Read

Posted on

Principles of FEA. The finite element method (FEM), or finite element analysis (FEA), is a computational technique used to obtain approximate solutions of boundary value problems in engineering. The finite element method pdf. 2 AN INTRODUCTION TO THE FINITE ELEMENT METHOD Problem 1.2: A cylindrical storage tank of diameter D contains a liquid at depth (or head) h(x,t). Liquid is supplied to the tank at a rate of q i (m3/day) and drained at a rate of q 0 (m3/day). Use the principle of conservation of mass to arrive at the governing equation of the flow problem.

  1. Serial communication on pins TX/RX uses TTL logic levels (5V or 3.3V depending on the board). Don’t connect these pins directly to an RS232 serial port; they operate at +/- 12V and can damage your Arduino.
  2. What is the difference between Serial.write and Serial.print? And when are they used? From the Arduino site for Serial.write and Serial.print: Serial.write Writes binary data to the serial port. Unable to read and write serial data between two arduino's (hc-05 paired) when using a sensor.
  3. Reading Serial data from an Arduino using C# In this example we will use analog pin 0 to obtain a value and send it via the com port (USB), we will read this with.
  1. Arduino Serial Readbytesuntil
  2. Arduino Serial Read Until

Introduction Reading numbers from serial on an Arduino is needed surprisingly commonly. Exactly what is happening might be kind of hard to figure out. Serial Serial communication is digital, which means all data is transmitted in 1's and 0's. Arduino Function Serial.read and Serial.readString: Serial monitor of Arduino is a very useful feature.Serial monitor is used to see receive data, send data,print data and so on.Serial monitor is connected to the Arduino through serial communication. How to compare string from Serial.read? Ask Question Asked 5 years, 1 month ago. Active 2 years, 11 months ago. Arduino Serial.read doesn't work. Mar 19, 2015  To be able to read a string from the serial port in the Arduino, we will need to know when the string ends. One way to do this is to insert a newline character at the end of the string. A newline character is a non-printable ASCII character that is called 'line feed' in the ASCII control code table.

Introduction

Reading numbers from serial on an Arduino is needed surprisingly commonly. Exactly what is happening might be kind of hard to figure out.

Serial

Timeout

Serial communication is digital, which means all data is transmitted in 1's and 0's. Typically, serial communication is done using ASCII letters. This means that, to send a number to the Arduino, the data sent is not the binary version of the number in base 2 (as an integer), but instead a sequence of characters for each digit in base 10 (which is human-readable). It is not an efficient use of bandwidth, but bandwidth is not usually a problem with Arduinos connect by USB.

Arduino Code

Code Explanation

Arduino Serial Readbytesuntil

serialEvent

serialEvent is a function that is called by Arduino before each loop. So, you can separate out your serial handling from the rest of your code. You could also put the contents of this function in the loop() function and it would be similar.

Fixed-Width

Commands need to be delineated in some way. Fixed-width commands are convenient on Arduino because the Serial.available function tells you how many characters are waiting. It is also possible to delineate by special characters. An extremely common and simple version of this is a Comma Separated Value file. However, since it is simplest to use C strings, the character arrays which the serial is read into is necessarily fixed-width anyway. To get around this issue, a vector or linked list could be used, so the serial string can dynamically grow and shrink.

Command Differentiation

In many cases, a robot takes multiple kinds of inputs. For example, there might be multiple speeds, or a direction, or some kind of trigger. Prefixing each command with a unique letter can make it easy to differentiate what the following string is supposed to do.

C Strings

C strings are arrays of char data types. They must be terminated by a NULL character 'textbackslash 0'. Without the NULL character at the end, there is no way for any function to know how long the string is. For example, a print function would print the string and then a bunch of garbage characters until it gets to a NULL or the end of the memory available. In this case, it's less likely for that to happen since we are only using atol, which will only read until there is a non-numeric character. A non-numeric byte is more likely to randomly be at the end of the string than a NULL character.

Arduino Serial Read
ASCII Table
DecimalCharacter
48'0'
49'1'
50'2'
51'3'
52'4'
53'5'
54'6'
55'7'
56'8'
57'9'

Char to Integer

Converting a single character to a number takes advantage of how characters are stored, which is typically in a format called ASCII. Think of a char of ASCII like an integer data type. Except that when printed, it is not a decimal number. The number in the int is translated into a letter on the screen according to a table. For example, the decimal number 97 is translated into the letter 'a'. Each letter is really stored as a 'number'. I'm showing it as a decimal (base 10) number, but it is really stored in binary (base 2). The C++ compile takes care of translating our base 10 decimal numbers into base 2 binary.

Here is how to take advantage of this:

Since ASCII stores the letters sequentially, starting at 0, just subtract the value where 0 begins from each of the characters to convert it to its numeric form. This works regardless of the base of the character storage. So, it converts the ASCII character representing the number in base 10 into an integer in base 2.

Char Array (C-string) to Long Integer

Now, each of the letters from right to left need to be multiplied by a power of 10. Here is the basic idea of what atol does:

digitalRead() and Serial Port Communication

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

  1. 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.
  2. Jumper wires (3)
  3. A 10,000 Ohm resistor more commonly referred to as a 10K resistor
  4. A very ripe banana, (1) – not completely useful, but nutritious

Step-by-Step Instructions

  1. Connect the pushbutton to the breadboard.
  2. Connect one side of the pushbutton to the 5-volt pin on the Arduino board using a jumper wire.
  3. Connect one side of the 10K resistor to the other side of the pushbutton.
  4. 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.
  5. 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.
  6. Connect the Arduino to your computer with the USB cable.
  7. Open the sketch for this section.
  8. 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.
  9. 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.
  10. Now go to the menu bar at the top and select Tools > Serial Monitor. Or you could use the shortcut key, Shift + Control + M.
  11. The serial monitor window will open and will be spouting off numbers. It should be a bunch of zeros.
  12. Press and hold the pushbutton – watch the serial monitor window, the numbers should now be ones.
  13. If the numbers are not scrolling, make sure you click Autoscroll at the bottom left of the serial monitor window.

This image built with Fritzing.

The Arduino Code

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.

Notice how the variable pushButton is declared and initialized all on the same line. Also, notice the descriptive name of the variable – pushButton – the variable name implies its use within the program – this is a good example to follow.

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

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

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

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

So what does the Serial library do?

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

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

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

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

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

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

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

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

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

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

Arduino Serial Read Until

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

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

All of this is in the following line of code:

This is why Arduino rocks – one line of code and you are on your way to dominating the world.

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

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

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

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

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

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

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

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

Try On Your Own Challenge

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

Further Reading