movimientoantipiquetero.com.ar

chainlink node raspberry piI2C LCD Node.js Coding on a Raspberry Pi


chainlink for backflow device ath chainlink chainlink node raspberry pi I2C LCD Node.js Coding on a Raspberry Pi
chainlink node raspberry pi gate latch metal box in chainlink gate I2C LCD Node.js Coding on a Raspberry Pi
now that weve wired up our i2c enabled lcd to our raspberry pi its time to write some code now were going to be referring a lot to the theory that we talked about in the last lesson so if you havent looked at the last lesson regarding the i2c the parallel module and its connections to the lcd display well you might want to go check it as you can see weve got the lcd powered up and bashful is logged in what were going to do now is write our node.js scripts in order to control this lcd now theres going to be a little bit of copying and pasting hopefully well have access to the the raw script whenever we put it in the description for this video now first lets do a real quick check of our wiring we can see that the lcd backlight is on more than likely that means that our vcc and ground connections our five volt and ground connections are correct on that lcd display question is is do we have our i2c connections correct in other words do we have access to the sda and the scl connections well theres a real quick way to check this out the raspberry pi has two i squared c buses we connected to the i squared c bus one so we are going to use a command line command called i2c detect and were going to use a dash y just simply to confirm so i dont have to hit yes and then one for the bus number and we can see in this little matrix of dashes that theres a 27 in the middle that 27 is a hexadecimal 27 so that means that that there is a device with the address 27 hex that is connected to this i2c bus turns out that that is the address of the i2c to parallel module that is connected to our lcd so it looks like at least we have a communication established were going to go a little bit further when we start writing our code to verify that our connections are correct so lets go ahead and start writing some code were going to begin by making a directory were just going to call this i2c lcd and go down into that directory that folder and then im going to do a touch index.js now remember were going to do our do all of this in javascript and um using node.js and whenever we do the init npm init it likes to have an entry point defined and thats what our entry point is going to be defined were going to we just created an empty uh file index all right so well do an npm init dash y in order to initialize our module and you found that it found our entry point of index.js now were going to be using two modules one module to communicate through the i2c bus and a second module to allow us to insert delays if you remember from the previous lesson there were there was a little bit of timing we needed to worry about whenever we wrote a byte to our lcd display so the first one were going to do well do an mpm install i2c bus and thats going to go ahead and install our bus all all of the functionality to be able to communicate with the i2c bus and then the next thing were going to do is were going to ins do an npm install of sleep and sleep is a module that gives us the ability to halt or pause the no js thread so that we can pause it for a millisecond or you know a millisecond resolution microsecond resolution or uh i think its a nanosecond resolution now lets go ahead and open up our javascript now a couple of things first of all in order to access those two modules were going to create our constants so i2c equals require i2c bus now understand that this is not going to that the the i2c object were creating here is not actually access to the bus were going to create a module or excuse me an object from this object that will give us access to the bus and then were going to create a sleep object so require sleep everybody requires sleep right all right and so this should give us access to all the functionality that we need now in order to make our code a little bit more self-documenting im going to create a couple of constants here to identify first of all the address of our i2c lcd so and remember that came up in our i2c detect command as address 27 and then i also want to create a constant that a that identifies our i2c bus so this is bus number one all right so thatll give us a little bit of uh you know a little bit of self-documentation here now the very first thing we need to do is open our i2c bus and this bus is going to be bus number one and the function that were going to use to open it is going to come out of our i2c object and so we will create a const i2c bus and this is going to be i2c and were going to use the open function now the open function requires two parameters the first parameter is the bus number which is going to be one and then the second parameter is our callback function if an error occurs all right and and actually its its the callback function that is called period whether an error occurs or not and the reason why we need this is because not only are we going to use this callback function to identify if an error has occurred but were also going to use this callback function to say yes we connected to the bus now lets execute some code specifically were going to do kind of a hello world type thing right and so if there was an error theres a couple of things we want to do first of all we want to save the you know send the error to the console screen but the other thing that we want to do is exit because this particular script the only thing that were going to be doing in this script is writing to the lcd display so if we dont have access to the i2c bus might as well exit without doing anything you know doing any further damage right so lets go ahead and say what the error was well do error opening i2 c bus and give it what the error was and then were going to go ahead and exit using the process object or the process module give it a code of one were just out of here all right now the code that were going to execute whenever things happen correctly when we connect to the bus correctly thats going to be located right here so this location right there that line right there thats where were going to insert our code to initialize the lcd display position the cursor and to send a string to that display all right so that should be enough to get us started with the and you know the connection now the first thing that were going to do just to make sure that our you know were not worried about the i2c lcd yet because the lcd is going to require some initialization but lets first see if that i2c to parallel module is working correctly before we do that and and the function were going to use to do that is called i2c write i2c write requires an error handler so lets go ahead and write a function to handle handle any sort of i2c error and were and and this this is going to be a callback function for our i2c write function now the callback function itself requires three parameters first parameter an error the second parameter is the number of bytes that were written the third parameter is a buffer containing the bytes that we tried to write so were going to have air bytes written and buffer there are the parameters for this particular function all right now really the only thing were going to be using this for is handling any sort of an error so if an error just simply console log error writing to i2c bus and just simply output the error okay we good with that this is just its just a generic callback function were going to use the i2c write function a lot so no need to simply over and over again write a call back function if an error has occurred now if you remember from the previous lesson there were eight bits coming out of that i2c to parallel module four of those bits went to a nibble to write nibbles to the lcd display one bit controlled the backlight one bit controlled the register select address line one bit controlled the enable uh that little thing that we were going to pulse in order to store a byte and then the last one was our read write so if it was a 1 we were reading if it was a 0 we were writing now for this particular this particular script were only going to be writing so were always going to have that set to a zero that said lets go ahead and see before we try and initialize the lcd display at all lets just try and see if we can actually write something to the i2c module and were going to do that by setting the by turning on and off the backlight so lets just create a constant and in fact maybe we ought to create it with the group of constants we have up here we will create a constant and well call this guy lcd backlight and it is going to be equal to the bit position uh actually position eight okay so it is the fourth position from the from the right so weve got bit position zero bit position one excuse me bit position one bit position two but position three so bit position three is connected to the lcd backlight that corresponds to an eight in hexadecimal if we write an eight to our lcd display uh or excuse me to the i2c to parallel module we should turn on the backlight will it work well lets go ahead and write a function to do that well do a function back light control on off and what well do is if on off in other words if were if that boolean were being sent is equal to a one then what well do is well turn on the backlight right otherwise were going to turn off the backlight now how are we going to do this well remember we are going to be using the i2c bus we created that object and that object has a function called i2c right now this takes four parameters it takes the address that were writing writing to um and i think we already i defined that as lcd i2c address right a constant the second parameter is the number of bytes we are writing the third parameter is the is a buffer of the array of or excuse me a buffer of the bytes we are writing and then the last parameter is the callback function if an error occurred so were going to write lcd i 2c address okay that should be our 20 you know our 27 hex right were only writing a byte and then were going to take a buffer from and what were going to pass to this buffer from is an array of all the bytes that we want to write well we want to just write one byte and that one byte is going to be our lcd backlight on constant so up here you know that lcd backlight were going to be passing that to our uh were going to be creating a buffer from that one element so lcd back light and then the third and you know the fourth excuse me parameter is just simply going to be our handle i to see error all right i think that should be it that should be all it takes in order to turn on the backlight is just to write to our i2c to parallel module the bit that turns on the backlight how are we going to turn it off well were going to do exactly the same thing this time what were going to do is send a 0 to that bit position so i2c bus dot i2c right and then all the same parameters except were going to send a 0 instead of lcd backlight and as i was doing that i noticed that you all didnt tell me that i had mistyped here you know you just feel free to raise your hand and say you know darn off you entered an error there im sure its not going to be the only one all right now how are we going to check to see if this backlight control works well im going to do something a little annoying first what im going to do is im just simply going to use the javascript set interval function in order or you know in order to create events every second to flip the value of the backlight so lets just create a variable well just call this backlight condition equals true and itll just simply hold whatever the last value that was set to the backlight is and so well have set interval and well call a function now inside of this function and remember that we so so theres our theres the function thats called whenever the interval occurs and set interval will set it for a thousand milliseconds which means it should be every second and well do something like backlight condition equals the inverse of backlight condition and we will do backlight control and we will send it whatever the current backlight condition is there you go hopefully that will work so were going to save this then well exit now keep an eye on that backlight what well do is well do a node index.js and well run it and once the code starts you should see the back light is flashing taking a little while for the camera to catch up to the back light huh so were going to go ahead and kill that leave the back light on so our co so our back light is is still on now well go back into index we will comment out that code that did the blinking because we dont really want to do that we did it it served its purpose it told us that our interface to the i2c to parallel module is working now lets go ahead and write some code if you remember from the previous lesson one of the important things about writing to the lcd backlight was this timing there was a lot of timing that was involved in order correctly right to the lcd module in order to get it to correctly grab a bit or a nibble and the way it worked was you set rs the register select you set the read write bar we set it to zero to right and you set the data and once you get those three things done you let it set for a while turns out its pretty short while its like 30 nanoseconds then you pulse the enable you make it go from a zero to a one and then you make it go from a one to a zero in order to latch that data then you wait for a moment in order to let everything kind of settle and then you can write the next byte so were going to write a a function called um i think its something i think lets just call it function raw timed right and were going to send two variables to this were going to pass two variables to this the first thing that were going to pass to it is the actual data now remember we are setting our lcd display up to except nibbles a four bit value because we remember on our i2c to parallel module we were using those the the lower nibble for the control lines the upper nibble was what were going to send data with so we are going to pass to it a variable called data in upper nibble now this data in the upper nibble thats thats thats the byte thats what were sending to it and were going to just put that data in the upper nibble so all we have to do is just simply put our control digits our control bits in the lower nibble and then we also need to know whether this is a command or a character were sending now the command the the and this is identifying whether the rs is going to be a one or a zero remember if rs is a zero thats a command if rs is a one that is a character were sending so just to make it so that nobody has to memorize that lets create a couple of constants these these constants are going to define for us whether we are sending a command or a character so im going to simply call this lcd regis register select remember thats our rs signal if its a command then its going to be equal to a zero that bit is going to be equal to a zero and the bit is going to be the least significant bit of our byte that were sending to the display now if its a character were going to want register select for a character were going to want that equal to a1 all right so those are the two values that our second parameter for our raw timed right is going to be those are valid values for the command or character now oh and one other thing remember that we need to also set our enable signal now which bit was enable well it turns out enable is bit position 2 which corresponds to the hexadecimal value 4. all right that should be all we need to define in terms of our constants for raw timed right lets first clean our data so well do clean data equals data in upper nibble just in case somebody messed up and didnt send us something with the only the upper nibble set were going to clear this clear the uh the bits that we need to have set to zero uh with that bit wise and with f0 and we also want to make sure that our rs our cleaned rs make sure that those are are only a one or a zero so were going to take command or care were going to do a bitwise and with one now comes the process of writing to our i2c bus so we have i2c bus thats the object we created with the open command right and if its successfully opened well we should have access to an i2c write command and the i2c right takes four as we saw before takes the four parameters one is the address of the device so lcd i2c address the next is the number of bytes were sending and its one and then a buffer that is created from the data that were sending so lets put this on the next line so we are going to create a buffer from an array with one element in it and that array is going to be clean data ored with lcd lcd backlight because we want to leave the backlight on ored with clean rs thats the data so weve got the data in the upper nibble weve got the backlight bit turned on and weve got whether its a command or a character being sent is set there too now the other bits enable and the read write those are going to be left as zero read writes going to be a 0 because were writing enable is going to be a 0 because we need to create that pulse now just because the force parameter is required we are going to handle i to see error and that should be all it takes in order to pass the first byte to the i2c module now once again the i2c module weve already established that thats working just fine we need to get this timing just right in order to get the lcd display to receive a byte now the next two instructions are exactly the same except one of the instructions is going to be used in order to turn on the enable so were going to do exactly the same thing all right so this this one right here this second right to the i2c bus notice that ive turned on the lcd enable and all everything else is exactly the same the only difference is im turning on lcd enable and then this third right to the i2c bus needs to turn back off the lcd enable so were writing the data with lcd enable off then again with it on and then again with it off and thatll give us our pulse thats going to lock that data into the lcd display at the end just to make sure if were sending a whole stream of bytes we need to pause two milliseconds between bytes being sent or between nibbles being sent just to give the lcd display time to you know have its signals uh stabilized so were going to do an m sleep of two milliseconds here and that really should be all thats required in order to make it so that we can write a nibble to the lcd display unfortunately the next function little bit much the next function is really just going to be sending a whole stream of bits or nibbles to the lcd module in order to initialize it whenever our lcd module is first brought up it doesnt know anything it doesnt know it doesnt even know what type of display its connected to it doesnt know whether when written to the cursor needs to increment to the right or to the left it doesnt know cursor position it doesnt know a number of things theres a sequence of nibbles that were going to send to this in order to initialize the lcd display i am just going to go ahead and paste this whole function in here well go through this quickly so hopefully not to put anybody asleep very first thing were doing we need to pause for about 15 milliseconds just in case this script is executed immediately upon powering up the lcd display everything just needs to be stabilized for 15 minute milliseconds before we start writing commands to it now the commands go something like this right a three and these are nibbles right a three and then another three and then another three for some reason the lcd display needs to have these written to it in you know with those enables pulsing those threes in or clocking those threes in for it to uh kind of reset uh after writing the first three we have to wait 4.1 milliseconds after writing the next three we have to wait a hundred microseconds and then the timing gets a little looser after that then we write a two i dont know why read the data sheet um and then we have another sequence of commands that we write now one of these commands were actually defining the number of lines that our lcd display has that n says weve got two lines the font f that says how many pixels by how many pixels each one of the characters are for this particular display writing a 0 to f identifies it as a 5 pixel by seven pixel five columns by seven rows for each character then we have this last command that we write to it this six that were sending to it the six says identifies the display movement as it is staying still it is not scrolling left and right as we write characters to it then s says that we setting s to a zero says that we are incrementing as we write each character rather than decrementing the position of the cursor so that should be enough in order to initialize our display what do we do after that well you know whenever the display first comes up the cursor is positioned hopefully in the upper left-hand corner of the display but we want to make sure that that is that is you know for sure exactly what is happening so lets go ahead and create another function that is going to allow us to position the cursor so lets create a function position cursor and its going to take two parameters its going to take the line number and the cursor number or excuse me the column number all right now how does this work well we are going to send a byte command and remember a byte command is sent as two pieces using the raw timed right so were going to send the most significant byte and then the least significant byte let me show you a quick diagram of exactly what the bits look like for this particular byte the first bit is going to be a one this one in the most significant bit position is gonna tell the lcd display that were about to get a cursor position command now the next bit is the line number a zero for the top line a one for the bottom line then we have two zeros so those are those thats going to be the first nibble that we send to the lcd display the next nibble is going to be the four bits identifying which column were in so 0 through 15 which of the 16 columns were going to be in now before we do anything lets go ahead and clean up our data so lets do let clean line equal our line number ended with a one and by doing this what were making sure of is that this is going to be a single bit a zero for the top line a one for the bottom line then lets also clean our column so well get let clean column and well have that equal to the column that was sent to this function as a parameter ended with f and that will make it so that its a 0 through 15 value all we need to do now is send the values to our lcd display so were going to do a raw timed right and were going to make sure that that most significant bit is set to a 1 so itll be an 8 0 hex ored with the clean line shifted to the position that is immediately to the right of that most significant one position so thatll give us moving the the line number that bit over six positions now this is a command so were going to need to use that constant lcd register select command and then we need to repeat this for the column number so well do a raw raw timed right and this we need to send the clean column number but remember its going to need to be in the most significant nibble for our raw timed write function to work properly so we need to take clean column and shift it left 4 bit positions and then follow it with lcd register select command and that should be all thats required in order to position our to position our cursor now just to make things a little easier for the use users of this code were going to create a couple of constants well do line one thats going to be equal to zero and because remember that the zero is line one the one is line two and so well have lcd line two equal to a one all right so we should be good what do we need when do we need to call this well remember that whenever we first created our bus interface see is it all the way up here there we go i said that um you know as soon as the bus is open if it opens successfully if there was no error then this is where were going to start executing code so the very first thing that were going to do is we need to initialize the lcd all right that takes care of sending that horrible set of bytes or nibbles to our lcd to get it up and running but the next thing were going to do is were going to position cursor right so we need to position the cursor where are we going to position it well for now lets just go ahead and position the cursor to i dont know how about the first column of the first line so well do lcd line one and then column zero all right so weve initialized the lcd weve positioned the cursor i guess the only thing to do now is write a string to the lcd so theres one last function that were going to create and this one last function is going to allow us to output a string to the lcd lets find a good place to put this well put it after the position cursor so after this lets create a function right string to lcd and were going to pass to it a string to display now were going to use the raw timed right remember it takes a nibble and so were going to first break the string into its characters and then were going to spray break each character into its upper nibble write that and its lower nibble write that then move to the next character so how are we going to do this well first were going to use the string to display were going to use the split function using an empty string as a delimiter and thats going to split our string into a bunch of character into all of its characters now each one of those characters needs to be operated on individually so were going to use our for each function and were going to call this function based on the character that we broke that into now inside of this function what we need to do is send our characters right and send our characters one bit at a time now well do we need to first convert the character to a an integer so well do let data to send equal our character care code at zero and so that should convert it to an integer and then were just simply going to use our raw timed right and well do data to send ended with f0 a hexadecimal f0 and thatll strip off the lower nibble so that were only sending the upper nibble thats what we have to send first so once weve done that then what were going to do is identify it remember we have to identify whether its a character or a command and were going to identify it with the lcd register select care now were going to do exactly the same thing with data to send except were going to shift it up so we can get the lower nibble into the upper nibble and then again clean it up by doing a bit wise and with f0 and make sure that we use the lcd register select care whoops and now we should have everything we need to write a character or write a string to the lcd display so writing a string to the lcd display i dont know how about something simple like hello world right i mean what else do we write so lets write write string to lcd and how about i dont know how about hello all right so weve got one two three four five six characters and so that leaves ten you know ten empty spaces so lets center this well move the cursor to five and then how about position cursor lcd line two and well insert where were going to position it in a moment as soon as we figure out how many characters world takes so write string to lcd and then well have world exclamation point looks like it too takes six characters so we will set this guy to position five column five and there you go that is our code now lets execute it node index javascript and there you go weve written hello world on the two lines making the correct positions for our cursor the code is done use this code to control your lcd display to display whatever you want to your user so that you can have better interaction than just simply an led Now that we have the I2C LCD module wired to our Raspberry Pi, its time to write the Node.js script to control it. In this lesson, we write the functions to initialize the LCD, write data to the LCD control and character registers, control the backlight, position the cursor, and write strings to be displayed.Takeaways: At the completion of this lesson, each student should be able to write the functions necessary to control a 1602A LCD module via an I2C to parallel adaptor. lcd i2c raspberrypi raspberrypi,lcd,i2c,