arduino serial read
If you haven’t go back and check them out now, because these are going to start to get a little bit more difficult now, not too bad, but I just want to give you a heads up so for the next four episodes I’m going to be talking about Different types of serial communication methods that you can use with the Arduino this week, I’m going to be talking about specifically just serial communication between the Arduino and the computer, simple txrx, serial communication, we’re going to use it with a programming language called processing, which is the Language that the order we know programming environment is based off of processing is great for drawing graphics and doing things like that. So we’ll experiment with that this week and next and have the information from the Arduino control processing and vice versa, and that should be pretty cool after that in the coming tutorials we’ll move on to I to see SPI and wireless communication techniques, so I hope you’re. Looking forward to it, I certainly am let’s get started with general serial communication before we actually start communicating to the computer. Let’S talk a little bit about how we do this with the Arduino. If you look at the unit, you’ll see that there’s a TX and rx pin on digital pins you’re on one now, you can use digital pin 0 on one, while you’re running your program for input and output, but during programming these pins are multiplex to be used.
As the programming pins for the Arduino, the RX pin is to receive the transmit pin of the transmit when we call serial functions in the Arduino programming language we’re, using these pins to talk back and forth between the computer and the Arduino. When you do that, you’ll notice that the TX and rx LEDs will flash to indicate that you’re, sending or receiving data the same story holds true for the Nano here. You can see the TX and rx pins on the Nano. The mega is a different story. You’Ll see that there’s still TX and rx on pin 0 and 1, but there’s also three more serial interfaces that you can use for communicating either with other artists or just any other device that accepts your communication. This comes in useful later on. If you want to be able to simultaneously program the device and have it plugged into a separate serial interface, when you plug your Arduino into the computer via USB you’re, actually making a serial connection to the Arduino, you might have noticed that the universal serial bus or Usb plug on your computer has four pins on transmitreceive power and ground, just like on the Arduino. An important thing to note is that transmit always gets hooked up to receive and receive always gets hooked up to transmit that’s, because the receiving line for the computer is what’s being transmitted from the Arduino. The same goes, the other way what’s being transmitted from the computer is received by the Arduino.
Now, serial interfaces operated around plus and minus 15 volts, so in the case of rs 232 or the interface that serially uses, negative 15 volts actually represents a logic 1 and positive 15 volts actually represents a logic 0. This is obviously much different from the 0 amp 5 volt TTL or transistor transistor logic that the Arduino uses to deal with this a converter chip is using the Arduino to translate between the two logic levels. You have to worry too much about this. The Arduino takes care of it for you if, in the future you’re ever using an atmega microcontroller directly, you’ll have to put in a converter chip yourself. A common example is in Mac’s 232 converter to get us started. Let’S just make a very simple program that we can use to send one character to the Arduino and receive that character back just to confirm that the Arduino got it. And then we can use it to do something more interesting like controlling, LED or a motor or something so we’ll start our serial session. Just like we always do using serial dot begin, and this will allow us to communicate with the arduino ver. Your serial 9600 is the baud rate at which were communicating. The baud rate is just an indication of the bits per second. Basically, the communication. Speed 9600 is a pretty common value to use, but you can use other ones too. The first thing we’re going to do is wait until we get something from the computer and that’s what this while loop is going to do serial dot available equals zero.
So what about what the available function of the serial class does? Is it checks to see if there’s something waiting for the Arduino that we’ve sent from the computer if we haven’t sent something it’ll, just keep looking so it’s just going to stay on this line and keep look looping through this line until it gets something from the Computer, ok, once it does get something we’re going to read it into a variable and we’ll just for now assume that we’re sending it in hint just to make things easy. So we’ll do serial dot read which is going to read our input from that we sent to from the screen and we will echo the output, so we’ll do serial dot print line and we’ll echo that output back to the user. So we have our Arduino plugged in via USB on the other end there’s, nothing connected to it. Just the Arduino. We just want to test the serial functionality. So if we send it a 1, we should get a 1 back. What a 49! Oh that’s, weird! What happens if we fix 0 48 huh that’s, peculiar, ok let’s, try to look into why we’re not getting the same numbers back that we send it all right. The answer to our problem lies in data types. If you go to Google and just look up an ASCII table asci, I you’ll find something like this and this breaks down to you the different types of data types and how they relate to each other, so we’re, sending a character or a char and we’re.
Sending a character of 0 or 1 and you’ll notice. If you look over here, you can see that the decimal representation of 0 1 is 40 and 49, exactly the numbers that we were getting. Ok, so here’s. What our problem is we’re clearly sending this, but telling it it’s an integer when really it’s, not an integer, that we’re sending we’re, sending it a char which is really a byte of data it’s different. So we need to handle the conversion on the Arduino end so that we print back out the right stuff. Let’S go! Do that there’s actually a very simple way to fix this, assuming you’re only going to be inputting integers from the computer, and this is a are you confused? Well, let me explain why this is happening. The reason why this fixes it is we’re sending in a char 0, not just a 0 note that the 0 is in single brackets that indicates it’s of the character type to the Arduino programming environment. Remember we look at the ASCII table and saw that 0. As a character is represented as 48 in decimal, so if we know that we’re sending it a 0 or 1 or 9 or whatever through the terminal – and we subtract the char value of 0 and decimal, it will subtract the correct number and return what the actual Integer value is so, for example, if we send it a 1 on the terminal which has a char value of 14 or a decimal value of 49, and then we subtract the decimal value of 0, which is 48 we’ll, get a 1 back.
This should fix our problem. Let’S upload it and see what happens perfect, that’s more like it now we’re getting the numbers in that we send to it. Okay, now let’s just more interesting let’s. Have this control and LED and report to report back to us whether or not the LED is on all right we’re, just going to use the onboard led to make this nice and simple for us, we’ll set it to LED pin 13, which is the one that’s On the board already remember, we have to set it as an output. Pin mode LED, pin, output, okay, so we’ve got our weight. We’Ve got it reading it in and making it into an integer correctly. Now so, let’s take a look at what we’re doing here. I will say if the value that we read in is equal to one remember: you use a double equal sign and you’re checking for equality. If the value is equal to 1, then we will serial dot print line, a nice little message back to the user. That says LED is on okay and let’s, put this in brackets and if that’s the case we’ll. Actually we also have to have it actually turn the LED on, of course. Otherwise that want to do much. Do us much good. Would it digital right LED, pin, high okay and and now we need to check if the value that we get is a zero, so we’ll send a two one turn it on and is zero to turn it off if Val equals zero serial dot print line LED Is off – and you can have this say whatever you want – digital right, led, pin and now we’ll turn it off.
Okay, make sure we get our brackets in there. Okay, that looks good, but we need to check one more thing right. You have to make sure that we check for what happens if it’s, neither of those so let’s actually make this in else. If statement an else, if statement only bothers to look at this, if this one didn’t happen, it just makes the code execution a little bit more efficient. You could have it as an if also but we’ll use an else if it makes a little bit better and then we’ll add in else. So this is what happens if this doesn’t happen and this doesn’t happen. It’Ll execute the else statement and we’ll just have it saved back to the user, serial dot print line invalid, so they know they did something wrong. Okay and we don’t need to have this echo back anymore. Let’S see how that works out. All right, let’s see if this actually works now something one LED goes on sale, another one. Of course it should not do anything soon. Zero LED goes off one on zero off; okay cool that looks good and you’ll notice that the TX and rx LEDs also blink, because we’re sending and receiving data. But now what happens if I sin something like a V invalid: okay, good that does what we want. Now, what happens if I send a whole bunch of garbage whoa it printed an invalid for every character. Okay, it looks like we need to make one more simple change to our program to get this doing exactly what we want.
The last thing we’re going to add to make sure that we don’t get a ton of invalid inputs when we put in more than one character, is we’re going to flush out the serial buffer and to do that, you just do serial flush, some OD flushing, a Toilet you’re, basically just removing all the garbage anything else that we don’t want, gets removed. So since we’re only looking for one character here, once we run serial that flush within this loop it’ll get rid of everything else and then start looking at new. So now we should only see that invalid problem happened just once let’s upload this and see how it works. Okay, I’ll send it a zero LEDs off one LEDs on and a bunch of garbage and just one invalid. Okay, perfect this programs not doing we want we’ve, got some simple basic serial communication between the Arduino in the computer, and you can of course expand upon this and send it all kinds of different commands do all kinds of interesting stuff, but for now let’s move on To do something a little bit different we’ll use the Arduino to control the computer. Here I have a simple 10k potentiometer hooked into the Arduino. Remember, we’ve used this one time before you don’t actually have to use a breadboard for this, because we already have all the pins that we need. The red gets hooked into voltage. The black gets hooked into ground and the green gets hooked into analog input.
Zero. Now let’s go to the computer and program it to read the input from the potentiometer and send it back to the computer, so we can use it in a program that can read inputs from a serial port. This program is going to be very simple: all it needs to do is get the data and send it over USB to the computer, so let’s define our pot pin to start off that’s where the pot is connected to and I’ll get input zero to our setup Function as we always do start our serial object, because we need that to both send or receive 9600 and now we’re just going to loop through and keep checking the value of the potentiometer. The important key here is that you add a little bit of delay each time, because, if you’re sending data at 9600, baud that’s a lot and way more than you need and it’s not going to give time for the analog. The analog digital converter on the Arduino to update fast enough so we’ll start off by reading the value and mapping it now. The reason I’m going to map this right away is because, where I’ll give you a little giveaway here, we’re going to used to control the color of something on the screen and colors are controlled from 0 to 255 they’re. Eight bits, unlike our analog input, which is ten bits, so we read this in and we’ll convert it from a 10 bit number down to an 8 bit number and we’ve done this before remember so, we’ll go from 0 thousand to 23 to 0 to 255.
So what we’ll be doing here is we’re just sending the computer a value between 0 and 255, and we can use later on. Do something cool serial print line. Remember print line isn’t just for sending as a terminal. It sends it to the computer, so we’ll print that Val and I’m going to put a lay of 50 milliseconds should be sufficient. You could actually probably do less than that, like 10 milliseconds, but this is fast enough. All right, let’s get that on the Arduino and now we’re, going to write our processing program to deal with having that input and doing something: cool. Okay, so we’re going to be using a programming environment, called processing to grab the input over serial and use it to change a color of something on the screen. Processing is perfect for working with the Arduino, because the Arduino programming language is actually based off of it. So if we just start processing in Google processing, that is the first thing that comes up and that’s, where we want to go all right. So just click on download processing and just like auger. We know you don’t have to install it. You just download the folder and run it right from there so pop it on your desktop or wherever you want to put it, extract it and then run processing. Now our processing programming environment is open. Does it look familiar? It should because the Arduino programming environment is heavily based off of it, so it should be familiar place for you start working now, I’m, not going to go over all the details of how to use processing.
So I don’t want to put you through the terrors of learning an entirely new programming language, but I’ll just give you enough to get you started. You can use basically any programming language to get input from over a serial connection. Processing is just one example, so the first thing we’ll do very similar order. We know we’re going to import a serial library in Arduino serials imported by default in processing. You have to import it, but we’ve imported libraries in Arduino before so. You should be familiar with that. The next thing we’re going to do is create a serial object that we’re going to call port and I’m going to define an initial variable called brightness, because we’re going to be controlling the brightness of the color on a screen and now I’m going to initialize that To zero, this is the value that will be changing using the potentiometer okay, now, just like Arduino we’re, going to have a setup portion of our program and in processing and heavily graphics based. So one of the things you set up in in the setup part of the function is the size of the graphical window that you want to open, so I’ll make it 500 pixels by 500, pixels and now I’m, going to initialize our serial port. Similarly, to how you do serial dup again in Arduino it’s a little bit different in in processing, but a similar idea, we’re going to do port equals new cereal.
This, because we’re running it on on this class. I happen to know that my comm port is comm. Three go check what your comm port is and your computer and make sure you set up to that. If you’re on a Mac, it’s going to be a little bit different, it’s not going to be represented like comm, three same thing goes for Linux, it’ll be like dev or something like that and we’ll do port dot buffer until and backslash n. So let me explain what that is doing umm buffer until is essentially that same thing as that, while loop that we were running earlier, it’s just waiting until it gets something. Backslash n is a special character that indicates a newline, so remember, we’re, doing print line which prints a carriage return after it sends you each value. Every value you get on the terminal is on a different line. If you remember the thing that tells it to go to the next line is backslash n, which you can’t see, but the computer’s terminal program can so we’re going to know when we’re getting a new value. By looking for that, backslash N and now we’re going to have another thing called draw, and this is what updates the screen is going to update our little 500 by 500 window. And this keeps repeating draw automatically keeps repeating to update the screen and all we’re going to do is change the background color to get started background, zero, zero, brightness.
Now the values for this are RGB values so I’m, just going to set the red value to zero. The green value to zero and what we’ll be changing with potentiometer is the blue value, so by brightness. Basically, what we doing is we’ll be turning it from black up to bright blue, which is a value of 255, okay and now we’re, going to look for a serial event. This, when you have the serial class running, will automatically check for that serial event. Based on the buffer, until that we have specified serial event serial and remember we named it upper case serial. Remember we named our object port and all we’re going to do is we’re going to look for that value and we’re going to say. Brightness is equal to that value that we get over the serial port brightness equals, float, port dot, read string until and we’re going to look for that backslash n character, organics. We want to read up to that, and that should get us our value, and all this is doing is converting that value into a float so that we can use it in our background function and that’s. It let’s run this and see how it works, always make sure you save first, and then you hit this little Run button to run your processing program. I’Ll turn the potentiometer up and you can see the screen gets brighter. As I turn it all the way up to the top, the blue value goes to 55 and I turn it back down and we’re back down to black.
Alright awesome, you just wrote your first program that sends data from the arduino to processing. You can do a million things with this and we’ll do more with it next week, but for now that should be enough to get you started and remember. You can always go to the processing website and look up tons of examples on how to use it. Ok and that’s our intro to serial communication with the Arduino next week, I’m going to be talking about I 2c and then the week after that SPI, which are two other methods for communicating between the Arduino and integrated circuits. If you have any questions about anything, I did this this week feel free to post a comment on youtube or an element 14 or my blog Charlie, bloom, calm and, as always, I’ll be posting the source code, including the processing source code that I use this week Up on my website and I’ll have download links all over the place that you can find that and that’s it. I will see you guys next week thanks a lot for watching thanks to element14, for helping me to sponsor this video series. They were kind enough to provide a lot of the materials that I’ll be using to create. These tutorials feel free to go visit their website at element14.com check out their community, which is a great place to talk to you about electronics, the Arduino and, basically anything else.
arduino serial read Video
arduino serial read news
Posted on Saturday August 24, 2019A Colorful Way To Play Chess On An ATmega328 HackadayWe’ve all seen those chess computers that consist out of a physical playing field, and a built-in computer that would indicate where you should put its pieces … … Continue Reading »
Posted on Wednesday May 01, 2019Display Serial Data on a Web Page – Using PHP or Python Scripts ElektorOccasionally it may be useful to visualize a continuous stream of data arriving through a serial connection as a web page in a browser. But how? … Continue Reading »
Posted on Thursday March 28, 2019Arduino Converts Serial To Parallel: The Paralleloslam HackadayAfter a youth spent playing with Amigas and getting into all sorts of trouble on the school computer network, I’ve always had a soft spot in my heart for hardware … … Continue Reading »
arduino serial read Social
Originally posted 2017-02-15 02:21:18.