#65 Arduino EEPROM Basics - easy to do and useful to implement

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back now today we're revisiting an old project what a lot a told but certainly one that we've completed in the last few months and it's this red pmw controlled children's bedroom light which basically has these ultra red they look white to you but these are in fact very very deep red LEDs as for them in total as a fourth one down there they cannot not quite picking up now this was used because from a child's bedroom point of view it gives out a light that enables you or them to see enough without falling over things in the night and to you know find that the armchair in the corner there really is an armchair not some Monsters Inc type animal ready to gobble them up so it allows them to see things like that in this red light but doesn't brighten up the room enough with a white light or even a nurse orange incandescent light that would then sort of wake them up and well or prevent them even sleeping incidentally I mentioned this is somebody who used to be in the Armed Forces as I mentioned about this red light being bright enough to see by but dim enough to sleep one it goes that's exactly why the army used it army apparently uses flood red lights so the soldiers can see what they're doing enough just about but it's so dark a light we are so insensitive to red that it doesn't actually light up the entire countryside so that was an interesting little point and so as a project the Audrina's just using these four LEDs is a 1 watt LEDs without a heatsink because we're just not using them driving them that hard so it's driving these 4 LEDs pulse width modulation which in itself would be a fairly trivial project we've made it a little bit more exciting but this 4 channel remote control controlled by this little gizmo here these are well cheap let's say from the Far East and I'm using four channels and I might have in fact amended it's in I last did it without telling use or snuck in a quick change so what some what happens now is that let me think see turns it on D turns it off amb increase will decrease the brightness so if we switch it off by pressing D we are all off and touch it back on again with C great and then we can go up in brightness as you can see there and down in brightness now there was one problem I say problem in inverted commas with this is that I was having a an issue where it wasn't actually responding particularly well and I thought has it hung and something happened side I'll go into the bedroom unplug the power obviously haven't got this lead connected there Nano then I'd unplug the power plug it back in again press the on button and this thing will come on at full power or at no power be 0 and then you'll be forever pressing one of these buttons wondering if it is ever going to come on or whether it is in some way faulty and across my mind I thought why on earth am I not saving the last value from these up/down buttons a and B into the EEPROM on this chip so that when you plug the power back in it just come back on to that value indeed why not well you know when you're creating a project for the first time you're trying to keep it simple not overload people with information and it seemed to work well enough at the time but since that doesn't say this plugging it back in again at a preset value seem to me to be a good thing to hang on to it incidentally the actual problem was nothing to do with this at all is this transmitter it just needed a new battery yeah I know it's the small things in life is it that really gets you and but just to mitigate my my sense of shame here when I press the button it did light up see that little red light at the top so that little red light was lighting up but that corresponding one there on the receiver one pointed didn't light up so I was prison this away this wasn't lighting up and I'd have to go around to the back of the Wardrobe what I've been trying to unplug it and plug it back and then suddenly it all screams lifer by then of course I was about three inches away from it so you know I should have checked the battery first I know anyway it still led me to the conclusion that it would be really useful to store the value in an EEPROM now that is done very very simply with the prom library its standard part of the arduino collection of libraries that come with it and the actual code is almost trivial but there's a couple of little niceties they've added since about since about 2009 2010 they've added something which i think is is really nice and stops me having packed having to code as stuff to show you how to do it so let's have a look at the the code right okay so this is the original code from the video that we did before there's a couple of little changes one I've put in this debug method this function so we can print stuff to the debug window well they aren't having to put an if statement each serial door print so this is just a replacement for that also of course we have now got this value as a global variable where this was originally a static int within the loop but we need to access it before that so announcing a global variable and we set that here so when when we switch on the Arduino now it goes and gets the value look from EEPROM and just displays a couple of messages here something major it's doing it right and then switches off the lights initially we didn't have that on either so what was happening while I was actually doing this I didn't understand what's going already I'd play about switch it'll back on and these lights will come on full brightness smack in my eyes and that was that was unpleasantly bright oh yes right at the top of this we've included the EEPROM lie below but as a standard library it comes with the Arduino so there's nothing to download it such you'll already have it so just moving back down to the code so we're reading by say EEPROM get we're saying from a position 0 go and get me the value that's stored in there now there's a couple of little niceties here that are not immediately apparent first of all when you the EEPROM size differs depending on which chip you've got I've just put in a little debug statement here look and this here to say go and get me my size so we know if we're going to run off the end of it of course we're not going to run off the end of it here we're only storing a pretty small value but you can get the length of the or EEPROM for the chip that you've got so on here it's 102 4 but on a mega RB 204 8 or 4 or 9 6 cobweb which doesn't matter anyway this that there's a lot of the problem storage there that's more that's going to waste which that's a good thing that we're actually implementing it here so having got this fade value we can then think right now I know what to set it up but let's just think about what's happening here we're declaring fade value at the top now is this global variable as an integer so quick question them how many bytes does an integer take 2 how does this know them and we say going gets me my value how many bytes from 0 it's supposed to get well the answer is that this get command is one of the new additions auspice a new it's a few years old now but it was added to the standard library way back to make it easier for people like you and me when we're programming this so we didn't have to think about are we are we just getting a character are we getting an integer are we getting a float or a double you know how many bytes as this will take up so they've added that in and jolly useful it is - so by specifying here an integer variable this knows in that get statement that it's got to get 2 now what I'm going to do I'm going to switch over to my monitor view ronley just the code window because you'll see some interesting stuff in my IDE as you probably notice I'm not using the standard Arduino IDE here but that's purely so I can demonstrate more stuff to you I don't I don't normally recommend that beginners or just general hobbyists move on to this ID it's probably a step too far too much but in case your interest is the Eclipse IDE with the Arduino add-on and very good it is too so let's go over to the monitor right so I've switched over to monitor view which looks pretty much the same what we're doing but look what happens now when I I hover over the get command well there we go so now look what it's showing me there is what the code is behind that and what it is it saying we're using a template which means I'm passing you in a type but it you will only realize what that is at runtime and it's saying to cut long story short what this is saying is how long how many bytes long is the type of variable that you've sent me so that I know how many bytes to go and get out of the EEPROM now you can actually send down hit the wire here not just an integer type like this one here or a single byte character a char or byte you can actually send a whole struct direct as a way of encapsulating several variables in a single container so rather than then saving many variables into an EEPROM at various addresses you can give it the whole collection in one go and go just just store these somewhere starting from zero and it will work out exactly how many bytes to use and store them oi brilliant nice and easy for us isn't it really now you might be thinking okay that's the get how do we store the variables in the first place we'll come on to that just a sec so the next bit of the code then where we've changed it look is when we're pressing the up switch or the down sweep so on on here it's the a and B I've used and it changes the value this is all the original code what we're doing at the last minute here and here is using the put command and the put and they get our twins if you like they're one one gets one puts and this one also says well how many bytes does the thing that you've sent me occupy and I'll go and get that many from the address starting with address that you've given so here we said go and get it from address zero because I've got nothing else to store and the EPROM starts at zero and off it goes and does it stuff let me just kill the workbench view as you can see the rest of that code right there we are so nothing's hidden this is I must admit this is what I do like about the Eclipse IDE does show me stuff like this behind the scenes now there's nothing to stop you going into the EPROM library and finding the put method and the get method and having a look that's all it's doing is having a little sneak peek behind the scenes and you two can just go and have a look at that there's nothing secret or magical about it you can just have a look at the library and see how it's written but anyway that's one of the advantages of this IDE and what we're doing here we're saying put whatever the value is in fade Val fade well being two bytes long it says okay I know that fade Val is two bytes because we've got this value here I'll go and work all that out and use the right command the underlying right command to right away the correct number of variables okay now that's not the end of the picture regarding eproms there's a bit more so let's have a look at the browser window where I happened to have some stuff on here right so this is the prom library from art from Arduino themselves and as you see the the functions that you can get start from read write update get put and EEPROM itself right let's start from the absolute basics read and write which were the original functions as part of the EEPROM library simply go and get one byte or a char out of the EEPROM starting an address that you've chosen so if we click on we just have a quick look at that one right so it says there look reads a bite from the EEPROM okay locations that never it to have the value to fire fiber course you might actually be storing with a two five five and a byte money so it's basically EEPROM read from address and then I might have included a little example here that they are value equals EEPROM read a where a is the address of the EEPROM starting from zero all well and good and its counterpart is right does exactly the same now that's that's all very good and easy but as I say if you've got an integer that takes to bite or a float that's got four bytes it becomes a little bit more tricky or would be more tricky if we didn't have those other methods so if we look at the well let's look at update first so update writes a byte again so it's part of that basics family of functions but it only writes it if the value is different so if you're saying go and write to memory location 10 the value 50 and it says well the value is already 50 there I won't bother that's good and why is it good well you may have noticed it on the previous page when we look to the right what it says here is look the EEPROM memory has a hundred thousand life cycles which you can write to any one value that we're writing to value zero here and I've done a quick better math hundred thousand even if we were to write 20-30 values per day to that zero I've still got about five years worth of life in that single memory cell or the two memory cells and integers going to your occupy so frankly I'm not really that worried but the very fact that the put and will check first because because they can have pretty much an infinite number of reads it's the right you have to be careful of it's a good thing that the the update does in fact consider whether or not the value is the same or not so that's fine so the readwrite an update all right a bite to the EEPROM then we get of course to the more sophisticated functions where we have the put and the get so the put writes any data type or object to the EEPROM so whatever it is you've got you don't have to think about it anymore you just say put this into the prom at the address I'm telling you and here's the data whatever it is integer as it says down here like where is it a primitive type eg a float well we don't use many folks in LOD we know world floats are particularly accurate but you can easily use a float or a or an integer a string variable anything like that or just a string array really because a string is just an array of bytes now the other thing it says here you can write a custom struct so as I mentioned before a structure I'm hoping it might give a little example here ah here we are so here's an example of a struct you just call it any name you like and you say what it consists of is this set of variables usually logically reate related so because I believe you had like a person struct then you might have something like gender height weight things like that right so they're all they're all related and they've all got their own individual little values so in this example here for example you refer to as my object dot field1 field2 name for example so that's how a struct is used under them I mean it's moving one step closer to full object-oriented coding it's not object or int a Liam it's not a class this is a struct which is a well as I say the first step towards that that's fine the fact you can give it a struct with all the variables inside it and it will just go away with the put and it just does it here custom variable for example great so that's the put and the get is its twin its counterpart and it returns any data value again so back in the code you can see that what we've what we've actually got here is the put which uses the update method which behind the scenes we'll use the right method and it will write the correct number of bytes away so let's have a quick demo what happens on here now so if I disconnect the power which is the same as resetting it we want to see what happens on the code windows serial output to show that it's actually working so let me bring up the code window and look at the serial output right so it's common five there we go there are so what it said is like the size it's found is 102 for which we expect of course from a nano or or a you know and that that message is coming from the initial setup so that's this bit here and then it says the initial light level it's found is 12 because that's what we set it to before so if I if I now increase that by making this brighter well first of all going to switch the model of course because it's now going to come on at level 12 so I'll switch it on there is not blinding pretty good and increase it and you'll see that it goes up 14 15 whatever so we said 21 okay and now we're going to reset again so we reset there it is look it says 21 brilliant so now if I press my on button it comes on at level 21 now you might argue well why doesn't it just come on automatically the previous level well frankly I don't want it to do that because this this has also got a light dependent resistor on the side here which I've covered up with some black tape because what it does is automatically switch off during daylight hours I don't really want it coming back on at night because we might not require it on and some leaving that but you could very very easily in the setup here instead of saying 0 analog right P and W 0 put fade value in so as it comes back on it will come back on automatically at the correct value which you would try that why not right let's upload that if it goes well you see the interest rate interesting left this has now been switched on at full power because it sort of disconnected itself now we go back to the serial this is 21 now it's actually done it obviously that you saw the light level dip there but let's let's see that happen when I reset it resetting it remembers Thames is powering it off far as we're concerned here so I'll reset that there we are and it says I found 21 and it set it to 21 and just to prove the point we're going to dim it down a bit way down there we are mmm too far then you can quite catch that on the garage we dimmed it down to 16 you can still catch this on the camera and I'll now keep banging on about this but the camera if there's one bad thing about not this camera but just filming LEDs in general is that they very often appear as sort of mainly white with color around them there is no white light here this this is pure red absolutely pure gorgeous red ruby red it's 650 nanometers I think the wavelength of these LEDs which is like a deep dark red used in aquariums a lot to make the plants grow because they they like red light to grow so anyway this is now set to value of 16 we said so I'm going to reset this they will go off watch the debug they were out said 16 and let me hold that back on a 16 do you know I'm I am leaving like that I said it wasn't going to excite them want it to come on automatically but are they as it's winter now I'm recording this in winter so the dark nights or what is dark about Hoppus faun at the moment so yeah maybe I'll believe that they are you see their pragmatism God gotta just take it as it comes right so that's it so it's going to come on out the right value and whenever we change that value it writes it away to the EEPROM so a hundred thousand writes later that EEPROM cell will probably fail I mean it's only a mean time between failure but so it could fail at fifty thousand I guess it might carry on for two hundred thousand I somehow think that I won't be needing this anymore by then and if I do if I'm still using this in n years time and suddenly it doesn't remember that value anymore because the problem cell is dead because I'm used it too much do you know what I'm going to do I'm going to say start from EEPROM value three because zero and one are going to be used by that integer value so the next value up is going to be three and I'll use that and I'll have it for another zillion years after that okay I say it so that was just an introduction really into eproms and I think now that I've done that intro I think I might be using that a little bit more our projects because it can be quite useful having an initial value I guess the the catch-22 though is before you save that value what's it going to read now it said in that Arduino page before it went or snap something's gone wrong it said if it's never been written to its 255 well we'll have to assume that we haven't written 255 to it so we know we haven't got a value and then this code here could say are I've just read a value here it says 255 I'll assume that I've never stored a value in that cell before we'll set something sensible like 10 or 15 or something down here and now if you do want to store value 255 in a cell then of course you won't have to use that technique because you couldn't tell then whether you've stored that 255 or whether it's never been written to before so what you'd have to actually do if you wanted to store a load of stuff an EEPROM you know a few hundred bytes worth then you'd have to have a probably a different bit code actually just to write to that that EEPROM probably in a setup or something like that or run once never run it again so at least you've got something written initially but for us it didn't murder I mean obviously when I first switch this on it just read some arbitrary value didn't matter what it was because the minute I press this up and down it stored the correct value in there anyway but for more critical systems you might want to think about how you going to get those values in there for first time and probably a simple little bit of code would probably do it for me okay that brings us to the end of the problem I think that's probably worthwhile using in future projects I think that's good and I'm going to leave this now so it does come on at the preset value rather than zero because it probably stops me having to switch it on doesn't it really would save that EEPROM then from even more wear and tear corners okay great stuff I'd be like that video don't forget to share give me a thumbs up and indeed if you've got any suggestions for future videos don't feel showing coming forward and say can you do this can you do that let's face it what's the worst going to happen I'll say no I won't say no also I'll put it on my list of things to do which is bit like saying no because I've got quite a few things on but I really do like suggestions coming forward because even if I don't take your exact suggestion forward you might trigger off a few thoughts in my own mind like oh yeah that's a good thing to do we'll explore that in a few weeks I always say a few weeks because quite often I have some projects on the go or partly completed and I don't like having too many things on the go because my my little brain can't take that much in and then get confused what I've done great ok so as I say don't forget to give me a thumbs up share it and if you haven't already subscribed be really really nice if you did and thanks for watching see you in the next video I hope you're finding these videos useful and interesting please leave comments down below subscribe share and give me a thumbs up thanks for watching see you in the next video
Info
Channel: Ralph S Bacon
Views: 28,935
Rating: 4.8974357 out of 5
Keywords: Arduino, electronics, C++, microcontrollers, programming, gadgets, non-volatile, struct, EEPROM, memory
Id: bjpbjpuur3E
Channel Id: undefined
Length: 26min 7sec (1567 seconds)
Published: Fri Dec 09 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.