YOU can Learn the ESP32 in 15 Minutes: Hello World!

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hey i'm dave welcome to my shop i'm dave plummer a retired software engineer going back to the ms-dos and windows 95 days and today i'm going to cover everything that you need to know to get set up and coding in c plus plus on the esp32 microcontroller but why the esp32 well it's a chip that's really growing on me i've been hacking on and with esp32 for about three years now and its combination of fast performance abundant memory dual cpu cores plus having bluetooth and wi-fi built in has made it a lot of fun a great deal of what you see in the background of my videos is actually run by esp32s such as the atomic fire lamp over my right shoulder the flickering candle the animated palm tree and even the background windows themselves they're all connected over wifi and receive their color information from my central server but more than being just powerful and flexible i actually find esp32 programming to be a lot of fun it's actually my hobby when combined with vs code and platform io which i'll show you how to do right away the dev test debug cycle is quick and easy you can get source level line by line debugging of your live application and it can make use of it all using the latest c17 magic because there are almost no other dependencies to worry about you get a chip a compiler and an sdk to play with like a lot of folks i was initially drawn to microcontroller programming by the ease of use promised by the arduino lineup it's a whole family of easy to use microcontrollers rather simple programming sdk but the arduino is more than 12 years old now and when people say arduino what they usually mean is the atmega328p the chip used in the arduino uno board as i said the esp32 is not an arduino it's actually a much more powerful professional grade microcontroller with many more features but here's the great part they've ported the arduino sdk to it so you can use the chip with all the same ease that you would encounter in the arduino line but while simultaneously reaping the power and performance benefits of the esp32 let's take a quick look at the specs of the esp32 i'll also include some of the arduino uno specs for comparison whereas the arduino is a single core 8-bit chip the esp-32 has two completely independent 32-bit cores each at a relatively blistering 240 megahertz how fast is that well does that help if i say that's about 500 times faster than a pdp-11 probably not maybe think back to your pentium pro 200 desktop days that's roughly where it's stack ranks in terms of performance coming in at 600 million instructions per second not bad for three dollars which seems to be the going rate on aliexpress i know that on my music spectrum analyzer project it's enough to receive a packet of data over wi-fi unpack it with lg compress and run a full fast free transform before displaying the results on a 1000 led matrix using pulse width modulation and it can do that 40 times per second which blew my mind when i first attempted it the esp32 supports five different power modes like active light sleep and hibernation where it can wait while drawing a scan two micro amps that's one five hundredth of a milliamp which is to say pretty close to nothing the esp32 has up to 18 12-bit analog to digital inputs and two 8-bit digital analog outputs it has 10 capacitive sensor inputs for touch screens in the lake it also has four spi bus connections two ida c bus connections and two ios bus connections spi and idc are great for connecting peripherals like sensors and lcd screens and eye to s is a standard for pushing pc and music around in a circuit the esp32 also contains an ultra low power analog preamp there are three uarts for serial ports and it can act as an sd card controller the esp32 can send ir remote control codes on up to eight channels at once those channels can also be used for driving other digital waveform communications such as controlling eight channels of addressable leds in parallel i use this ability to run the four arms of the atomic fire lamp and the eight spokes of the tiki fire umbrella the module itself contains a hall effect sensor which can be used to detect the presence of a magnetic field so for example you can use it as a sensor and a security system or even automotive fuel injection system it also supports multiple internal real-time clocks and because the chips all support wi-fi all of my projects tend to set their own clock using the sntp protocol to fetch the time from a time server at google or similar as you might have guessed by now when combined there's actually more functions than there are actual pins and that's because pin assignment is often dynamic so some features like the dax are constrained to a narrow range of pins whereas others like the pulse width modulation for leds can be almost on any pin you simply say for example that led channel 0 is on pin 5 or the serial uart is on pins 10 and 11 and so on naturally however you can't enable every feature on the chip all at once just because there aren't enough pins that said i've never run out or wanted for more with the two dozen or so that are available there are also some odd constraints that result from how the chips internal resources are managed such as one of the dacs being unavailable while wi-fi is in use but they are few and far between otherwise the chip is very flexible and quite orthogonal the chip actually ships from espressif in one of two formats the chip module with a pcb antenna or a chip module with a coaxial connector for an external antenna normally however you wouldn't use the module directly at least as a hobbyist instead you would order an esp32 development board that houses the module in a standard little dip package of some sort let's have a look at the various form factors that are available now first we have the most basic of modules which just has a power regulator antenna and all your pinouts available from the module next we have a board that features the coaxial connector so you can run an external antenna following that we have the heltec board with an on-board blue tft screen following that we have a basic module again with just an antenna and a power supply but this time in a more longitudinal package that will fit better in a breadboard the larger red olemex board features an on-board ethernet nick so you don't have to be reliant on wi-fi at all times next to that we find the tt go board which has a nice built-in tft display coming up after that we see an esp32 rover actually two of them three of them in a row all identifiable by the longer chip module package the rover is known for supporting ps psram and can ship with four or eight megabytes of additional ram right on the chip module now this is actually a board module stuck into a breakout board that gives me individual screw terminal access to each one of the output pins and i find it really handy for prototyping and finally an arduino uno form factor so you can take advantage of some shields you might have on hand already now it doesn't matter how great the chip is if the development environment sucks there are certain things that i just hate working on like trying to build a responsive web user interface in the browser talking to a rest api and a server somewhere it's just a nightmare to debug end to end and it makes it like actual work for me the esp32 dev environment is excellent however and i'm not talking about eclipse the official one from espressif oh i'm sure it's excellent and it has many fans you can also use the arduino ide itself if you add the necessary board support files but we're going to go one better we'll be using visual studio code which is becoming the editor of choice for much of the programming world it seems i'm going to quickly run through how to install visual studio code and then the platform io extension that we need installing visual studio is actually pretty straightforward so i'm going to run through it really quickly just so you see all the steps that i've gone through to set up my dev machine so something you need to see visual studio code installed but i want to at least fast forward through the steps so you don't have a machine and go but what's missing from my machine so as long as i go through all the steps necessary you should be fine as long as you follow along so we're going to go and download it i am running windows now you have the choice of user installer or system installer and i would say that if you have the rights to do so on your machine get the system installer and lean towards 64-bit should start the download there it's already complete i think it's important here that you pick add to path so that you get it added to your command line path that will allow you to launch projects from the command line that's it'll now launch when we finish setup so this is my machine with i've got a couple extensions installed but i do not yet have platform io installed and that's what you need so let's go install that one together i'm going to type in platform io that will find it and i just click install 319 milliseconds i'm a busy man and that's pretty good so that's all it took to install and if i'm correct now we can just click on the little platform i o alien dude here or bug i guess it's probably an ant i'm not sure what it is actually and that will get us to this little tool toolbar quick access menu for platform io from where we can pick pio home and we'll say open that it'll just get us to the home of platform io good place to start let's write our first app but what does that even mean when there's no keyboard no mouse and no display well all we want to do for this first attempt is to compile link upload and then run our program we'll turn on the serial port monitor and platform io our app will print hello world to the serial port and if all goes well we'll get our text displayed back in the ide stay with it though as our next step will be to add a sexy high-risk tft display but we need to walk before we can run from here our most obvious action is new project so let's do that we'll give it a name my first test and we're going to pick an esp32 module to tell platform io what it is we're going to build for now the thing is sometimes you may not know exactly what you have so you may not get a pin definition for the built-in led and things like that if you don't use one specific to your board but you will get all the general esp32 support by just using espressif esp32 dev module so i'm going to use that i'll type in esp32 scroll down a bit expressive esp32 dev module now i'm going to build with the arduino framework or the sdk because that's what we're doing today but you could also build the expressive framework uh using the one from the manufacturer and use that as well so you can go back and forth i never have really found a strong need to there are some edge cases where you might need the manufacturer's latest and greatest but so far they've got everything like psram and support for everything that i need in the arduino framework version it'll take a couple seconds to chew away and create the project now it's already done there we go i'm going to stop this in case it's bothering anybody here there we go it will create the folders for you there's an include a lib these will be empty source will have your main.cpp all the vs code settings are automatically set up for you by platform io so you don't generally have to worry about them and all of your project settings are in a file in the route called platformio.ini that's the file that's been up on the screen here and they're pretty straightforward at its most basic it's saying that the environment that we're going to use is esp32dev platform is espresso 32 the board is again esp32 dev and the framework is arduino the things we picked in our setup so we could have typed them manually and i often do or just copy and paste because the wizard or whatever it was when i went through the new project menu all it's doing is cranking out the folder structure and the platform io so if you can create that on your own great if not the wizard will do it for you i'm now going to plug in my dev module let's build it by picking platform io we could just jump right to upload but that's one thing at a time we'll say run build task it's running away in the bottom window here and it's done it took five seconds which is not bad for first ever clean build on the machine i would say now i'm going to upload it and i'm not going to tell it where to upload it'll try to figure it out if it doesn't work you can put upload underscore port equals and then like com3 in the platform io.ne if you need to but if you don't have a bunch of other weird esp32 plugged in it'll probably find it i even have a couple plugged in but let's see if we can find this one we'll do upload finds it on com17 it writes and there you go it's uploading now it's already done it's at a million baud and you can actually do 1.5 million but you can see your dev cycles are pretty quick five second builds two second uploads you're not waiting around for minutes plus you can do it over wi-fi and that's for a later episode but you can do the flash over wi-fi so you don't even have to plug it in let's look at the source code to whatever it is we just uploaded that'll be in main.cpp and it's completely blank it does nothing but it is the template that you need to start with that has two functions setup and loop those are the only two functions you have to provide you can include arduino dot h makes things a lot simpler and once you've done that setup is called when the chip boots and then loop is called repeatedly forever so you put your one-time setup code in setup and you're running loop coding loop and you get it from there i'm sure could it be that simple and yes it is let's try it let's upload okay it's successfully uploaded i'm going to open in the serial monitor serial monitor and we'll tell it to column 17. clearly the wrong speed so this is where we go into platform io and we say monitor speed 150 and 200 because that's what i put in the serial init it's probably default to 9 600 but why why stay that slow let's close the serial monitor if i reopen it is that sufficient no if i have to trash it there we go access denied if that happens as suggested to me i'm going to unplug and replug the device back in okay there we are hello world repeatedly because i put it in the loop part of the function that's probably not the wisest thing to do but i mean it'll be gated by how fast serial is or isn't so okay now that we can walk let's try running a little bit i'm going to set aside the ultra basic esp32 dev module we just used to take advantage of something called the m56c plus it's a self-contained module featuring the esp32 at its heart on top of which it adds a nice lcd screen a microphone real-time calendar clock a couple of buttons a three axis imu for tracking movement and even a battery for unplugged operation you get it all for about 1995 to 25 depending on availability and links to the m5 are in the video description it even comes with an awesome watch strap so you can wear it on your wrist i guarantee you'll be the only person in virgin atlantic first class wearing one there's a whole lineup of products including the m5 stack which has models that feature additional memory a larger display a bigger battery and so on you can buy many add-on sensors for everything from sound and light to volatile gas and time of flight the range of things you can sense and control with the m5 line is quite impressive you won't get bored for lack of variety anyway let's take a really quick look at the m5 site all right so here i am at m5stack.com to kind of walk you through what their offerings are so they have these bigger units which are stackable i don't even have a lego plug that's on the back but uh as you can see one passes through to the next with these pinwheels and you can stack functional modules together for customized assembly don't know how deep you can go but i don't know if there's a limit or not you can also see they have external modules like this little sensor here that plugs into the port and you get little plug-in ones that go into the m5 stick that's this orange unit right here and that's the one that we're going to be using so you can see they have quite a range here's the one i generally buy because it cracks me up that you can wear it as a watch but if you don't need that i think they're like 1999 so here's the one that's 22.99 is it in stock it's in stock they have five in stock buy now if you're watching this video live buy now now looks like the esp32 basic one is uh out of stock but it is available on amazon but it's 25 on amazon when they're out of stock so now in terms of sensors environmental hat ambient line sensor encoder units thermocouples thermocouple wow a little poe port that's kind of cute i think this was a bigger one though uh m56c hat ambient light sensor thermal printer kit well that's cute now this goes on for pages so heart rate monitor thermal camera units all kinds of stuff it's kind of low res looking but uh neath anyway and so that's just a quick little look at some of the offerings from m5 stack again not a sponsored video i just want to show you what's up there because they're really easy to work with the actual devices so i recommend them because they've got all the stuff you want built in like a microphone and a screen and all that stuff the big thing that i'm going to do differently now that we have an lcd screen of course is to display text directly on the screen so that we can see what's going on and start to make more interesting apps all we have to do is include the right header and lib and then add about five lines of code we simply need to initialize the display set the rotation clear the screen set our color and start drawing it sounds easy and it is so let's get right to it now to take advantage of the display first we have to include a library and this will be our first example of doing that you can do it manually by including a line in platform io dot any you'll see the line that results at the end but we'll use the ui to do it initially we go to platform io libraries we're going to search for tft espi this is the one we want let's see if that comes up with anything yet but this is indeed the one we want so we click on it then we say add to project which project we're going to add it to my first test it has a single line libdepps and the one library that it is adding a reference to that could even be just as simple as a github address and that would work too to add a properly formed github project to your esp32 project so if somebody has code like this in this case coming from github as well it is something you can link to directly by simply putting it in your any file no magic no downloading of the code yourself behind the scenes and putting it in a folder and all that now there is a bit of magic that i have to include here and i got it from the documentation so it's not really a magic it doesn't count if it's in the docs but we need all the pin outs and everything for how the screen is actually connected to the esp32 so if the library knows how to address the screen after all it could be wired to any of the pins right so i've got all that information here i've got the driver to use the width the height the csdc the reset pin the mosey the clock everything i could possibly want to know i'm just defining as defiance pass to the compiler and the tft espi library knows how to make use of these but all you need to do is copy and paste and of course i'll put this code up on github somewhere so that you can follow along easily or just download it if you want to make sure you've got the exact copy of what we did during the episode all right so we've linked with this library but we haven't done anything first let's include the library's header we're going to declare an instance of the tft espi class now it we're not going to tell it anything about what pins to use or anything because it's going to get that all from the defines we passed to the compiler and so in the actual code we just simply create one and it will do the right thing now you can see i had trouble uploading here and experience has taught me when in doubt slow the upload speed down a bit because i think it defaults to a million and that's a little too fast sometimes it could be my cable could be my port it could be the box going through the keyboard i have no idea it could be usb 2 at this point there it's uploading now cool and it displays hello world exactly as i wanted it to the code is really simple now that i know that it works i'll tell you what i did serial begin was there before of course for us to be able to initialize and use a serial port then we initialized the display we set its rotation to three which is one or three will work if you want a portrait mode that'll be two or four or zero or two i guess actually we fill the screen with blue and it gives you a color definition which works with that one because otherwise you have to tell the rgb and this is a little faster shortcut we also set the text color to be white in the foreground and blue in the background we set the text size to be three which is actually just triples the size of the base font and then we print hello world that's your basic hello world in c plus on an esp32 featuring an lcd display not bad for 20 minutes about a dozen lines of some pretty straightforward c code one of the things i love about the esp32 environment is that it's a little bit school that there's not a lot of voodoo other than the one project any file there's no meta files no preprocessors no magic runtimes that need to be installed you can certainly get away with knowing a lot less but in case you're curious here's the basic way of land there's a little real-time multitasking operating system running on the chip called free rtos it's not unix but it's unix-like in the way that it supports sockets files and so on rtos provides you the ability to create threads or tasks and to manage the memory heap and all the other basic functions in the c runtime that you'd expect from an operating system between the c runtime the arduino sdk and the rtos layer odds are that the feature you need is in there somewhere next time we'll look at adding a small web server to the chip so we can connect to it over wi-fi via the web browser i really appreciate you joining me out here in the shop today if you're not already subscribed to the channel i'd be honored if you consider subscribing and if you found this episode some combination of entertaining and informative please give it a like if you have any interest in matters related to autism asperger's or asd please check out my book on amazon secrets to the autistic millionaire it's got nothing to do with money and everything to do with living a successful life on a spectrum it's everything i know now that i wish i'd known back then remember i'm just in this for the subs and likes so please be sure to leave me one of each before you go today in the meantime and in between time i hope to see you next time right here in dave's garage [Music] you
Info
Channel: Dave's Garage
Views: 294,888
Rating: undefined out of 5
Keywords: esp32, arduino, microcontroller, programming, esp32 programming, esp32 cam, esp32 projects, esp32 tutorial, esp32 bluetooth, esp32 web server, esp32 wifi, esp32 arduino, esp32 vs arduino, programming for beginners, programming in c, programming knowledge, programming language, programming fundamentals, arduino uno, arduino uno projects
Id: XLQa1sX9KIk
Channel Id: undefined
Length: 22min 52sec (1372 seconds)
Published: Tue Mar 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.