Arduino Course for Beginners - Open-Source Electronics Platform

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
arduino is an open source electronics platform based on easy to use hardware and software this course from ashish will teach you everything you need to know to start creating your own electronics with arduino and you don't even have to have any hardware to follow along hello everyone thank you for joining this arduino course for beginners this course is created for people who have no or very little prior experience in electronics and programming so in this course i'm going to help you get started with your arduino adventures and by the end of the course we will have some basic knowledge of the most common hardware components such as breadboard resistor led etc and their usage in electronic circuits you will also be able to write arduino programs on your own this course is built in such a way that it would cover things which absolute beginners face while understanding the code written by other creators while creating this course i've kept in my mind what problems i had faced when i started my own journey with arduino and made it like a course which i wished i had watched before starting anything at all if you don't have an arduino and other components with you right now you can still follow along the course and get one later to perform your experiments which you have in your mind so let's get started this section will be a quick overview to practical electronics and some of the most common electronics components that we will be using throughout the course with the arduino you can always revisit this section if you find any term confusing or you need to understand something again so let us start with electricity i'm pretty sure that you all must have heard of this term at least once in your lifetime if not let me tell you that electricity has played a major role in letting you watch this video on your computer or your laptop electricity is a type of energy that can be built up in one place or flow from one place to another when electricity gathers in one place it is known as static electricity and electricity when moves from one place to another is called current electricity have you felt a sudden painful jolt when you brush someone's arm or crackling sounds when you take off your sweaters or have you tried to attract paper bits by a comb all this happens due to static charges that were accumulated at a place believe it or not even the lightning that happens in the sky happens due to static electricity so why does this happen everything in this universe is made up of atoms which are made up of protons and electrons protons are positively charged and electrons are negatively charged in an atom generally the number of protons and electrons are equal hence the electric state of atom is neutral but when some atoms lose their electrons they become positively charged and when some atoms gain electrons they become negatively charged the short learning which i want you to take from this lesson is that similar charges repel and opposite charges attract each other as shown in this diagram static electricity is a very deep topic and has a separate study called as electrostatics which will go beyond the scope of this course the other reason i told you about static electricity is that the static electric charges in our hands may damage your electronic components sometimes so just make sure you keep your stuff on hard and solid surfaces and keep your components back in the anti-static packets which you got along with the components current electricity is the form of electricity which makes all of our electronic gadgets work this form of electricity exists when charges are able to constantly flow as opposed to static electricity where charges gather and remain at rest current electricity is dynamic charges are always on the move we will be focusing on this form of electricity throughout the rest of our course so in the next video we will talk about voltage which is one of the most important concepts for the existence of this current electricity so i will see you in the next video in this video we will learn about voltage voltage is a force that makes electrons move through a wire it is measured in volts and the symbol is v okay voltage is also called as electromotive force or emf which drives the electrons inside a wire voltage was named after a scientist called alessandro volta so now technically speaking the voltage is a difference in electric potential between two points for example the difference between the positive and the negative terminal of a battery let's say this battery is of 5 volts so the potential at this point can be 5 volts and the potential at this point can be 0 volts so the potential difference is 5 volts okay it can also be 10 volts here and 5 volts here resulting into a difference of 5 volts it can also be 0 here and minus 5 here then also the difference will be 5 volt so this battery provides some force on electrons so that they can move inside a wire like this you can see these electrons are moving inside a wire and and this bulb is glowing now let us understand voltage by a water analogy this is a water tank and the pressure at the end of the hose can represent voltage the water in the tank represents charge more the water in the tank the higher is the charge and more is the pressure at the end of the hose we can think of this tank as a battery a place where we can store certain amount of energy and then release it if we drain our tank a certain amount the pressure created at the end of the hose also goes down right what i mean to say is we can think of this as a decrease in voltage like when the flashlights which use batteries glow dimmer as the batteries run down and then there is also a decrease in amount of water that will flow through this hose less pressure means less water is flowing so we can consider this flowing water as current which we will discuss in the next video in this video we will learn about current on a very basic note the rate of flow of charge is known as current when electrons move from a negative terminal to positive terminal of a battery they give rise to current the unit of current is amperes and the direction in which the current flows is is the opposite to the direction of electrons what that means is if electrons are moving in this direction then the current will move in this direction all right so now on a more technical note current is the amount of electric charge that flows when one coulomb of charge moves past somewhere in one second and that current is one ampere coulomb is the measuring unit of charge all right and to measure the current at any point we can use an ammeter we can also understand the flow of current by water analogy let's say this is a tank filled with water and this water is flowing due to potential energy of water which we learned is called as voltage in the previous video so here the size of hose is acting as resistance a bigger size hose offers less resistance and the smaller size hose offers more resistance so the path which has lower resistance more current can flow through that path and the path which offers more resistance less current can flow through that path okay so here since the voltage is equal and resistance is lower in this case the water flowing here will be more and the water flowing here will be less okay so this has given rise to another term called as resistance and we will learn about resistance in next video but before that i would like to tell you that there are two types of current one is direct current and one is alternating current in dc current the flow of electrons stay in the same direction and the voltage stays steady with the time as you can see from the graph while in ac the flow of electrons change and the voltage does not stay steady with time that also you can see from the graph don't worry if you could not understand all this at this moment at this point you only need to understand that the current which you see in your home that is the current which drives your fans and other appliances like air conditioners televisions etc is known as ac current and the current which you get from the batteries or specific power supplies is dc or direct current the current you get from your laptop or computer's usb port is also dc current and for our course we will be dealing with the dc current only if you have understood just this part you are good to go so i will see you in the next video now we will take a look at resistance the electrical resistance of any component is a measure of difficulty of passing an electric current through that substance with more resistance in a circuit less electricity will flow through the circuit based on this term resistance there is an electrical component known as a resistor a resistor is a device which limits the flow of a current in a circuit we may need resistor inner circuits where we need to drive leds which require limited amount of current so that we can prevent it from burning off right now if we see the resistance with the water analogy we can see this is a pipe and this is one another pipe the water can flow easily through this pipe because it offers less resistance the flowing water can be synonymous to flowing current okay and when there is more resistance less current will pass through it one important thing which you would like to know is that the current always takes the least resistive path for example if you offer two parts for current to flow it will try to flow from the least resistive path out of them okay so in the next video we will study the relation between current voltage and resistance and in upcoming videos we will also learn how to identify the value of the resistor the value of the resistor is measured in ohms more the value of resistor more the resistance it will offer in the circuit so now i will see you in the next video so till now i'm sure that you all must have inferred that somehow all these terms are interconnected if you have then you are absolutely correct there is a law known as ohm's law which tells the correlation of these terms a german physicist george simon ohm stated that the electric current flowing through a metallic wire is directly proportional to the potential difference across it provided temperature remains the same that means like i said earlier more the force more the current so potential difference is the force here which we talked about earlier so mathematically we get voltage v is equal to the product of resistance r and current a or i for that matter [Music] if any one of them is unknown we can find out by doing the math there's an easy way to find that out there is a triangle known as ohm's law triangle [Music] we just hide the unknown quantity and we get the answer for example if we hide v so i and r looks in a multiplication position so v is equal to i multiplied by r if we have to find resistance we will cover up r then v and i appear in a division like position so r is equal to v divided by i then we have i as unknown quantity then we can do i is equal to v by r based on the similar reason this interesting image shows that if we consider voltage current and resistance as people how would they behave the voltage would keep pushing the current and resistance would create difficulty for it to pass isn't it funny so now let's see how do we use ohm's law to calculate the unknown values in a circuit this is a circuit and this is a very basic circuit right and here is the resistance of value 600 ohms and this is the circuit representation of a resistance which is a zigzag line right here we have a battery which measures 12 volts and this is the connecting wire so can you figure out how much current is flowing in the circuit right now we will use ohm's law to find out okay so here what quantity is unknown i which is current right so we will be using i is equal to v by r so we'll do 12 upon 600 which would be 0.02 amperes okay so now if we change the value of our resistance and make it 6 ohms instead of 600 so can you tell me the new value we just need to do 12 by 6 now which would turn out to be 2 amperes all right so did you observe that if we reduce the resistance in a circuit the current flowing in the circuit increased earlier it was 0.02 amperes and now it is 2 amperes right now if we change the voltage of the battery as well let's say 24 so now the current flowing will be 4 amperes right so we increase the voltage and reduce the resistance so the overall current flowing in the circuit was 4 amperes so i guess you must be able to see the relation between voltage resistance and the current flowing in the circuit one thing you need to learn about using resistances in a circuit is that there are multiple ways we can use resistances like we can use them in series we can use them in parallel so now i will show you how to use resistances in series and parallel if i give you a general idea about resistances in series in parallel i would say in a series circuit the output current of the first resistor flows into the second resistor and into the third resistor therefore the current is the same in each resistor while in parallel all the resistors are connected to each other in a manner shown here right all the ends are connected to each other like this so now we will look at series circuit in a bit more detail so if we look at the circuit here we can see that three resistances r1 r2 and r3 are connected in series across a battery of a certain voltage okay we don't know what voltage is that right now but let's say that voltage is v right so now since there is just one path for current to flow in this circuit the value of current flowing in the circuit would be same let's say it would be equal to i okay so due to that in a series circuit the voltage drop across each resistor would be different and interestingly the sum of v1 v2 and v3 that is the voltage across each resistor will be equal to the voltage of the battery right let's say if the voltage of the battery is 12 volts okay so the voltages which we will get across these resistances would add up to 12 and from this circuit we can create an equivalent circuit like this where where the equivalent resistance is the algebraic sum of all the resistances which we have used so let us now see an example where you can apply this learning right so here we have a circuit with five resistances connected in series and which are connected across a battery which measures 9 volts so what we need to do is we need to find the equivalent resistance the current flowing in this circuit and individual voltages across each resistance right let's have our diagram handy with ourself which we use to remember the ohm's law so let us first calculate the equivalent resistance as i told you to calculate the equivalent resistance in a series circuit we just take the algebraic sum of all the resistances right so here it will be 20 plus 20 plus 20 plus 20 and plus 10 which is equal to 90 ohms right now since we have this equivalent resistance to calculate the current flowing in the circuit we would use this formula i is equal to v by r right the only difference is instead of using r we will use r equivalent because we will be using the equivalent resistance in the circuit right that would be 9 upon 90 which is 0.1 amperes so now we know that 0.1 amperes is flowing in this circuit so to calculate individual voltages across the resistances what we can do we'll use the ohm's law once again but this time we will use this formula we need to calculate v is equal to i into r so i is 0.1 and r is 20. so 0.1 into 20 is 2 volts okay similarly this will also measure 2 volts this will also measure 2 volts this will also measure 2 volts and this one will be 1 volt because 0.1 into 10 is 1 right so now you notice that 2 plus 2 plus 2 plus 2 and plus 1 is equal to 9 right as we saw earlier as well all the voltages also add up to the voltage of the battery which is 9 volt all right so this is about series circuit now we will look at the parallel circuits so the resistors are in parallel when one end of all the resistors are connected by a continuous wire of negligible resistance and similarly other ends are also connected to each other through a similar wire since all the resistances in parallel are connected directly to the battery you can say the voltage across all of them is equal right but unlike the resistors in series where there is just one path for current to flow in parallel circuits there are multiple paths for resistance to flow like in the circuit you can see current has two parts if the current flowing in the circuit is i then it will divide into two parts let's say i1 and i2 if there were more resistances we would say like i3 and i4 etc and when that current flows it comes back and it adds up and becomes i again and if we wish to calculate the values of i1 and i2 again we can use the ohm's law here right because the voltage across this resistors is equal to the battery's voltage and the value of resistor is also known so we can calculate i using ohm's law right you should remember that v1 and v2 will always be equal to v and i will be equal to i1 plus i2 right now we can also calculate the equivalent resistance this combination of resistors will offer in the circuit and to do that there is a very simple formula one upon req that is equivalent resistance is equal to 1 by r1 plus 1 by r2 plus 1 by r3 and so on right so let us see one example and let us keep that diagram handy again right so what we need to do in this question is we need to find out the current flowing in this circuit and the individual current flowing in these resistances right so one approach can be we can find out the equivalent resistance first using the formula we just saw let us place the values inside it right 1 by req is equal to 1 by 1 plus 1 by 2 plus 1 by 2. we have just replaced these values by the values of the resistors right so now we will take lcm and which turns out to be 4 by 2 but please note that this is 1 by r equivalent all right so to calculate r equivalent we will take the reciprocal of this value which would turn out to be 0.5 ohms right so now we have the equivalent resistance of this circuit we can simply use the formula i is equal to v by r v is 3 and r is 0.5 so the answer would be 6 amperes right so 6 amperes will be flowing in this circuit now if we want to calculate the individual currents in these resistances what we will do is we will use this formula again because v is same and r is known right so in this resistor v is three and r is one so in this resistor three amperes will be flowing in this resistor three by two that means one point five would be flowing right and in this resistance as well 1.5 amperes would be flowing so now if we add up all these currents in these three resistances that would come out to be six so again we verified the things we just studied and i don't know if you noticed or not the equivalent resistance of 0.5 was even lesser than the lowest of the resistance is used right so this will always be the case the equivalent resistances in the parallel circuits will always come out to be lesser than the lowest value of the resistance in those combinations all right so this was about parallel circuits and now we will see a slightly more complicated circuit which involves series and parallel circuits both so here we have such a circuit where resistances are present in series as well and panel as well so now i will tell you how to find out unknown values in such kind of circuits right so first of all we need to work in sections of the circuit for example in this circuit i have taken up this section we can see that these two resistances r3 and r4 are in series right so using our regular resistances in series formula which is r equivalent is equal to r1 plus r2 plus r3 and so on right so here are two resistances so we have just taken some of those two resistances which is 10 ohm then in our mind we will think of it as one single resistor right which is in parallel with this resistance right so now we will use our parallel formula which is 1 by r equivalent is equal to 1 by r1 plus 1 by r2 right so both of the resistances are of 10 ohms so if you plug the values in this formula you will get 5 ohms you can try it on your own right then we will reduce that circuit in our mind only with 5 ohms as a resistance then this equivalent resistance is in series with 7 ohm resistor right again we will use our resistance in series formula so the equivalent resistance will turn out to be 12 ohms so now let's have our ohm's law formula handy right so what we can find out now is how much current is flowing in this circuit how we can do that we will use this formula i is equal to v by r where v is 24 and r is 12 so we will get 2 amperes right so now if we want to know voltages across each resistance and current flowing in each resistance we can work our way backwards now right so we know two amperes is flowing in this circuit so we can calculate the voltages across these two resistances right so first we will calculate across 7 ohm resistor so how will we do that we'll use v is equal to i into r i is 2 and r is 7 so we will get 14 volts across this okay and here we will get 2 into 5 10 volts right so you can see 14 plus 10 is 24 we are satisfying the things which we have studied so far now we'll move one step back so we knew voltage across this resistance was 10 volts right so in this section also the voltage will be 10 volts so now since this is a parallel circuit in both of the resistances voltage will be 10 volts so now we can calculate current in this right by using this formula again right v is 10 and r is also 10 so 1 ampere would be flowing inside it the answer would be same for this one as well right now if we take one step back again we'll have to calculate voltage across these two resistances how will we do that again we'll use this formula v is equal to i into r so current was 1 ampere in this so in this as well it will be 1 ampere so we know 6 into 1 is 6 volts and across this one we'll have 4 volts using the same formula right so we have all the unknown values right now since there were so many values i can understand it must be a little difficult for some of you to visualize it so i have created a circuit to help you visualize a little right let's have a look at it so here is that visualization so you can see exactly similar circuit which we saw earlier and i have written all the values of current flowing in each resistance and now i will show you the voltages across each resistance let's keep this formula handy as always now i will show you voltage across each resistance it is 14 volts across 7 ohms 6 volts across 6 ohms 4 volts across 4 ohms and 10 volts across 10 ohms right so if you have understood this part i can say you have understood really important fundamental about using resistances in series and parallel but you may also find circuits like this okay where we have two batteries right in circuits like these you cannot simplify the circuits which we have seen so far so there are some advanced concepts such as kirchhoff's law okay which is used to calculate the unknown values in the circuit but that will go beyond the scope of our course right now so one last thing about resistances i want to tell you is identifying the resistance is based on the color bands let's have a look at that now resistances come with the four or five color bands denoting the value of resistance it will offer in the circuit so you see in this diagram there are four band resistors and there are five band resistors okay in a four band register the first band will tell you the first digit the second band will tell you the second digit the third band will tell you the number of zeros which you need to put after the first two digits and the fourth band tells the error percentage means it can be that much percentage above or below the stated value now see the first band in this case is yellow so it the first digit will be four the second band is violet so the second digit will be seven the third band is origin color so the multiplier will be one thousand so we get the value of forty seven thousand and the fourth band is gold so that means it will have a tolerance of plus minus five percent so the net value of this resistance is 47 000 ohms or you can say 47 kilo ohms with the tolerance of five percent now we see the five band code okay the first band tells the first digit the second man tells you the second digit the third bank tells you the third digit fourth band tells you the multiplier here and first bank tells you the tolerance we'll consider this example first band is orange and color so the first digit will be three second band is green so the value will be five the third band is black in color so the third value will be zero and fourth band is red so the multiplier will be hundred till now we get three five zero and double zero so this is 35 000 and the last band is brown in color so one percent of tolerance so the net answer will be 35 kilo ohms with a tolerance of one percent you can refer to this chart whenever you need to find the value of your resistances and i understand this might be confusing at first but with practice you can understand this very easily in this section we will finally take a look at the main thing most of you must be already waiting for this section to come up so this blue colored thing which is present in your kit is the arduino board i will tell you what is it what it can do and how to start using it finally but before that i would like to tell you about some terms which you may or may not have heard about before starting this course so these terms are microprocessor microcontroller memory integrated circuits etc microprocessors are known as a brain of any computer they consist of a cpu which stands for central processing unit which is responsible for doing all the computations and calculations which we need to carry out they need additional peripherals like ram and rom in order to function example of a microprocessor is the cpu which is being used in your computer right now if you go and check the history of microprocessors you will see the microprocessors have come a long way after so many improvements in developments microprocessors and graphic processors are used where there is a need of heavy computational cycles like artificial intelligence deep learning self-driving cars and high-end robots our daily applications like playing games movies browsing internet also requires our microprocessors and then there are microcontrollers microcontrollers are integrated chips that are basically tiny computers but are powerful enough to do a lot many tasks like controlling motors pumps other appliances or they can send some sensor data to internet cloud control basic displays and even act as brains of some intermediate level robots applications are endless trust me the power requirements of such integrated chips is very low and you can even run these chips on batteries for days or even months they have all the peripherals like memory and flash storage built on the same chip [Music] if we compare microcontroller and microprocessor in terms of cost a microcontroller is far cheaper than a microprocessor however microcontroller cannot be used in place of a microprocessor and using a microprocessor is not advised in place of a microcontroller as it makes the application quite costly if we compare them in terms of speed if we say human brain is fast then microcontroller is faster and microprocessor is fastest but please note we are talking in a very general way this won't be applicable if you compare an old generation thing with a new generation thing as technology advances it won't make any sense to compare and also some exceptions are always there it is really possible that you or any human may be faster [Music] so do you have any guesses in which category or arduino board will fall if you said microcontroller then you are partially right an absolutely correct answer is that arduino is a circuit board which has a microcontroller on it which act as the heart of that board the arduino board given to you in your kit is arduino uno which uses microcontroller atmega328 there are many more kinds of arduino boards available with other microcontrollers which may be superior or inferior to atmega328 but arduino uno is just right for our applications you may want to pause the video here to have a look at all these boards and admire how beautifully they are made so arduino is basically a company in italy which made it really easy for people to use microcontrollers for their application the hardware and software of arduino is open source which means one can have access to the files and even modify them for their own work since it is open source many other companies take their design and manufacture their own versions of arduino so it is really easy to clone and modify the arduino board [Music] what makes arduino a beginner-friendly tool is their easy to use hardware and software it can be used even by people having no technical background earlier people would have to use very difficult assembly language to control their votes but it is not the case with arduino as you progress with the course you will see yourself with how easy and intuitive this tool is you will see that uploading the code is as simple as plugging a usb cable and clicking upload button in this video we will see the parts which make up the arduino board so this is the diagram of arduino board which shows the typical components which are placed on it so we will begin by the digital pins the pins which you see here which are numbered 0 to 13 and some pins have a special symbol like this curvy symbol on in front of it which denotes pwm capability that means these pins support pwm other than that these pins take digital input from sensors and give digital output to some of the components all right so then we move to the built-in led which is shown by this led this led by default is connected to pin number 13. so whenever pin number 13 goes high this led will turn on and when that pin goes low this led will turn off and then we will see the power led this is the power led if this led is turned on that means your arduino is plugged in that means it's turned on right now then we have the main component the atmega328 chip it controls all the main functions of this board whatever is happening on this board is due to this ship then we have analog pins we have six analog pins on this board numbered from a0 to a5 we can connect any analog sensor on these pins then we have the power pins these are the power pins you can power any component using these pins but make sure that you don't your components are not drawing a lot of current basic sensors and basic you know breadboard circuits are fine with these pins then we see the power dc jack if you want to connect your arduino externally through a dc jack you can connect it through this jack the voltage input it can accept is 7 volts to 12 volts then we have tx and rx leds these are the tx and rx leds rx means receive tx means send transmit okay so whenever these leds are blinking that means cardinal board is either communicating with the computer or some other another arduino board then we have the usb jack you connect your arduino board with your computer using this jack then we have the reset button this button reloads the entire code which is running on the arduino board but please note that this button does not clears the memory of arduino here are few technical specifications of arduino uno by technical specifications i mean the information which you look while buying any item or a computer like in a computer you see how much disk space is there how much ram does it have or processor it has similarly arduino being a microcontroller board has some technical specifications like it has atmega328 as the main chip which does all the work it operates at 5 volts the input voltage which it can accept is 5 to 12 volts although according to the data sheet of arduino the recommended voltage is 7 to 12 volts and the limits are 6 to 20 volts so i recommend that 5 to 12 volts is a good enough voltage to operate arduino with and then there are 14 digital pins out of which six are pwm capable don't worry about pwm right now but just think having pwm capability is a good thing then there are six analog pins which are used to connect analog sensors to the arduino you can understand that these pins are connected to arduino through a 10 bit adc an adc is an analog to digital converter we'll talk about adc in further lectures the i o pins are capable of supplying 40 milliamperes of current so these pins cannot drive large appliances or the things which require more current like motors and it is not even recommended that you run motors through arduino but few leds and most sensors which we use in this course do not draw very large amount of current so these pins are just fine there is a 3.3 volt pin as well and the maximum current you can draw from it is 50 milliamperes and there is a flash memory of 32 kb which we use to store all the programs into it 32 kb must be seeming like a very less amount but trust me you can store lines of 1000 even 2000 lines of code in this and it won't even exceed 10 percent of the storage then it have sram of 2kb which is a volatile memory or more generally it is similar to ram on your computer again it would seem too less and i would repeat what i said for the flash memory that it is more than enough it also has 1 kb of eep rom which can store some variables even after you switch off the arduino clock speed is 16 megahertz clock speed is the speed that microprocessor executes each instruction one megahertz representing one million cycles per second i understand a lot of information in this video might have just bounced over your head and it's perfectly okay if you don't understand some portion of it i would just say don't lose your motivation and just stick with us and i promise you you will understand each and every part of it by the end of the course if not we are always here for help in this section i will tell you about arduino ide but before we jump into that first of all we need to understand what an ide is a text editor like a notepad along with the compiler is a bare minimum requirement to start coding but wait what is a compiler compiler is a special program that processes statements written in a programming language and turns them into a machine language which are basically zeros and ones which a computer can understand but unlike text editor an ide which stands for integrated development environment is a software tool which has a lot more functionality to make programming easier for the user it has a built-in text editor and compiler along with easy to use tools menus and buttons to compile run debug analyze and save the programs considering arduino they also have one official ide which has buttons to verify and upload the sketches they also have easy to use menus to select boards and ports throughout the course we will be using the official arduino ide and for that in the following sections we will be downloading and installing the official arduino ide now i will tell you how to install the official ide for that go to any browser and go to arduino dot cc then click on software tab you will find many options here we will go with the windows installer click on just download if you don't wish to donate anything donations like these help open source communities to fund themselves once the download completes we will click on it and when the installation starts click on i agree make sure that all these boxes are checked these are the essential drivers which are required for your computer to detect and communicate with the arduino board right we'll click on next then we will choose the destination folder i will go with the default one and click on install this process may take some time let's wait for it to finish in my screen it says completed but some people may get additional prompts to install those additional drivers all you need to do is just click on install on them as well and then once it is completed for you click on close and then you will find the arduino icon on your desktop then you just double click on it and here is your fully functional arduino ide window so now we will set up our computer for uploading our first basic sketch there is nothing complicated we need to do the first step is we need to plug in an arduino board with the usb cable given in the box and once you do that your computer will automatically detect that arduino has been plugged in and it will list it out on a specific serial port in windows it is known as a com port which you need to go and select using tools go to tools and then go to port and you will automatically see a com port here it is very likely that you see com3 if you have plugged your arduino for the very first time okay so you will see arduino genuine uno written over here if you're using mac then you will see something like this forward slash dev dot forward slash cu dot usb modem this is the port number and you will see arduino uno here instead of adding omega i have tested with arduino mega that's why it is showing it like that but you will see arduino uno right if you're using linux you will see serial port as power slash dev tty acm0 it is most likely will find it at at acm 0. after this step we are just ready to upload our first basic sketch so finally we will be testing our arduino boards and for that we will be using the built-in example sketches so in order to upload our first sketch we'll go to examples we'll go to basics and then we'll select the blink sketch this is the first sketch which will be uploading on our arduino board we'll go to tools we'll select our board as arduino uno and we'll select our port as com3 and then we will go to upload button we'll click on upload in order to upload the code there are more buttons like verify save open we will talk about these buttons in upcoming sections but now we'll click on upload it will first compile the sketch and then it will upload the code to the board after uploading you will see done uploading will appear in the messages done uploading right and you will see that led is blinking with this gap of one second is it blinking right if it is blinking then congratulations you have done your first example at this point the viewers without an arduino must be feeling a little stuck but please don't worry i've got you covered all you need to do is use an online simulator like autodesk tinkercad and for that you need to go to the website of tinkercad right this one and you need to create an account if you don't have it already you can choose the option relevant to you make an account and after creating your account you will see a screen like this all you need to do is click on circuits and create a new circuit at this window we need to search arduino uno r3 and just drag it over here right so now you have your virtual arduino board it will just function exactly like the actual arduino board and whatever code i write in official id in the following videos you need to write that into this window right so you will have this window over here whatever code i am writing in the official id you just need to write that in this area okay so just as you saw few moments ago that we tested our arduino board so we will now test our arduino board over here right so this is the same code which we wrote over there only the comments are not here right so this is exactly the same code and what we need to do after that is start simulation okay so it will be plugged in and you know we can see the outcome of our code over here and in some portions of the video you will also see that i am printing out the outputs of some code on the serial monitor so the serial monitor in this simulator is this right so you will see whatever outputs i'm showing in the videos you can see them here okay so don't worry if you don't have an arduino at this point you can still follow along the video so before we move forward from this point having the knowledge of a few more things would be greatly beneficial things such as breadboard how to make connections in breadboard some safety instructions for the arduino and some basic terms like input output sensors analog digital bit and byte you may want to revisit this section as well if you find yourself forgetting about these concepts as we proceed all right so let's start with breadboard is this the first thought which came to your mind when you heard of breadboard well that's a perfectly genuine thought when i stepped in the field of electronics i myself had this thought but when i learned that the breadboard in electronics is a completely different thing i had a really big face pump moment let's discuss this in the comment section as to why it is called as breadboard or from where it got its name we will just talk about breadboard in terms of electronics breadboards are meant to make quick non-permanent connections between electronic components and they are covered with tiny socket holes which are connected in rows the breadboard itself is broken into four sections there are two inner sections full of short horizontal rows and two outer sections with long vertical rows if we remove the adhesive which is present on the back side of the breadboard we would see something like this a series of metal connections do you see that we'll discuss what this means in a few moments but before that i would like to show you the strip which is connected inside that it is a single strip which is taken out from that bright board and it would look like that so you see this entire metal piece so when we make connections on that you know it is internally connected automatically coming back to this if we consider this row corresponds to this all right so that means these points are connected among themselves automatically and like these rails are connected so these points are also connected from inside we'll discuss this in detail we'll also make circuits don't worry about that if you're wondering what kind of circuits which we can make on a breadboard i would like to tell you that you can also make your arduino uno on the breadboard and it would look like this now we will see how to use a breadboard we will also understand the connections in details all right so this is our breadboard okay can you see all these holes like this which are getting highlighted in blue right these are the holes which i was talking about in the previous video okay so i'll now show you how they are interconnected for example these points are interconnected the yellow ones all of these are interconnected within themselves right like these all these points are connected among themselves but please note that this point is not connected to this point internally this point is only connected to these points like can you see right like this all these points are interconnected all right so then we'll see these points these are the external points all of these points are interconnected like this right like this and like this in some breadboards there are connection break at this point so you will just see that the points are connected only till here and these points are connected till here but this point is not connected to this point in some breadboards but in most breadboards they are all connected like this can you see that okay so now i'll tell you if you want to connect some two points for example we'll take a wire and we'll join this so now these points are connected among themselves right no matter how many wires we join here these will still stay connected okay and what if i connect a wire here then all these points will get connected you see that right i think it is really interesting to see that how easy it makes for us to make connections on the breadboard right so now we'll see how to make a series connection right we'll take a resistance like this okay and we'll place it here okay now i will take another resistance and connect it here okay so now these resistances are in series now if i make want to make another resistance which i want to take in series i'll place them here all right now these three resistance are connected in series how for example these points were interconnected okay and so we connected our resistance here so that means they are in a continuous position now similarly here as well okay i would like to tell you that some people make a mistake and to make a series connection they would go like this they will place the resistance here instead of placing it here right this is a series connection but this is no connection at all because these points are not interconnected however if we join a wire like this okay so now these points are interconnected okay i hope i am making you myself clear now we will try to understand a parallel connection similarly we'll take resistance and we'll place it here now since in parallel connections all of the points are you know originating and ending at the same point and these points are interconnected so we'll take another resistance and place it here so these two resistance are in parallel now if you want to take a third resistance as well and we'll take it and place it here now these three resistances are in parallel okay now we want to make a connection in which there is a resistance in series and then there are two resistances in parallel we'll try to make that circuit this is one resistance okay and then if we make it like this like this okay and we take another resistance and we place it like this okay so what that is representing it is one resistance which is in series with this resistances which are in parallel among themselves and then there's another resistance which is in series with that okay okay it's all right if you don't understand what i'm trying to say right now we'll understand this with one more example so before i begin with my second example i would like to tell you a few more things like i told you in the previous video if we connect a wire like this the points become interconnected but if you take a resistance and connect it like this [Music] so these points are not connected to these points right because it is a resistance you see that in this case these points are interconnected but in the case of resistance these points are not interconnected now another point which i want you to notice every connected wire like this it won't make any sense as these points are already interconnected right so this wire will not make any difference so now another thing which i want you to notice if we take a resistance and if we place it like [Music] this like this then also it won't make any difference because as we all know the current always takes the least resistive path so connecting a resistance like this won't make any difference because the current will travel through the rails only this resistance will not add any value to the circuit okay but however if you connect a resistance like this then it would make some sense okay how it will make some sense we will see [Music] for example we can connect it like this and like this okay so this is a series connection now these two resistances are in series but yeah please note that these resistances [Music] are not in parallel [Music] but please note that these resistances are not in parallel because these points are not connected okay now we will begin with our second example in this example we will just connect an led with a push button and a battery okay so when we press the push button that led will blow so we will make a circuit on that so for that we'll take a push button circuit first of all okay this is a push button circuit i'll tell you about push button circuit that internally this terminal is connected to this terminal okay so automatically this rail will get connected to this rail okay like this and this terminal is connected to this terminal so these points will also get interconnected okay so when you press this button this terminal gets connected to this terminal okay so these points will get connected when you press the switch all right then we will take a battery let's take this battery this is a simple battery 3.7 volts let's place it here this has a positive terminal and a negative terminal we'll try to connect it on a rail let's make this wire as red color [Music] and the black wire as the black one okay in general electronics this is also a convention that the red wire represents positive terminal and the black wire represents the negative terminal okay so now these and this entire rail okay so now this entire rail is positive okay as we have connected the positive terminal of the battery here just see that and this terminal is negative this entire rail is negative okay so we will start by placing a resistor as we should always connect our resistance in series when using a led replace it like this okay so that means this point and this point are interconnected so this resistance is in series with this switch and then we'll take one led this is the led okay as we all know there is a positive end and the negative end of the led so this is the positive end and this is the negative end we have connected the negative end to the resistance okay to make the led glow we have to always connect the positive end of the led to the positive end of the battery okay so first of all what we'll do is we'll take a positive wire from here right as we know all know this entire rail is positive so from any point on this rail we can take positive line so we'll take a positive line from here and connect it here this is a general wire so we'll make it blue all right and then what we want is we need to complete the circuit like this we'll take a black wire from here and connect it here okay so this is our circuit completed whenever we press this switch this circuit will get completed the positive wire will go from here the current will flow from positive terminal like this it will flow from resistance and it will go from circuit and then to the negative end and back to the battery but whenever we release the switch the circuit will break and the led will stop glowing this was the very basic example of a led in a breadboard in this video we will discuss about some of the safety instructions which we need to follow while using our arduino board the first thing which you need to keep in your mind is you never have to connect any voltage of over 5 volts on any i o pin okay any i o pin means any input output pin be it the digital pins or the analog pins you just don't have to connect any voltage over 5 volts on those pins you can only use any voltage over 5 volts to power the arduino through v in pin and you cannot connect any pin to a voltage over 5 volts okay next important thing you need to notice you never have to connect a 5 volt pin to a ground pin this way you will short it down an arduino will turn off the next point is you have to take care of the polarity polarity means you cannot connect positive and negative terminals in opposite ways what i mean is you cannot connect a positive pin to a negative pin of some other component and vice versa if there is no polarity management circuit on the device which you are using it will simply damage the device one more point is you have to be careful while using external power in this course for all the project the usb power would be more than enough but still if you need any external power you can use it through dc jack or vn pins and in that case also please make sure you don't connect any voltage above 12 volts although arduino can tolerate up to 20 volts but still it i advise you that don't use above 12 volts otherwise you will see unnecessary heating on the arduino board okay so these were the main safety instructions which you need to follow or you may damage your arduino and while doing our experiments i will also tell you about more safety instructions if they are required okay so now i will see you in the next video in this video i will talk about input and output and how to understand it with respect to arduino you may want to revisit this video when we study pin mode function it has been taught in arduino programming section so generally speaking input is something which goes inside an output is something which comes outside speaking technically input is some data or signal which goes into the system an output is some process information or signal which comes out of that system the system may be anything like in our case the system would mostly be arduino the sensors would be giving the data to the arduino an arduino would act on that data and give an output based on how you have programmed it please bear in mind that input and output is never absolute it is always relative to something as you may have been told in junior classes that input and output devices are fixed yes they are fixed but with the reference to the computer like a mouse is an input device to the computer but if we look in the mouse itself there would be several more devices like an optical reader which takes the movement of the mouse as input and gives its output to the chip inside which processes it and gives the cursor position as output similarly we have been told that a printer is an output device yes it is certainly an output device but with reference to the computer now if we see in a printer as well there is a main controller board which takes the input signal which is coming out from the computer and then that controller takes the decision that how to move the motors which colors to select in order to print the page i hope i am making some sense to you now if we see in reference to our course there are lot many things and sensors which act as input devices for the arduino like these sensors which you can see on the screen but wait what are sensors like humans have five senses which help us get aware of our surroundings and act accordingly similarly in electronics sensors help the controller to take decisions and give output based on the environmental data which is given by them sensors tell something to the controller by changing its voltage a change in voltage is read by the controller like we see in this image there is an autonomous car which has n number of sensors placed on it which are telling it about the other cars battery level fuel level humidity level etc now i will briefly discuss about these sensors which you see on the screen we will also discuss these sensors in detail when we use these particular sensors first of all we see is a rotatory potentiometer this device would change the resistance it offers if we rotate the knob clockwise or anti-clockwise then we have a momentary push button switch when we press the switch it will close the circuit when we release the switch it will open the circuit then we have a force sensing resistor this resistor changes its resistance based on the force applied on it then we have a light dependent resistor this resistance change its resistance based on the light falling on it then we have a triple axis accelerometer usually we see accelerometers in phones and gaming devices okay the basic purpose of the accelerometer is to get the orientation of the object on which it is placed then we have a joystick joystick is nothing but a basic input controller you must have seen it on on the gaming consoles then we have a microphone or a sound sensor it will capture the environmental sound and noises we may want to record them or take some decisions based on that then we have a touch sensor it will just see if someone is touching it or not then we have a rotary encoder this sensor senses the rotation basically for example you can attach this rotary encoder to a motor shaft and we can see the rotational speed of that motor how many rotations it have taken or you must have seen a rotary encoder in our volume knobs in your music systems and we have an ultrasonic distance sensor it uses ultrasonic waves like in a sonar to tell the distance between the sensor and the object in front of it then we have a temperature sensor as the name suggests it will tell the temperature around it we have a temperature and humidity sensor it tells humidity as well along with the temperature then we have an infrared sensor this is basically an obstacle detection sensor it can tell if an object is there in front of it or not then we also have output devices output devices are those devices that can convert the output given by the arduino to some human readable form right for example i'll tell you there is an led which is known as light emitting diode it will basically just glow if any pin on arduino is set to high and we have an rgb led this is a special led it has all the colors uh red green and blue leds and it can form n number of colors and we have a buzzer it will make some sound if high voltage is given to it it will make some sound then we have a loudspeaker like this general speaker which you have seen in your mobiles and your laptops which you know you play music through and we have an oled display it is a full-fledged display you can just click graphics on it we have lcd display this is liquid crystal display you can display characters on it okay for example some value of a sensor or someone's name etc the seven segment display consists of seven leds arranged in a rectangular fashion as shown it is used to display numbers on it for example if we want to display seven so we will highlight this one this one and this one so we will see that seven is appearing if we want to show zero then we will highlight these ones okay and if you want to display eight we will highlight all of them and it will look like it is coming on we have a dot matrix display dot matrix display is an array of multiple leds okay we can show even characters on it you must have seen a dot matrix display in delhi mid to display or even you know scrolling displays you must have seen in front of some shops which display the shop's name so that is a dot matrix display then we have a dc motor it basically moves clockwise or anti-clockwise if it is given some voltage across it then we have servo motor so this is a special kind of motor we can stop it at specific angles then we have a stepper motor a stepper motor is just like a dc motor but you know the entire rotation is divided into equal steps so like servo motor we can also stop it at specific angles and even lift heavier loads we can drive heavier loads using a stepper motor so to conclude this video i would like to say since we know about input and output properly now we can easily understand how arduino interacts with the environment and we can make some meaningful projects after knowing this see you in the next video in this video we will talk about analog and digital so to begin with i would like to tell you that our world is full of analog information like the colors we see fragrances we smell flavors we taste sounds we hear temperature we feel all these have just endless number of values there can be millions of colors smells flavors sounds etc this was in a very general sense now if we see in the terms of electronics something which is analog can have infinite number of values let's say your wall clock it can show endless number of time intervals though they are not marked on it but yet it holds the capability to tell you even smaller fractions of time while digital is something with discrete or limited number of values like a digital clock that clock can show you limited number of digits let's say it's 2 pm or 10 minutes past 2 pm depending on the precision of that clock it may still show you seconds but it is still limited to some values only so before we move further we also need to understand one more term called as signal signals are used to convey some information to someone like these road signs are passing on some information to us like that we need to stop or we cannot park here etc similarly when we speak we generate a signal and that signal contains the information which we want to pass on to the listener like i'm speaking right now i'm generating an audio signal which you can see on this screen this is the audio signal which i have generated with my voice okay so this signal passes through my mouth through air and to your ear and you make some sense out of it okay this audio signal is analog in nature because there are infinite number of values which are changing okay in electronics signals are some time varying quantities which convey some sort of information usually that time varying quantity is voltage that means with time the voltage changes okay or sometimes it is also current but usually it is voltage then those time varying signals are passed between the devices in order to send and receive information which might be video audio or some sort of encoded data usually signals are transmitted through wires but they can also be passed through air wirelessly okay so there is an entire field of study which is based just on signals like digital signal processing signals and systems there are some entire subjects based on signals only okay but for the scope of this course we will just tell you the required information so now there are two types of signals one is analog signal and one is digital signal like we just saw that our sound is a type of analog signal similarly there can be other analog signals as well typically an analog signal would look like this that means we have plotted time on the x-axis and voltage on the y-axis so with time the voltage is increasing then decreasing then going into negative part then increasing this is an analog signal it is not necessary that it stays this smooth it can be little messy as well like this this is also an analog signal okay and then there is a digital signal in digital signals there are only two discrete values either it is on or either it is off where 5 volts represents on and 0 volts represent off so you can see with time here it is on it is off on off on off on and off right now the sensors and the sensors which we use in our course will either give analog signal or either digital signal and we have to use arduino to read those signals okay we will see an example let's say we have a gaming console here this is the joystick this is a device which would give some analog signal and these are the buttons and these devices will give some digital information so this game machine can be programmed to read this state of the joystick for example we can make a character move forward when the joystick is moved forward we can also control the moving speed of that character for example if we make the joystick move a little forward then it will move slow and if we move it a little more ahead then it will move faster but this cannot be the case with the digital button we can just press the switch and tell the character to move forward we cannot decide the speed we can only set one speed there or we can program it to move forward fast based on some time okay but we cannot input the speed through buttons right so this joystick is sending some analog signal into the computer or the controller which is placed inside this gaming machine and these buttons are sending some digital information like either they are pressed or not they are on or off okay but this is a digital signal i'll give you one more example we have potentiometer here and we can move it as a knob and we have infinite number of values here we have a switch which is either on or off this represents analog and this represents digital so whenever we use analog sensor or component we will use analog pins of the arduino and we use digital pins for digital sensors or components okay i have given more insights on this topic in analog read and digital read video in programming section once you reach there and watch those videos as well it will be crystal clear to you and if not you can just comment down below or send us a message in this video we will talk about bit and buy so basically what is bit bit is the smallest storage or memory size which can store a value of either zero or either one this box represents a bit here okay similarly if eight bits are combined then it forms a byte okay so we can say eight bits is equal to one byte so one byte can store zero as the smallest number why because in that case all the eight boxes or all the eight bits are holding zero inside it as i told you it can either store zero or one okay so each box has a multiplier and we take the value from this box and we multiply it with this box okay and calculate the value here and in the end we take sum of all the values so here what will happen 0 multiplied by 2 to the power 0 2 to the power 0 is 1 0 into 0 is 0 0 into this is 0 0 into anything is 0 similarly in all the boxes we'll get 0 so hence the smallest value will be 0 and when will be the maximum value if all the boxes are 1 right so here we will get 1 into 2 to the power 0 that is 1 and 2 to the power 1 is 2 here we'll get 2 here we will get 4 here we will get 8 here it will be 16 here it will be 32 64 and 128 now if you take a sum of all these value you can calculate it on your own we will get 255. now in case if we want to store a value greater than 255 so now in that case we will have to take one more bit aside this so it will become 9 bit so the 9 bits can store 511 values and what if we have to store a bigger number than that then we will add another bit here as well and it will become 10 bits so 10 bits will be able to store a value till 1 0 2 3. so now what you have to keep in mind is you will be needing this information whenever i say 8 bits 10 bits 12 bits in the course so we can only store values up to the upper limit of that space okay so this information will be sufficient for you to carry on the course but yeah if you want to know more about binary math you can just google binary maths and you will get really good links from wikipedia and other websites and you can just read them if you face any problem you can always comment down below and we will get back to you in this section i would cover all the essential programming concepts that you would need to get working and make projects with arduino i would like to tell you that programming is a game of practice the more you practice the more you get better at that so i would cover all the essential topics to get you started and i will also tell you about concepts as we progress ahead so let's get started before we know about arduino programming we need to understand what programming is programming is something which we do to tell a computer or a controller what to do computers don't understand the language which humans use to communicate with each other computers only understand the zeros and ones so by programming we tell it what to do in our language and a compiler or an interpreter converts that into a language which computer understands and the computer finally acts on it so there are many programming languages over there like c c plus plus c sharp python java etc all of them have their own benefits and drawback the language which we use to program the arduino is a mixture of c and c plus plus which arduino makers call as wiring the best part of this arduino language is that it is uniform across all platforms that means it won't matter if you use windows mac or linux the output of the code would be the same so in the next video we will start to look at arduino programming in a bit more detail so let's start by opening up our arduino ide once it opens you would see this window all right and we see void setup and void loop is written over here let's discuss whatever we see on the screen the first line which we see is white setup right it says void then setup then a pair of brackets then a curly bracket then we have two forward slashes then it says put your setup code here to run once and then we have another curly bracket so what we have done here is we have declared a mandatory function known as setup and this is how you declare functions in arduino then we have a curly bracket in most programming languages these curly brackets indicate a block of code this left curly bracket means the start of the block and this right bracket shows the end of the block whatever code which goes inside this block belongs to this function the arduino id would help you recognize the blocks of codes when you click on these brackets for example when you click here it would highlight this bracket automatically that means this is the start of your function and this is the end of your function whenever you click here then it would highlight the ending can you see that all right then we see these two forward slashes these forward slashes mean a single line command whenever we write a comment compiler entirely ignores whatever is written after that so coming back to void setup this mandatory function contains all the lines of code which we want arduino to run once when it is turned on or when we press reset button we'll talk more on this in coming lectures similarly there is void loop in this section we write all the code which we want arduino to run all the time repeatedly that means whatever is written in this section would run until the arduino is switched off the void loop runs after void setup so in the next lecture we would see the blink led example which we used to test our arduino in a little more detail we will understand each part of that code so see you in the next lecture so till now we have understood that the two functions void setup and void loop are the bare minimum requirement to run our code successfully why are they mandatory i will just show you in a moment i am removing these functions and i'm hitting compile the compiler has passed us with errors just read them undefined reference to set up undefined reference to loop that means it is expecting that we must define these functions now i will bring back these functions now i'll hit compile once again now the compiler has successfully compiled the code so with that we can say arduino ids compiler expects these two functions like some other programming languages expect the main function without that we won't be able to run the code we would get an error as you saw so now we will see some more concepts in the blink led experiment so for that we'll go to files examples basics and then blink all right so this is our blink example this is that same code which you used to test your arduino so we see a huge chunk of lines here this is a block of multi-line comments like we saw a single line comment in the previous video we start a multi-line comment by a forward slash followed by a star and then we write whatever we want and then we end it by a star followed by a forward slash so anything which is written in between is ignored by the compiler you may ask then why do we write comment the answer is the comments help us to document the code and they also help the other readers of the code to understand what is done by the author of the code comments also help the author himself to recall what he did when he sees his code at the latest date since comments are totally ignored by the compiler not writing them won't make any difference for example if i remove this comment and compile the code it will still compile and with the comments also it will run just like that since these comments don't serve any technical purpose in the code most developers develop a habit of not writing that comment which is certainly not a good practice it is always advised to write comments for the reasons which i have already told so now i will tell you about the white spaces and case sensitivity the spaces which you see here here here etc like here here won't make any difference as the compiler would ignore them like the comments now what i mean to say by this is that we can add as many spaces as we want wherever we want and the compiler won't say anything to us it will still successfully compile the code like this all right but what we can't do is we can't add spaces in between like this otherwise you will get an error if you compile now just see that how many errors we have got so now you will ask what's the purpose of white spaces white spaces basically help us indent the code it makes it easier for us to identify the blocks of code it also makes the code look beautiful for example if i write something like this what do you think this code looks better or this code looks better obviously this one all right so proper indentation is important now coming to case sensitivity arduino language is case sensitive what i mean by that is the small character would come as small and the capital character would come as capital you can't interchange that for example this void cannot be written as this it will give an error white does not name a type right in this pin mode you see we cannot write pin mode as the spin mode right then also it will give an error so same goes for everything what you see here like this high cannot be written as high or anything for that matter you have to write it exactly like this don't worry if you find it too difficult to remember you will get a hang of it very soon remember what i told about programming it is the game of practice it is very common to mess up so the arduino ide has built-in provision to help us with that what i mean to say by that is if you ever mess up the casing by mistake you would see that this is no more coming in orange color right now it is coming back in orange color same goes here so if you ever see that these critical words are not showing up in color form you can guess that you have made a mistake now let's see what is done here in void setup it says pin mode led built-in output this pin mode is a built-in function which configures the specified pin to behave either as an input or an output this input and output is in reference to the arduino if something is going into the arduino through that pin we would configure it as input or if something is coming out of arduino through that pin it will be output in our case the built-in led would need some sort of output from arduino so we will use output so if we see this line in a little more detail we have written pin mode then there is an open bracket then we have written led built in then there is a comma then there is output then there is bracket close and there is a semicolon i won't be surprised if you have not noticed this semicolon till now i won't be surprised because it is very very common to forget this semicolon this semicolon marks the end of a statement that means we are telling the compiler we are done with the statement we have finished one line if we remove the semicolon we would get an error you see expected semicolon before this so back to that what this line means is we have called a built-in function called pin mode which has two arguments the first argument is the pin number which we want to configure and the second argument tells the configuration which we wanted to act as this built-in led is connected to pin number 13 of arduino so we are telling arduino to set the configuration of pin number 13 as output okay this is what this line is doing also note that this line is written inside the block of void setup can you see that right one more thing which i would like you to note is this led built in the compiler is automatically replacing this led built in with pin number 13. also note that the compiler is not changing the led built-in to 13 magically it has been fed somewhere else in the arduino libraries we will talk about libraries in coming lectures but what i want to say right now is this is equivalent to this for those people who felt a little uncomfortable when i said we are calling a function here which has two arguments i just want all of those people to know that we will talk about functions calling functions arguments of the functions in a lecture in which we talk about creating custom functions then you will be able to relate what i said here exactly but for now just understand we use pin mode function like this we will have to use it whenever we have to configure a pin and using this inside setup means that we want this function to run once whenever we turn on the arduino or reset our arduino all right so now we will see what we have done in void loop the first thing which we see is digital right when it is led built in then we have high okay so the digital right is an inbuilt function which is used to manipulate the digital pins and we can also manipulate analog pins using digital write if the pin is declared as output in setup using pin mode digital write will write a value of 5 volt if we use high and to 0 if we use low but do make sure that you have declared the pin as output otherwise you may see unexpected results then after this line we have delay the value of 1000 in that as the name suggests the function would pause the code here for a specified amount of time in our case it is 1000 milliseconds which is one second whenever we use delay the entire state of arduino is preserved and nothing happens during that time then we have another digital write which would write low to the led built in that means the led will turn off here the led will turn on here it will turn off and this another delay will keep it off for one second and this entire process would keep on repeating unless we turn off the arduino as this is inside white loop so if we change the value of this delay to 500 we would see the change in blinking pattern i would like you to try changing the value to your desired value and comment down below if you were able to see the change now i would like to tell you that you can also store this delay value into an imaginary container known as a variable we can do that by writing int my delay value is equal to 100. you can also choose some other name over here there are some rules to deciding the variable name and you would gradually get to know those rules as we progress along the course so now we can take this value and replace it here so now while the arduino is looping on this code it will take my delay as 100 instead of some other value what benefit do we have to use this we can change our value to any other value like this or this if we need to change the value instead of changing here twice and making our code error prone we can just change the value here it will save our time and also make our code less prone to errors we can also make our own variable to define led pin and we would do that by hint led pin is equal to 13 and now we will take this led pin and we will replace it here right so this code will work exactly like the earlier code and it would work flawlessly the benefit of using our own pin is that we can change the pin which we want to use later for example now i have decided to use pin number two instead of pin number 30. so instead of changing the pin number from 13 to 2 everywhere i can just change it to 2 here so now automatically the compiler will pick the value of 2 of led pin and replace them here now some of you must have noticed the way how i am writing led pin like small ln and capital p what's the reason behind that now since we cannot keep spaces between our variable names and led and pin are two different names so the compiler will throw an error and some people came up with the solution that to name variables like this we can keep the first character of the second word capital in order to differentiate so now our eyes are able to differentiate between led and pin properly and this technique is known as camel casing if i have to use more than two words what i can do is like this so now my eyes are able to read my led pin as three different words instead of one word but for the compiler it is just the one word and now some of you must be wondering what is int why we have declared it here right so in the next video we'll talk about this int and the other data types variables and more about variable scope see you in the next video so now we will discuss about variables what exactly are variables variables are nothing but they are kind of a virtual containers which is used by a compiler and we store some values in those containers there are different kinds of data which we can hold in different kinds of container for example at your home you store sugar in some kind of specific container you store tea in the tea container oil in the oil container similarly in programming as well we have some sort of containers and those can store some sort of specific data the variables are available in different sizes and we will be talking about just the default variables which are used in the arduino programming the thing to note here is the bigger the container the bigger the value it can store the first kind of data type which we will see is int if you can recall we used in in the previous video when we declared our led pins and our own delay values right so int is basically a container which can hold integer values and i hope you're aware what integers are if not i will just quickly tell you that the number line is there the whole numbers from negative infinity to positive infinity are all integers which you can see here in this video like minus 4 minus 3 minus 2 0 1 2 3 4. the numbers greater than 0 are positive integers and the numbers less than 0 are negative integers the default integer which we use in arduino programming has a range of minus 32 768 to positive 32 767 right what this means is the size of integer can hold these many values if you try to store value beyond this range the values will overflow now i will tell you how to declare an integer in programming language first of all we write int then we name the variable like in this case the name of the variable is value and then we assign a value to it for example we have assigned a value of 5 here right and then a semicolon we can also assign it like this we write int then value and we do not initialize it with any value if we do not initialize any value the default value is zero i'm going to say is if you do not initialize the variable the default value which it holds is 0. we will see more about integers in a moment so before i can demonstrate how int exactly works in the code i need you to understand these three critical functions first one of them is serial.begin bracket 9600 bracket close basically this function enables the serial communication between arduino and the computer what serial communication is and what these functions are we will talk in detail in coming lectures but for now just understand we have to write this statement whenever we want any communication to happen between the arduino and the computer then there is serial.print bracket semicolon right we use this function whenever we want to print something on the serial monitor we will see serial monitor in a moment and then we have serial.println the difference between serial.print and serial.println is that serial.println just adds an additional line after printing whatever is ours to print now without any further delay we'll just see how integer works so now let's start afresh by learning about integers let me open a new file all right so i'm declaring my first ever integer above void setup like this end my variable is equal to 5 it is similar to something which we saw in the presentation when i told you about integers now to see the value of my variable i will start serial communication with my computer like this and now to see the value i will print the value serial dot println my variable so now i'll upload the code it's done uploading and now i will see the serial monitor so you see it has printed the value of 5 that means the value of my variable is 5 as we have declared similarly when i said if we don't initialize it the value is 0. we'll upload the code and see it again let me name the file first right if now we have not declared it so now we will see the value it says zero so now i will be telling you about how we can modify our variables for example the first thing which we can do is we will take our variable and add 2 to it so first operator which you see is plus these are arithmetic operators and we will be discussing more about these shortly now if you print our variable serial dot println my variable right so the answer would be 2. so the value of my variable initially was 0 and now we have added 2 to it so we have 2 here now for example if we had initiated it with 5 so now we will get 7 right okay as expected now the variable value is 7 right so now we will manipulate it by subtracting 2 from it now again we will get 5. let me tell you that shortcut to open the serial monitor is this button and then we will print it right 5 7 and 5 i hope i am making some sense similarly there is a multiplication operator and a division operator what we can do is let's say my variable is equal to my variable into 5 let's say now we will print my variable so what will we get in guesses we'll get 25 right so 5 was the value of my variable and we multiplied it by 5 so we have 25 so now let's divide my variable by 25 divide by 25 what will we get one right right we got one now the important thing which i want you to notice the integer variable can only hold integers it cannot hold fractions for example i'll tell you let me delete all this code for example if my variable was 3 right and what if we divide my variable by 2 mathematically the answer should be 1.5 but since our integer variable can't hold decimal values so can you guess what will it print now it will print one why because it can only hold integer part of any decimal the answer was 1.5 but it will pick only the integer value so if you want decimal values as well we will use float and we will soon learn about float now there is another interesting operator known as modulo it will give us the remainder after division let's say my variable is 6 okay and i want to get the value of the remainder we will say my variable equal to my variable modulo the symbol of modulo is percentage sign if we divide 6 by 2 so it will completely divide by 2 right so the remainder will be 0. so if we print my variable we should get a value of 0 right as expected if we divide six by three then also we'll get a remainder of zero but if we divide six by four we get a remainder of two right we will get two here now as expected now can you guess what if we divided by five what remainder will be we'll get one right as expected so these were the main arithmetic operators which we can use to modify our variables now for example we have more than one variables in our code my second variable and the value of that is 5. now what we can do is now let's see what will we get my variable plus my second variable right so can you guess what will we get we'll get 11. right as expected and what if we subtract it are we correct absolutely similarly we can also multiply what should we get 6 multiplied by 5 is 30 right what another thing which we can do is we can store these values here our results are not getting stored here they are just being displayed what if we want to store it we can take another variable my third variable and let's not initialize it okay so what we can do is let's take my third variable okay and let's store the value of my variable plus my second value and let's print the third variable we should get 11 here exactly so similarly we can do whatever we want with the variables but the most important thing which i want you to notice is before using the variable we have to declare it for example if we do not declare my third variable okay and try to do something with it the compiler will throw us an error my third variable was not declared in this scope all right we'll discuss about scope in coming videos just wait for some time you also need to note that you have to declare the variable before using it right you cannot do this as well for example you try to declare your third variable here right then also you will get an error you have to declare it before using it you cannot declare it after using it right the last important operator which you need to know to be comfortable with the course is the increment and the decrement operator there are two types of each one is post increment and pre-increment and post decrement and pre-decrement all right the purpose of increment operator is to increase the value of variable by one and the purpose of decrement operator is to decrease the value of our variable by 1. i'll show you this by an example we have my variable and the value of my variable is 6. so now if i take my variable and do plus plus this plus plus is post increment operator okay and now if i print my variable you will see that we will get 7 right so now if i use pre-increment operator then also we'll get the value of 7 see so now you may ask what is the difference between post increment and pre-increment well there is actually a difference and you will get to know that difference when we use this operator in some expression for example let's see it here if i increment the value of my variable like this like you see in serial println then we will get value of 6 right as you can see and the reason behind this is in a post increment operator the value is first used in the expression and then it is incremented now if i use a pre increment operator then we will see we get 7. so by this we can understand by pre-increment operator the value is first incremented and then used in the expression similarly there is a decrement operator if i do my variable minus minus will get 5 because my variable was originally 6 so we decremented it by 1 so hence we get 5. i hope this concept was clear to you this concept will be used multiple times during our course so if you have any questions you can comment down below and in the next video we will look at the float data type so now we will look at the float data type this data type is capable of holding decimal places as well like we saw in the integer it cannot hold decimals the float can hold decimals as well and the default float which we use in arduino programming is precise up to 7 decimal places let me explain you in a better way like for example the number you see here is a real number it is 863.267 right so in integer if we try to store this that would store only 863. it will just ignore 0.267 so if you want to use 0.267 as well in our memory so we will use a float data type so how do we declare that we write float then space then the name of that float variable in our case it is value equals to 5.6 or some other value which you want then a semicolon you can also start a float uninitialized like we have done here so let's now see the demonstration of how float is used in a programming we will declare the variable my float variable is equal to 7.8 for example let's see right we can see the value of 7.8 now let's see what happens if we try to store 7.8 in an int variable as i told you in the presentation we'll get seven right can you see the difference now now we will just quickly see the limitation which we had while using int variable that while dividing we could not store the decimal part so we will see if we can store that in float variable or not let's see it quickly i'll take my float variable and i'll divide it by 2 okay and we'll see the my float variable then let's comment this statement all right 7.8 divided by 2 is 3.9 we can use all the operators which we used in the int variable like plus minus divide multiply increment decrement and floats as well okay so you can try that on your own and comment down below if you face any problem so now we will take a look at the bool or boolean data type they can be used interchangeably right but we prefer to use bool so the pool data type holds one of the two values true or false that means if something is true or if something is false for example we can declare a boolean variable by writing pool then the name of the boolean variable in our case it is is tall equals to true or false we'll take another example bool is fit is equal to false semicolon so this way we can declare a boolean variable but i want you to note is that one high or true are considered same by the id also 0 low and false are also considered same by id by id i mean the arduino ide right and anything other than 0 is also considered true by the id we'll look at what exactly this means in the coding now let's understand the boolean variable in the coding we will initialize it by writing bool we will then name it and initialize the value of say one as i told you one high or true all these three hold the same meaning and similarly zero low and false all hold the same meaning if we try to print the variable let's see what happens it will print zero right because false is zero if we use low then also it will print zero same goes for true if i use true here it will print one and the other thing which i told you if you use anything other than 0 that would evaluate to 1 let's see that also for example i used 78 let's see what happens it still gave 1 okay so that means bool can either store 0 or 1 that's all and what's the purpose of boolean operator we check if something is true in the code we'll see examples while doing our experiments shortly i would like to tell you if we want to check some condition is true or false or we can initialize our condition as well if something is true then only we have to do certain tasks you will understand more about boolean data type when we progress in the course like when we study about conditionals or when we study logical operators or when we study the comparison operators we will now move on to another data type which is known as byte the next data type which we have is byte so as the name suggests it is a byte long data type it can store up to eight bits of data so as we all know eight bits equals to one byte okay so a byte is just like an integer but the only difference is the range is 0 to 255. so if you're sure that your variable is not going to exceed 255 you can always use a byte type data type so using a byte data type will save your arduino memory so we will now see how to declare a byte we will write byte space name of the variable equals to and the value which we want to add in that then a semicolon we can also declare it without initializing the variable and in that case the default value it will store is 0 just like integer so let's just quickly declare our byte and let's print it right so now we will look at our next data type which is char which is a short for character as the name suggests it can hold a single character any character present on the keyboard for that matter including all the symbols like comma question mark numbers like 1 2 3 characters like abc small caps everything let's see how we declare a character we write char space name of the variable equals to and the character which we want to use and that is enclosed in single inverted commas and then a semicolon we can also declare our character like this as we have seen it many times already the thing to note about characters is that it is still stored as a number what i mean by that is there is a full ascii table each character on the keyboard corresponds to a number for example you can see a capital a is 65 and a small a is 97 right similarly you can see the entire table it contains all the characters which are present on the keyboard and each character has its own number so we'll look at this in a bit more detail using our coding all right so we will declare a character care my care is equal to right now we will print it right we get an a now if i write 65 which is the ascii value of capital a you can refer ascii table to verify then also it will print a and let's have a look right it printed a now still these are numbers that means it should be possible for us to perform some arithmetic on that for example if i take a here and add 1 here right so what should we get we should get 66 as expected why because the value of a was 65 and it added 1 to that so we get 66 right a char data type occupies one byte in the memory okay so i hope you understood the char data type i can understand if you got a bit confused with the character arithmetic don't worry about that right now this information is enough to start our projects so now we have discussed all the important data types like end float char bool byte and you have also learned when to use which type of data type so i'm pretty sure that you won't face any issues while performing your experiments although there are more data types like arrays and strings which are slightly more complicated than data types which we have already discussed and they would also involve the usage of loops so we will cover them after discussing loops as that would make it easier for you to understand for now we will move on to our next topic which is variable scope and qualifiers see you in the next video now since we are familiar with the data types and what variables are we can easily understand what is variable scope and qualifiers so let's start by understanding what is scope in many programming languages including arduino's programming language it matters where you have declared your variable speaking of which we have two kinds of variables global variables and local variables a global variable is the one which can be seen by every function in the program while local variables are only visible to the function in which they are declared in the arduino environment any variable declared outside of a function example void setup or void loop or any other custom function is a global variable so let's start by understanding that in a proper manner so what positions would qualify for writing a global variable the first position which we see is just above white setup the place where my cursor is blinking right now for example i write int myvariable is equal to 6. so in this case my variable is a global variable as i told the global variable is accessible in all the functions let's see that by printing my variable in void setup right it will print here as well it will print in void loop as well let's make an identifier so that we can identify in which function it got printed serial.println printing in setup all right and let's differentiate printing in blue all right let's have a delay here because it will print very fast otherwise okay let's see that now right printing in setup and printing in loop it is printing again and again because we have written that in loop as we already know loop function will run until and unless we turn off the arduino so that means my variable could be accessed by white setup as well as while loop that means this is a global variable now we will see a local variable instead of declaring it above void setup let's declare it in void setup so now let's try to compile this we should get an error why because my variable was not declared in the scope it is saying in void loop you did not declare my variable so i don't know what my variable is so let's comment out this line now it will compile okay it compiles successfully and it will just print in setup once right if we declare my variable in void loop and just uncomment this line now we'll get an error in white setup because it was declared in void loop but it was not declared in void setup you see in void setup my variable was not declared in the scope now let's see a consolidated example of using global and local variables together the variable which i declare here is in my global variable is equal to 789 so let's print it here and let's also print it here okay this one is accessible to both white setup and while loop let's declare a local variable here and my local variable is equal to 852. so this is a local variable and it will be accessible only to white setup now let's try to print this my local variable note that we can use same names if we are using a different scope for a variable for example we can use my local variable in void loop as well and we can give it a different value 1 2 3 for example and let's print this as well so what should be the expected result when we print my global variable it should print 789 and when we print my local variable when the code is running in void setup it should print 852 and when it is running in while loop it should print one two three so let's upload this code is that as expected let's see printing in setup it printed my global variable which is 789 and my local variable which is 852 right now in printing in loop it printed my global variable 789 and my local variable which was 123. so i hope you are clear with the scope of the variable when programs start to get larger and more complex local variables are useful way to ensure that only one function has access to its own variables this prevents programming errors when one function modifies the variables used by other functions this concept will get clearer to you as we progress in the course so now we will look at qualifiers qualifiers basically define additional behavior to our variables there are two main kinds of qualifiers one is constant and one is static the constant qualifier is declared by using const keyword it is a variable qualifier that modifies the behavior of the variable making a variable read-only this means the variable can be used just as any other variable of its type but its value cannot be changed you will get a compiler error if you try to alter a value to a constant variable we will just see by an example we will declare our variable constant int my variable is equal to 5. now my variable is a constant integer you cannot add anything to it you cannot subtract anything to it you just can't alter it now let's try to alter it let's see what happens we got an error assignment of a read only variable my variable however you can use constant variables for doing the math for example i take another variable here is equal to my variable into 5 right since this is not possible we'll remove it we'll try to print the value of my second variable right so can you guess what will we get we got 25 right because my variable was 5 and we multiplied it by 5 so we got 25. so you may wonder what purpose do constant variables serve in the code well we can use constant variables wherever we don't want our variables to change their value for example there are constants like pi which is 3.14 and we can have our arduino pins as constants like if we don't want to change them in the code i would also like to tell you that there is an alternative in c plus plus language to define our constant variables which is hash defined as define is a useful c plus plus component that allows the programmer to give a name to a constant value before the program is compiled define constants in arduino don't take up any program memory space on the chip the compiler will replace the references to these constants with the defined value at the compile time so let's see this by an example we start by writing hash define then the name of the variable which you want let's say pi okay and the value of that variable which you want it can be anything for that matter it can be an integer it can be a float it can be a character so let's say the value of pi is 3.14 right also note that we don't terminate the defined lines by a semicolon we just write this so whenever we use pi in our code the compiler will replace the value which is written next to it there the compiler won't care if it makes any sense or not you have to make sure that it makes sense okay so now let's declare another variable of float type and let's name it area of circle okay and let's do the math pi into r square let's say r is 5 so right and now let's print the area of circle right let's upload it we see it has automatically picked up the value of pi from here and it has done the math and it is giving the answer to us right as hash defined doesn't use any memory in the arduino it is really tempting to use hash define but it certainly has some downsides the compiler will replace all the references to pi in the code and you will start facing problems so it is recommended that you avoid using hash define so use constant int in place of hash define unless you are falling short of memory so in the next video we'll talk about the static qualifier see you in the next video so we will discuss about static qualifier in this video let me just quickly clean up this mess first because it's looking so confusing so just observe what's happening let's declare a variable here int xyz is equals to zero let's increment it here and let's print it let's see what happens can you see that we initialized it by zero we incremented by one and then we printed the value so when the loop started again the value would be incremented to two let me stop this auto scroll the value will increment to two then three then four and five and six and then so on okay it will keep running until i stop it so in this case we have declared xyz as a global variable but i would like to tell you that it is not a good practice to declare many global variables so we should try that we use local variables as much as possible so let's try to use it as a local variable we will remove it from here and we will declare it here right we declared index y equal to zero then x y z plus one then we will increment it by one and then we will print it okay let's see what difference will it create can you explain why is this happening okay we initialize it by zero we incremented it we printed it okay and then it was again initialized to zero then again it was incremented by one and then printed so it will keep on printing one unless i stop this loop so now we are in a trouble if we declare it in loop then it would reset every time the loop is called and it is recommended that we don't use global variables that much so what is the solution the solution is that we use a static qualifier to do that we will just add static in front of our variable what this will do is this will just initialize it by zero and it will not reset it to zero every time the loop is called let's see if it solves our problem yup it did solve our problem so using a static data qualifier is a good way to preserve the data of our variables between the function call i hope it was clear to you please comment down if you have any questions see you in the next video in this video we will look at the comparison operators comparison operators are basically some operators which would tell you if something which you're trying to compare is true or not they would return a boolean value of either true or false so we have about five or six comparison operators we will just look at them in a moment the first operator which we have is a lesson operator and is represented by a less than symbol then we have a greater than operator then we have less than equal to then we have greater than equal to then we have equal to equal to and then we have not equal right so we have a total of six comparison operators we will just look at them what they exactly mean let me comment this out first uh let's have our variables end variable one right let's say it is seven and we have int variable two and it is eight let's try to compare them variable one less than variable two as i told you this operator returns something which is true or false so what will happen here is since variable one is less than variable 2 right so it will print 1. let's see so now let's do one thing let us make this variable 1 as 9. now we should get 0 because this statement will become false right as we expected so now can you guess if i make variable 1 as 8 what will happen let's see this should return 0 yes why because 8 is not less than 8 okay it is equal to 8 but it is not less than 8. so we'll talk about this condition very soon okay now we will have the greater than operator let's replace this right and change the variable to 9 back okay so what do you think it should return it should return true because variable 1 is greater than variable 2 of course i hope you understood these two operators then we have less than equal to we use it like this a less than symbol and equal to symbol this is a less than equal to operator what this does is it will return true if the value on the left side is lesser than the value on the right side or it is equal to the value on right side you got my point so previously when i made variable 1 equal to eight and variable two also eight it returned zero but in this case it will return one because eight is equal to eight so let's try that right so similarly a greater than equal to sign also works like this you can experiment this on your own then we'll look at the equal to equal to sign equal to equal to sign means it will compare if some values are equal or not it will return true only and only if a value is exactly equal in our case we will get one because our value are eight and eight we got one if i make it nine for example we should get zero then we have a not equal to operator we use it like we take an exclamation mark and then i equal to symbol this operator will tell you false only if the two values are equal okay in all the other cases you will get a 1 not equals 2 right if some value is equal to some other value it will say it is false because that is equal but if you take any value in our case for example variable 1 is 9 and variable 2 is 8 we should get a 1 because that is true right so we have covered all the operators and how they work so you may be wondering now what is the use of these operators in programming well these operators will help you compare the values for example you make a project in which you have to compare the values of the sensors so you may have to use these operators right and when you use these operators you will get some decision making capability in your code i would like you to note one important thing about the equal to operator many people make a mistake while using the equal to operator they use 1 equal to sign instead of 2 equal to signs while 1 equal to sign is the assignment operator that means you are assigning a value to a variable like in this case we have assigned a value of 9 to variable 1 we have used one equal to sign okay and when we use double equals to that means we are trying to compare something so to avoid making mistakes i really recommend that you double check the operator which you have used i will talk about this once more in the control structures when we study about the if conditional block and in the next video we will learn about the logical operators in this video we will look at logical operators the concept of logical operators is very simple we use logical operators whenever we want to take decisions in the code each operand is applied to some condition which may be either true or false we'll just see that in a moment so basically there are three logical operators and operator or operator and a not operator so let's see how it works in coding so the and operator the and operator is represented by two ampersand signs these signs can you see that then we have or operator and which is represented by two straight lines we'll find this symbol just above your enter key and then we have a not operator it is an exclamation mark that's all so let's see the and operator so the and operator expects that all the conditions which we are using in it are true in order to return a true these operators also return a boolean value of either true or false they also operate on boolean values which is either true or false like i told you they work on some conditions please hold on as i further clarify this let's start with the and operator let me just open the cl monitor so that we can understand it side by side the first thing which we have written is the lessons about and operator it is printed exactly as we have written it okay then it is true and true is true and true is written like this okay then we have written true which is the boolean value then the operator and operator and then another boolean value which is again true the true and true returns a true and both of the two conditions are true then the answer is true for all the other conditions it is false for example true and false is false false and true is false false and false is also false so whenever we use it to take decisions we tell the compiler you have to proceed only if both the conditions are true okay if they're false you don't have to proceed okay i hope it is clear if it is not clear don't worry we will just understand while we write our main programs then we have printed some empty lines so that we can have a space over here then we will see the or operator this is the or operator it is printing the lessons about our operator with the or operator even if one condition is true then the resultant will be true it will be false only if all the conditions are false for example we can see true and true is true true and false is also true false and true is also true false and false is false right then again we have printed some lines so that we can see the gap here then we will see the not operator the not operator basically just reverses the value of the boolean for example the not of true is we have used the operator here and the not of true is false as you can see it's zero then the not of false is true right it has just reversed the values it is very useful when we write our conditional blocks which we will see shortly all right so let me just make some changes so that you can connect it with the comparison operators which we used earlier okay so instead of straight away writing true here let's take 5 greater than 3 which is true right and instead of writing true here we can say seven is less than eight okay both the conditions are true hence we will also get true right then also we got true because both the conditions were true if we make one condition false let's say here 8 is less than 5 which is false right so true and false is false let's see that right this is giving false we'll just quickly look at our condition let's say both the conditions are false let's make false conditions 5 is less than 2 which is false 4 is greater than 9 which is also false so we should get 0 right which is this line false and false is 0. i can understand it can be a bit confusing for some people but i can assure you that once you start writing the codes on your own you won't face any problem while using these operators and even if you still have some problem we are always there to help you you can always comment down in the comment sections so see you in the next video we will now look at control structures so basically till now we have seen that the code is executed line by line that means the lines of codes are executed in the order in which we have written them for example we have written some code to perform action one then we have written some code to perform action two then in that case the action one will happen before action two when the action do completes then the action three will take place so similarly there is a controlled flow of code where we can decide which parts of the code we want to execute based on some conditions we start our code and then there is a condition that condition can be anything for that matter and based on that condition if it is true or false we can take some other actions let's say we have a switch we'll check if switch is pressed then we want to take some action if it is not pressed we want some other action to take place and the flow of code will depend on that condition either it will go this way either it will go this way and after completing that task it will jump back to the main code execute the lines which are present in the main code and then it will exit so the first control structure which we will study is the if statement we will understand about if statement and we will see how to use if conditions in our code in the next video so let us see the if condition whenever we write an if statement we write a condition along with that let's say we are trying to use a if block will take a condition which we want should be true if it is true then whatever we write in the if block will execute and then it will move on to the statement which is written just after if statement okay and it will execute that as well and then it will exit but if in case it is false then it will entirely ignore whatever is written inside the if block and it will straight away jump to the code which is written after if statement it will execute that and then it will exit let us quickly look at the syntax of if statement let's see this is our code we have written some statements we write if then we start a bracket we write our condition let's say something greater than something or if something is true if something is false if something is not true anything for that matter i will put a condition in this bracket then we close the bracket and then we open up the curly bracket then we write all the statements which we want to execute then we close the bracket and then we move on to write more statements in our code so this is the syntax of if statement so let us try this in our coding so we have an example here we'll write cl dot begin 9600 here so that we can communicate with the computer so we will write the first statement serial.println this line should print irrespective of anything and yes this line will print irrespective of anything then we will write if true serial dot println this line should print only if the condition is true please note that we don't use a semicolon here let's write another statement if false serial.print ln this statement will not print as the condition is false it is over and then again we'll write one line which should execute irrespective of anything let's say this only this line should also print irrespective of anything so let's upload this code and see what happens so we see this line should print irrespective of anything this line should print only if the condition is true this line should also print irrespective of anything did you know that when the condition was false the block did not execute let me do one more thing let's see a block of code executing let's print one more line here the second line right so now this line should also print there the second line because it was the part of this if block so whatever is written inside this block it will execute no matter what once the condition is true but of course writing true and false exactly like this does not make any sense right so we have to build our conditions let's say if 5 is greater than 3 then this should execute we'll see the same result because this evaluates to true right so basically we should keep in mind that a boolean value should come inside this okay in this bracket there should be a boolean value then only it would be able to take some decision based on that boolean value so now we can complicate the condition as well like for example and 2 greater than 5 but this is false right like we saw in the and operator both the conditions should be true in order to return true right but this is true and this is false so we'll now see that this block of code will also not execute let's see right as expected this line should print irrespective of anything this line will print anyway this line will also print anyway so now let's try to print this line we'll make this condition true 2 is less than 5 or 5 is less than 2. this condition is true because one of the condition is true right 2 is less than 5 that is true but 5 is not less than 2 this is false but in the or condition one statement has to be true to get the resultant as true so this line should print now let's see yep so we were able to print this line because we made the condition true okay so when we will be making our projects what kind of conditions will we use we will be using conditions like the sensor value on the left hand side should be greater than the sensor value on the right hand side then these things should happen so i think you were able to relate with this thing okay now i will tell you another thing for example i took a bool many people do mistake in this so let's start by taking a bool my bowl is equal to false so now what i want to do is i want to print these lines only if my bool is true so i'm comparing it if it is true then only it should print but this is not true right because the value of my bool was false so that's why the resultant was false so hence the lines present in this if block will not execute now if we write false then it will execute because it is actually false so we can see these lines got printed so now i'll talk about the mistake which people do what they will do is they will use one equal to sign instead of two signs so what will happen is the compiler will assign a value of true to my bool and the resultant value of my bool will become one okay and this block of code will execute right so the expression which we write in the if condition is executed first and then the value of that thing is taken into consideration if it is true or false okay in our case my bool was assigned a value of true and then the value of my bool was used please take care you use double equals to if you want to compare the values now we can do nesting of ifs as well what nesting means like this is our if block we can write more if blocks inside are the parent if block let's say we write another if if true serial dot print this is nested if right we can write another if inside that if false serial.println this is another line which will not execute so this was nesting of if this is the parent if this is a child if and this is a further child of this if right we can see like this this is the flow this bracket closes here and this bracket closes here for example in any case the parent if is false then all the child lifts will never execute please keep that in mind i hope you understood this point i'm pretty sure that you will be able to understand it in a better way when you perform your experiments so in the next video we'll look at the else statement see you in the next video now we will try to understand the else statement first of all let us look at the flow of the code we always write an else statement preceded by an if statement let's have a look at the flow of code when we use an else block there's an entry of code there is an if condition if that is true the body inside the if block is executed the body inside the else block is entirely ignored then it moves on to code after if statement after executing that code it would exit in case if it is false it will straightaway go to else block and it will execute the code which is present inside the else block and it will ignore the code which is present inside the if block after executing the else block it will go to the main code the code which is written after the if statement it will execute that and it will exit before looking at the syntax of else block let us compare it with just the if block so this was the if statement in if statement if the condition was true it would execute the body inside the if block then it will go to the main code execute that and then it will exit so when we use just the if condition without else that means we don't define what we want to happen if the condition which we have specified is false if it is false it will entirely ignore the if body and it will jump to the main code but in else we define what we want to happen if something is false so that we write in the else block so now let us look at the syntax of else block we start by writing if we specify the condition we open the bracket we write our actions here if the condition one is true then we close the bracket and then we write else if if we have more than one conditions for example if you want different sound levels of an alarm based on the temperature ranges for example at a lower temperature an alarm would ring slowly and at another condition let's say another range it will ring a bit louder and if it exceeds something then we want it to ring at the maximum intensity okay so these things can be controlled by if and else blocks okay after writing else if we write condition two if that condition is true think b will happen okay and then finally we write an else if none of the conditions is true then the else block will execute the thing which you need to notice here is if the condition one is true it will execute the thing a and it will straight away jump to the main code after this the control won't go in else if it will go in else if only if the condition one was false for example the code was running here it found that condition one was false so it will jump and check if condition 2 is true or false if it is true then it will do the thing b if it is also false then it will go to else block and do the thing c we will just understand this with the coding example so here we have example for understanding the else statement let's do this we'll write if true serial dot print ln my first statement so you can see the compiler has automatically considered this bracket which was originally for void setup for the if block okay we have to make sure we are using our brackets correctly now after closing the if block we will write else serial dot println my else statement all right so now we will close the white setup bracket now let us upload this code okay so my first statement got printed because my initial if condition was true and it will ignore the else block now let's make it false now we will see that statement written in the else block will print let's have a look my statement okay we can do the nesting in if else statements as well like we saw in the if video like i can start another if here if some other condition will write another task here so we can use endless if and else and the nesting of them as well by now you must be feeling a little bored by just printing the statements here and there and not doing any real things so in the next video i will show you a small simulation by using if and else blocks so see you in the next video so before i show you that fl simulation i would like to show you this comic just have a look at this comic for like 5-10 seconds and if you get the joke in this comic i can say that you have pretty much understood the concepts which we have discussed so far okay don't worry if you could not understand this once you finish the part where i'm teaching about functions i'm sure you will be able to understand it really well so just in case if you could not understand this i would say that please complete that section and then come back to this comic again and i'm pretty sure that you will have a good laugh okay so now let's move on to that simulation i was talking about so as i told you that i will show you a simulation of fns blocks in the previous video so i will just show you that simulation on the left hand side of the screen you can see a circuit diagram and on the right hand side of the screen you can see the code which i have used to make this simulation the simulation tool which you see on the left is autodesk tinkercad you can do a google search about it if you want to know more about this i'll help you in the comment section if you face any problem with this every one of you must have performed the blink example and i have very clearly explained you the functions which we used in that like pin mode digital write and delay so i will assume that you will not face any problem understanding this code so let's begin i have declared temperature as the global variable and initialize it with 0. in the void setup i have written pin number 678 as output as i want to connect our led to those pins so in the loop section i've started with if i'm trying to tell the compiler if the value of the temperature is less than 50 and is also greater than 10 that means it lies between 10 to 50. so i want to perform this action please note the values of 50 and 10 are not inclusive as we have not used the equal to sign if you want to include those values you will have to use an equal to sign as well you must be remembering less than equal to operator and greater than equal to operator right you can always revisit the videos if you have any confusion so coming back to this i've simply written the code to blink the led which is connected to pin number six pin number six is this one that means this led is connected to pin number six also note that all the leds which are connected here are connected to a common ground which we have connected like this then we have written else if that means if the previous if was not true then check this condition if temperature is greater than 50 and temperature is less than 70. that means the temperature range is 51 to 69. so if this condition is true then we want to blink the led connected to pin number seven which is this one pin number seven this and then finally we have written else if none of the conditions is true then do this thing which is blink the led which is connected to pin number eight which is this led so let's say we have temperature is equal to zero currently we have hard coded this temperature but in your projects you may have some sensors like lm-35 or dht 11 which will return you the real time temperature of your room and code will act accordingly but just to test these conditions will be taking some arbitrary values so let's see in which condition will this zero fall okay if temperature is less than 50 or greater than 10 but it is not relying in this condition then it is greater than 50 and less than 70 so this is also false so ultimately the else code should run so let me start the simulation right you can see else statement was being executed that means this led is blinking so now let's change the value from 0 to 52. so can you guess what will happen for the value of 52 okay let's see the value of 52 is less than 50 which is false so ultimately this condition will be discarded it will check this one then so the temperature is greater than 50 yes temperature is less than 70 yes so this condition is true so hence the led connected to pin number 7 should start blinking so let's start the simulation so led connected to pin number 7 has started blinking pin number seven okay so let's change the temperature value to 25 so now this condition will get true so let's start the simulation right so this led is blinking so one small homework for you guys is you have to comment down below what will happen if i select the value of 100 we can take a pause of 5 seconds and just go and comment down below after then come back and see the results so led which is connected to pin number 8 is blinking why so because 100 will not fall in any of the conditions so the compiler will execute the else statement one interesting thing which i want you to notice is that right now we have chosen arbitrary values but with the sensors you will get really changing values you may get the values which are changing so the arduino will act in the real time okay if you don't change the value and the sensor changes that value and we get different values from that sensor then the arduino will automatically check which condition is true right now and it will execute that statement and it happens really fast so with the real sensor you may see led which is currently blinking may change very quickly for example if you light up a candle near the sensor then the led which is blinking may change and then if you remove the candle then you may see that the other led may start blinking depending on the condition which is currently running so i hope you are clear with this if you still have any questions you can always comment down below or send a message to us so we will now take a look at loop structure which is another kind of a control structure in this kind of control structure we will basically learn how to write instructions in order to repeat certain tasks instead of writing code again and again for doing a certain task we can learn how to repeat that task again and again for example the void loop function which we use in arduino is a loop structure whatever we write in that repeats on its own but that is a default loop structure however you may need something to repeat itself in a loop as well so for that we have loop structures basically we have three kinds of loop structures one is for loop the second is while loop and then third is do while loop we will look at them one by one in their dedicated videos but before that let us see how it exactly works so we make the entry to the code then there is a condition based on which we take a decision if it is true then the certain action which we have defined will take place and then it will come back and check the condition again if it is still true then it will repeat the action and this will go on and on until this condition becomes false if it becomes false it will break out of that loop and perform the action 2 which is written after the definition of loop after performing that action it will exit so in the next video we will look at how exactly the first control structure which is for loop works we will see the syntax and we will see how it exactly works in the coding so see you in the next video so now let us begin with the for loop this is the first loop control structure which is very commonly used in computer programming first of all let us know about for loop this is an entry controlled loop what this means is the condition based on which the task will be executed is checked first if it is true then only those statements will be executed and we use for loop when we know how many times we need to run a loop so let us see the syntax of for loop first of all we write 4 then an open bracket then we initialize some variable here that variable will count how many times the loop has to run then we write the condition here that condition can be anything this condition mostly includes the variable which we have initialized then we update the variable which we have initialized we close the bracket then we start a curly bracket we write all the statements which you want to get executed then we write curly bracket let's see how it is used now let's say i tell you that you have to write your name 10 times on the serial monitor so what will you do serial dot print ln let's say my name is ashish so with your existing knowledge you will take this line you will write it 10 times and then you will upload it you'll check on see that monitor you can see it is getting printed 10 times so now if i tell you that you have to type it 100 times so would you be willing to write it hundred times i guess no and some of you may tell that we can write it in void loop instead but writing that in void loop would mean that it would keep printing ashish until and unless you turn off your arduino you won't be able to count if it has printed 100 times you can sit there and count that but that certainly doesn't make any sense so for that a for loop comes to a rescue let's see how we use it we write for we declare a variable int i for example is equal to 0 and we write a semicolon then we write a condition here for example how many times you wanted to run let's say i less than let's say we want to print my name 10 times so let we will write 10 here and we will update the condition i plus plus okay we'll now write serial dot print ln ashish and now we'll see what happens right we can see the arduino printed my name 10 times on the screen one two three four five six seven eight nine and ten okay we can also add multiple statements in this block let's say we can write hello world okay let's see what happens it will write ashish hello world ashish hello world yep as expected it has repeated all the things 10 times now what advantage do you have is now if someone tells you that you have to write it 20 times instead of 10 times so instead of copying pasting that code again you can just alter 20 here now it has written it 20 times now for example i want to use this declared integer i in my code serial dot print and then we can directly use this i here we can also perform arithmetics on that for example i can do i plus 2 i plus 5 i minus 5 i divided by 2 anything for that matter but please note that the scope of this variable is just inside this block you cannot use this declared variable in other blocks even if you are using it in some for loop or some other function the scope of i will exist only in this block get my point let's see it will print 0 to 19 okay like 0 1 2 3 4 5 6 7 and 19. okay now let's understand how it was working all right so whenever we write a for loop the first step which happens is initialization this statement is executed only once when the loop is entered for the first time and after this the second step is it checks the condition this statement is probably the most important one it checks to verify whether or not certain expression evaluates to true if it is true then the loop execution continues then after that if the condition is true it would move on to execute the statements as the third step after executing the statements it will move on to the fourth step which is updating the variable it is typically used to increment or decrement some variable after the update the fifth step is to check the condition once again if that condition is true again it will come back to execute the statements once again then this step continues again and again until this condition becomes false the moment the condition becomes false it will come out of the loop and start executing something which is written after this loop now since you know this let us understand the code which we wrote earlier so now let us pay a closer attention to everything which we have written i've written for end i is equal to 0 then a semicolon and in the condition we have written i less than 20 that means i is acceptable till 19 okay and then we have written i plus plus you must be remembering that plus plus is used to increment a variable by one we have written serial.println bracket ashish that means when the value of i is 0 it will print aashish once then it will print hello world then it will print the value of i itself which is 0 right now then after executing this loop body it will increment the value by 1 and the value will become 1 okay when i is 1 it will check if i is less than 20 which is true it will execute the body once again then the value of i will become 2 it is still less than 20 then again it will execute the body so it will continue till 19 but after executing it for the 19th time and when the value becomes 20 this condition will become false the moment it becomes false it will not execute these lines and it will exit out of the loop in case if we had written less than equal to 20 then the condition would have been true but it would have actually printed it for 21 times because it is starting from zero other thing which we can do is start it from one instead of zero it will be equivalent to this but usually it is recommended that we start from zero so after knowing this really easy trick i'm really wondering if you're wishing that you knew this trick before when your teacher gave your punishment so in the next video we will look at how to do nesting in for loops see you in the next video so like we saw next thing in fl's block we can do nesting in for loops as well for example if you want a printer pattern like this a series of stars there are five columns with five stars in each row let me just comment it so that it doesn't create problem in our code we can simply go ahead and write and we'll write serial dot println and we'll print five stars right so let's see what happens when we upload this oops we forgot to add a double quote so now again oh we forgot to write the name of variable and i let's see so it printed a lot of stars but now what if we want a pattern like this a staircase type of pattern let me comment that as well okay so how will we do that it's very simple we'll do nesting of for loops let's say for enti is equal to zero we want five rows okay so we will write five then i plus plus then we will write another for loop we will take another variable here and j is equal to zero and we'll write condition as j less than equal to i and then j plus plus okay and then we'll write serial dot print please don't will not write print ln here because we don't want it to print in next line so cl.print and just a single star and then once this for loops executes we'll print a new line dot println so let's see what happens here okay we got that pattern now i'll explain you how it worked actually we took our first for loop we initialized our integer i as 0 and we gave a condition i less than 5 that means it will not run when i exceeds 5 okay or becomes 5 then we wrote another for loop and we took another variable j is equal to 0 and j less than equal to i that means in first iteration the value of i was zero and the value of j was also zero okay so here j less than equal to i that means zero less than equal to zero this is true and then j plus plus so it will print a star okay then the value of j will increase and it will become 1 so now this statement will become false because j is 1 and i is still 0 then it will exit out of this loop and then it will print a line then that means after printing one star it will print another line okay and then it will come back to this for loop now the value of i is 1 is still less than 5 okay and then it will come into this for loop so now the value of j is 0 and the value of i is 1 okay so 0 is less than equal to i it is true it will print a star then again and j will become 1 this condition is still true because 1 is less than equal to 1 right so it will print another star next to the previous star so it will print 2 stars let us see in the serial monitor right after printing this star it will print this star then the value of j will become 2 and then this condition will become false because i was still 1 then it will exit out of that loop and print another line it will print another line here okay then i will become 2 then it will go back in this loop and it will execute the loop three times 0 1 and 2. similarly it will go until and unless this condition also becomes false okay so i can understand it must be really tricky right now if you just go back and listen to what i said once again you will understand that and in case you don't understand you can always drop a comment or message us so in the next video we will look at another kind of loop structure which is known as a while loop see you in the next video in this video we will look at while loop a while loop is also an entry controlled loop an entry control loop is a loop in which the condition is checked beforehand before executing the statements which are written in the loop body this is the same thing which i told you for the for loop all right but the difference between the while loop and a for loop is in for loop we know how many times we want to run the loop but in while loop we don't know how many times we want to run the loop it is based just on the condition if some condition is true the while loop will continue to run endlessly and the moment the condition becomes false the code control will move out of that while loop that condition can be based on some variable or some external conditions like some values given by the sensor let's say we are making a robot and we are using a distance sensor we can use a while loop or to move forward unless the distance is less than some quantity and we want to exit out of that by loop as soon as the distance gets lesser than that specified value so that the robot can avoid the collision from any obstacle in front of it right there can be many more while loop uses but we will begin with the basics we will use our traditional method of printing variables on the serial monitor so let's see that so we have our sketch here let's use a while loop to print a table of two let's say we'll start with an integer which we will use as a condition int multiple is equal to one let's start with 1 while multiple is less than equal to 10 okay serial dot print ln multiple into two okay and let's increment the multiple all right let's see if we are able to print the table of two oops we forgot to write serial dot begin always write serial dot begin whenever you have to communicate with the computer let's upload that again right 2 4 6 8 10 12 until 20. this is a table of two now let's print a table of a complicated number let's say 73 yep this is a table of 73 so now let me show you how a while loop can run endlessly if the condition is true let us just make it true directly this will become an endless loop right now it will not just print numbers from one to ten it it will just print them endlessly let's use the easy number so that we can verify it won't stop at all let's see look at the speed it is printing all the even numbers can you see that let's have a look 2 4 6 8 10 12 14 16 as you know there's no upper limit to the numbers so it will just go on endlessly until the limit of int expires so i have another example to show you how a statement controls a while loop how it prevents the while loop from running when it is false and how it initiates the while loop when it becomes true so in the white setup i have written serial.begin 9600 to initiate the communication and then i have void loop i have declared a static variable a test variable which is going to control our condition i have defined it as static because i don't want to reset the value of test variable with each run uh if you have any confusion regarding the static part you can check the dedicated video which we have made to explain the static qualifier for a data type now while loop we have started the while test condition greater than five that means right now the test variable is zero as we have not initialized it so zero is the default value so zero is greater than 5 that means this is false so this statement should not execute okay but in the next line we have written variable plus that means we want to increment that variable the variable value will become 1 and it will print the value of the test variable then we are waiting for one second so that we can observe the changes which are happening and then again it will come here it will see while test variable greater than 5 that means it is still false because test variable's value is 1 right now and it is still false so it will still not go into this loop then it will become 3 then it will become 4 then it will become 5 at 5 also this will not become true because we have not used the equal to sign and when it becomes 6 this condition will become true and this will start printing let's see that right one two three four five six and we are in the while loop now okay this while loop is repeating endlessly because we have not specified any exit condition and please note while this is in while loop these statements are not being executed the control of code is looping here only since this loop is looping infinitely we might want to use some break conditions we will learn about break and continue statements in coming videos but in the next video there is one more loop which is known as a do while loop we'll learn about do while loop in the next video we'll take a look at the last loop control structure which is the do while loop the first thing which i want to tell you about the do while loop is it is very very similar to the while loop it works exactly like the while loop but the only difference is it executes at least once and that is why it is the exit control loop in exit control loop the statements are first executed and then the condition is checked while in while loop first the condition was checked if the condition was true then the statements would execute in this first the statement block will execute and then it will check the condition if the condition is false it will exit out of the loop and not execute those statements further and if the condition is true it will go back and execute the statements once again if you look at the flow the first thing which happens is it executes the statement block then it checks the condition if it is true then it will go back otherwise it will come out of that loop a do while loop is also used when we don't know how many times an action has to be repeated but we are sure that it should run at least once in those cases we use a do while loop a do while loop is used where the statement has to execute once otherwise we won't be able to take decision for example we are expecting a user to give us some input and we want him to give a positive integer as an input but for example he is trying to give a negative input so we can write a do while loop while the user gives us the negative input keep telling him to give us the positive input and the moment he gives us the positive input will break out of that loop okay so let us see a basic example of using a do-while loop like we did earlier we printed a table of two we'll again print the table of two using a do-while loop instead of a while loop so let us see how we do that we have initialized a variable known as multiple and initialized it with one okay we'll write cl dot begin to start the communication with computer so we'll write do then the bracket open we'll write cl dot printenet multiple into two then bracket close and semicolon and then we will increment the multiple and then we will write while multiple is less than equal to ten that means this condition is true until the multiple becomes 10 the moment it becomes 11 this condition will become false and this statement will stop executing so we'll get a table of 2 here let us see what happens right so we got a table of 2 2 4 6 8 and then so on till 20. i have another example for do while loop so that we can understand how exactly it works so in the white setup we have written cl dot begin 9600 so that we can communicate with the computer in while loop i have initialized a static into variable which is known as test variable we have written do then we are writing cl.println this will print at least once because a do while loop will execute the statements at least once so ok we have written this statement and we have written a delay of 500 milliseconds so that we can observe what is happening otherwise you know serial monitor prints things really fast so now we have written while test variable is greater than 5. while the test variable is less than 5 this statement should not execute but it will execute at least once then we have written test variable plus plus that means we are incrementing the value of text variable here we are printing the value of test variable and we have given a delay of 500 milliseconds so that we can observe what is happening okay so now when we open the serial monitor what we will see is that this statement will execute once then the test variable will increment okay and then the value of test increment will print then it will go back into do while loop and print this statement once again because in do while loop it has to execute once at least then while it will check the condition it is false then it will come back here it will increment the variable and it will repeat till the test variable becomes greater than 5 okay the moment it becomes greater than 5 so this do while loop will keep on repeating let's see how it happens let us upload the code first right this will get printed at least once and this is printing again and again but now just see the loop control didn't come out of this okay because this condition is true now it will go back again and execute the statement these statements are not being executed because it is still in do while loop so i hope you understood this do while loop so now i have a really interesting illustration which you can see on the screen to explain the difference between a while loop and a do while loop and i think it is a really good example to show the difference between the two every one of you must have seen the roadrunner show and we see this guy was using the while loop while this guy was using the two while loop let's discuss in the comments who was using these control loops in a proper manner if you remember i told you that we might have to break out of the loops based on certain conditions like for example in an endless loop we might need to do something to stop that loop so the break keyword is used to exit from a loop by bypassing the normal loop condition which we have specified let's see how we do that so this is the modified version of the code which we used to print the table of two okay this is the regular while loop which is running okay we are printing a table of two here and we are incrementing the multiple here but now we have specified a break condition okay like if multiple is equal to five then we will write breaking out of the loop now and we will break out of the loop by just using this keyword let's see what happens all right so we printed it till four two 4 6 and 8 and then it breaked out of loop although it should have run till 10 it should have printed till 20. so if i comment this break condition and upload the code again so we got the entire table so this is the basic use of breaking out of the loop now let's see if we are inside an endless loop let's say while true this is an endless loop we are writing serial dot println endless loop okay and we will increment the multiple multiple plus plus okay let's say i want to exit after printing it 10 times if multiple greater than 10. we'll just break out of it let's see what happens okay we forgot to close the white setup bracket right if we didn't use this break statement it would ultimately keep running endlessly let's see that quickly okay can you see that it's printing endlessly so i hope you understand we use a break statement wherever we want to break out of the loop so in the next video we will see the continue keyword and we will also compare the break and continue keywords like we saw in the break statement that whenever we use it code control instantly moves out of the loop entirely discarding all the remaining iterations of the loop right like we were trying to print numbers from 1 to 10 and when we used if our variable is equal to 5 then break out of the loop so it entirely skipped 6 7 8 9 10 but in some situations we might want to use the remaining iterations right so in that case the continue keyword helps us with that the continue statement skips the rest of the current iteration of the loop and it continues by checking the conditional expression of the loop and proceeding with the subsequent iterations that means it won't skip the rest of the iterations let us see how it works so we have void setup serial.begin 9600 as usual then we are writing for loop to print numbers from 0 to 10 right because we have initialized i with 0 and we have used less than equal to sign so that means both the ranges are inclusive so it will print 0 to 10 and then we are incrementing the variable now notice we have used if i is equal to 5 then continue whenever we use the continue keyword it would straight away take us to the condition pack and it would skip all the statements which are written after that so let us see what happens so we see 0 1 2 3 4 5 is missing and 6 7 8 9 and 10. so it skipped the iteration in which i was equal to 5 right but it did not skip the rest of the iteration if we had used the break keyword it would have skipped the rest of the iterations it stopped at 4 only let me switch back to continue here again and let me explain the difference between the break and continue statement let's say we write a statement here serial dot println end of our for loop this is the end of our for loop that means after our for loop completes this statement should print regardless of anything so when we use continue we'll see the loop was going 0 1 2 3 4 and when the value became 5 it jumped back to check the condition okay so in this way it had to skip this statement it cannot execute the statement right so when it became six again then it will start printing it again can you see that and after completing the loop it came and printed this line when we use break then the control will directly come here whenever the break statement is encountered the code control goes directly out of the loop can you see that after printing 4 it printed end of for loop so we may have different situations to use continue and break statement and we have to make sure we use them correctly so in the next video we will look at the last keyword which is return so see you in the next video the return keyboard the return keyword would probably the easiest thing to learn right now the only function of the return keyword is to terminate the function no matter what it doesn't take any conditions it doesn't take any limitations or anything whenever the code encounters the return keyword it would just terminate the function there are two ways to use a return we will be covering the first way to use the return keyword we will cover the second way in the coming videos let us see how we use it in one manner all right so i have this code written here okay in the white loop what i'm trying to do is i'm trying to print line one line two line three and line four okay and then i have a for loop which prints number from zero to nine right let's see if it is working properly yes it is working as expected line one nine two nine three nine four and zero to nine right so now i want that anything return beyond this point should not execute so i'll just write return so we'll see what happens it will just print line one and line two it will go back here and execute this lines only anything written after return will not execute okay now you may want to remove it from here and use it here right let's see what happens it will just print zero because when the value of i was zero it printed zero then it encountered return so it will just terminate this function and you see that it is not printing one two three four five six seven eight nine because return keyboard just terminates the function all right so now i will see you in the next video so the next topic in our course is switch case like if statements switch case controls the flow of programs by allowing us to specify different code that should be executed in various conditions in particular a switch statement compares the value of a variable to the value specified in the case statements when a case statement is found whose value matches that of the variable then the code in that case statement is executed so let us see the syntax of switch case first of all we write switch then our open bracket then we write the variable which has to be compared when we close the bracket then we start a curly bracket then we write case space label 1 where label 1 is the number which has to be compared to the variable okay then we write colon note that this is a colon not a semicolon okay then we write the statements all the statements which we want to be executed when label1 is equal to the variable we write those statements in this block then we write break semicolon then we write case another label then all the statements then we write break we can write as many labels we want in our code and the last statement which we write is default colon all the statements break when none of the labels is true then the default statement is executed if we don't write default and none of the statement is true then nothing will happen all right so now let us see the flow of code in switch case here is the switch conditional statement then it will see case one if it is true it will execute the statements written in that block if it is false it will check case 2. if it is true then it will execute the statement if it is false it will go to case 3 okay and so on if none of these are true it will automatically jump to default and it will execute the default statements and after executing one of these statements the flow will go on to the statement which is written after the switch let's see in the coding right and the white setup we have written serial dot begin as usual then i'm writing int x is equal to 5 here x is the variable which we want to be compared okay then i'm writing switch then x here because i want x to be compared then i'm writing case 1 colon serial dot println choice 1 break similarly i've written 6 cases now can you guess what statement will be printed here since x is equal to 5 my guess is choice 5 should be printed because it is not equal to 1 it is not equal to 2 it is not equal to 3 and so on ok so let us see what happens right as expected it is printing choice five now if i change the value of x to let's say one so now choice one will be printed right now in case if we don't write break here the purpose of writing break here is that we want to exit as soon as one condition becomes true let's say if we don't write break here so it will continue to execute the statements since x is equal to 1 so now it should print choice one choice two and choice three okay because there's another break after choice three right choice one choice two choice three that's the purpose of break now let's see one more example without break let me use x is equal to 3 then it will start from here will not use break here it should print choice 3 choice 4 and choice 5. right as expected now the important thing which you need to notice is the variable which we use here and the labels which we use here should be of int or character type if you use float or if you use another array type it will just throw an error this switch case is a really handy tool if you want to write a lot of if else statements instead of that you can just write case 1 case 2 case 3. we can also specify ranges in case statements and how we do that let's say we want to take a range of 1 to 10. we'll write 1 we'll write three dots and then 10 okay then let's say 11 space to 20 okay let's say 21 3 dots to 30 then we write 31 space you have to write a space there okay if you don't write a space you will get an error so let's see these cases only let me write break again okay so now let us see three what will happen it will write choice 1 right now for example if i take 35 it should write choice 4 right so i guess you got an idea how easy it is right instead of writing if in else we can use switch case so i hope you understood this topic so i'll see you in the next video so at this point we are in a position to study about the two remaining data types which are arrays and strings these two data types are also very commonly used in the projects which are made using arduino so in this video we will look at arrays an array is a variable which can store multiple values of same type let's say we have a compartment of a defined size which has multiple containers in it like this and those containers can store same kind of information let's see this by an example to declare an array we first write the type of data which we are going to store in it let's say int then we write the name of that array let's say my array okay and then we write a square bracket and we write a number inside it that number denotes the size of that array okay let's say five so when we write this the compiler will create a space in memory where you can store those five integers okay now let's store some values in this array to do that we write equal to then we write curly bracket and then we will write five values which we want to store in it let's say one two three four and five semicolon each value is separated using a comma and each value inside an array is known as element of array okay and each element has an address of its own that address is known as an array index that index starts from zero okay let me show that to you again let's say we have an array of size 10 so 0 is the first index and 9 is the last index okay and the value which is present at that index is known as element let's see that again encoding so this is the zeroth element and this is the fourth element okay now let's try to access them how do we access them serial dot println my array and then the value which we want to use let's say 2 can you guess what will be printed here tell me 0 1 and 2 so 3 should be printed so 3 has been printed here for the first time users it must be really confusing that why it was started from 0. it is very simple because computer counts from 0. while humans are in a habit of counting from 1 that's why we are finding it a little confusing now let's change the values which are present inside this we'll take bigger and complicated values let's say 745 74 259 85 and 421 these are the random values which i have chosen now if we try to print the second element it will print 259 because 259 is present at second index okay while zero is this one first is this one second is this one third is this one and fourth is this one now we can alter these elements as well directly now let's say i want to change the value which is present at second index so we'll write my array 2 is equal to let's say 7 0 6 okay now the value of 259 will be replaced by 706 let's see let's print that again now it should be printing 706 right we can alter any value for that matter we can do our regular calculations on array elements as well let's say i have one variable in my variable okay now let's say i want to add some elements and store that value in my variable i will write my array okay 0 plus my array 1 plus my array 2. okay so now my variable will be holding the sum of 7 45 74 and 249 not 249 706 because we have altered it here so let's try and print my variable you can check on your calculators if the answer is correct or not so the sum of 745 74 and 706 is 15 25 1525 okay similarly we can also do multiplication subtraction division etcetera all the operators can be used here okay now we can also use elements of these arrays in for loops as well now let's use a for loop to print all the elements inside my array so how we will do that for int i is equal to 0 i less than 5 because size of my array is 5 then i plus plus okay so now what will we do is serial dot println my array then i okay so it should print all the elements which are present inside this array so it is here 745 74 706 85 and 421 right you can compare them here 745 74 706 because we updated the value here then we have 85 then we have 421 okay now can you tell me what will we do if you want to add 5 to all the elements of my array okay so now let's say we'll write for i is equal to 0 i less than 5 then i plus plus will take my array i is equal to my array i write plus 5 so now 5 will be added to all these elements so now 5 has been added so now let's print this again if we print this again so now we will see 5 has been added to all the elements let's see we are here it is printing from here right 745 plus 5 is 750 74 plus 5 is 79 then 706 plus 5 is 7 11 85 plus 5 is 90 421 plus 5 is 426 okay this way we have added five to all the elements now we can take some of all the elements in the array as well how will we do that let's say int sum is equal to zero we have initialized it by zero and for int i is equal to zero i less than five then i plus plus okay what will we write is sum is equal to sum plus my array i okay and after that we'll just print sum right it is printing 2 0 5 6. so you can check on your calculators if these values are summing up to 2 0 5 6 but just make sure you don't add these values you add the new values the new value was 750 79 7 11 90 and 426 these were the new values these okay if you add these values you will get two zero five six so now we have learned about how to use arrays how to access the values in an array and how to manipulate those values what we saw till now in this video were one-dimensional arrays that means they just have one dimension now there can be multi-dimensional arrays as well like two-dimensional arrays three-dimensional arrays like you can see here this pink box is a two dimensional array it has rows as well it has columns as well okay and then we have three dimensional arrays as well like it has depth as well it has rows it has columns and it has depth as well multiple two dimensional arrays make three dimensional arrays i just give you one use case where two dimensional arrays are used for example we want to store the pixel data image pixel data of a black and white image okay so we use two dimensional arrays and let's say we have to store pixel uh image pixel data of a colored image which also has rgb values so we use a three dimensional array for that in this video we will talk about strings strings are basically a sequence of characters whatever we write or you can say whatever messages which we write in our code are all strings let us see in the code the easiest way to spot strings is that you will find them written inside double quotes for example serial dot println this is my string okay this is my string is the message which is going to be printed on the serial monitor but in itself this is my string is a string okay now technically speaking a string is nothing but an array of characters and as we have already studied arrays it would be really easy for you to understand so now to declare a string we write char in the name of the string which we want for example i have written str 2 then as we start an array by a square bracket and a number inside it we similarly write it for string as well now example i want to write ashish which has a s h i s h as characters in it okay and the number of characters in ashish are 6. okay so what we will do is we'll always take one additional number to declare our string because an ending to the string is denoted by a special character which is known as a null character okay the compiler automatically takes that into consideration when you write a string or when you declare a character array okay in this case compiler will see okay the person is trying to declare a character array which has these characters so it will automatically add a null character at the end so now when we print this line it will print ash ish on the serial monitor okay another way is to declare that null character explicitly for example i will write chart str3 then 7 is equal to ash ish and then a null character this is the null character which is denoted by a backslash and a 0. this way the compiler identifies that okay this string has come to an end okay so it can differentiate between one or more strings without this character there will be problems the compiler may think that the another string which is coming after this string is a part of that same string there has to be some way to differentiate between different sentences okay so that's why we use a null character which is backslash zero another way to declare a string is we write char str4 which is the name of the string and then we write square brackets without a number inside it the compiler is smart enough to dynamically allot a number to this bracket it will see okay the user has written a string which has ash ish inside that it will automatically allot a memory of seven to this string because ashish has six characters in it and including a null character it will become seven okay we can also write it like this str five seven ashish simply in double quotes we don't have to work this much it is also okay if you take a larger memory for a smaller string but it is not okay to take a smaller size string let's say five for a longer okay now you will see it will only print five characters and then there is another way we will talk about it shortly but right now let me comment this out okay we'll just upload this code and we will see what we are getting all right so this is my string then it wrote ashish for this ashish for this ashish for this again and it printed it till here okay we forgot to write serial dot print ln for str right it just skipped the last character because we didn't give necessary memory to it so according to me this is the best way to start writing strings because obviously you do not know how many characters your string will contain and obviously you don't want to spend that time to count those characters right the compiler is smart enough to calculate the size for you okay now there is another way to write a string which is by using a string class okay here let me just comment this out this is a string class we are up calling a string class and we have named str7 inside it and we have just written whatever we want inside that let's say ashish and when we print this it will straightaway print this ok what's the benefit of using a class is that it is a built-in class first of all and it has a lot of functions inside it and those functions can be like we can compare a string okay easily we're just writing string name dot equals if it is equals it will return true and similarly there are many more built-in functions inside the string class for example i can tell you we can print a character at a particular sequence for example i wrote str and then serial dot println str can at 0 so it will print a because at 0 position a is present similarly there are many more functions like or starting with ending with and there is a function like we can convert the entire string which is written in lowercase to uppercase and vice versa uppercase to lowercase okay we can convert integers into string we can convert strings into integers okay so you may be needing all these functions while we perform our experiments strings are used to contain information messages or error messages or any logs okay we can store them inside strings and we can also display strings on our serial monitor on the lcd display or any kind of display okay we will now understand what are functions a function is a set of statements that take inputs and do some specific computation and produces some output the idea is to put some commonly or repeatedly done tasks together and make a function so that instead of writing the same code again and again for different inputs we can call the function we'll understand what i just said as we progress in the video so before anything else let us first understand how to identify a function in the code whenever we see something like this like void name and then there are brackets then there is a curly bracket then there are some statements written and then there is a curly bracket again this is the basic structure of a function okay where void is the return type that means we start writing a function by telling the compiler the return type of that function where void means that function will not return anything now instead of white here we can use int boolean or float or anything for that matter whatever we want to return from the function then we write the name of the function we can name it whatever we want like we saw digital write or pin mode they were the functions and the name of that function was pin mode and digital write and then in the bracket we write parameters or some people call them arguments if there are no parameters or arguments to the function we just keep this bracket empty or if there are parameters then we specify those parameters how do we specify the parameters we will see in the next video okay so i'll just basically tell you how do we call a function now like this in the blink example we called pin mode function to call a function we write the name of the function along with the parameters of that function in pin mode led built in comma output was the parameter of this function right similarly we have called digital right here and the parameters of that digital right was led built in comma i these are the two parameters of digital write so in the built-in libraries of arduino pin mode and digital write must be defined somewhere that's why arduino is able to understand what this exactly means otherwise it won't be able to understand right okay so it has made us really easy that arduino guys have defined this function somewhere for us okay and we just have to call it now let me tell you the benefits of using the functions the first biggest benefit of using the function is it helps us to reduce the code redundancy what that means is for example we want to do certain tasks so we may write the statements again and again again and again so that would make the code unnecessarily bigger so what we can do is we can make a function and we just call that function whenever we want to do that task you will understand this point better when we write our own functions then the functions make the code modular and reusable what that means is for example we are making a robot there must be thousands of lines to make the robot go forward so what we can do is we can make a function to make the robot go forward and we can use that function whenever we want to make robot go forward and you have written the code to make the robot go forward so now you are trying to make another robot so you can use that same code for the another robot as well so that means it became modular and it became reusable then the other advantage of using functions is edit provides abstraction and aids teamwork for example you're working in a team of four so one person can write the code for robot to go forward one person can write the code to go backward one person can write the code to move right and left and one can write the code to make the robot jump okay so once these functions are written so they can together be used as one code all right so these are the advantages of using a function now in the next video we will learn how to write our own functions so you will understand these points in a better manner so i'll see you in the next video so let us now create our very own function we will create a customized function to blink the led okay so let's do this after void loop we will write void let's say the name of our function is my blink function okay since there are no parameters in this we will leave the bracket empty now so let us just cut this code from here and paste this exactly like this in my blink function okay so now we have created a customized blink function now let's try to call this function in void loop right this way we have called our function in the void loop all right so when we upload this code on arduino you will see exactly the same results all right so let us see this so let us compare both the codes with the help of a simulator all right so we had this code right this is the same blink code let's see how it is working right as expected the led at pin number 13 is blinking right so now let us replace the code which we have just written we will just take it as it is right so now let's start the simulation so i hope you got my point about creating our own functions but now most of you must be wondering what's the point of making this right yeah i agree with you there is no point of making a function for this small task right but you will find it useful when you're writing really huge programs or you want to customize this thing i will show you a customization right now all right so now let's say you have more than one leds which you want to blink in a row okay so with your existing knowledge what would you have done you would have done it like this so you would have repeated these lines of codes and just replaced the pin numbers here right and then you would have declared these pins as output right but now what we can do is we can make a customized function let us do that we are here so let us now make a new function void my custom led right now what we will do is we'll specify a parameters in which we can tell the compiler led pin number so for that we will write int pin number okay so this function is going to accept one parameter which is pin number right so now let's do this we have this code okay so now instead of led built in we will write pin here okay so automatically whenever we call this function with the pin specified the compiler will replace this pin number with the specified pin number okay so let's do this let us just confirm what pins we have used we have used nine seven and two okay nine seven and two so let us say int led one is equal to nine hint led two is equal to seven and int led three is equal to two right so now we just have to declare all these pins as output led one okay see all right now we'll just replace them like this since we have declared them as output now what will be do is we'll just take this function okay we'll write my custom led with pin number led one okay then again we'll call this function with led 2 right and again we'll call this function and we'll just pass led 3 this time okay let's see if this compiles or not yes it compiles successfully now let us see the simulation of this code right so we have this code here let us start the simulation okay can you see that yep it's working perfectly first the led one which is connected to pin number nine is blinking and then the led connected to pin number seven is blinking then the led connected to pin number two is blinking so now let us see one more time what exactly happened here okay so with the pin mode we have declared all the leds led one two and three and i'm sure you know how to do this we have done this many times till now okay so now we have created a function void my custom led with a parameter of pin if you wanted more parameters here we'll just write comma and then end let's say parameter 2 right so this way we can specify endless number of parameters in our functions but since we don't need them we'll not use them okay then we have started to define what this function will do which is it will blink the led okay which led will it blink the led which we have passed as the parameter okay because we have used pin here for example if you had declared number here instead of pin then you would have used number here instead of pin got my point so now since we have finished declaring our function now we can use it successfully we'll just take this function and we will call it here with the parameter so now we have called it for the first time with led1 now what will happen the compiler will see okay a function has been used which is my custom led with the parameter led1 so it will see the value of led 1 which is declared as 9 okay so it will call my custom led with 9 so the control will go to my custom led with pin 9 and it will execute it with pin 9. once it is done executing it will come back it will see it has been called again with another parameter which is led 2 and the value of led 2 is 7 this time so it will go back and execute the function again with the value of 7 all right so this is the basic concept of function customization okay so instead of writing this code again and again with changed pin numbers we can just do this so i think all of you can agree with what i'm saying right now right one more thing which needs emphasis is i've used void here like i told you we use void whenever our function is not returning us anything okay so by the time you must have realized void loop and void setup are two functions of their own they are the main functions which are used by the compiler we have discussed about this earlier as well so now let us try to make another kind of function we'll open a new file and we will start writing our function first of all let's write serial.begin this is the most crucial thing to start communication with the computer okay so now let us try to write a function which would return us something okay so let's say it will return us an integer so we'll write in earlier we wrote void because we didn't want it to return something to us if you're not able to understand what returning exactly means you will just see that in a moment so we'll write in let's say we want to repeatedly calculate sums of two numbers okay or let's say sum of three numbers so we'll write in sum okay int a comma int b comma int c okay where a b and c are the three numbers which we want to take some off all right so now we will write a curly bracket and then we will write return a plus b plus c okay so whenever we call this function we'll get a value of a plus b plus c let's see how we get it in white loop or void setup you can use this function anywhere you want let's say we'll use this in white loop okay let's say we write int let's call it d okay equals to sum of 7 comma 8 comma 9 all right and let's print the value of d okay so what should it print the sum of 7 8 and 9 which is 24 right we can use it again as well with the different set of values let's say we'll make another function int e is equal to sum 1 comma 2 comma 3 let's print this we should get six right so we're getting six 24 and 6. it's repeating as we have used it in while loop so you don't have to worry about that okay one thing which i want to tell you is you can use this function like this as well serial dot println directly in serial print sum 4 comma 5 comma 6 right just make sure to close all the brackets here we have closed the bracket of the function sum here we have closed the bracket of serial.println okay so this is called as calling function inside a function this is a very useful thing which we can do in programming but first of all let us see the result if it is working or not right so we are getting 15 here because this function call here is printing its value okay now we can have more than one functions in our code now let us declare another function which would return a boolean value let's say bool um now we are making a function to check if a person is tall or not based on the height we enter as the parameter all right so let's name it is tall okay and int height okay so this function's name is install and the parameter is height okay so we'll write if height is greater than 100 return true okay or else return false okay so this function will just return true if the entered height is greater than 100 and if it is less than 100 it will say false okay so now you will see how handy it becomes when you have to check height of multiple people okay so now let's call this in white loop here we have while loop so let's say serial dot print ln is tall let's say 123 okay now let's just comment out the print statements so that they don't interfere okay this function is still being used in int d and e so you can say these functions are working simultaneously so we are just checking is tall or not okay let's see before i open the serial pointer you can guess your values right it's printing one because it has returned true so now let us take another value which is less than 100 serial dot println is tall 20. so now we should get false so it would be printing 1 0 1 0 because 1 is for install 123 and 0 is for install 20. so let us see that right 1 0 1 0 one zero one zero one zero okay so if you have to check height of multiple people instead of writing this code again and again for that specific height you can just call this function and enter the height so you can see how much work you have reduced for yourself right by writing the functions so this was all about writing a custom function there is no upper level to the complexity of the code which you write in your functions you can write endless number of lines in this function okay like i told you in the introductory video there may be a thousands of lines which are responsible for making robot go forward so in that case we can write void move robot forward and we can just define our parameters here and we can just write our statements like this right and this is our function this block of code won't make your robot go forward i'm just giving you an example of how do we write functions okay so whenever we call move robot forward in void setup or in void loop so the compiler will take this function and it will execute that alright so in the next video we will take a deeper look at some of the inbuilt functions which the arduino makers have made for us so see you in the next video now in this video we will talk about digital input and output what that means is we'll take a closer look at the functions which are made by the arduino makers to read or write any sort of digital data all right that data can be some sort of input given by external sensors and it can be some sort of output data which is given by arduino to other peripherals or components right so there are basically two functions which are digital read and digital right so let's take a look at digital right digital right is a function which we have seen multiple times before as well so can you recall what it used to do it used to write a state of low or high to a digital pin so we'll see digitalwrite is a function where digitalwrite is the name of that function all right and it has two parameters which are pin and state where pin is the pin number which we want to manipulate and the state is the value of either high or low where high means five volts and low means zero volts okay just make sure whenever we use digital write we have to declare that pin as output using pin mode all right and i have told you this earlier as well right next we will see digital read as the name suggests it will read the state of that pin okay so here digital read is the name of the function which we are calling and pin number is the parameter which we pass into it to read the state of that particular pin okay if it is high or low so we will now see a combined example of using digital read and digital write and how will we do that we will turn on one led if a switch is pressed okay so let's see how we do that so i have the circuit diagram here in which i have used a regular led with a resistance in series which you can see and here i have used a push button switch so before we move further let us first look at what a push button switch looks like from inside then only you will be able to understand it in a better manner so here is a push button switch all right which has four terminals out of which these two terminals are internally connected and these two terminals are internally connected as well okay so when we press the switch these two terminals get interconnected and these two terminals get interconnected when it is not pressed these two are not interconnected okay so let's get back to our example okay so the code is we have started with int button is equal to seven as we want to read the state of that button on a digital pin number seven all right in white setup i have declared pin mode led built in which is the led connected to pin number 13 as output and using pin mode we have declared the button as input because button will be telling its state to the arduino hence it is used as input okay serial dot begin 9600 as usual then in void loop i have written serial.println digital read and bracket button so here i am asking it to print the state of the button either it is pressed or not in the circuit which we have used we will get one on pressing the switch and we will get zero when we do not press the switch all right so let us see that works when i'm pressing the switch i'm getting one here when i'm releasing the switch i'm getting zero here look at the serial monitor here right now i'm not pressing it so it's saying zero when i'm pressing it it's saying one okay so most of you must have also noticed the led which is connected on pin number 13. when i'm pressing the switch the led is getting turned on and when i'm releasing the switch oops it's not getting turned off why we'll see that in the moment probably there is some error in the code i've done this intentionally so that you can pay attention to that okay so here i have written if digital read button is equal to high then digital write led built in to high when i press the switch write a high state on pin number 13 but i have not written what to do if the button is not pressed so let us do that now we will write else digital right led built in low okay so now let us start this again there was an error the closing bracket to void loop was missing so let us start it again all right so when i press it the led turns on and when i release it the led turns off okay so you guys must be wondering what's the point of doing this we can use a simple series connection and we can turn on the led why are we using arduino in that i absolutely agree with you you can do that very easily using a series connection but what you have learned here is you have learned how to use the functions like digital read and digital write you will be able to implement this learning in further projects the experiments which you do all right so now let me just quickly explain you the circuit which we have made here as most of you must be facing difficulty in that all right so here is the switch i showed you the diagram previously right so these two terminals are interconnected automatically when i press the switch this terminal will get connected to this terminal okay so i have taken a wire and connected one terminal to 5 volts okay and then i have taken a resistor and i have connected it to ground so here we have made a series connection of a switch and a resistor like this and from this point as all these points are interconnected we can connect them anywhere so at this point we're reading the voltage which is present between this and this point okay so as we want to read the state we have connected it to the pin number seven so when we press the switch this line will get connected to this line and entire 5 volts will drop across this resistance okay so hence the arduino will read it as high state and when the switch is released this point will be connected to ground via resistor so it will be read as low okay so i hope you understood the basic idea let me try to clarify it even more so this is the equivalent circuit of the circuit which you saw earlier okay you can consider arduino's 5v pin connected to this switch this is a push button switch right similarly these terminals are connected and when we press the switch these two terminals will get interconnected okay so in the series we have used a resistance and we have connected one point of that resistor to ground okay and we are trying to read voltage from this point can you see that we have connected this point to arduino's pin number seven all right so this way we are able to read the state of a switch now what advantages can you have after reading a state of a switch you can trigger multiple actions like running a motor or sending some message right you can do anything for that matter so now you have learned about digital input and output and in the next video we'll look at other functions like analog read and analog write so see you in the next video in the digital input output video we saw that we can use a function called as digital read to read the state of a pin if it is high or if it is low that means we can just read the binary situation if it is on or either of that's all but while making our projects we may have situations in which we have to read not just those discrete values we may also have to read the continuous voltages so for that we have analog read this function can be used only on the analog pins which are numbered a0 to a5 right before seeing how it works we'll have a basic idea of analog information in the environment so just look at this flowchart we have the real world which is our environment at the top we have some sensor which is converting that physical parameter into an analog signal that physical parameter can be temperature some distance or humidity or anything for that matter that analog signal is then converted into digital signals by an analog to digital converter arduino has a built-in 10 bit adc the short for analog to digital converter is adc so whenever i say adc you can understand that i am saying analog to digital converter so that digital signal which is converted from analog signal that is used by arduino to make decisions our arduino board is capable of measuring voltage between 0 to 5 volts and this is the conversion scale what i mean by that is since the resolution of adc on the arduino board is 10 bits it can hold up to 1023 values okay so if we compare that if the arduino reads 0 volts so the corresponding adc value will be 0 and if it reads 5 volts the corresponding adc value will be 1023 and we can use unitary method to find out values in between for example you can tell me what will 3.3 volts mean i hope i don't have to explain you the unitary method right this is the scale if zero volt means zero and five volt means one zero two three so one volt will mean one zero two three divided by five so if you know the value of one volt you can find out value of any voltage so now many of you must be wondering from where this value of one zero two three came from so i'll quickly explain it to you we have a cupboard here which contains ten boxes and each box can contain a value of either zero or either one okay so this is the cupboard and these are the boxes individual boxes to find the value of the cupboard we take the value of each box and we multiply it with the multiplier the multiplier is this 2 to the power 0 is 1 2 to the power 1 is 2 2 to the power 2 is 4 and similarly 2 to the power 9 is 512. so when we have values of all the boxes we take sum of all those values and we get the value of that entire cupboard so the maximum value which can be held by a cupboard in which all the boxes contain a value of one will be one zero two three how this box will evaluate to one why because two to the power zero is one and one multiplied by 1 is 1 okay so now the 2 to the power 1 is 2 and we multiply 1 by this so we get 2. so we will be summing all the values from here to here like 5 12 256 128 64 32 16 8 4 2 and 1 when you sum all these values you will get 1 0 2 3 you can try it on your calculator right now so now let's take a look at analog read function this function is simply used by calling analog read with the parameter of pin number where pin number is a0 to a5 so whenever you call this analog read function it will return you the value at red on that pin so let's see how we use that so now i have used two one kilo ohm resistances in series can you see that so now i have connected it to 5 volts this point is connected to 5 volts and this point is connected to ground so you can imagine a series circuit so now i want to read voltage across this resistance since both the resistances are equal the 5 volt will split equally on both the resistances so we should be getting 2.5 volts let us see that so in the code we have written serial.begin to start communication with the computer and then we have written serial.println bracket analog read a0 that means i want to read the voltage which is given at a0 so let's try this code so we are getting 511 which is nearly 2.5 volts we saw in that scale right so let's change the resistance and see what value do we get now let's make this value as 2 kilo ohms right so we got a change in value more the value of resistance more the voltage will drop across it we can see the value of voltage across this resistance is increasing if we make it too high it will increase it will also change if we change the value of this resistance let's say we make it 10. so now the voltage across this resistance will drop and hence we get a lower value now let's see this with a light dependent resistor this is a special kind of resistance which changes its resistance when light falls on it in most of the light dependent resistance the resistance will decrease if the light on it increases okay so let's try this replace this resistance with a light dependent resistor okay we'll connect this in series we'll take it here all right and we take this wire now you see this bar will represent the amount of light falling on it right i can control it like this so right now here it is total darkness and here it is very bright okay so when it is dark the resistance of this component is very high okay so the voltage which drops across this resistance will be very low you can see the value is very low right as soon as i increase the light on it you will see the value in the serial pointer is increasing can you see that because the resistance of this component is getting lower and and more voltage is dropping across the resistance okay now this is the principle of analog read now you can see how we can read the value of analog sensors using analog read all right so in the next video we will look at the another function which is known as analog right so see you in the next video in this video we'll take a look at analog write function which is another important built-in function of arduino all right so to begin with analog write i would firstly like to tell you that it has no relation with the analog pins or analog read function it is absolutely a different thing what exactly it is we will see shortly so to use analog write function we write analog write bracket pin number on which we want to use analog write and then we specify a value which ranges from 0 to 255. we close the bracket and use a semicolon right the important thing which we need to note is the pin numbers which we can use here are specific which are pin number 3 5 6 9 10 and 11 only okay while all of these pins except pin number five and six support a frequency of 490 hertz and pin number five and six support a frequency of 980hz we don't need to worry about this frequency right now i'll explain as we progress so before we dig deeper into this we need to understand a few terms like pwm pulse width and frequency and even before that we need to know what exactly this analog write function does the analog write function generates a square wave of a specified duty cycle on that specified pin the major purpose of using analog write is to control the brightness of led or to control the speed of a motor right the first thing which we need to see now is a square wave what a square wave is on the screen you can see a square wave is coming this is a square wave okay although this is a varying square wave now a square wave is nothing but a signal which has only two discrete values either on or off as you can see here it is on and here it is off and the time for which it is on and off is varying you can see here right i can understand it must be little difficult to comprehend but please stick with me okay so the next term is pulse width the amount of time for which a signal is on is known as pulse width for example in this wave this is the pulse width denoted by this arrow in this portion this is the pulse width denoted by this arrow in this portion this is the pulse width noted by this arrow all right the next term is duty cycle duty cycle is the fraction of period for which the signal was on right so we have five sections here with zero percent duty cycle twenty five percent due to cycle fifty percent duty cycle seventy five percent duty cycle and hundred percent duty cycle okay so in this section the voltage for entire time was zero okay so that means the duty cycle is zero percent in this section we have about one fourth of the time as on you can see that right for the one fourth time it is on and three fourth time it is off so that means the duty cycle is 25 in this section we have about half of the time as on time and half of the time as of time so here the duty cycle is 50 similarly here the on time is 3 4 hence the duty cycle is 75 and here for the entire duration of time the voltage was on hence the duty cycle was 100 okay now if we look in terms of arduino when we call analog write with a value of 0 it will give us 0 percent duty cycle that means the signal will be off for the whole time if you write analog write 64 then it will give us a duty cycle of 25 percent if you write 191 it will give a value of 75 percent and 255 will give us a duty cycle of 100 okay so now to summarize pulse width modulation or pwm is a technique for getting analog results with digital means digital control is used to create a square wave which is a signal switch between on and off all right and this on and off cycle takes place really fast and this on off pattern can simulate voltages in between full on and full off by changing the portion of the time the signal spends on versus the time that signal spins off all right so next thing which we need to look at is the pwm frequency okay let's see this is a wave which has a duty cycle of 50 that means half of the time the signal is on and half of the time signal is off changing the frequency of the signal will not change the duty cycle of the signal okay so let's say we have a signal of 50 duty cycle here and if we make the frequency to 400 so the waves will become closer that means now it will turn on two times and turn off two times right you can see here on and off earlier it was on one time and off one time okay but you see the duty cycle is still 50 you're getting my point so you see the duty cycle of the signal has not changed because it was still on for the half of the time and off for half of the time even if i make it 600 it is still on for the half of the time and off for half of the time okay so i hope it was clear to you and if in case it was not clear to you we'll take an example and blink the led and change the brightness of the led all right and we will also monitor it along with a digital oscilloscope an oscilloscope is nothing but it is a device which is used to analyze the signals okay so let's see that so here what we have done is we have taken an led on pin number 10 and correspondingly we have connected these wires to an oscilloscope to see the signal so let us see the code first so you must have noticed that we have not written anything in void setup and we don't have to use pin mode whenever we want to use analog write function okay so now in void loop i have written analog write pin number 10 and a value of 50 okay and then i have given a delay of one second then analog write 10 with the value of 100 then 150 then 200 and 255 here we will get a duty cycle of 100 okay so now let's see how it looks like pay attention to the wave which is coming on the oscilloscope and also note the brightness of this led i'm starting the simulation let's see can you see that the brightness of led is increasing gradually as the code progresses and it gets backs to 50 and then it again increases also note the pulses which are coming in the oscilloscope so now we will see an example in which we are changing the brightness of led in steps of five we will also look at the pwm wave simultaneous let's look at the code first we have defined an led pin which is connected on pin number ten in white setup we don't have to do anything in void loop we have written four which is a for loop and we have defined a fade value in fade value is equal to 0 and you have to do this till the fade value is less than or equal to 255 okay and then we are incrementing the value of fade value by 5. this is an another compound operator you can say it is similar to writing fade value is equal to fade value plus 5 all right so now we have written analog write and we have written led pin here which is pin number 10 with the fade value okay so how it will go it will write zero here then after the delay of 30 milliseconds it will write five year then after a delay of 30 milliseconds it will write 10 here then 15 and 20 and 25 and so on till 255. once this value becomes 260 you know in the last iteration the value will become 260 so this condition will become false and it will exit this for loop and come inside this for loop in this for loop we have used fade value starting from 255 and we have written a condition fade value greater than equal to zero that means you have to do this till the value is greater than or equal to zero okay then we have to use fade value minus equal to five and this is similar to writing yes you guys did right it is similar to writing okay so now it will start at 255. after a delay of 30 milliseconds it will write 250 and 245 then 240 and 235 and so on till 0. in the last iteration when the value is 0 the value will become -5 and the condition will become false so this loop will exit and it will come back into this for loop and this will repeat endlessly till we turn off the arduino let's start the simulation note the brightness of the led and note the signal which is coming in the oscilloscope let's see okay it's interesting right the brightness of leds increasing from 0 to 55 and the pulse width is changing when it is 0 you can see it is entirely off and when the duty cycle becomes 100 it becomes entirely on the pulse width is changing here so i hope you have understood this really well the last topic which we need to learn about before starting our experiments is the arduino libraries so libraries are a collection of code that makes it easier for you to connect to a sensor display or a module for example there is a built-in library to control a liquid crystal display that library makes it easier for us to talk to that display right for example you can see that library will control what character will come at what position apart from this library there are many more libraries which are available for download on internet and there are also built-in libraries for example the built-in libraries are these libraries which are already present in arduino id i'll just name a few like ap rom is used to read and write elements from the permanent storage a gsm library is used to connect gsm or gprs network with the gsm shield the liquid crystal library which you just saw controls the liquid crystal display which is used for reading and writing data on sd cards a server library will be used to control server motors similarly there are many more libraries which are already there on arduino the code which is written inside those libraries can be written by someone else or you can write the code on your own as well once we have the library in our arduino id it's very simple to use that library i'll just show you how so this is our code let's say we have to use liquid crystal library so we will write hash include liquid crystal dot h okay whenever we use this library we'll be able to use the functions which are written inside the liquid crystal library okay similarly if we have to include another library for sd cards we'll write sd dot h okay this will include the sd library in our code we'll be able to use all the functions which are written in sd library i would also like to tell you that the functions which we were using like digital write were also declared in some library i will just show you how it was written so there's a built-in code which is written inside wiring underscore digital dot c which is located at this location see program files arduino hardware arduino avr course arduino and then finally the file now if we see the declaration of digitalwrite we will see that this is how a digital write function is declared in that library okay you just see that void digitalwrite pin number and value okay where pin is the pin number and value is high or low it accepts two arguments right we sorted while calling this function so this is how the digital write is called we'll not get into details of this function as it may be very complicated for you to understand okay similarly digital read has been declared here this is a declaration of digital read similarly pin mode is also declared right so i hope you were clear with libraries and i hope you understand how using libraries saves a lot of our time okay so in the next video we will see how to add additional libraries in our arduino ide so see you in the next video so now let's see how do we install external libraries in our arduino ide there are basically two methods one is through the library manager and one is through adding external zip files okay let's look at the library manager first we will go to sketch include libraries then manage libraries it will open the library manager this is the library manager it will update all the libraries which are already there in their database and it will show us the list of available libraries for us you can see there are a lot of libraries already there right these are all the libraries which are present in their database right now so let's say we need a library for dht 11 which is a sensor which captures temperature and humidity of our environment so we will write dht11 here and it will search all the libraries which are available for dht 11 by different authors so let's say this is a very reliable library by adafruit which i've already installed okay so just in case you have not installed it already you will get install here you can select the version here which version you want to install usually this is the latest one and then you just click on install and it will install the library in your id okay once it is installed we can use it in a regular manner hash include dht dot h okay if this dht library was not installed in my id it would not appear in this orange color okay let's take an arbitrary name include ashish dot h since there is no library named ashi so this ashish is not coming in orange color although this is not a very hard and fast rule you might not get orange color in some libraries but it is a really good indication that you're working in a right direction okay so now let us look at the other way which is adding them manually let's say again we want a library for our dht11 sensor we will write dht 11 library for arduino usually libraries are available on github you may also find zip files on some other websites but github is usually preferred as you would get the latest version of the library from there so again adafruit has hosted their library on github so let's click on that and then we'll go to clone or download we'll click download zip and it will start downloading it you have to see where you have downloaded it you have to go to sketch include library then add dot zip library in my case i downloaded it in my h drive so it is here we'll click on this and just click on open in my case it was already installed so it gave me a warning a library name dht sensor library master already exists okay but in your case it will say the library has been successfully installed so these are the basic two ways in which we add libraries at this point i can really say that you have reached a milestone and you are in a position to dig deeper as you have so many tools under your belt now we have covered the foundations of electronics then we covered what is an arduino board and then we discussed what is inside arduino we covered the difference between the microcontroller and microprocessor then we saw the arduino ide how it works and how we use it we also saw an online simulator which can be used if someone doesn't have an arduino right now then we saw how to use a breadboard then some topics like input output analog digital etc and then we started arduino programming and i tried to cover all the essential topics which are required for you to start your own projects from this point you can try making various led patterns you can try to work with a seven segment display which can be used to display different digits on it then you can try to interface sensors like ultrasonic sensor or ir sensor and try to read their values using the built-in functions which we have studied already then you can try to control the motion and speed of a dc motor then i recommend you to try using a servo motor after this you will be in a position to make robots like a line falling reward a robot which is capable of following light lines over a dark background or vice versa you can also make obstacle of winning robots then you can make remote control cars like this and once you get comfortable with this you can also make complicated robots like a hexa port like this which has several servo motors used in it right which is moving forward you can also check out some posts at my instagram ashish underscore things and check out some of my other projects as well so i hope that you found this course really useful to begin your journey with arduino thank you so much for your time and have a nice day ahead
Info
Channel: freeCodeCamp.org
Views: 435,009
Rating: 4.9538193 out of 5
Keywords:
Id: zJ-LqeX_fLU
Channel Id: undefined
Length: 244min 22sec (14662 seconds)
Published: Tue Jun 08 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.