#149 ESP32 Dual Core Programming + Speed 💨Test vs Arduino UNO (fast!)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
and welcome back this week we're going to be talking about the ESP 30 to that lovely chip down there from expressive and it's got dual cause and I'm gonna show you exactly how you can use this within the Arduino IDE I'll show you how to put the board definitions onto there and then we're gonna put a very very simple program yeah you've guessed it it's the blink program but we're gonna put it on one core then we're gonna put it on the other core then both calls simultaneously and have them talking to each other it's really really easy and you can do it even if you're a beginner now the ESP 32 of course is much more capable than just running a little blink program so let's put a program on here as well that calculates prime numbers say to the first 500 and then see how quickly it does it compare to the Arduino Uno is it really worth the upgrade all this and more keep tuned I want to give a big shout out to PCB way the sponsors of this video as you can see from the top of the page here they're celebrating their fifth anniversary and they've got some goodies to share with you so they've got a lucky draw they've got gift shop discounts and of course a whole raft of coupons just take a look at some of these offers you might be well surprised and the quality of their PCBs are second to none they really are good have a look at their website and enjoy their celebrations why not try out their website now $5 for 10 pieces of board excellent quality I personally wish PCB way a long and prosperous future happy birthday PCB way right the first thing to tell the Arduino IDE then is work what about all these balls where are they from so far up your Arduino IDE blank sketch like this doesn't matter what's in it and go to file preferences and you'll see down here a big long list of stuff or maybe yours will be blank it doesn't matter either way click on the bit at the end here that brings it up in a nice list except I've got so many entries it's not a nice list let me just expand that now the bit that you need for the ESB 32 boards is this line here now I'm going to put that line immediately below the description in the video here so what you can do is just pause the video I have a look below the video description copy that line and paste it into your Arduino IDE now if you're sort of doing this in real time a zero so paste that in there then click the ok button which I'm cleverly hiding there is look ok and then ok again now at this point it will tend to freeze a bit as it works out what it needs to do and download boards and things to just give it a little bit of time when it's responsive again go to tools boards forget what you may or may not have already in here go to boards manager now this will also take a little bit of time look at the downloading platforms index thing down here you've gotta wait till that finished otherwise you can't do anything so we'll come back in about a minute right now that's done there ok the message is gone so at the top we want to type in ESP 32 and lo and behold there it is ESP 30 to bias press F systems great now obviously I've got mine installed 102 installed you probably won't have so select the version which would be the highest version 102 in this case and then click install if you've got a lower version in here you probably need to upgrade at this point ok so select the highest version you can and there'll be a button over here that says upgrade whichever one you do install or upgrade click it and then click close down there and that will also take a minute or two before it works out what it needs to do so we'll come back in about that time right we're back now at this point we need to select the actual board you've got I say actual close enough is good enough so in your arduino ide go to tools go down to the boards go down to the ESP 32 section which might might be run at the top for you but wherever it is just keep scrolling down until you reach the ESP 32 arduino there is now out of all this big long list of boards here which goes on quite some time on my thing here if you know exactly which board you've got and you can find it in that list and obviously you're going to select that if you can't quite get to it and you've got a sort of a generic ESP 32 board what i suggest is you select something like the lowly indie 32 or the we MOS lowly in 32 i think they're they're the same we MOS and lowly now have sort of merged what else is there you could select the Adafruit ESB 32 further that's pretty generic and vanilla something like that so let's select the Lowell in D 32 because that's a nice standard board now at this point you'll see that the tools bit at the top has gone blue which means it's thinking about things so once again we'll have a little cup of coffee and come back in a minute or so try it's done it right so now the tools is is not blue and you can you can select other things and make sure it's on the right port okay so on my system it's on port 16 for example so I can select that at which point it will ever never think as it talks to that device and interrogate to make sure it really is on the end there once it's done that everything would be fine shouldn't ask you too many of these questions but if it does just put the same as what as well I've got here the upload speed is 9 to 1 600 so it's pretty fast upload flash frequency I've got as 80 partition scheme as default if you ask screw that question at all core debug level none not debugging anything at this time and the port of course would be the whichever port you've got it on right okay now we're ready to do our very first sketch on the ESPN 32 right now everything we do from here on in will run perfectly well on the arduino ide after all you've just put the boards on Avenue it knows exactly what those boards are and the language and everything else and there'll be new new sentences structures that you can use but it's going to be very very similar to what you've done already now I'm not gonna use the Arduino IDE but all the sketches will be perfectly ought to be in our IDE compatible I'm gonna use something called slur but workspace this one here is eclipse and it's it's just bit of a more grown-up one but to prove the point this code which we're going to be using in our first example I'm going to actually paste that into here and just show you that it runs absolutely fine so here's the code we're putting in let's go through this line by line because you'll need to understand a little bit of how this works it is the blink program though so there's not a lot okay so let's certainly start from the top right the very first thing you'll notice is that we're defining an LED on pin 27 now if you're used to the Arduino IDE 27 is not a number you'll be used to using but on the ESP 32 there are many pins that you can use right up to 36 37 so we're gonna choose one that's on my board at least fairly easy to use if we look down here we noticed that pin 27 is in fact right at the top here and pin 26 is underneath here so I've just chosen those because this is the little we masti 1 mini board if your board is different you might find that pin 27 is a north quick place and it might be down here somewhere so don't use 27 if you don't want to just choose one that's convenient but make sure it's one that you can use as an output pin now on my github I'll put a list of all the pins that they recommend you do use for input/output and stuff or just stick with 27 it's safe we know it works but you can see it running there now it's not that's not the sketch by the way that's the sketch we'll end up with after a few programs so apart from defining the LED as pin 27 the setup starts the serial monitor at 1 1 5 200 we might as well make it fast this time because we are talking about speed eventually and the LED is of course an output pin because it's driving that LED we'll skip over the next two just for now these two here are basically the blink program it turns the LED on high for half a second and then it turns it on low for half a second 500 milliseconds and the only extra bit that we're putting in here is this line here to say which core am i running on bearing in mind there are two cores on this particular processor this says which core am i running on and it's the standard loop and the standard setup and the ideas which cord running on we've got two there number 0 and 1 any guesses well let's run and find out right let's build the program this does take some time I must say if you're doing it the very first time because it's actually gonna compile all the underlying supporting libraries to the ESP 32 so don't be surprised if it takes that mini o2 the first time after that it's a little bit quicker so let's compile and get it onto that board all right so it's now out loading to the board which you can just about see in these red lines down here and it's gonna reset it and the board's running that code as you can see them down here it's now blinking one pin on port 27 brilliant well Lisa it's running but what caused it running on well it's far up the I do Arduino debug or serial monitor as it's known and a look there are did you get it right it's running on call 1 and that is in fact the default call other tasks are running behind the scenes that you probably don't know about because this chip is actually running a little tiny operating system that we have no involvement in whatsoever and it's called rtos real-time operating system in fact the real full name is free rtos because it is free and it's open source so this is running a tiny operating system think Linux or Windows but it's tiny and it controls the tasks that are running on each core for example this is currently running a program to deal with Wi-Fi even though we're not using it there's still something in here that runs Wi-Fi and keeps things in order and Bluetooth we haven't got anything in our code to run any of that but the RTOS most certainly is doing that behind the scenes so bear that in mind because that might come in useful in the future sketch so we're running on core 1 ok how do we change that how do we change stuff running in our loop from core 1 2 cos 0 just reveal fun but it's all part of the learning process ok we need a different sketch so this is the new sketch then to run it on cos 0 now you might ask how am I gonna let us running on cos there it will exactly the same way as we did it last time it will tell us so let's just walk our way through this we're still running on pin 27 the led fine but now we have to create a new task by default everything runs on tasks well and that's all handled behind the scenes for you but now we're taking control we're saying I'm determine whether it's going on toss one or a cool one right there's cause zero I'm going to determine what happens there so first thing I've got to do is make a task so we're going to call it task zero just keep the naming convention the same the setup is okay up to this point here now this is where we create a task now this is saying we're creating a task that's pinned to a core and as its name implies we're saying this task instead of artists are TOS instead of you deciding where it's going to go I'm taking this and I'm pinning it to a particular core so I'm determining which is the best core to put it on rather than you telling me which one you're gonna put it on which is what it would do normally you can create tasks and it will just edge-lit to the least used core for example okay so to avoid that we're gonna say no I'm telling you where this is going so the first thing to say is write what function are we actually going to run then well normally in a Arduino setup you have setup and then you have loop well now we're gonna have to create another one called well I've called it loop zero you can actually call it anything you like but it just makes sense to keep the names the same here so I'm going to create a function called loop zero you can always see what do you see the beginning of it down here anyway let's not jump ahead loop zero that's the name of it that the text name that it wants to have this is the amount of memory that it needs and I've chosen the thousand for now but it's probably a bit trickier to work out exactly how much memory you're gonna have the input parameters what we haven't gotten e even though the loop 0 down here has said I've got a parameter or potential you can have a parameter we haven't got any the priority task well zero one two whatever we're saying is zero which is normal there's a handle to the task I was handle well handle is is basically a name a pointer within memory that we can get hold of an extract at any time so we can get hold of that task again so this task is running we can say to our toss look that tasks that we've called task is zero can you grab hold of it please because I want to talk to it we'll kill it whatever it's just a task handle we're not using that here but the most important thing is here is where is the task gonna run which core we're saying its core zero okay and that's it so we're creating this task we've told it which function to run we don't care about any of this really but we do care about the zero bit so what does what does loop zero look like well it looks just like loop actually no no different they do well the one difference is it's got a run forever because there's nothing here to say run forever in the standard Arduino IDE your loop why does it run forever what magic on earth makes your loop run forever well unknown to you perhaps there's a program above all that that says run the setup and then run the loop forever I say it's just a sort of a shell program that the Arduino IDE puts around your sketch for you but here we're having to take a little bit more control so we're saying here's loop zero and within that I'm running forever and that's how you say run for say for loop so normally you say for you know integer count equals zero count is less than 20 count plus one count plus plus and do it 20 times or whatever but now is saying run forever and here's our exact same code that we had in loop zero no different at all the only difference now is we're gonna say run on core zero and there's another little caveat I'm not quite sure about this I'm still investigating this in the original loop that we have the one which this code here was originally in if you don't put something in here like a delay at one one millisecond it won't chisel this task or else shuttle it then I flip run it I don't know why there's something going on with our toes or maybe it's because we it's the Arduino shell program around the outside maybe the compiler is too clever it says well you haven't put anything in loop therefore there's nothing for me to do the end I don't know but I will try and find out right so that's it then so that's basically moving the code to this loop to zero compile that and let's see what happens far up the debug window there it is exactly the same running on core zero it says and the LED is still flashing exactly the same way as it did before no difference so in fact it's not that exciting is it really okay we've moved the code the codes now running on a different core a parallel core to what we were running on to call one is now just sitting there going delay one millisecond delay one millisecond delay one knows it's all just do it but call zero now is flashing our little LED off big deal can we not run to blink programs one core zero one on call one how about that good idea let's do that next right this is the third sketch then very very similar to the ones we've done already and yes I'm there when the Eclipse IDE but just ignore that all this stuff over here we'll just pretend is running on the arduino ide because we've already proven that it runs quite happily so what do we got to do to make this run on two cores we want two independent blink blink programs running at the same time right the first thing you got to do then is create another tarce i've already created task zero in the last sketch now we create another one task one for call one and of course we need another LED pin so we're using pin 25 as I said because of my system it's convenient for me to have the pins close to the top of the board there so I've got 27 there and 25 right next to it but as I say you can choose any pins that are suitable but if you want to be guaranteed it's gonna work use those pins okay so in the code then loop zero is what we created last time and this code in here after this do forever we've got all that there and we've got the line in to say which corn I'm running on and in loop one now we're also saying do forever exactly the same code except I've made it flush a little bit quicker low so that's 200 milliseconds on and off whereas the previous one is 500 just so that we can see a difference really the setup is almost identical to last time we've got an extra LED pin to set as an output the zero core task is exactly the same running on loop zero and now we've got an identical one running on court one and the loop once again the original loop does nothing except delay because we've taken control of all our hoops now we're gonna go nope we're in charge we know what we do in right okay so let's compile this send it to the board and then we'll have a look at the debug window and see what happens right it's uploaded and as you can see now we have to blink programs running over here one flashing quite quickly that's the red ones that's the new one we've just added in with a slightly faster flash rate and the original one which is still going quite happily now these are two independent cause now this is like having two Arduino boards running side by side okay there's a little bit of you know joined up thinking in the middle of this but these are two independent cause running and one does not affect the other obviously not because we're we have the delay in for the first blink the delay on underlay off it's not affecting this one at all is it it's flashing nice and fast great let's have a look at the debug window or serial monitor and see what it tells us and as we can see there's his look running on call 1 0 1 1 0 1 1 1 0 we had three ones there if it quick why is that well because this one here is flashing a lot quicker and running around it it'll loop a lot quicker than this one the blue one over there so you're obviously going to get more core one loop completing compared to loop yeah loop zero task zero well cause 0 too many zeros in this sketch so there we are that's it it's working now you might be thinking at this point that's great I can I can do my long running task on a different core for example on my home alone project one of the things I do is go and get the date and time every 10 milliseconds it tries to update it on the screen but when any anything happens it's long running like I have to upload stuff to the internet that clock display obviously stops because it's being blocked not deliberately I'm not I'm not footing his plane like that I'm just calling a routine guide please send this up to the cloud and off it goes up to the Claire but while it's doing all that the clock display is frozen because there's only one core to run on now though what I could do and they were thinking of is I could run that particular process on a different core and the clock display would run continually without being frozen I mean it's eye candy it's not having any functional effects but just be a good little example to do for a future video perhaps okay now next thing then we've got these two independent blinks running that's great but what if in the real world because it's unlikely we've been having dual blinks what if we wanted the first core core zero that's flashing a little bit slower so it's got a slow running routine in there to say we read one fast running thing you can't keep blinking away like that and completing your loop and doing more loops and I am until I'm ready for you to say go so we want cause zero talking to call one and saying okay you can continue now and then it runs it yes okay now you can continue again okay so we have the two cores talking to each other for a little semaphore let's talk about that next right we're going to amend that last program just a little bit and the main thing you can see straight away on this sketch is we've added in this semaphore so it's a type of semaphore handle underscore T and I've called it uniquely enough semaphore because that's what it is what's a semaphore a semaphore is just a flag right say yep or no in fact is not a no it's the case of not too there at all or yes it's up the semaphore is available and I'm saying go now there are various types of semaphore and we're not going to cover hardly any of them here and we're just gonna say there is a semaphore task X you can continue so let's have a look how we're going to use this and it's very very simple we're going to keep the exactly the same LEDs as we had last time void zero which is running all by itself slowly and this time I've made it really slowly so it goes on for 2,000 seconds at milliseconds doesn't make it too long a and then off for a second and then it gives that semaphore away to her once it doesn't know who once if it says I'm prepared now to say okay whoever was waiting for me in my long-running program you can now continue if you were waiting then there won't be no tasks waiting for it or there might be a task and of course in this sketch we most certainly do have a task loop one which remember was the quick red flashing LED this is saying yeah I'm gonna flash in 500 milliseconds and then wait for a second and then I'm going to take that semaphore if it's there and I'm prepared to wait forever if need be until that a semaphore becomes available okay so it's quite simple you go well where does it where does it come from this semaphore well we've created it at the top of the sketch now so there it is but once we've done unless and until we give it here it's not available having given it and then the other tasks except sitting goes yeah that's fine I've I've taken it here I see it's gone now so the semaphore goes up the task if any takes it it goes down and it's gone you can't take it again if you want to take it again if I had two statements like this semaphore take it wouldn't work it would take the first one I think oh well there isn't another one to take okay so what we've said is this task cannot continue beyond this point until this program up here this little loop has given the semaphore so what I'm hoping to see is the blue LED and the red LED starting off together but then the red edie just stopping it will finish a lot quicker just stopping and the blue LED will pondless Li go along and flash its LED at a much slower rate and then they'll starting it because the blue LED is said okay well let's try out let's compile this and see what happens so whilst this is compiling this just have a look at this red and blue going man down here they're totally out of control neither one of them is controlling the other the red LED because it's running so quickly does a lot more iterations than the blue one now look we must be uploading now it stopped now look now you see that they both start together but the red one finishes a lot lot sooner and waits effectively for the blue one to start again so if I am zooming on the camera they just give you a better idea what's going on so they're both flash together the red one finishes very very quickly but the blue one hangs about for a lot longer and doesn't let the red one start until it's ready and given that semaphore so the blue one as the flash goes off and after a second if you see that delay there after a second it then gives the semaphore this red one which has been sitting here now for all that time going Oh at last the semaphores arrived I can grab it and run round my little loop again so that's one way of talking between tasks and controlling them so they don't go mad and do their own thing isn't that cool now we could talk about other things about how we share variables and things like that but I think that's for another time quite honestly and if you want to get an ESB 32 d1 mini like I've got I'll give you links of those and some alternatives so they're all down there and the links below this video and better off go to my github because in the github there'll be PDFs and things about the ESP 30 that you can go away and read if you want to of course but it does give you a lot lot more detail and there's a whole world of stuff out there for the ESP 30 - believe me it's is mind-boggling and the expressive help guide is is brilliant it's absolutely fantastic I'll put that up there as well so it's all about the things you can do with an ESP 32 and RTOS and stuff like that so if you're really gonna get into this you do need to read those but we'll do some more sketches on these they too well especially if you comment and say yeah I like this sort of stuff then I'll definitely be more encouraged to do another video and that just about brings us to the end of the ESP thirty-two multi-core programming demo if you're ready to buy now do so ball means but don't forget to look at the stuff in the github if you want to hang around and see the speed test that is how fast is this little thing compared to the standard Arduino at the back hang around but it's it's just a bit of fun really I mean we're pretty sure it's gonna be fast about how much for us and that's the question doesn't it anyway if you kit if you skip me now thanks for watching see you in the next video oh you're still here good good right what I've got here is a bit of code then I'm just sort of slung this together sort of half calculates prime numbers through a sieve mechanism and then a couple of loops as well so it's just a bit nonsense I just want it something that that ran for a little time basically so bronze just having loop scan that was enforces loops within themselves I'm not sure how much that resource proves much there this is actually you know calculating floats and stuff like that and whatever it's just a bit nonsense as I say but I've loaded this up onto the Arduino so let's have a look to see how long that takes if I open up the serial monitor here it will reset the Arduino and spring into life so let's see how long it takes to calculate the first five hundred and one prime numbers but with a lot of gubbins in between as well alright set ups completes running running okay it's still running calculating hobee's doing something ah there we are nine thousand eight hundred and eighty-five milliseconds so a fraction short of ten seconds then okay it's doing it again there let's let's make sure the second iteration then is pretty much the same as what the first one is made sure we didn't do anything funny oh nine another millisecond look at that okay well that's good enough so we call it you know nine point eight seconds right let's switch over to the ESP 32 now and upload the process is exactly the same program this program that you're seeing here exactly the same no changes and see what that does alrights trying to upload here we go it's uploading the program now so keep you on the idea hang on what okay same program running on the ESP 32 as you can see there 61 milliseconds compared to 10 seconds hmm interesting yes well obviously I knew the answer because I've done this before and it surprised me just how much faster it was in fact how many times faster does that make it I can't do this in my head I'm gonna have to get the calculator and come back to him well the calculator tells me that it is in fact a hundred and sixty times faster for this particular bit of code anyway obviously you're gonna have things like I squared C SPI anything like that it's gonna slow it down it can only run as fast as the bus will let it but natively core wise 160 times faster and this is on a single core remember and we could put this same calculation and I'm not gonna do it sorry we could put this same calculation on both cores and they would both run in 61 milliseconds it's just amazing isn't it how much faster well worth them having a look bet you think because even if you just wanted a fairly basic well even if you want to convert all your code from the Arduino over to an ESP 32 without any of this dual-core stuff it would run approximately 160 times faster than what it does already if you've got you know a fairly meaty type of program or indeed an ESP 30 esp8266 which is probably about 10 times faster than an Arduino if you're struggling with that if you're causing I'm no dread on there you know home automation type stuff this is gonna just knock spots off of that if you code it right you can use both the calls and make it really really productive okay well there we are then interesting little end to the video isn't it okay so comment down below give me a thumbs up if you think it's worth it follow any links there may be affiliate links I'm not sure yet there might be especially demand good and thanks for watching see you in the next video I hope you're finding these videos useful and interesting there are plenty more videos to choose and a couple are shown below and if you'd like to subscribe to this channel just click on my picture below and enjoy the rest of the videos thanks for watching
Info
Channel: Ralph S Bacon
Views: 52,028
Rating: undefined out of 5
Keywords: ESP32, esp32 tutorial, Dual Core, Semaphore, Programming, C++, Easy, Simple, how to, esp32 arduino, Beginners, arduino benchmark, Arduino, ESP32 vs Arduino, Speed Test, ESP32 vs UNO, esp32 review, esp32 project, arduino uno, esp32 arduino ide
Id: jpVcCmh8sig
Channel Id: undefined
Length: 31min 18sec (1878 seconds)
Published: Fri Jun 14 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.