Paul McWhorter arduino lesson 18


If you’re new to the Arduino, you probably need to go back and catch up on those lessons. If you’re adept with programming the Arduino and just want to learn how to use an LCD display, you can just jump right in with me on this video. If you remember in lesson 18, what we had done was we built a device, a circuit to measure distances, and we did that with an ultrasonic sensor. The ultrasonic sensor sends out a ping, and then it looks for the returned ping to bounce off of something and then based on the time that the ping travels to the target and back. You can calculate the distance of the target and what we had done in uh in this lesson. Number, eighteen is that we had displayed our results using a servo and the servo had a little arrow on it and that little arrow would move back and forth on the servo and display the distance. And so this is kind of a cool way to to get to know how to use a servo and play with the ultrasonic sensor. What we’re going to learn today is we’re going to learn how to use the LCD display and the cool thing about an LCD display. Is it really lets you sort of kind of break free from your computer, monitor and you’re, not sitting there having to have it hooked to a computer and not having to sort of be tethered to the serial monitor to get? You know to get information back to the user by having an LCD display, you could hook this whole thing up to a battery and you could sort of go mobile with your project and all of a sudden.

Your project starts acting a lot more like a real product, it’s capable of autonomous operation it doesn’t have to be tethered to a computer to use, and so learning how to use an LCD is a pretty important thing if the LCD that we’re going to be using Is the LCD from the smartphone inventor kit? Hopefully you guys have picked up one of those smartphone inventor kits. If you have, then you can just exactly do what I’m doing in this in this lesson, and everything should work if you don’t, if you have a different LCD for this lesson to work, you need to at least have a similar one. A lot of these are very, very similar. This one is a 16 column by two row, LCD display and if you have a sixteen by two chances, are it’s very it’s very similar to this. The real trick in getting these things to work, the trickiest part, is to hook them up and it’s, not that complicated to hook them up, there’s, just a whole bunch of wires, and when you start getting these wires running everywhere, is real easy to lose track, and So I’m, just going to tell you that you’ve got to be really really careful on this project. It’S not hard. But what you got to do is you got to get you a big glass of iced tea or maybe, unlike a good, strong coffee and take a deep breath and kind of okay I’m going to take a few minutes I’m going to be careful, don’t rush, it Put every wire and carefully check it work with a buddy if you’re in my class work with a buddy, have them check your as you check them.

You know sort of one person looking at the schematic and the other person checking it, and I think that if you work with someone else or else there’s a lot better chance that I that you’ll get it right. This is one that, as I work with students that it seems, like students, have a tendency to make a mistake in here and get frustrated and you just got a slow day on and you got to check your work. You got to check your work carefully. Okay, it’s a good start, hopefully you’re using the same on. You know same LCD as I am, and then it’ll be a whole lot easier and the next thing that we got to do is we got to get this thing. Wired up and I’ve got mine, wired up and I’ll help you get your ears wired up. The first thing to understand is how the pins are numbered and on these like this, there are sixteen pins and if you have the one from the smartphone inventor kit, the when you have it oriented like I have it oriented down here. Okay, the upper left pin is, pin one, so this is pin one pin to pin 3 up to pin sixteen pin 1 is VSS. If you have a different LCD, then you need to look at the spec sheet or the instructions that came with it and maybe for yours, 10 one is not VSS. Maybe VSS is somewhere else, but the key thing is wherever VSS is.

It needs to be hooked to ground and wherever VDD is, it needs to be hooked to five volts, and so, if you don’t have this pin layout, it doesn’t matter just make sure that your VSS, whichever pin that is, is hooked. Agree on your VDD is hooked to five volts. Your v 0 is hooked to the pot pin and you will be able to to get it to work using this table and if you are a more visual person, you might want to look at this, and this will show it to you as well. But here is the LCD: you can see that VSS is pin 1 and that should go to ground. Pin 1 right here goes to ground. I have my top row set up as a ground rail with the Arduino, and I have my second row hooked up as a VDD going to 5 volts from the Arduino, so first column first pin looks to ground second pin hooks to 5 volts third pin hooks To the center tap of your potentiometer, the value of the potentiometer doesn’t matter, this is the one from the smartphone inventor kit. The pin three goes over to the center tab of the of the potential we’re, creating a voltage divider here, and so the top leg of the potentiometer goes to five volts. The center goes over to this pin 3 and then the bottom leg goes to grants. You’Ve just created a voltage divider.

This is really important because this this LCD, this is basically setting the contrast of the LCD and you need that or your device is not going to work and I’ll come over here and show you here’s my potentiometer. If I turn it down, you can see the stripping screen sort of blanks out if I turn it up to high it just blanks out with right too much brightness, and so you kind of turn it down until those squares just disappear. You see here, I’m still seeing the squares, and so I turn it down till the squares just disappear and that I think I can get a little brighter than that right there. That is just perfect okay, so you want that and then similarly go across looking at this and looking at the table and getting your LCD hooked up. What we’re going to do is we’re going to break this into two lessons: lesson 19 we’re going to do today and we’re just going to basically get the LCD talking to the arduino and we’re going to get it to where you can print something on it like What you can see over here, I’m, just printing, a little countdown timer and a little count up, timer and so that’s. What we’re going to do today but don’t, take your your ultrasonic sensor circuit apart, because then in lesson, 20 we’re going to have this working we’re going to have your ultrasonic sensor, working with your with your display, if you’re, just interested in how the display works.

All you need to do is just follow along in lesson 19 today and hook things up like this. Okay. Now, once you get it hooked up, it’s pretty straightforward right to get it set up here on top tech boy, comm lesson: 19: I sort of go through the commands you need to up at the top include the library you need to then create your LCD object. You need to then begin it in the in that you need to basically turn it on or initiating it initiated and avoid setup and then tell it where you want to print to and then issue a print command LCD print. Whatever you want to print okay, let’s let’s go in and that’s just a quick overview. Let’S just go in and let’s do the code I mean come along with me, get your things set up. You might need to pause and get your circuits set up and then and then come back and just code code up with me here as I go through, it I’ll explain things step by step. Okay, the first thing that you’re going to want to do is is that for this to work for this LCD to work, you need to use the LCD library. This is one of the libraries that comes with the Arduino software, so you don’t have to download the library it should already be there. How do we put a library in you put? The pound include, okay, pound include and then Open bracket.

What is this called? This is called liquid crystal and look at that. It turns orange saying that it, as is that library, that’s, good dot page and then close your brackets and then put in your comment. What we’re doing is load the liquid crystal library, okay, now after you’ve loaded, the library what you have to do is you have to create your liquid crystal object right? You got to tell it I’m going to have this object. This liquid crystal object that I’m going to work with, so you give it the liquid crystal command and I’ve misspelled it because it didn’t turn orange okay, I put that upper case which it shouldn’t be okay, now it’s orange it recognizes it now. You name the object. The good thing is, you can name it anything that you want, you could call it my display, you could call it my LCD, you could name it whatever you want I’m going to call it LCD all uppercase, you call it whatever you want, but whatever you name It up here that’s the name you have to use throughout the program. So now we got to tell it what the pins are and just go with me here and then I’ll explain it it’s. 10. 9. 5. 4. 3. 2. Okay. So what we’re doing here is we’re creating the liquid crystal object named LCD, so we named it LCD alright. What is this 10 9 5 4 3 2 that’s telling Arduino how we have hooked the device up and that gets back to this table.

Okay and basically, what we’re telling it is, is that the first position, the first parameter it wants to know where RS is well. Rs is 10 okay, so let’s come back up here and see if we can call this back up. So this is RS is 10, and then what are we telling it we’re telling it that e is 9? Okay, we tell it E is 9 and then, after that, what you’re? What you’re telling it is you’re telling it Det, ah that ah db4 through db7 arduino pens, five through two okay, and so this DB for through DB, sub or pens, five, four, three two – and that is what you are doing here in this command. And so, if you did not hook that first one to pin 10, if you hooked it to 12, then that should be a 12. So this is just telling the Arduino, where you put hooks up those pins. If you want more deep details, look at the lesson. 19 or you can just sort of take my word for it and go, go, go with me here. Okay know where you have a counter, so we’re going to be counting up and counting down so we’re going to need a variable for that counter, and it can be an int and I’m going to call it my counter and I’m going to set it to zero. Okay and then what we’re going to do is declare your variable my counter, I think, that’s, all we need to do up front that’s, pretty much the bookkeeping we need to do.

We’Ve got some stuff to do in the void set up. If you’re going to use this, if you’re going to use this LCD, you have to load the library, you have to create the object and then you have to start it. What do we call it? We call it LCD if you’d called it. My LCD, you should put my LCD here, but whatever you call it, the object, name, dot begin and then tell it. How big it is. Ours is 16 columns by 2 rows. So we tell it’s 16, comma 2. Now this is a little tad bit confusing because in math class they typically always tell you to start with row comma column like row by column, but for these displays they want you to put the column 4, so column, row. Ok, so we’re going to tell our we know our LCD has 16 columns and 10 rows. That should do it to turn it on and now, when you’re going to print to this to this LCD, you need to tell it where you want to print. You want to start here like we did. You want to start here, can print anywhere you want, but you’ve got to start by telling it where to start, and I want to start here. Okay, now there’s two things a little confusing. We start with column, so it’s column kana my row and then we start with counting zero. And so this, where my M is here, this would be column what column zero because it’s the first one and it would be row what it would be row zero and so let’s uh let’s tell it to start there.

Okay, so we are going to go to. Ah, LCD dot set cursor where we want to go to zero zero. What we’re going to do is we’re going to set LCD purser to upper left, one display okay, so that just means when we start to print. We are printing at column, zero row zero and if you don’t do that, there is no telling where it would end up so now. Let’S do our first print, so we’re going to say LCD print. This is how we put something on the display and where we’re going to print we’re, going to print my timer, like that I’m, just printing, a string to it, and then I open quote my string and then close quote, and then we are going to end in Our semicolon, and then we print our first line. Okay. Now what do we do that? Because we only need to print this one time we can do the counter here over and over up and down, but you only really need to print that one time. So I put it in the void setup. I think we are good as far as our void setup goes, and so now we need to create our little timer our little counter. Well, how would we do that? We want to count up to ten, and then we want to count down from ten. How would we do that? We would do that in a loop. We could use a for loop or we could use a while loop, I sort of like using for loops.

So I think I’m going to do mine with for loops we’re going to go for okay and then what are our conditions, our parameters? Well, I set up my counter to be my counter so to see my counter equal, where I want to start I’m going to start counting with one I think and then semicolon, and then we want to go as long as my counter is less than or equal To I think I’m going to count to ten okay and then every time through counter equal. My counter plus one start my clause in my Clause. I always when I create a for loop. I always go ahead and create the start of the clause. The end of the clause and then everything between this open curly in this closed curly is going to be the commands that are done in the loop. If I like to put this closed right when I create the open, otherwise I forget to do it and then I end up with problems, and so I just create it right there and then here where we put our commands that are going to be executed. Hopefully, you’re getting familiar with for loops at this point, but just remember you need three things. You need the starting condition and then you need to loop as long as this is true, and then you need to increment your counter, so I want to start the first time through my counter is equal to one.

I want to loop as long as my counter is less than or equal to 10, because I put the equal there. It will go through the tenth time with my counter equal to ten and then each time through. I increment my counter by one okay. So now what do I want to do inside of here? Well, I better tell it before I print, I better tell it where to go so I’m going to say LCD, and how do I tell it where to print set cursor, okay, and where do I want to print this time? Well, I don’t want to print over my timer. I want to print on the second row and I want to print on the first column, so the first column is column. What it’s column, zero okay first column is column zero on the rows. This is row zero. This is Row 1, so 0, comma 1 would be the first column in the second row. Okay, I put that like that and what we’re doing is set current cursor to second row or let me start with column with first column, which is 1 0 and second row, and the second row is Row one. Hopefully, you understand that I know it can be a little confusing now what we want to do. We want to do our LCD dot print. What are you doing in a print we’re going to print my counter your counter, and so that will print it there.

Then? What do I want to do? I want to put my unit’s LCD print and I’m going to put a space, because I don’t want the word seconds right against my number, so I put a space there seconds seconds close that are we doing it units all right. So I think that that is pretty good, oh, and we want this to be seconds, so you want to kind of put a delay in there. This is not perfect, but this is pretty good for delay, okay and delay by 1000 seconds, which, which is which is one second, so this thing’s, you cut count roughly as a second, so this will count from one to ten. Well, I want to then copy that, because now I want to count backwards, and rather than topping all this in, I will just paste it and then change it. So here’s my second loop. I want to count backwards, so I counted up to ten. What I want to do now, I want to count backwards from ten. So what I start with is, I start with ten, and then I want to go as long as I am greater than or equal to let’s count down to zero. What you need to see is is is that what you need to see is is that, if you’re counting backwards, you’ve got to flip this operator that I want to count as long as I am greater than zero up here, I’m counting as long as I’m looping.

As long as I am less than or equal to ten here, I’m looping as long as I’m, greater than or equal to zero because I’m counting down, so you got to think a little bit about the sign of the the way that these operators point. Okay and now, since I’m, counting backwards, I need to tell it that I’m counting backwards by saying every time I need to decrement in my counter by one and then the rest of this, I think, should be the same. So let’s go ahead and run this and see what happens? Oh it’s happy. I didn’t make any mistakes on there in a good day. Okay, let’s look at this. We are counting up to six seven, eight, nine, ten and then we’re counting backwards. Nine, eight seven, six! Five four three two one and then we’re going to count up one two. So what is something that isn’t quite right about this code? I want you to look here and see if you can see where the mistake is. Okay, look at this. We have an extra s seconds with two s now when you see that what you initially think is – and this is like how crazy it gets when you’re trying to debug things you’re going to say. Oh, you miss billed seconds in your string, but look at this. I didn’t, I spelled it right. Oh you misspelled it in the second one, no let’s build it right.

So where is this second S coming from well let’s! Look at this and right now this just takes one character to print this, and then you skip a space and you print this. But when we get to ten look, what happens it’s going to scoot it over to the right? You see how it scooted it to the right by one to make room for the zero on the 10. Well, when you did that, then on that one print line it could this was the last s in seconds and then it scooted back. The thing is with the LCD display when you print something it stays there till you tell it to go away, and so, when this moves over it puts that s there and then it hops back that s stays there, and so this is one of the vexing Things about these LCD displays is once you put a character there it’s there, and if things are scooting back and forth, because your number of digits changes, then you can get messed up. So what I like to do is I like to go in and when there’s, something like I don’t, have to worry about the first line, because there’s nothing changing on the first line, but on this second line, what is a good practice is is to clear the Line and this print it from scratch and you can clear the whole display, but I don’t want to clear the whole display.

I just want to clear that one line and so the way that I would clear that one line would be after I do. My LCD set cursor to zero one, and then I print after I delay here I’m going to go ahead and I’m let’s see. Where would be the best place to do this? I just think, as we come in to this loop before we do anything else, we want to clear that line, and so what we need to do is first of all tell it where to go any time we print. We tell it where to go. Cursor, okay, we’re going to go to column zero, which is the first column row 1, which is the second row. Okay, so we’re going to go. There set cursor to first column, second row, all right and now what am I going to do I’m, going to do an LCD net and I’m going to clear that line just by printing 16 blank characters, so let’s say: 1. 2, 3. 4. 5. 6. 7. 8. 9, 10. 11, 12, 13, 14, 15, 16 and close it depth. This is going to everything line because I’m printing blanks, okay, now after I do that, I’ve got to make sure that I come back in now and do this until it you know so I’ve printed. All the way across here so I’ve left the cursor here so I’m going to go back and tell the cursor to go here again.

Okay, so I tell it to go here again and then it will print the count and it will print seconds. But what this is going to do is every time through the loop. It is going to clear this second row. It happened so quick that it won’t really be something that you see blinking, but when it clears it, it’s going to take care of the second s problem, and I need to do this on the second one too. Okay, so I got to do this in my second loop: okay, we’re, going to clear that row and now that should take care of this extra s problem. Okay! So now let’s look my tanner let’s zoom in on this, and I think maybe it’ll help you see it a little a little bit better. I should have sumed in earlier. Okay, don’t focus; okay, so now let’s watch this okay, so we’re counting down 7. 5. 4. 3, 2, 1, 0 and then 1, 2. 3. 4. 5. 6. 7. Now watch this as it goes across 9 10. It moves across and this time when it moves back. It erases that s and it erases that s, because we printed this blank line here right and so I hope – I’m, not confusing you with this. But it’s just understand that that it’s kind of tricky working with these things and if you end up with something that doesn’t right but look right. A lot of times.

It’S, a ghost character from an earlier print statements and if you’re having one just go in and print blank lines and make sure that you put the print statements like notice, there’s, nothing really here, but between here and here I just print it blank. And then I print what I want, and that happens quick enough that you really don’t see any discernable. You don’t see any discernible blinking here: okay, it’s just looking it’s, just looking really really good okay, so you see that it’s really pretty easy to use these things. The hardest thing is to just get them wired up. The hardest thing is to get all these wires hooked up, but I try to make it as clear as I can in the online tutorial lesson: 19 top tech boy, comm and then you’ve got to include the library. You’Ve got to tell it where those pins are hooked up. You have to turn it on, tell it how many columns and rows and always always always set your cursor and then remember. Sometimes you have to print blank lines, and so this is looking good. I think we are ready to move on to lesson 20 when we’re going to hook this LCD up to our ultrasonic sensor.


Paul McWhorter arduino lesson 18 Video







Paul McWhorter arduino lesson 18 news








    Paul McWhorter arduino lesson 18 Social






    We’re opening the Arduino IoT Cloud to other platforms, starting with the ESP8266 by Espressif Systems — NodeMCU, SparkFun’s ESP Thing, ESPDuino, and Wemos (to name a few) — along with other inexpensive, commercially available plugs and switches based on this module.
    safe image.php?d=AQB6YIkvOaOwGAQg&w=720&h=720& content%2Fuploads%2F2019%2F10%2FDSC00029 - Paul McWhorter arduino lesson 18

    How fast can you run the 40-yard dash? Find out with your own wireless timing gate system.
    safe image.php?d=AQDAnlSUYJivJY5S&w=720&h=720& mOWHBn2WBw - Paul McWhorter arduino lesson 18



    (Visited 34 times, 1 visits today)

    About The Author

    You might be interested in

    Comment (22)

    1. I have tried this thing up to 3 times and it still gives me the same result: on, but not printing any texts and i don’t know what to do,maybe the LCD is bad or something. Pls tell me what to do.

    2. It might be nice if you go into preferences and up the font size for us old people. Just suggesting…. Thanks for the lessons.

    3. I followed along and ive gone over every wire 3 times I even took out the audio sensor and there respective wires leaving me with the LCD and potentiometer the codes running fine I think my LCD display is broken any other suggestions id really like to know???

    4. Thanks Paul. Great lesson. I noticed that when it counts up there is an extra second delay before it starts counting down again. How do we fix that?

    5. The double ‘s’ problem on the display, could be fixed easier. What I did instead was putting an extra ‘space’ after ‘seconds’.
      So the code looks like this:
      LCD.print(” Seconds “);
      instead of:
      LCD.print(” Seconds”);

    6. Hi Paul, love your lessons. Just a quick query… Why not just print a space after the word to clear the unwanted ‘s’ thusly “seconds “. Even better would be a command eg: “PRINT USING ##” (as basic did it) to format the double digit to stop the word shunting a character to the right when count reaches double digits? I will look that up as not super arduino language savvy yet.

    7. Hey why isn’t my LCD display print ING anything….it has turned on but isn’t printing anything……no problems in my code

      1. If there’s no problems with the code, then you must have hooked everything up incorrectly. It wasn’t displaying anything last night, and I got so frustrated. However, the next morning I realized that I flipped my potentiometer’s 5V and GND wiring. After that, it fixed itself.

    8. I connected my LCD screen to breadboard first,but the lcd didn’t even turn on , so I connected the power lines to the arduino board directly , the lcd turned and showed only one line of rectangles , I have only the pin RW connected to breadboard , I’m stuck now and have no idea what to do , any ideas ?

    9. thank you for this great tutorial , i try with 5 k potentiomete and without a., but the LCD is showing nothing

    10. instead of pushing my spacebar 16 times to remove the characters, could I just make a string with 16 spaces and place them where I need to put them?

    11. Mr. McWhorter. I just want you to know that im a mechanical engineer at Tennesse State University and you have literally changed my life. I have so many ideas that I want to make work and without you teaching me I wouldnt have gotten to understand this

    12. 26:50 – I don’t know about back in 2014 when this video was posted, but these days at least, you can use a PCF8574T LCD controller module to control it with I²C, so you only need four wires (two for data and two for power); they even include a built-in pot, so you don’t even need to do that. On the one hand, it’s great that things are easier, and plug-and-play like blocks, on the other hand, it feels less rewarding to be even more removed from doing things manually. :-\

    13. Missing the Forest for the Trees… 🙂

      Back in the section where the Time and Temp. are being displayed, there was an “s” left over from a LONG “Seconds” post and you inserted a routine to ClearScreen,LCD. (which works BUT it uses SO MUCH Memory to do it! 🙂

      To have solved this immediate problem, all that had to be done was to change the “Seconds” display line as follows…
      ….. Seconds ” (added 5 spaces or so after “Seconds” which would remove the left over “s”… 🙂

      When I saw you adding all of that coding to clear the whole screen, I tried to Yell & Scream “Just add some spaces after the “Seconds”!!! <===== But, you could not hear me... I thought I'd try to let you know THIS way via comment... Hope you read these comments... 🙂 Have a good one & keep up the good work! BTW, I am a retired Computer Systems Consultant; previously IBM dept. manager, etc. to programmer, systems analyst, senior systems analyst, Data Processing Dept. Mgr., Controller & Director of Data Processing, self employed Consultant, etc. I am 83! A real OLDIE... You first got me hooked on helping me with my Fusion 360 Frustrations and then to Arduino! The Arduino language seemed easy to learn & I enjoyed seeing the POWER it had from your Tutorials & Lessons... It got me BACK into programming! Love it... and am trying to develop programs of my own using some Yours as guides! Thank you very much for being there... Most of the time, I am watching via Youtube on a TV and cannot Comment... I will try t make the LIVE shows so I comment if I choose to.

    14. Another great lesson Mr. Mcwhorter! I had fun with this too, did my own thing a bit and learned a lot as usual. I used the lcd.clear(); command for the ghost letters(I had ghost numbers) and my LCD has an i2c backpack for easier wiring if you don’t mind an extra line or two of code. Thank you sir, this is knowledge I’ll be using often.


    Your email address will not be published. Required fields are marked *