welcome to the Arduino programming for absolute beginners course in this video we'll start from scratch and introduce you to the fascinating world of Arduino and programming whether you are a complete beginner or have some basic knowledge this course will provide you with a solid foundation to begin your Arduino Journey let's begin with an overview of Arduino and its applications Arduino Is An Open Source Hardware and software platform designed to simplify the process of building electronic projects first let's start with the hardware it consists of a circuit board called Arduino board this board is the heart of any Arduino project and the best part is that its design is open source this means you can freely access and download the design files to understand how it's actually made so you have the freedom to experiment learn and create projects without any limitations now let's discuss the software aspect the program that controls and runs the Arduino board is called the Arduino software it is also called Arduino IDE which stands for integrated development environment this user-friendly software enables you to write compile and upload code to the Arduino board the great news is that the Arduino software is also open source and completely free to download from the Arduino website now let's take a closer look at the Arduino board itself there are different types of Arduino boards available but they all have one element in common the microcontroller you can think of it as a small computer on a single chip it's like the brain of the Arduino board although different Arduino boards may have different types of microcontrollers but their main purpose Remains the Same the main purpose of the microcontroller is to run your code and control the connected Hardware the most commonly used microcontroller in Arduino boards is the artmiga 328p it has digital input output pins analog pins and a range of other impressive features to provide easy connectivity the pins on the microcontroller are directly connected to the sockets on Arduino board these sockets are called headers headers simplify the process of connecting external devices and modules to the microcontroller without the need of any soldering they also allow us to connect additional circuitry to the Arduino board quickly and easily when prototyping with a breadboard breadboard plays an important role in making prototype your circuits it allows you to experiment with various electronic components and connections before making any permanent circuit you can easily link the breadboard circuits directly to the microcontroller using the headers on the Arduino board the headers have been arranged around the board and labeled for easy to use they are divided into three main groups digital pins analog input pins and power pins each group of pins provide some special functions let's begin with the digital pins Arduino Uno board comes with 13 digital pins think of these pins as the on off switches of the Arduino these pins are used to send and receive digital signals digital signals have only two states either off or on in electrical terms off State represents zero volts while on state represents 5 volts with no intermediate values this Simplicity makes digital pins perfect for various tasks such as reading button presses controlling LEDs and communicating with other digital devices next Arduino boards features analog input pins unlike digital pins that handle just on off signals the analog pins can work with analog signals which provide a continuous range of values between 0 and 5 volts this feature is especially useful when dealing with sensors that produce varying voltage levels for example temperature sensors light sensors and many others fall into this category can we use analog input pins as digital input output pins there is a common misconception that analog input pins cannot be used as digital pins but in reality this is not true all six analog input pins on the Arduino can be used as digital input output pins so in total you have 19 digital pins available to work with this flexibility allows you to connect additional digital devices to your project you might have noticed that there are no pins labeled as analog output pins however in reality there are analog output capabilities hidden among the digital pins if you take a closer look you will notice that some of the digital pins are labeled with the tilde symbol these pins are called pwm pwm stands for pulse width modulation and it is a clever technique that allows you to simulate analog output using digital pins in a highly effective manner although these pins are technically digital but they offer a unique way to control various aspects of your projects such as controlling the speed of the motors dimming LEDs and create smooth fading effects as if they were actual analog output pins lastly the power pins provide a stable and reliable electrical supply for the Arduino and all connected components ensuring your projects run smoothly the Arduino can be powered using different ways one way to power the Arduino is by using the USB port you can connect the Arduino board to your computer using the USB port this allows you to upload code to the Arduino and also test your projects it's a convenient option when you are working with the Arduino and your computer together another option is to power the Arduino with batteries or an external power supply this method is useful when you want to use the Arduino independently without connecting it to a computer or USB power source to power the Arduino this way use the DC Barrel Jack on the Arduino board make sure to provide a voltage between 7 to 20 volts however it's recommended to use a 12 volt power supply when using the DC Barrel Jack there's a lot more to explore with the Arduino board but for this course this information is enough to begin your Arduino Journey to write and upload code to the Arduino you'll need a software tool called the Arduino IDE there are different versions of the Arduino IDE available on the official website however for this course we will use Arduino IDE 1.8 it is a beginner-friendly software tool and works well with any Arduino board it provides a simple platform for beginners to get started with coding and uploading code to the Arduino boards you can find the download link on the right side make sure to choose the correct version for your computer click on the download link that matches your computer's operating system the download will start automatically and the file size is relatively small so it shouldn't take too long now open the downloaded file to start the installation process the installation process is straightforward and user-friendly just follow the on-screen instructions and you'll have Arduino IDE installed in no time after installation is done you can launch Arduino IDE from the start menu or the applications list now the Arduino IDE is installed and running so navigate to the files menu from the drop down menu select preferences in the preferences window first increase the font size a little bit to make the code more readable next let's enable line numbers to help you keep track of your code you simply select display line numbers checkbox and the line numbers will appear on the left side of the Arduino IDE now let's talk about the path setting this is the location on your computer where your code will be stored so whenever you save your code it will be stored in this location you don't need to worry about the other settings for now simply click the OK button to apply the changes you have made next it's time to connect your Arduino board to your computer using a USB cable make sure your board is powered on if you will see the green LED indicator it means the Arduino is powered on now you are ready to upload your first program onto your Arduino before you upload the program you need to tell the Arduino IDE what kind of Arduino you have so go to the tools menu and then go to the boards option here you will find a list of supported Arduino boards simply choose the one you have for example I will select Arduino Uno the last step before programming is to tell the IDE on which com Port your board is connected to so again navigate to tools and then go to the port option as you can see the Arduino board is connected on com port 5. you just select the com port and you're good to go now we are ready let's make sure that everything is working as expected first navigate to the files menu from there go to the examples here you will find a collection of code examples to perform various operations you can try it yourself from the basic section you just open the blink sketch this is a simple sketch that blinks the onboard LED the Arduino have an LED connected to the PIN 13. and we are going to Blink this led before uploading the sketch to the Arduino board let's ensure there are no errors in the code you just click on the verify button and Arduino IDE will check for any mistakes in the code if everything is correct you will see a done compiling message in the bottom status bar if there are any errors in the code the error message will be displayed here now it's time to upload the code to the Arduino board you just click on the upload button and Arduino IDE will transfer the code to the Arduino board as you can see the onboard LED start blinking it means everything is set up perfectly now we have tested our setup and its works fine so let's dive into programming first open a new sketch in the Arduino IDE here you can see a program with some code is already written for you a program is just a set of instructions and it performs specific tasks such as turning on an LED or driving a motor in the context of Arduino a program is also called the sketch Arduino uses the programming languages C and C plus plus which are the most widely used languages of all time these are extremely powerful and versatile languages however programming in c or C plus plus can be a bit challenging for beginners the good news is that Arduino follows a structured programming approach which provide you a well-defined way of writing a program let's take a closer look at the basic structure of an Arduino sketch an Arduino sketch is divided into two main parts void setup and void Loop these functions are very important and must be included in every Arduino sketch without these functions the sketch will not compile and it will gives you an error message let's see what happens when we try to compile the code without the setup function the Arduino IDE will show an error message in the console it reminding us the importance of having the setup and the loop functions it is important to note that every Arduino function has its own name for example the name of this function is setup if you notice the color of the Void setup function is orange this color indicates that the Arduino software recognizes it as a built-in function however if we change the case of the setup function then you will notice it turn into black color this means that Arduino doesn't recognize it anymore the reason for this difference is that the Arduino code is case sensitive so always use correct case otherwise the program will gives you an error message this is an important point to remember especially during late night coding sessions the code for each function is written within curly brackets it is called the body of the function the opening curly bracket indicates the beginning of a function while closing curly bracket indicates the end of a function it's required to have matching sets of curly brackets for each function if you miss one of the brackets then the code won't compile and you are presented with an error message that looks like the one shown in the console the setup function is an important part of an Arduino program and it runs only once during startup this means any code written inside this function will execute just once so this function is perfect for setting up initial configurations or performing tasks that need to happen only once the next function is the loop function this is also highlighted in Orange so the Arduino software recognizes it as a built-in function instead of setup function which runs only once the loop function runs over and over again the code written inside this function runs continuously until you press the reset button on the Arduino board or you just remove the power inside setup and loop functions you will notice some text written in Gray these are comments comments are written in plain English and provide explanations about the code the Arduino software completely ignores comments during compilation and uploading so they won't affect how the code runs this comment is started with the double slasher symbol it means it is a single line comment single line commands are perfect for adding short explanations to a specific lines of code there is also another type of comment called multi-line comment multi-line comment starts with a slash followed by an asterisk symbol and ends with an asterisk symbol followed by a slash multi-line commands are used when you need to add more details for the code that expand to multiple lines comments are helpful to improve the readability of the code when someone else reviews the code clear commands can help them to understand the logic and purpose of each section of the code all right now we have covered the basic structure of an Arduino program now we are ready to dive into writing our very first Arduino code as you know Arduino comes with a built-in LED which is connected to pin 13. our goal is to create a simple sketch that makes this led blink the first function you will need is the PIN mode function this function allows you to configure a specific pin on the Arduino board for either input or output I have used PIN mode function inside the setup function because it needs to run only once when your program starts you'll notice the word pin mode is highlighted in Orange as I mentioned earlier it tells you that Arduino already knows about this function if you look at the case of the pin mode the letter M was written in uppercase and there are no spaces in the function name this is known as camel case style where the first word is in lowercase and the first letter of each next word is capital for example digital read analog write and so on so the pin mode is written in camel case to follow the standard naming conventions in Arduino it's not only increase readability of the code but also ensures that Arduino recognizers and interprets these functions correctly this function needs two parameters the first one is the PIN number this is the PIN number on your Arduino board that you want to configure for example the onboard LED is connected to the pin 13 so if you want to control the LED you'll set the pin number as 13. the second parameter of the pin mode function is called mode this parameter indicates how you want to configure the pin there are two options you can choose from input and output if you set the pin as an input it means the pin can receive signals from external components like sensors this is perfect for Gathering data from the outside world on the other hand if you set the pin as an output it allows you to control external components like LEDs or Motors in our case since we want to control the LED so we will set it to Output notice that the word output is written in uppercase and the color is blue whenever you see uppercase words in blue it means they are predefined constants in the Arduino language we will explore more about constants in the upcoming sections that's all you need for setup next moves on to the loop function the first function you will need here is the digital write function with this function you can send a digital signal to a specific pin the orange color tells that the Arduino already knows about this function so we are in good hands this function needs two parameters the first one is the PIN number this is the PIN number on your Arduino board that you want to control in our case the pin number will be 13. now let's talk about the second parameter which is called signal value it can be either high or low as we have discussed earlier the digital signals have two states either off or on in electrical terms these can be referred to as either a high or low value since the Arduino Uno requires 5 volts to run so the high value is equal to 5 volts and low value is equal to 0 volts so we will set the signal value to high it will send 5 volts to the pin 13 which is enough voltage to turn on the LED so with just a single line of code we can light up the LED connected to pin 13. next we will add a delay of one second the delay function stops the program for a specified amount of time in milliseconds in our case the value is 1000 milliseconds which is equal to one second during this time nothing happens and the LED remains on after the delay we use the digital write function again but this time we will set the signal value to low this statement tells Arduino to send 0 volts to pin 13 which will turns the LED off finally we will add another delay to keep the LED off for one second since this code is placed inside the loop function so it will run over and over again and it will give you the blinking effect before you upload the code let's cover one more important concept you'll notice that each line of code ends with a semicolon the semicolon plays an important role in the C language the semicolon tells the compiler about the end of the statement it's like a full stop in a sentence which tells the compiler where the one statement ends and the next one begins if you forget to include the semicolon the compiler might get confused and show errors when you try to compile your code so to keep your code running smoothly always remember to include a semicolon at the end of each statement now let's upload this sketch to the Arduino board and see the magic of the blinking LED in action as you can see the LED is now blinking just as we expected it shows the code is working fine and our LED is dancing like a disco star how cool it is but remember this is only the beginning of your Arduino Journey let's take a closer look at the code and understand how the microcontroller runs it when you power on the Arduino the program starts running it begins by entering the setup function it executes the instructions one by one ignoring any comments in our case there is just one instruction which sets pin number 13 to Output this tells the microcontroller that we want to interact with external components like LEDs after executing the instruction there is a closing curly bracket it indicates that the setup function ends here as the setup function runs only once so the microcontroller moves on to the loop function inside the loop function the microcontroller repeatedly executes the instructions to create the blinking effect for the LED the first instruction tells the microcontroller to send 5 volts to pin number 13 which lights up the LED the next instruction tells the microcontroller to wait for one second the microcontroller follows this instruction and waits for a second then it moves to the next instruction which tells to send 0 volts to pin number 13 which will turn off the LED and once more it waits for one second after this we have a closing curly bracket it indicate the end of the loop function now the microcontroller goes back to the start of the loop and repeats the instructions from the beginning there you have it the beauty of the code lies in its Simplicity that keeps everything running smoothly I have used tinkercad to create the code simulation tinkercad is a powerful online tool that offers a user-friendly environment to design circuits and it provide a simple tool to write code for Arduino boards denka CAD has everything you need including sensors and a breadboard making it perfect for beginners to experiment and learn tinkercad also provides a built-in code editor it is fully compatible with the Arduino IDE you can simply paste your code here and run it by clicking on the start simulation button tinkercad comes with a powerful built-in debugger that will take your learning experience to the next level you can debug your code by adding breakpoints on specific sections of your code breakpoints allows you to stop the program's execution and examine what's happening let me show you how they work to set a breakpoint simply click on the left side of the line where you want to pause the code this blue indicator tells that the breakpoint is active for this line now let's see it in action so click on the step over button to execute the code line by line this incredible feature makes it easy to understand how your code behaves at each step which is extremely valuable for Learning and troubleshooting so we will use it a lot throughout the course now let's move on to the next section in this section we will create a simple LED Chaser using an Arduino take a look at the wiring diagram here I connected six LEDs to the Arduino with each LED connected to pins a to 13. the code for this project is very simple in the setup function first we will use the pin mode function to set all six pins as output since the first led is connected to pin 8 so we will set pin 8 as an output and then we will repeat the same process for the other pins to avoid rewriting the code multiple times we will duplicate the same code and then simply adjust the PIN numbers where the LEDs are connected remember the LEDs are connected to pins 8 to 13 and we have set all these pins to Output now let's move on to the loop function here we will use digital write function to turn on all the LEDs together for example we will turn on the LED connected to pin 8 by setting its signal value to high then we will repeat this process for other LEDs by setting up their PIN numbers to create a blinking effect we will add a delay of 300 milliseconds after turning on all the LEDs this delay will keep the LEDs on for 300 milliseconds after the delay we will turn off all the LEDs by setting their signal values to low finally we will add another delay of 300 milliseconds it will keep the LEDs off for 300 milliseconds now let's test the program to see if the LED is blinking correctly as you can see the LEDs are blinking just fine let's move back to the code to make the code more organized we will add a comment line to separate the code for the two patterns now for the second pattern we want the LEDs to turn on one by one and then turn off one by one this will create a cool LED Chaser effect to achieve this we will add a delay of 300 milliseconds before turning on each LED this delay will give the appearance of LEDs chasing each other after turning on all the LEDs we will turn them off in the same sequence and with the same delay of 300 milliseconds instead of typing the exact same code over and over again I am reusing the same code that we write earlier this trick helps you to write code quickly and saves your time and efforts now the code is ready we have added two LED Chaser patterns inside the loop function let's give it a try by uploading it to Arduino as you can see the LEDs are blinking with both patterns all LEDs turning on together and then chasing each other in a sequence the coolest part is that we have only used two functions to create these patterns digital write and delay by using these functions in different ways you can make really cool LED patterns that chase each other however if you keep adding more patterns directly into the loop function it can get really long and messy it becomes hard to handle all this code and it is always difficult to make changes in the large code to solve these problems the C language has a simple solution called functions functions are like the building blocks of your code that make your life easier they help us to keep our code neat and organized you can make the functions in two simple steps first create a function and then call the function let's get started by creating a function it's actually quite easy just copy the setup or Loop function from a blank sketch and then paste it into your code after that give the function a new name you can choose any name you want when naming functions always use a name that describes what the function does I have named this function as blink all LEDs because it says exactly what the function will do remember blank spaces are not allowed in the function names let's understand where you can put your functions in the code there are two suitable places in your code to put your functions you can either place your functions above the setup function or below the loop function the choice is entirely yours no matter where you put the function the working of the function Remains the Same next let's add some code into our newly created function we will not write any new code for this function we have already written some code in the loop function it is the code that makes all the LEDs blink together we will just copy this code make sure you have deleted it from the loop function don't worry about the code for the second pattern we are going to leave this as it is now move back to our newly created function and then paste the code here that's it your function is now ready to roll and when you use this function all the LEDs will blink together with a 300 millisecond delay now we have completed the first step of creating a function now let's move on to the second step called the function to call a function means to start using it you can call the function anywhere within the setup or Loop all you need to do is copy the function's name along with the parentheses and then paste it inside the loop function this is the same place where we deleted the code earlier before going further let's take a quick test by uploading the code to the Arduino as you can see the two LED patterns are still working even though we have removed the LED blink code from the loop function this is due to the function we have created now let's understand the inner working of the function this will give you the idea about what happening behind the scenes to understand the working of the function I have put all the code in the debugger now let's run the code step by step the microcontroller starts executing instructions from the beginning of the setup function after that it moves on to the loop function inside the loop function the very first instruction is our function this instruction tells the microcontroller to go inside this function execute all the instructions within it and then return back let's click on the step over button to see what happened as you can see the microcontroller is now inside a function here it will execute all the instruction One By One The microcontroller is very fast and it only stops at delay function otherwise the instructions without delay will execute very fast you cannot even see it now we have reached at the end of the function from here the microcontroller jumps back to the loop function as you can see it then continues executing the statements after all function the ones related to the second pattern and after executing the remaining statements in the loop the microcontroller again jumps at the top of the loop and from here it will again jumps to all function and this cycle continues until you power off your Arduino now you have the basic idea about how functions work now let's explore the true magic of the functions so first we will create a new function let's name this function as pattern 2. remember always use descriptive names for your functions instead of writing new code for this function we will reuse the existing code from the loop function so just copy the code and then remove it from the loop function now we have only one function remaining inside the loop let's move back to our newly created function and paste the copied code here that's it the function is ready now we have two functions to play around the first one is Blink all LEDs which we have created earlier and the second one is pattern 2. to add additional LED patterns in this code I have already created a third function I have named this function as back and forth it makes the LEDs blink in both the forward and backward directions this adds a neat effect to our project so feel free to experiment and have fun with these functions to make different LED Chaser patterns we will call these functions from the loop function we will start by calling the blink all LEDs function three times this will make all the LEDs blink together after that we will call the pattern to function one time this will turn on the LEDs one by one and then turns them off in the same order then we will use the blink all LEDs function again to Blink all the LEDs three more times finally we will call our third function the one that makes back and forth pattern now the code is ready if you look at the Loop function it looks much simpler now there is a very little code inside the loop function and it is easy to understand and manage this technique helps you effectively handle a large volume of code making it more manageable even for beginners now it's time to upload this code to the Arduino board now look at the LEDs closely these should blink using three different patterns to enhance the blinking effect I have adjusted the delay between blinks to 200 milliseconds making it a bit faster and smoother that's all about the functions now let's move on the next section in this section we will explore an exciting tool in the Arduino IDE called serial monitor it is a powerful tool that allows communication between your Arduino and your computer it enables you to monitor the sensor values in real time directly on your computer screen the best part is it works in both ways so you can exchange data between your computer and Arduino this tool is particularly useful for finding and fixing issues in your code let's write some code to Kickstart the serial communication with your computer first in your Arduino sketch you will need to add the serial dot begin function this function is responsible for starting the serial communication now you might wonder about the number inside the parentheses this number is the speed at which your Arduino communicates with the computer it is also called baud rate the standard speed is typically set to 9600. in simple terms this line of code tells the Arduino to start communication with the computer at a speed of 9600. after establishing the serial communication you can use the serial dot println function to send data from your Arduino to the serial monitor let's give it a try in this example we'll send a simple hello world message to the serial monitor inside the print Ln function I have written the text hello world within double quotation marks in programming when you want to print text you always need to write it within the double quotation marks now let's test The Code by uploading it to the Arduino the code is uploaded and to check the output you need to open the serial monitor you can open it from the tools menu by clicking on the serial monitor option you can also open the serial monitor from the top right corner by clicking on this button here is a keyboard shortcut for the lazy ones like me press Ctrl shift plus M from the keyboard and it will open the serial monitor quickly here first you need to select the baud rate which is 9600. since we have set the baud rate to 9600 in our code it's required to match it in the serial monitor as well if you select a different board rate you may see unexpected or Garbage data in the serial monitor now take a look at the output in the serial monitor it's just as we expected the hello world message is beautifully displayed on the screen that's a quick introduction of the serial Monitor and we will use it a lot in the upcoming sections of this course now let's move on to the next section of the video in this section we will learn about the variables a variable is like a container that holds information or data think of it as a labeled box where you can store different types of data you can create a variable in two simple steps first Define the data type of the variable and then give your variable a name with these two steps you can create different types of variables let's create our first variable first we will Define the data type of the variable here the term int is a keyword it tells the Arduino that we are using the integer data type integer is a data type that is used to store whole numbers a number with no decimal point is called whole number however if you want to work with decimal numbers then you will need a different data type called float a number with decimal point or fraction is called float number or decimal number next give your variable a name that not only makes sense but also effectively represents the type of data it will hold I have set the variable name as variable 1. this name acts like a label on your box remember spaces are not allowed in variable names now the variable is ready to hold values since we did not store any value in the variable so the variable will automatically set to a default value which is usually 0. let's confirm this with a quick test to observe the value stored in a variable you can use the serial dot println function you just provide the variable name inside the parentheses this will print the value of this variable on the serial monitor as you can see even if we haven't assigned a value but still it's showing the value 0. this is because Arduino automatically sets its default value to 0. next step is to store data into the variable the process of storing data into a variable is called initialization this is like putting things into your labeled box let's say if you want to store the value 50 in the variable then you can write variable 1 equals 50. this statement will store the value 50 in the variable you can also assign a value when you create the variable you can do this by putting the equal sign at the end of the variable name and then enter the value that you want to store in the variable now let's upload the code and observe the value in the serial monitor as you can see the value is displayed correctly this way you can effectively store and display values using variables next let's explore a special type of variable called constant you can create a constant in one simple step you just use the const keyword when you create the variable and this will convert the variable into a constant think of it like a box where you can place a value but once that value is inside it's locked and you cannot change it after creating a constant if you try to change its value then the compiler will gives you an error message it looks like the one shown in the console constants are great for storing unchangeable values such as PIN numbers PIN numbers remain fixed throughout the program execution it ensure the security of your data so the constants are very useful when you want to keep some data read only now let's talk about another important concept about variables called scope of a variable scope refers to the area of your code where a variable is visible and can be used you can imagine it as the boundary of your variable for example if you create a variable inside setup function then it can only be used inside the setup if you try to use this variable inside another function such as Loop then the compiler will gives you an error message this error tells you that you cannot use a variable outside its scope the same way if you define a variable inside the loop function then it can only be used within the loop function however if you want to use a variable across your entire code then you can place it at the beginning of your code this kind of variable is known as Global variable and you can use it anywhere in your code including the functions now you have idea about the variables let's take a closer look at the different data types that Arduino offers let's begin by exploring the Boolean data type imagine it as a switch with only two positions true or false this Simplicity allows you to store only two values within it which correspond to either one or zero in programming we use Boolean variables for decision making and checking conditions in our code next let's explore the byte data type think of it as a small container designed for holding integer data it has a limited capacity it can hold values in the range of 0 to 255. this type of data storage is particularly handy for tasks that involve smaller numbers such as storing PIN numbers for example Arduino Uno has 21 gpio pins and the byte data type can efficiently manage these PIN numbers without unnecessary memory usage let's move on to the in data type n stands for integer and it is used to store whole numbers think of it as a medium-sized container which is designed to store integer data this capacity allows you to store data from around minus 32 000 to plus 32 000. this is suitable for handling a variety of everyday numbers in your projects now when you need to work with very large numbers the long data type comes to the rescue the long data type acts as a larger container it can store values up to around 2 billion which is quite a lot next in the row is float data type this data type is specifically designed for storing real numbers which include decimal points or fractions it is used for accurate and precise measurements that involves quantities which are not whole numbers such as temperature readings or sensor values the next one is double data type double is similar to float but it can handle even larger real numbers if you need to deal with very precise measurements or calculations double can be your choice the last one is character data type it is used to store individual characters like letters or symbols when working with the character data type it's important to remember that you need to enclose the character within single quotation marks let's understand another important concept called garbage value this is an error in your code that the compiler is unable to identify it so it will not show any error message in the console such errors fall under the category of logical errors since the compiler cannot catch these errors they often lead to unexpected outputs in your program let's explore it further with an example here we have an integer variable in which we store the value 4000. and if we multiply this variable by 10 then the correct answer should be forty thousand however when we execute this statement the result isn't what we expect instead of 40 000 the variable holds some unexpected data this issue is called garbage value as we have learned earlier an integer variable can hold values up to around 32 000. so if you try to store a bigger number than its capacity then it gets confused and gives you a garbage value instead of the number you want to solve this issue make sure to use the right data type for your number this way you can avoid garbage value next in Arduino programming we have special symbols called mathematical operators that help us to perform calculations on variables these operators include addition subtraction multiplication and more let's dive in and see how they work to understand the working of these operators I have already created a simple program and I am running this program using the debugger here we have created three integer variables and we have stored some default values inside these variables let's have a look at the current values in the variables variable 3 holds 0 variable 1 is set to 15 and variable 2 holds the value 2. first the microcontroller will add the values of the variable 1 and variable 2. and then it will store the result which is 17 into variable 3. let's execute this instruction now if you look at the variable 3 it has 17. the same way we will go step by step we don't need to cover these in detail again but if you look at the division the variable 3 ends up with 7 but the real answer should be 7.5 this is just because the integer data type is designed to store whole numbers so any decimal portion will be deleted automatically so if you need answer with decimal point then you can use float variable it allows us to keep the fractional part next the modulus operator gives us the remainder after division so the variable 3 will have the remainder value which is 1. next with the increment operator the microcontroller adds 1 to the current value so the variable 1 will go from 15 to 16. on the other hand the decrement operator subtracts one from the current value so the variable 2 will go from 2 to 1. now we have a clear understanding of how operators work and how they affect data stored in the variables next let's explore another type of operators called relational operators they are used to specify the condition in the program these operators compare two values and gives a simple answer true or false so it is best for specifying the condition in the program in C language there are several relational operators these operators help us to understand relationships between values let's understand how they work the first one is the greater than operator it gives true if the value on the left side of the operator is greater than the value on the right side otherwise it gives false as you can see the value 10 is on the left and 20 on the right side since 10 is not greater than 20 so the result will be false the other operators also work in a similar way you can just test each condition and see if you get true or false value it's like asking questions to your Arduino and getting simple answers let's explore another example to better understand the concept in this example we will focus on the less than and equals to operator this will gives true if the number on the left is smaller than or equal to the number on the right however if the number on the left is not smaller or equal then this operator will return false in our case both numbers are same and they are equal now since the operator checks for both being smaller and equal so it will return true because 1 is equal to 1. that's all about the relational operators they help us to compare numbers and make decisions in programming let's move to the next section of the video in this section we will discuss about Loop a loop is a helpful tool in programming that allows you to execute a block of code repeatedly for a certain number of times it's like telling the Arduino to do something over and over again there are three types of Loops the while loop the do while loop and the for Loop among these the for Loop is a popular choice for programmers because it's quite flexible so let's focus on understanding the for Loop if you understand it correctly the other Loops will become simpler for you the for Loop consists of three main components initialization condition and increment let's break down the structure of the for Loop step by step first we have the initialization path here we set a starting point for the loop we will start the loop from one to set the starting value we will use a variable let's name it to I and we will set its value to 1. it's like telling the Arduino to start counting from 1. then comes the condition here you need to specify how many times the loop should run let's say we want to repeat the loop for 5 times so we will set the condition to I less than equals 5. this condition will be checked before every iteration of the loop as long as the condition remains true the loop keeps running let's move to the increment part after each iteration of the loop it increment the value of the I variable by 1. it is used to keep track of how many times the loop has run finally we have the loop body this is where we place the actual code that you want to run over and over again in this example we will just display the value of the I variable on the serial monitor now the loop is ready let's understand the working of the loop the Loop's Journey Begins With The initialization path in this step the microcontroller determine the starting value of the loop which is set to 1 in our example it is important to note that the initialization path runs only once at the beginning of the loop and won't repeat after that the loop checks the condition in our case 1 is less than 5 so the condition is true as a result the microcontroller moves into the loop body inside this body it execute our instruction which just display the value of the I variable on the serial monitor and then there comes the ending curly bracket it indicate the end of the loop from here the microcontroller jumps back to the start of the loop now it comes on the increment stage here it will set the value of I variable to 2. next the condition is checked again since the 2 is less than 5 so the condition is true now the loop enters into the body again this time it will print the new value of the I variable on the serial monitor this way the loop runs for 5 times after completing the fifth iteration the value of the I variable is now 5 and increment operator increase it to 6. and then the condition will check again for the final time at this point the condition is false because the 6 is not less than 5. so the microcontroller skips the loop body and jumps at the end of the loop let's give it a try by uploading it to Arduino as you can see the serial monitor displays the numbers 1 through 5 one after the other what if you want to run the loop in opposite direction here is a simple trick this time we will start the loop from 5 and it ends when the value of the I variable becomes less than 1. and finally decrease the value of I variable by 1 using decrement operator with these changes the loop will run in the opposite direction here you can see now the serial monitor displays the numbers in opposite direction and there you have it this is the simple introduction to the for Loop in Arduino next let's create a simple project to explore the power of loops in this project we will learn how to control the brightness of an LED using a technique called pwm pulse width modulation is a technique to create analog like signals using digital pins as we have discussed earlier the Arduino board has six digital pins that support pwm these pins help us to generate varying voltages between 0 and 5 volts which is useful for controlling the LED brightness it is important to note that pwm technique only simulates analog signals and that cannot produce real analog signals to generate a pwm signal we will use a special function called analog write this function requires two arguments the first one is the PIN number that you want to control and second one is a value between 0 and 255. this value tells the Arduino how much power you want to send to the connected component if you will use the value 0 it's like telling the Arduino to give zero power to the PIN and if you send 255 then full 5 volt of power will send to the pin which will turn on your component at full power so if you will use the value 128 it means you are sending half power so the component will drive with half power this is like using a dimmer switch for a light it's not super bright but it's not completely dark either so it will remain in the middle let's understand it with a simple example first look at the wiring diagram here we have connected an LED to pin number nine on the Arduino which is one of the pwm pins and we are going to control the brightness of this led using the analog write function let's move to the coding part first we will Define a constant variable to store the pin number for the LED which is connected to the pin number 9 on the Arduino then in the setup function we will set the LED pin as an output using the pin mode function this step tells that we are going to control this pin and then in the loop function we will use analog write function to control the brightness of the LED the analog write function takes two parameters the first one is the PIN number where the LED is connected and the second parameter is a value between 0 and 255. let's start by turning off the LED completely by setting the value to 0. when the value is 0 it means we are sending 0 volts to the LED it will turn the LED off after turning off the LED we will add a delay of 500 millisecond this delay will keep the LED off for half a second next we will use the analog write function again but this time we will set the value to 128. this will send 2.5 volts to the LED to make it half bright or dim after turning off the LED we will add a delay of 500 millisecond to keep it off for half a second next we will use the analog write function one more time but this time we will set the value to 255. this time we are sending 5 volts to the LED so the LED will fully bright finally we add another delay to keep it fully bright for half a second now let's upload the code to Arduino and observe the LED Behavior as you can see the LED starts from the off State then after a 500 millisecond it becomes half bright and after another 500 milliseconds it becomes fully bright next let's explore how to use the analog write function with the for Loop to create a smooth LED fading effect as we have discussed earlier the pwm values have a range from 0 to 255. so we will start the loop from 0 and we will run it for 255 times to start the loop from 0 we have set the initial value of I to 0. then we have set the condition I is less than or equal to 255. this condition will run the loop for 255 times then we will use I plus plus to increment the value of I by 1. then inside the body of the for Loop we will use the analog write function to control the LED brightness the first argument will be the pin number where the LED is connected and the second argument will be the value of I during each cycle of the loop the variable I is incremented by 1. this way the loop go through all the numbers from 0 to 255 as the value of I increases with each cycle the analog write function will send higher voltages to the pin making the LED brighter more and more to make a smooth LED fade effect we will add a 10 millisecond delay after each cycle of the loop now the code is ready let's give this a try to see how it works as you can see the LED is currently fading smoothly from off to on but it turns off immediately so if you also want the LED to turn off smoothly then we can do this by adding a second Loop in the code and running it in the opposite direction to save time and effort we will make a copy of the existing Loop to run the loop in opposite direction we will start it from 255 and this Loop will continue as long as the value of I remains greater than zero and finally we will set the counter variable to I minus minus now we have two Loops the first Loop will gradually turn on the LED and the second Loop will gradually turn it off so it will create a cool LED fading effect let's upload the code to check it's working now look at the Led as you can see the LED smoothly turns on and then gradually turns off creating a pleasing fading effect that's all about the analog write function let's move to the next section in this section we will learn about the digital read function this function is used to read digital input in your Arduino projects the digital read function required just one parameter which is pin number here you will need to enter the pin number you want to read the state if it reads the zero volts on the pin then it will return low and if it reads the 5 volts on the pin then it will return High let's make a simple project to understand the working of digital write function first look at the wiring diagram here we have connected a push button to the pin number 8 of Arduino now we will use the digital read function to determine whether the button is pressed or not first we will declare a constant variable called button pin here we will specify the pin number to which the button is connected in the setup function we will set the button pin as an input pull up using pin mode function the input pull up is used when you're working with buttons button is a simple component that closes an electrical connection when pressed but when it is not pressed then the status of the pin can be uncertain this means the pin is neither High nor low to solve this issue we use a pull-up resistor it keeps the pin High when the button is not pressed and when you press the button it will turn the pin into low as the Arduino has internal pull up so no external resistor is needed by setting button pin as input pull up we activate this internal pull-up resistor this helps us to reliably detect the button presses in our Arduino code next we initialize the serial communication at a baud rate of 9600. this will help us to display the results to the serial monitor for debugging purpose in the loop function we will Define a Boolean variable called button stage it will be used to store the current state of the button then we will use the digital read function to read the current status of the button connected to pin number eight if the button is pressed then this function returns low and if the button is not pressed it returns High and this value will be stored in the button State variable and then we will use the serial dot println function to display the current state of the button in the serial monitor finally we will include a delay of 100 millisecond to make a short pause between readings this helps us to avoid any issues caused by button bouncing now let's upload this code to the Arduino board and open the serial monitor to see the results in action now if you look at the serial monitor it displaying one it indicates the current voltage on the pin is 5 volts so it means the button is not pressed let's press the button to see what happens as you can see the serial monitor is now displaying the value 0. it indicates the current voltage on the pin is now zero volts so it means the button is now pressed this way you can read digital inputs in your Arduino projects now it's time to take the next step and dive into controlling an LED using this button but before going further let's explore about the if statement the if statement is a fundamental Concept in programming it is used for making decision in the code the if statement consists of two main paths the first one is condition and the next one is body the if statement first checks the condition if the condition is true then the statements inside the body will be executed however if the condition is false then the code inside the body of the if statement is skipped and the program moves to the next line after the if statement after the if block we have some code that is outside of it this code will always execute regardless of whether the if condition is true or false now we understand the if statement let's take a step further with the if else statement the if else statement has two blocks the first one is if block and the other one is else block the if else statement allows us to execute one block of code when the condition is true and another block of code when the condition is false the working of the if else statement is very simple first the condition is checked if it is true then the code inside the if block will execute and if the condition is false then the code inside the else block will execute so the both blocks will never run together only one block gets executed and it will depend on the condition you set that's all about the if statement now it's time to write some code to control the LED using a push button first let's have a look at the wiring diagram here the LED is connected to the pin number 11. and the push button is connected to the pin number 8 same as before now let's move to the coding part this is the same code that we have written before to display the button state in the serial monitor here we will declare another constant variable to specify the PIN to which the LED is connected in the setup function we will set the LED pin as an output using pin mode function this step tells the Arduino that we want to control this pin in the loop function we have already some code it reads the state of the button using the digital read function and stores it in the button State variable and below this line we will use an if statement to check if the button is pressed or not when the button is pressed its state will be low and when the button state is low then the condition will become true and the code inside body will execute so inside the body we will turn on the led by setting its pin to high using the digital write function and then we will add a delay of 100 milliseconds to handle the button the bounds however if the condition is false then code inside the body of the if statement is skipped and the program moves to the next line after the if statement here we will just turn off the led by setting its pin to low now the code is ready let's upload it to Arduino now if you look at the Led it is currently off let's press the button to see what happens as you can see when the button is pressed the LED turns on and when the button is released the LED turns off you can also configure the button to toggle the state of the LED this means when you press the button for the first time the LED will turn on and when you press it a second time the LED will turn off you can do this with a single change in the code first we no longer need this digital write function so we will remove it now let's focus on the digital write function inside the if statement instead of using the high value directly we are going to do something cool we will use the not operator with the digital read function to toggle the state of the LED then we will provide the LED pin as the parameter of the function now we have set this function to read the voltages on LED pin now let's talk about the not operator not operator is used to invert a result if the result is true then it will turn it into false and if the result is false then it will make it to true now let's put these together first the digital read function will read the voltages on the LED pin for example if the LED is off then the digital read function returns false but the not operator will make it to true in Arduino programming language true means high so the LED will turn on the same way if the LED is on then the digital read function returns true but the not operator will make this value to false and false means low so the LED will turn off now the code is ready before uploading the code we will just increase the delay to 500 millisecond this will just increase the delay between each button press now click on the upload button to upload the code to Arduino let's press the push button to see the working of the project here you can see when we press the button the LED switches between on and off like a little digital dance that's all about the digital read function now let's move to the next section in this section we will learn about analog inputs the Arduino board has six analog pins these pins are labeled from a 0 to A5 unlike digital pins that can only read high or low signals analog pins can read a range of values to read the analog values we will use a function called analog read this function just requires one parameter it is the PIN number from which you want to read the data this pin number corresponds to the analog pin where your sensor is connected this function returns a value within a range of 0 to 1023 here 0 represents 0 volts and 10 23 represents 5 volts to read the analog data we will use a sensor called potentiometer this is a simple analog sensor that acts like a variable resistor as we turn the knob its resistance will change and we can measure the change using analog read first look at the wiring diagram here we have connected a potentiometer to the analog pin which is labeled as a 0. now we will write some code to read values from the potentiometer first we will Define a constant variable to store the analog pin number we are using in the setup function we will start the serial communication for monitoring the results and then in the loop function first we will create a variable called Port value this variable will be used to store the analog readings from the sensor to read the values we will use analog read function this function requires just one parameter which is the PIN number where the potentiometer is connected analog read function will read a value from the potentiometer and then store it in the port value variable next we will use serial dot println function to display the value on the serial monitor now the code is ready it's time to upload it to Arduino board after uploading open the serial monitor to observe the values received from the potentiometer let's turn the potentiometer knob as you can see the values in the serial monitor are changing these values represent analog readings from the sensor the minimum value is 0 and the maximum value is 1023 this way you can collect data from any analog sensor using Arduino now let's make a project here we will use a potentiometer to control the brightness of an LED first take a look at the wiring diagram here we have connected an LED to the pin number 9 which is the pwm PIN as we discussed earlier the pwm pins are used to control the brightness of an LED or the speed of a motor additionally we have connected a potentiometer to the analog pin just like in our previous project now let's move to the coding part here first we will Define a variable to store the pin number of the LED next in the setup function we will use the pin mode function to set the LED pin as an output as you have noticed we have not set the potentiometer pin as an input this is because all the pins on Arduino are configured as input by default next in the loop function we have used the analog read function to read the value of the potentiometer and we have store this value in the variable called Port value this variable will play an important role to calculate the brightness of the LED as we rotate the knob of the potentiometer the value of the variable will change and we will use it to control the brightness of the LED however there is a small challenge here the analog read function returns values within the range of 0 to 1023 however to control the brightness of the LED we need analog write function which works with pwm signals pwm works in the range of 0 to 255. this means we have to convert the analog values into a suitable range of pwm values to solve this problem Arduino language provides a function called map function let's see how to use this function first we will Define a variable to store the converted value we have named it as fade value now we will call the map function to convert the analog value to the pwm value this function requires three parameters the first one is the value that you want to convert in our case it will be the pot value this variable will hold the most recent value as you rotate the knob of potentiometer the second parameter is the source range for the analog sensors this range is from 0 to 10 23. the third parameter is the target range which is the pwm range of 0 to 255. now when this function will execute it will convert the value of the variable to the range compatible with the pwm and the resulted value will be stored in the fade value variable now comes the exciting part here we will use the analog write function to control the brightness of the LED this function takes two parameters the first parameter will be the pin number where the LED is connected and the second parameter will be the fade value by adjusting the fade value you can control the brightness of the LED now the code is ready let's give it a try as you can see when we rotate the knob the brightness of the LED changes accordingly so this way you can read analog inputs and control digital outputs congratulations we have reached at the end of this Arduino programming course throughout the course we have learned about controlling the analog and digital signals and we have made some simple LED controlling projects these are the building blocks to create the real world applications we hope this video course has provided you with the confidence to explore the endless possibilities in Arduino programming thank you for joining us on this educational Adventure happy coding and may your journey be filled with Innovation and success bye