Debugging an Arduino Sketch Running on an Uno

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everybody and welcome to the arduino craft corner in today's episode i will tell you how to debug an arduino sketch that runs on an arduino uno or on a tiny using only open source hard end software and spending not more than 10 euros so how are we going to accomplish that so this is probably something that happened to you before as well you had this brilliant idea for this project and then you came up with an arduino sketch like this one here and when you are done with coding you press upload now the thing is uploaded to the arduino board and now you are going to test it so uh it is supposed to cycle through different uh stages with different blinking patterns when you press a button let's see whether this works well you press nothing happens again you press nothing happens uh so well we have to to find the box in this program and how do we do that often you just insert print statements and then you see how the program proceeds but there's a much more convenient way to do that and that is using a debugger the arduino ide does not offer such a thing but there exists this popular open source debugger gdb the new project debugger okay usually what you do when you debug a program is that you have a debugger gdb for example and then this is used in order to debug the program that runs on the same machine as the debugger so you start the debugger and then you control the program and do things if we are talking about embedded systems then things are a bit more complicated so there you have a target system that is different from the host where the debugger runs so now first of all the debugger has to know the architecture of the target system and then well it has to control the sketch that runs on the target system it's not on the host anymore so how is that accomplished well gdb has means to do remote debugging that is debugging on a remote computer and it uses the remote serial protocol for that purpose so what about the target system how does that support remote debugging well first of all almost all microcontrollers these days have something which is called ocd it's an on-ship debugging hardware and that allows external components to examine the internal state of the mcu and control the execution in case of the arduino uno and they are tinies there a debug wire protocol can be used to control the ocd now the debug wire protocol is something completely different from the remote serial protocol and here we have a missing link and that is filled by the hardware debugger which is just an arduino uno and well we have the firmware dw link running on it so how do we get there well first of all we have to install a debugging environment and that could mean that we download and install platform io or alternatively we just could install avr gdb and then we have to do some mods on the configuration files of the arduino ide okay that's the first step second step is um well we have to get hold of the firmware and download it and then install it on our hardware and debugger third we then have to set up the hardware which means that well we have to connect the hardware debugger to the target and we also have to do some modifications on the target perhaps well and fourth then we can start to debug and that's all so four easy steps okay let's start with the first step and we will download platform io i will show you how to install platform i o under windows but on the mac os and linux it's basically the same so first of all we have to look for platform io there it is and what we want is we want to download it and first of all we have to download vs code visual studio code so now vs code is installed and what we want to do now is we want to install an extension named the platform io here it is and we want to install it and now platform io is installed as you can see here this and i can that's the one where you get access to all the different functions in platform io so in particular you can open the home window and what we will do is we will install the platform admiral rvr [Music] that's the one we want to install okay so now we can look at projects examples and what we want is we want this arduino blink example and here we are so now we have our first project imported and that's the arduino blink project um the source code can be found under the source folder and this is just the same same as we know from the arduino well there is a slight difference we have this statement include arduino.h in here because well we want to explicitly add all the arduino stuff okay and then there is platform io ini this is a file where all the different parameters are set and i don't want to get into it now when we uh again select now the end symbol here and well let's look at the uno board here we have now different options like building the um the binary code so now let's upload the code and again we are successful okay and with that we will stop here i believe when you work first with this kind of ide then you are probably in trouble but there are very nice tutorials on the web where you can learn about the basic things you have to know so now the second step downloading the firmware so i show you how to do that under linux but it is the same on the mac oso windows again we have to start a browser and now we look for github that is the platform where you find the firmware so github and now on github we search for dw link and there it is already and we download the latest release as source code it's the zip file we save it [Music] and that's basically it now we only have to move it somewhere where the arduino ide can find it and so let's look into our downloads all folder there it is and we need another window um the folder for the sketches which we find here okay that's where we want to move it and now we have to extract it good so we don't need this zip file anymore and what is now the only thing to to do is now um uploading it to our arduino board okay so we start the we start the ide okay now let's load the firmware we have it here and we can upload it now to the board and now we are done and can go to the third step okay the third step setting up the hardware and before we do that i have to make a warning so the warning has something to do with the reset line of your target this line is used as the communication line with the hardware debugger and it is a bi-directional open drain line so it needs a pull-up resistor and that should have around 10k now if it doesn't have such a resistor the communication doesn't work if it does have a too high resistant then it doesn't work and so on and well it also doesn't work if you have a capacitor that is either well connected to ground or to vcc or to some other to some other external device because then well we don't get any signals across this line and if we don't get the signals across the line then it can happen that you can move your target into debug wire mode but you don't get it back to normal mode okay so the arduino uno for example they have such a capacitive load on on the reset line in order to implement the um auto reset feature and that is something you have to remove before you start to debug the arduino uno i will show you how to do that you all know the arduino auto reset feature which is responsible for resetting the um the atmega when we want to upload a new sketch and that is something we cannot have when we want to debug the chip using debug wire so what we have to do is we have to disable this feature and that can be accomplished by opening this solder bridge here so you take a sharp knife and then what you do is you go here and then you cut the existing bridge with your knife and after you have done it you can try that out by trying to upload a sketch again and if you get an error message then you were successful now you may ask can i revert to the earlier stage yes you can in by putting some solder on here but i keep one of the uno boards disconnected at that point here so that i can use it for debugging so we are now ready to connect the debugger with the target and remember the target that's the board where you have cut solder bridge so how do you connect these two well before we start connecting i show you two optional components but they are very helpful so one is an led with the plus lead going into seven and the minus lead going into six whereby you should put a serious resistor in there and this is then the system that that tells you about the internal state of the debugger it's optional but it's very helpful another very helpful thing is a capacitor which goes between reset and g and d and well g and d is then of course the minus lead and reset is the plus lead if you have put such a risk capacitor here then you don't get the auto reset and well that means that the debugger starts a bit faster it's also an optional thing but it's helpful so now let's um connect the debugger to the target and well we start with connecting g and d and the next thing then we will do is we will connect the spi lines and that is sck mozi and meso and these are the pins 13 12 and 11 and well you connect them one to one from the debugger to the target board so as a next thing we connect the supply of the target board that is the 5 volt pin here and we connect that to the pin 9 of the [Music] debugger and well that means that we source the entire target through this pin well that is sort of at the limit um because well you get maximum 40 milliampere out of here but it's okay and it helps you in order to to start up the debugging session because now you can power cycle the board here and that is essential when you start the debug session so and finally we connect the reset line of the target to pin 8 which is well the debug wire control line and that's basically it and i'll show you how that looks in reality so that is how it looks in reality and that means now we are ready to start debugging you remember that we had imported the example sketch arduino blink to platform io and that's what we want to use as our first program that we are going to debug we have to do some small modifications before that though and that is we have to add one extra file called extra script pi and we have to include a modified platform ioni file and both of these files you can find in the um in the directory where the dw link files are so it's under examples and then it's in pyo files and there you find the two things okay in platform io any we have to specify some additional things and that is here for the debugging we have to say uno because we are debugging uno board and here we have to place the serial port that's easy to find out which one it is so we go to platform io again then devices and then the devices and here we see all the connected devices that's the right one here has been copied now and we can go back to the file platform io any and now we replace this um both of them and now we are almost ready the only thing we have to do now is we have to save this file depending on the operating system that may take different uh characters now it is uh saved we don't have any uh dot here anymore and so we can start debugging so we click on run and debug here and then in the left and then it comes up pile debug and that's why we click on and now it is trans uh compiled and wow here we are now we have stopped at the init function which is an internal arduino function and now we can go through the program so we can look at this kind of icons here we can control uh execution first thing means we we start and run and that means we make a step over a function that means we step into a function out of a function and that means we restart so let's step over in it over init variant and over set up and now we are entering loop so let's let's make a step in and now we are here at digital right if we make another step then the led will go on and indeed it does so we can make another step then we wait a second now it goes off again now we wait a second again and so on and we can also set breakpoints for example here so if we now run we will stop here again now we can say again run and we stop again at that point here okay that should be enough for now what we now want to do is we want to close the session but now remember uh our target is still in debug wire mode we haven't changed that back we have to do that if we want to use it again as a normal mcu and the way to do that is um we go here back then to the project task debug and there we find the custom custom things and under custom we find debug wire disable and that's what we click on and now it says connected so at mega debug wire is disabled so now and the um mcu on the uniboard is back to normal and can be programmed as as usual and i guess there are a lot of other things to explore but that will be done in another episode from the arduino craft corner bye bye
Info
Channel: Arduino Craft Corner
Views: 6,407
Rating: undefined out of 5
Keywords:
Id: 7Pm9_LtXdhM
Channel Id: undefined
Length: 21min 44sec (1304 seconds)
Published: Sat Jan 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.