1 www.eTechnophiles.com
2 www.eTechnophiles.com WELCOME TO THE COURSE “First of all, thank you so much for becoming a part of the eTechnophiles family. Hi, I am Ankit. I am a Youtuber and tech blogger with a passion for electrical and electronics engineering. I love to tinker with electronic devices and gadgets and have been doing so for over 6 years now. During this period, I have made more than 500 projects and helped thousands of students through my blog and youtube videos. I created this blog in order to post Electronics and embedded related projects for Beginners. But now we are a smalI team of 5 and upload all sort of cool stuff related to Electronics and Microcontrollers here. I also upload circuit diagrams and other necessary material from my YouTube Project videos here. I run a Youtube channel by the name of TheElectronicGuy: https://www.youtube.com/theelectronicguy. You can check it out for more cool Electronics and Arduino Projects.” You can also connect with us on:
3 www.eTechnophiles.com Master Arduino without any Hardware! Using TinkerCad Imagine learning Arduino programming without ever having to buy a physical Arduino board. With this ebook, you will be able to do just that! Through a series of step-by-step tutorials, you will learn how to use the free online Tinkercad simulator to create virtual Arduino circuits. You will then learn how to program these circuits using the Arduino programming language. By the end of this ebook, you will have a solid understanding of how to use Arduino without ever needing to purchase any hardware. This book is divided into five sections/units: 1. Introduction to Arduino In this section, you will learn about the basics of Arduino and understand what you can do with it with some real-life project examples. 2. Introduction to TinkerCad and Basic Electronics TinkerCad is introduced in this section. We will also design some basic electronic circuits on TinkerCad. 3. Arduino Programming Basics The fundamentals of Arduino programming are covered in this section. 4. Know your Arduino UNO board Before working on any microcontroller, you should be aware of its specs, features, and board layout. This section is dedicated to Arduino UNO. 5. Arduino Projects This section is solely dedicated to Arduino projects. And we will make a ton of them without any hardware on TinkerCad! Should you learn Arduino without a physical board? To be honest, No. The feeling of working on a board with electronic components all around is unmatched, that feeling of seeing an LED glowing using your own code for the first time. You learn a lot by working on hardware like debugging circuits, understanding components limitations, and, most importantly patience! So why go for this ebook then?
4 www.eTechnophiles.com Who is this ebook for and why should you consider it? 1. Simulating a circuit firsthand is a crucial step in designing your circuit. TinkerCad is not that advanced, but it offers a lot of features, everything a beginner needs. 2. Someone with a basic understanding of electronics but wants to learn more about microcontrollers and development boards. 3. For those who have seen many Arduino projects, and watched many tutorials without actually implementing anything. You can start prototyping right away. 4. Absolute beginners who want to get into programming. Arduino is the easiest to program. 5. You have a physical board but do not want to take the risk of frying it. Create the circuit on TinkerCad first, program it, simulate it, and if everything works fine, go for it. 6. And last but not least, you don't want to invest in the physical board yet.
5 www.eTechnophiles.com Introduction to Arduino What is Arduino? Arduino is an Italy-based company that manufactures Microcontroller boards called Arduino Boards which is used in many electronics and day-to-day applications. It is one of the most popular microcontroller based Development boards on Earth since a lot of hobbyists and students from all over the world use Arduino Boards in their day-to-day projects. One of the key reasons for Arduino being so popular is that it is Open Source Software and Open Source Hardware-based Board. Arduino was originally designed for artists, designers, and hobbyists, but it has found many practical applications in industry. Some of the biggest names in technology are using Arduino, including Google, Microsoft, Apple, and Amazon. One factor that separates Arduino from all the other Microcontroller boards is its community of thousands of users. Queries, Projects, and questions are posted daily on the Internet by its community members. Checkout: Arduino forum
6 www.eTechnophiles.com What do you need to get started with Arduino? The great thing about Arduino is that you don't need much to get started. All you need is a working computer with Arduino software installed and an Arduino board. In addition to an Arduino board, you also need a USB cable to connect the board to your computer. Physical Arduino board and a USB cable But now you don't even need that. By the end of this ebook, you will master Arduino even before buying one. What can you do with Arduino? The sky is the limit when it comes to what you can do with Arduino. It can be used to control lights, motors, and just about any other electronic device you can think of. You can use it to build robots, create musical instruments, or even make your own video games. The possibilities are endless! In this ebook, we'll be focusing on using Arduino to create simple projects. But once you've mastered the basics, you'll be able to tackle more complex projects on your own.
7 www.eTechnophiles.com Types of Arduino Boards Since there are so many applications and areas where Microcontroller boards like Arduino can be used, it is impossible to fit all features in one type of Arduino Board. Hence there are many types of Arduino boards available in the market according to application and user needs. Some have more pins while some have more features. For example, the Arduino portenta is the most powerful Arduino board intended for industrial applications. Similarly, Arduino Mini, due to its small size, is best fitted for small projects where size is a constraint. Arduino Portenta and Arduino Mini Some of the popular Arduino Boards are listed below: Arduino Uno Arduino Mini Arduino Nano Arduino Mega Arduino Micro Apart from Arduino UNO, Arduino Nano also became very popular soon after it was launched. Due to its form factor popularity, a lot of Nano variants have been launched since then. You can read about each one of them on our blog in detail. Arduino Nano Arduino Nano Every Arduino Nano 33 BLE Sense Arduino Nano RP2040
8 www.eTechnophiles.com The Simplest Arduino Board for Beginners: Arduino UNO Out of all the Arduino boards, Arduino UNO is the most popular one and it is also the simplest board for beginners. It was launched way back in 2005. Arduino Uno Anyone can start making projects using Arduino UNO just after a few minutes of reading and practicing. We will focus on Arduino UNO in this ebook, and TinkcerCad also offers only UNO as of now. Some cool Arduino Projects Given below are some beginner level Arduino projects. Arduino Radar An Arduino Radar is a project that uses an Arduino board and a radar module to detect and track objects. The radar module emits a radio wave that bounces off objects and returns to the module, providing information about the distance and direction of the objects. The Arduino board processes this information and displays it on a display device, such as an LED matrix or computer screen. This project is a great way to learn about electronics, programming, and radar technology and to build a fun and interactive project. Arduino Radar
9 www.eTechnophiles.com Controlling LED wirelessly using IR remote This projects make use of an infrared receiver and remote control to control the brightness and on/off state of an LED. The infrared receiver captures signals from the remote control and sends them to the Arduino Uno, which processes the signals and controls the LED accordingly. Irremote controlled using Arduino Uno Smart door unlock system using RFID module and Arduino This project allows you to unlock a door using a passive RFID tag or card. The RFID module reads the tag or card and sends the data to the Arduino, which processes the data and opens the door if the tag or card is recognized. The project requires an RFID module, an Arduino board, a relay module, and an RFID tag or card. Through this project, we can gain knowledge of RFID technology. Smart door unlock system using RFID module and Arduino Servo remote control using Arduino. This project allows you to control the position of a servo motor using a remote control. The remote control sends signals to the Arduino, which processes the signals and controls the servo motor accordingly. The project requires a servo motor, an Arduino board, a remote control, and a connection cable.
10 www.eTechnophiles.com Servo remote control using Arduino SmartBand using Arduino In this project using ADXL345 acceleration sensor we can calculate acceleration and based on that we can calculate how many steps were completed. You can simply display that steps on serial monitor and LCD16x2. Smartband using Arduino
11 www.eTechnophiles.com Simple Circuit on TinkerCad Introduction to TinkerCad and Basic Electronics In this section, we will learn the basics of electronics and how to use TinkerCad. What is Electronics? Electronics is the study of Electricity, Magnetism, and their applications. We use electronic devices in our day-to-day life without even realizing it. For example, a light switch is an electronic device that uses electricity to turn a light on or off. Electronics is all around us and can be found in various applications such as cell phones, computers, cars, and appliances. What is a Circuit? A circuit is a closed path through which current can flow. For current to flow, there must be a complete circuit. If there is a break in the circuit, the current will not flow and the device will not work. For example, if you were to take a battery and connect one end to an LED (light-emitting diode) and the other end to a resistor, the current would flow through the circuit and the LED would light up. See the diagram below. What is a Circuit simulator? A circuit simulator is a tool or software that allows you to design and test electronic circuits without needing any physical components. Take the above circuit for example; the same circuit is simulated in TinkerCad shown below: So it is a computer program that you can use to test the behavior of a circuit before you even build it or to troubleshoot an existing circuit. Circuit simulators are used by electrical engineers and hobbyists alike. There are many different circuit simulators, but they all operate on the same principle. They use mathematical models to simulate the behavior of electronic components and circuits. This allows you to test various design choices without building physical prototypes. We have compiled a list of top circuit simulators here. You can go through the list to learn more about them.
12 www.eTechnophiles.com What is TinkerCad? Tinkercad is a free web app that lets you design 3D objects and electronic circuits and write code. This ebook is focused on circuit design and simulation only. How to get started with TinkerCad? Step1: Signup Go to the official Tinkercad website or click here www.TinkerCad.com and signup with your login details. Sign Up Step 2: Log in Now login to your TinkerCad account. You will see three sections on your homepage: 3D designs, circuits, and codeblock. Log In
13 www.eTechnophiles.com Step 3: Create a new circuit Go to the Circuit section and click "create your first circuit." Upon redirecting, you will see the work dashboard. New Circuit TinkerCad Circuit Dashboard Tour TinkerCad Dashboard
14 www.eTechnophiles.com Search Bar and Component placement On the right side of the dashboard, there is a search bar. Type battery and select 3V coin cell from the list. Drag and drop it on the dashboard. Battery Now search for Led and resistor and place them on the dashboard. Search for LED
15 www.eTechnophiles.com Search for Resistor How to change the value of the components? To change the name and value of the resistor just click on it, select ohm, and enter 100. The resistor band color changes accordingly. Changing value of the resistor
16 www.eTechnophiles.com Similarly, to change the color of the LED, click on it and select the color from the drop-down menu. Changing LED Colour In the same way, change the name of the cell. Please note that the value of the battery can't be changed. It's a 3.0 V cell by default. To see all the components of TinkerCad, simply go to the component option and select ‘All’. All Component list
17 www.eTechnophiles.com How to wire a component? There are 11 categories of components to choose from: 1. General Components such as a Resistor, capacitor and inductor 2. Input devices such as sensors, buttons 3. Output devices - Leds, Motors 4. Power supply - Batteries, Solar cell 5. Breadboards 6. Microcontrollers - Arduino, Attiny 7. Instruments - Multimeter, Oscilloscope 8. IC's - 555 timer, OPAMP IC 9. Power control - Transistors, Mosfets, Relay, Regulator 10.Logic ICs - AND, OR, NOT 11.Connectors How to wire the components? And Tools We have placed and set all the components required to create our first circuit. Now it's time to connect them together. Point the cursor over the battery's positive terminal, and drag it to create a wire. Connect this wire to one of the resistor ends. You can also change the color of this wire. To do so, go to the menu above and select a color of your choice. Changing wire color
18 www.eTechnophiles.com There is an option for changing wire type also. We only need a normal one right now. Wire type The toolbar gives you two other essential functions- rotate and flip. Select the LED and either click the rotate option or press R on your keyboard. Similarly, to flip the LED, click the mirror option or press M. Now connect the remaining terminal of the resistor to the Anode(slightly bend terminal) of the LED. Finally, join the Cathode of the LED to the negative terminal of the battery. Circuit Connections How to Simulate the Circuit? To simulate this circuit, click on Start Simulation. You will see that the LED glows instantly. Simulation running
19 www.eTechnophiles.com Now stop the simulation and click on the Notes tool from the tools menu. Drag it on the dashboard and write anything you want. Add notes Notes can help you to organize various parts/concepts of a complex circuit. Note: The name of your design will be some random words by default. To change it, click on it and type the name of your choice. Change the fie name What are Input Devices? We need input devices to take input from the user (or data of a physical quantity). The most common ones used in Arduino are: push buttons and sensors.
20 www.eTechnophiles.com Input Devices What are Output Devices? Output devices are those which give some form of output to the user. The most common ones used in Arduino are LEDs, Displays, and motors. We used one such output device in the above circuit. What is it? Output Devices What are Sensors? Multiple Sensors A sensor is a device that detects/collects data of a physical quantity from the physical environment, such as light, heat, motion, moisture, pressure, and more. Many types of sensors are available in the market, which we will discuss in later chapters. For now, let's use a light sensor to make a simple circuit. Note: A sensor is also a type of Input device. Example Project: Motor speed control using LDR Sensor
21 www.eTechnophiles.com Objective: This project will give you a basic understanding of sensors and how they affect the circuit. The voltage across an LDR sensor changes based on the input from the surroundings, i.e., light. This causes a change in the speed of the motor connected in series with the sensor. For this circuit, place the following components on the dashboard: 1. LDR 2. DC Motor 3. Battery- 9V Connect them as shown below. Motor speed control What is an LDR Sensor? An LDR is a Light Dependent Resistor, a sensor whose resistance changes based on the intensity of the light it's exposed to. The voltage applied to an LDR also affects its resistance. In simple terms, when there is more light, the resistance of the LDR decreases, whereas in low light, the resistance of the LDR increases. Real LDR Sensor What does LDR suppose to do in this circuit?
22 www.eTechnophiles.com In this circuit, we have placed an LDR in series with a DC motor. So when there is more light, the resistance of the LDR decreases which means more current flows through it, and the motor starts rotating faster. Whereas in less light, the resistance of the LDR increases slowing down the speed of the motor. Now start the simulation. There are three stages of this simulation: 1. Circuit under no light - When there is no light, the resistance of the LDR is very high, and almost no current flows through the circuit. So the motor won't rotate(~6 rpm). Circuit under no light 2. Circuit under moderate light - When there is some light(not too much), the resistance of the LDR decreases, and a small current flows, enough to rotate the motor. Circuit under moderate light
23 www.eTechnophiles.com 3. Circuit under intense light - When there is intense light(more than moderate), the resistance of the LDR decreases even more, and more current flows giving a high speed to the motor. Circuit under intense light Click here to see the simulation video. And click here to copy the circuit to your tinkerCad dashboard. Now stop the simulation and try changing the values of some components in the circuit, like the battery voltage or the type of motor. See how it affects the speed of the motor. Arduino in TinkerCad Arduino To simulate an Arduino circuit on TinkerCad, we are going to place Arduino UNO on the dashboard.
24 www.eTechnophiles.com Now run the simulation to see how Arduino behaves. The "on" LED is the power indicator. If you look closely, you will observe that the Led' L' is blinking. This LED is internally connected to pin 13 of UNO. And as explained before, Arduino requires a program that it can execute to perform the desired task. So we got the hardware but where is the software part? How is Arduino blinking the LED on its own? In TinkerCad, UNO comes with a pre-loaded program that blinks the LED connected at pin 13. To see the program go to the code section. Here you will see three different options to program UNO. The first two are “block” and “block + text” coding. And the last one is “C++” or the official Arduino programming language. Select it and click on continue. Block Code On your right is the program causing the LED to blink. More about it and Arduino programming basics in the next section.
25 www.eTechnophiles.com Code Area So this is the place where we will type, edit and debug the program. If you are familiar with Arduino IDE and want to work on it, you can just copy the code from there and paste it on tinkerCad.
26 www.eTechnophiles.com Arduino Programming Basics What is Arduino IDE( Integrated Development Environment)? As given in techtarget.com, the definition of IDE is: "An integrated development environment (IDE) is a software suite that consolidates the basic tools developers need to write and test software" In simple words, Arduino IDE or Arduino programming environment is a software/tool that helps us code and program Arduino Boards. Arduino IDE is the basic interface of Arduino software that you see on your Laptop screen after installing it. It contains all the tools like compiling and debugging required to upload and run programs on Arduino boards. So getting familiar with Arduino IDE is important if you are starting your journey with Arduino. Note: You do not have to worry about Arduino IDE if you only want to focus on TinkerCad. The TinkerCad has a built-in programming environment for Arduino, as seen in the example above. So you may skip this part entirely. Arduino IDE Dashboard Given below are the dashboard images of Arduino 1.0 IDE and new improved Arduino 2.0 IDE. Arduino IDE 1.0
27 www.eTechnophiles.com Arduino IDE 2.0 Verify / Upload - compile and upload your code to your Arduino Board. Select Board & Port – The Arduino board connected and detected by the system is shown here along with the port number. Sketchbook – All of the stored/saved sketches or programs can be found here. Boards Manager – Here you will find the packages of boards other than Arduino. Library Manager - browse through thousands of Arduino libraries, made by Arduino & its community. Debugger – This feature is used to test and debug the code in real time. Search - search for keywords in your code. Open Serial monitor - opens the Serial Monitor tool, as a new tab in the console. You can use it to print characters and numbers; read and print the sensor's input using a set of commands called Serial Commands. For additional commands, check these Menus: File, Edit, Sketch, Tools, and Help. Note: To know Arduino IDE in detail you can check out this post on Arduino.cc itself: Arduino Software (IDE) Basics of Arduino Programming The Arduino Integrated Development Environment (IDE) is a Java-based platform used to write and upload programs to an Arduino board. The IDE is open source and runs on Windows, macOS,
28 www.eTechnophiles.com and Linux operating systems. The “Wiring” programming language simplifies C++ syntax for use with the IDE. What are VOID SETUP and VOID LOOP? Void Setup and Void Loop are the building blocks of any program in Arduino. Every Arduino Program necessarily needs these two functions to work. Void Setup() : Void means not returning any value. Void functions are called so because these functions do not take any input parameters whenever they are called. The setup means setting the proper conditions for the initialization of a program. Void Setup is a function called only once when the Arduino board starts. So generally, all the initialization code (for ex: Initializing pin 13 as the output pin) goes inside this function. The structure of Void setup function is: Void Setup() { } Example: void setup() { pinMode(13, OUTPUT); // put your setup code here, to run once: } Void Loop() : A void Loop is an infinite loop. The code written inside this function keeps on executing continuously in an endless loop. There is no ending for this loop i.e., the code written here will run for an infinite number of times unless the board is reset or powered off. The structure of Void Loop function is: Void Loop() { } Example: void loop() { digitalWrite(13, HIGH); delay(1000); digitalWrite(13, LOW); delay(1000); // put your main code here, to run repeatedly: } Arduino Program that does nothing Copy the program given below and paste it into the code editor window.
29 www.eTechnophiles.com void setup() { // put your setup code here, to run once: } void loop() { // put your main code here, to run repeatedly: } Now run the simulation and observe. The Arduino will not do anything because there are no instructions for it to follow. Since there is nothing to initialize in the setup function, the loop function runs continuously. Comments in Arduino Programming Comments are the lines of code that Arduino does not execute. These are written in a program to increase their readability. A comment is a note that programmers write to help them remember what's happening in the code. Code comments help you and other people understand how the code works. If you are a beginner in Arduino programming, we recommend using comments in every program. They can also be helpful for debugging, as you can comment on sections of code that you suspect are causing errors. Note: Comments are not read by the compiler, i.e., the compiler does not consider any comment a part of the code. What are Single Line comments in Arduino Code? Single-line comments in Arduino are written using // symbols. Anything that is written after // symbols till the end of the line is not executed by Arduino. Example: int x; void setup() { // put your setup code here, to run once( Yes this is a comment! ) } void loop() { x = 5; // This is a single line comment. Anything after the slashes is a comment // to the end of the line } What are Multi-Line comments in Arduino Code?
30 www.eTechnophiles.com Multi-line comments in Arduino code are indicated by /* and */. Anything between these symbols will be ignored by the compiler. This can be useful for commenting out large sections of code or leaving yourself notes about what a particular block of code is supposed to do. However, it's important to note that /* and */ must always come in pairs - if you forget to close a multi-line comment, everything after it will be ignored by the compiler until the end of the program. How to use Multi-Line Comments in Arduino Programming? Before starting a Multi-line comment: use"/*" To end a Multi-line comment: Just end the Multi-line comment by typing" */" Example of a Multi-line comment: void setup() { pinMode(13, OUTPUT); // put your setup code here, to run once(this is a single line comment) } void loop() { digitalWrite(13, HIGH); /* This is a Multi-line comment ending ........................here, ........................no here ! */ delay(1000); digitalWrite(13, LOW); delay(1000); } Rule of using a semicolon(;) in Arduino Programming. The semicolon is perhaps one of the most important punctuation marks in any programming language. In C++, it denotes the end of a statement. Without a semicolon, the compiler would not know where one statement ends and the next begins. This would make writing even simple programs extremely difficult. In addition, the semicolon is also used to separate different object definitions and function declarations. Example: int x = 5; // This statement assigns value 5 to variable x If there is no semicolon(;) at the end of a statement, it leads to an error. Example:
31 www.eTechnophiles.com int x = 5 // this will lead to an error because there is no semicolon(;) at the end of the statement. Variables and Datatypes What is a variable? When you sit down to write a program, there are certain pieces of data you know ahead of time. For example, let's say you're writing a program to calculate the average grade in a class. You would need to create a variable to store the total number of grades entered and a variable to store the running total. In this case, the total number of grades would be a constant(number of students), while the running total would vary. So variables are used to store these kinds of known or unknown quantities of information referred to as a value. In most programming languages, you must declare variables before you can use them. This means that you need to specify the type of data that the variable will hold (such as a number or a string), as well as the name of the variable. Ex: The following code creates integer variables named "runningTotal" and "numberofstudents". int runningTotal; int numberofstudents = 30; The second variable is assigned a value of 30. If you later want to change the value of this variable, you can simply reassign it using the same operator: numberofstudents = 20; But the first variable is unknown and will be used to save the user's input. Note: A variable is a way to identify a memory location using a name. This makes it easier to work with the memory location since you can easily remember the name. What are Datatypes? Datatypes are used to specify the type of data that a variable can hold. For example, an int datatype can hold an integer value, while a float datatype can hold a floating point number. In addition, data types can be used to specify the size of a variable. For example, a byte datatype is one byte in size, while an int datatype is two bytes in size. By specifying the size of a variable, you can ensure that the data it contains will be properly processed by the Arduino board. An int data type is two bytes long and ranges from -32,768 to 32,767. A float data type is four bytes long and ranges from 3.40282347E+38F to 1.17549435E38F. A char data type is one byte long and can hold 256 different characters.
32 www.eTechnophiles.com How to use variables in Arduino Look at this program: int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); } The variable "ledPin" is declared whose name is ledPin, whose value is 13, and whose type is int. This variable is then used by its name later in the program where it simply replaces it with the stored value i.e, 13. So pinMode(ledPin, OUTPUT) is same as pinMode(13, OUTPUT) and digitalWrite(ledPin,HIGH) is same as digitalWrite(13,HIGH). What are global variables? In the example program above, the variable ledPin is a global variable since it was declared outside the function. This means that it is available for use throughout the entire program. Global variables can be accessed by any function in your sketch, including setup() and loop(). Since both the setup() and loop() functions use ledPin, any changes made to it in one function will also show up in the other. See the example below: int ledPin = 13; void setup() { ledPin = 3; pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); } The digitalWrite() function in loop() will have a value of 3 passed to it, since that is the value assigned to the variable in setup(). What are local variables? Look at the example given below:
33 www.eTechnophiles.com int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); int pin = 3; pinMode(pin, OUTPUT); digitalWrite(pin, HIGH); } void loop() { digitalWrite(ledPin, HIGH); } Here, a new variable is declared: name is pin, value is 13, and type is int. The only difference is that it is declared inside the loop() function. So it is a local variable. This means that it can only be used inside the function in which it is declared. In this case, only the loop() function knows about this variable and any changes made to it will not affect any other part of the program. So doing this inside the loop() function will result in an error: digitalWrite(pin, HIGH); The Most Commonly Used Functions in Arduino The pinMode() function pinMode() function is used to configure a pin as input or output. Input means you can then read the incoming voltage/value at this pin, and output means you can get an output voltage at this pin. For example, in the program above, the line pinMode(ledPin, OUTPUT); configures pin 13 as an output pin. If you want to configure a pin for input, you will use INPUT instead of OUTPUT. The digitalRead() function This function reads the value of a digital and analog pin. The value can be either HIGH(5 V) or LOW(0 V). For example, if you want to read the value of pin 5 connected to a sensor, you would use the following code:
34 www.eTechnophiles.com int sensorPin = 5; void setup() { pinMode(sensorPin, INPUT); } void loop() { int sensorState = digitalRead(sensorPin); } In this example, the value of pin 5 is being read and stored in a variable called sensorPin. The digitalWrite() function This function writes a value to a digital pin or sends a voltage value to this pin. The value can be either HIGH(5V) or LOW(0V). For example, if you want to write the value HIGH to pin 13, you would use the following code: int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); } In this example, the value HIGH is being written to pin 13. The analogRead() function This function is used to read the analog value from an analog pin. The value will be between 0 and 1023. For example, if you want to read the value of analog pin A0, you would use the following code: int analogPin = A0; void setup() { pinMode(analogPin, INPUT); } void loop() { int sensorValue = analogRead(analogPin); } In this example, the value of analog pin A0 is being read and stored in a variable called sensorValue.
35 www.eTechnophiles.com NOTE A: analogRead function can be used only on Analog pins. NOTE B: analog value 0 means 0 volts and analog value 1023 means 5 volts. The analogWrite() function This function is used to write a value to a digital PWM pin(explained in the next section) on Arduino. The value can be between 0 and 255. The analogWrite function can only be used on digital pins 3, 5, 6, 9, 10, and 11 of the Arduino UNO board. These are special digital pins called PWM pins. NOTE A: PWM value 0 means 0 volts, and PWM value 255 means 5 volts. For example, PWM value 127 gives approximately 2.5 volts. "analogWrite(3,0)"// Gives output 0 volts to digital pin 3 "analogWrite(3,255)"// Gives output 5 volts to digital pin 3 "analogWrite(3,127)"// Gives output 2.5 volts to digital pin 3 NOTE B: analogWrite() function is not related to analog pins or analogRead function in any way. For example, if you want to write the value 150 to PWM pin 3, you would use the following code: int ledPin = 3; void setup() { pinMode(ledPin, OUTPUT); } void loop() { analogWrite(ledPin, 150); } In this example, the value 150 is being written to digital pin 9. The delay() function This function is used to add a delay in your program. The time is specified in milliseconds. For example, if you want to add a delay of 1000 milliseconds (1 second), you would use the following code:
36 www.eTechnophiles.com int ledPin = 13; void setup() { pinMode(ledPin, OUTPUT); } void loop() { digitalWrite(ledPin, HIGH); delay(1000); digitalWrite(ledPin, LOW); delay(1000); } In this example, the delay() function is being used to add a 1 second delay between the HIGH and LOW states of pin 13. The millis() function This function returns the time in milliseconds since the Arduino board started running the current program. For example, if you want to know how much time has elapsed since the board started running the program, you would use the following code: void setup() { Serial.begin(9600); } void loop() { unsigned long currentMillis = millis(); Serial.println(currentMillis); delay(1000); } In this example, the millis() function is being used to print the time in milliseconds since the board started running the program. The Serial.begin() and Serial.println() functions are used to initialize the Serial port and to print the value of currentMillis to the Serial Monitor. Serial Commands in Arduino In order to communicate with the Arduino board from a computer, you will need to use the Serial class. The Serial class has a lot of useful functions that can be used to communicate with the Arduino board.
37 www.eTechnophiles.com The basic commands are explained in this section. The Serial.begin() function This function initializes the serial port and sets the baud rate for communication. The baud rate must match with both the computer and the Arduino board. The baud rate of Arduino is set inside the program, whereas the baud rate of the computer is set directly from serial monitor. For example, if you want to initialize the serial port with a baud rate of 9600, you would use the following code: void setup() { Serial.begin(9600); } In this example, the Serial port is being initialized with a baud rate of 9600. What is the baud rate? Baud rate is the speed of communication between two electronic devices i.e, Arduino and computer. You can't choose any random baud rate. The supported baud rates are 300, 600, 1200, 2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, and 115200. Note A: Since Tinkercad is just a simulation, you don't have to set the baud rate to view data on the serial monitor. Any baud rate will work as long as you are using serial.begin command to initialize serial communication. But while using a real Arduino board, you have to select the proper baud rate from the serial monitor that matches the one you have chosen inside the code. Note B: This command must be typed inside void setup() before using any other serial commands. The Serial.print() function This function is used to print data to the serial port. You can print text/numbers/data using this function. For example, if you want to print "Hello World!" to the serial port, you would use the following code: void setup() { Serial.begin(9600); } void loop() { Serial.print("Hello World!"); } In this example, the text "Hello World!" is printed to the serial port. The Serial.println() function
38 www.eTechnophiles.com This function is used to print data to the serial port and add a new line after printing it. It works in the same way as the Serial.print() function but adds a new line at the end of the output so that the next data is printed in the next line. For example, if you want to print "Hello World!" to the serial port and add a new line after it, you would use the following code: void setup() { Serial.begin(9600); } void loop() { Serial.println("Hello World!"); } In this example, the text "Hello World!" is printed to the serial port followed by a new line. This is useful when you want to print multiple lines of data to the serial port. Serial.available() and Serial.read() Serial.available() function is used to check if data is available on the serial port or not. The Arduino board will wait until some data arrives at the serial port before executing further instructions. Serial.read() function is used to read data from the serial port. It reads one byte at a time and stores it in a variable that can be used later on in your program. For example, if you want to read data from serial port and store it in a variable called currentMillis, you would use the following code: int serialData = 0; void setup() { Serial.begin(9600); } void loop() { if (Serial.available() > 0) { serialData = Serial.read(); Serial.print("I received: "); Serial.println((char)serialData); } } In this example, the serial port is checked for available data and if any byte of data is available, it is read and stored in the variable called Serial. Data, and then printed on the serial monitor.
39 www.eTechnophiles.com Know your Arduino UNO Arduino Uno is a simple development board based on Atmega 328p microcontroller. But what is a development board? Is Arduino a Microcontroller or Microprocessor? Arduino UNO is neither a microprocessor nor a microcontroller. It is actually a development board that uses a microcontroller called Atmega328p to perform various functions. You can say that atmega328p is the brain of the Arduino UNO development board. Microprocessor: It is the brain of any embedded system. It performs all the numeric and logical calculations. It is responsible for executing the commands that are given to the system (like Arduino UNO). But it's important to note that a microprocessor cannot execute commands by itself. It needs external components and devices like RAM, ROM, Registers, I/O, etc, to send or store the data. Microcontroller: A microcontroller is made up of a Microprocessor and other units required to perform certain functions like Memory units, Inputs/Outputs, ADCs, etc. That's why a Microcontroller can execute commands by itself. But it's not easy to program a Microcontroller directly due to the absence of a USB port, GPIO header, etc., and hence it is not recommended to beginners. Development board: A development board is an integrated circuit board designed to facilitate the development and prototyping of electronic projects. It typically contains a microcontroller,
40 www.eTechnophiles.com an array of electronic components (cables, resistors, capacitors, LEDs, etc.), and connectors for easy access to the components from outside the board. It allows engineers and hobbyists to quickly create prototypes or test new ideas without having to build their own circuit boards or write complex code from scratch. Now that you know what a development board is, let's move on to the basics of Arduino UNO. Arduino UNO Board layout Arduino UNO is made by assembling different components/parts. Given below is its detailed board layout: Arduino Board Layout Let's see what each part does, starting with Atmega 328p microcontroller: Atmega328P Microcontroller– The ATmega328p is a single-chip, high-performance, efficient microcontroller created by Atmel in the megaAVR family. It is an 8-bit AVR RISC-based microcontroller chip. It consists of 32 KB ISP flash memory with read-while-write capabilities, 2 KB SRAM(Static RAM), 1 KB of EEPROM, and 23 general-purpose I/O pins. Atmega 16U2 Microcontroller– The Atmega 16U2 is used as a USB-to-serial converter in Arduino UNO. Voltage Regulator- A voltage regulator regulates a varying input voltage to a stable, predefined output voltage. On the Arduino board, there are two voltage regulators: 5V and 3.3 V. Note: The board can be powered through the Vin pin, barrel jack, or USB port.
41 www.eTechnophiles.com Arduino can accept voltage between 7-12 V from the Vin pin or the barrel jack. The onboard 5 V regulator converts this input voltage to stable 5 V, which then powers the Arduino circuitry, such as the Atmega328p microcontroller. It protects your Arduino board from any sudden changes or fluctuations in the power source and will keep an output of around 5 volts no matter what happens. This 5V output is also fed to the 3.3V regulator, which gives a stable 3.3 V output connected to the 3.3 V pin. Barrel Jack – The Barrel jack or DC Power Jack is used to power the Arduino board using an external power supply. The barrel jack is usually connected to an adapter. The board can be powered by an adapter that ranges between 5-20 volts, but the manufacturer recommends keeping it between 7-12 volts. Note: Above 12 volts, the board may overheat and below 7 volts, the voltage might not be sufficient to power the board. USB B-port–The USB Interface is used to plug in the USB cable. This port can be used to power the device from the 5V supply. It allows us to connect the board to the computer. The program is uploaded to the board serially from the computer through the USB cable. Crystal Oscillator– is a fundamental component of the board and plays an essential role in its operation. This oscillator provides the 16MHz clock frequency that defines the speed at which the instructions run, meaning it has a huge impact on how quickly code executes. The crystal oscillator is also essential for maintaining accurate timing within programs, keeping multiple processes and events in sync. RESET Button–It is used to reset the board. It's recommended to press this button every time we flash the code to the board. How many pins are there in Arduino UNO? If you take a look at the Arduino board, you will find that there are a total of 32 pins on it(excluding the ICSP header). Out of these 32 pins, 14 are digital I/O pins, 6 are analog pins, 3 are GND and single 5V,3.3V, Vin and reset pin and more.
42 www.eTechnophiles.com Arduino pinout What are GPIO pins? GPIO (general purpose input/output) pins are used for connecting external peripherals(such as sensors and motors) to Arduino UNO. It acts as a bridge between a microcontroller and the outside world. These pins can be configured to read or write data to an external device. The Arduino UNO board contains 14 digital I/O pins and six analog input pins; more about them later in the book. What are Digital Pins? Pins 0-13 are digital input/output pins on the Arduino UNO board. The Arduino digital pins can read only two states: when there is a voltage signal and when there is no signal. This kind of input is usually called digital (or binary), and these states are referred to as HIGH and LOW or 1 and 0. LED (13): A built-in LED is connected to digital pin 13. When this pin is HIGH or 1, the LED is switched on; when the pin is LOW or 0, it's switched off. What are Analog Pins?
43 www.eTechnophiles.com An analog pin can be considered a detector; it can read varying voltage(or analog signal) levels, which correspond to different numerical values. They're especially useful for reading data from sensors like temperature and light sensors. The Arduino Uno consists of 6 analog pins, which use an inbuilt ADC (Analog to Digital converter). These pins can serve as analog inputs but can also function as digital inputs or digital outputs. These pins accept inputs in the form of Analog signals and return values that range between 0 and 1023 (because the Arduino Uno has a 10-bit Analog to Digital converter or 210 resolution). An Analog to digital converter works in three stages: sampling, quantization, and digitization. Because the Arduino operates on a 0–5 volts range, the step size of the device is 5/1023 = 0.00488 volts or 4.88mV. Thus, we can interpret a 4.88 mV input voltage to any of the analog pins as 1, 9.77 mV as 2, and so on until 5 V as 1023. Anything below 4.88 mV is considered 0 and above 4.99 V as 1023. Other pins on Arduino UNO: GND (Ground pins): Five ground pins are available on the board. Every signal that goes to Arduino needs a reference. The GND pin is the reference. Whatever devices you connect to the Arduino should have their own ground; the negative side of the power supply is normally the ground. All of the grounds of different devices should be wired together to have a unique reference voltage. RESET – Use to reset the Arduino Board. If this pin is supplied with 5 V, the board will reset automatically. I/O Reference Voltage (IOREF) – This pin is the input/output reference. It provides the voltage reference at which the microcontroller is currently operating. Sending a signal to this pin does nothing. 3.3V and 5V: These pins provide regulated 5v and 3.3v, respectively to the external components connected to the board. Vin– It is the modulated DC supply voltage, which is used to regulate the ICs used in the connection. It is also called the primary voltage for ICs present on the Arduino board. The Vcc voltage value can be negative or positive to the GND pin.
44 www.eTechnophiles.com 30+ Projects on TinkerCad(Step by Step) Given below is the list of projects we are going to make in this section and what you will learn. Digital output Blinking LEDs Piezo buzzer PWM output Fading an Led using PWM How to use Serial Monitor and Plotter Digital Input Pushbutton with Arduino Analog Input Potentiometer with Arduino Using a Potentiometer to control an LED Sensor Guide LDR Temperature sensor IR sensor Ultrasonic PIR GAS Sensor Displays 7 segment display LCD 16 X2 Motors DC Motor Servo Stepper Using a Motor Driver IC L293D Advance Projects: Multiple sensors and LCD Mathopia
45 www.eTechnophiles.com Just like "Hello World" is always the first example in any programming language, "Blinking the inbuilt LED" is always the first project in Embedded systems. 1. Blinking the LED(inbuilt + external) The inbuilt LED is connected to pin 13 in Arduino UNO. So to control the LED we have to control this pin. We are going to control the external LED also from this pin. Note: The digital pins of Arduino give 5 V. So before connecting an external LED, always use a resistor in series. In this case, a resistor is internally connected between the internal LED and pin 13. But for the external LED, you need to connect a 100 ohm resistor. Circuit Diagram: For this project, you only need an Arduino board. Select and place it on the dashboard. You can also copy mine from here (TinkerCad file). Blinking the inbuilt LED Then go to the Code section and type the following program: Program:
46 www.eTechnophiles.com void setup() { pinMode(13, OUTPUT); //set digital pin 13 as OUTPUT pin } void loop() { // this loop runs continuously i.e, LED Blinks with 1 second delay digitalWrite(13, HIGH); // give output 1 or 5v on digital pin 13 delay(1000); // pause or delay this HIGH logic for 1 sec or LED is on for 1 sec digitalWrite(13, LOW); // give output 0 or 0v on digital pin 13 delay(1000); // pause or delay this LOW logic for 1 sec or LED is off for 1 sec } Code explanation: First and foremost, pinMode(13,OUTPUT) sets pin 13 as output. Now for the blinking effect, the LED should turn ON and OFF in a loop. So inside the void loop(), it is first turned off and then on using the digital write command. The delay of 1 second in between, i.e., on for 1 second, then off for 1 second, and so on, makes the LED blink. You can also connect an external LED to pin 13 using a 220-ohm resistor, just like in the circuit diagram given above. 2. Using Piezo buzzer with Arduino A piezo buzzer is an electronic device that produces sound when powered by a voltage source. It utilizes the Piezo effect to generate sounds using a wide range of frequencies and tones. Components: Arduino uno Buzzer / piezo speaker 100 Ohm resistor There are only two pins on the buzzer i.e, +ve and -ve or red and black, respectively. If you connect it across a 5 V supply, it will produce a constant sound. You can try this on TinkerCad. The buzzer can generate different sounds based on the frequency of the square wave signal given to its +ve pin. This is achieved by using predefined "tone" and "notone" function. Circuit Diagram: Create the circuit diagram as shown below. You can also copy mine from here (TinkerCad file).
47 www.eTechnophiles.com Piezo buzzer with Arduino Program: Go to the Code section and type the following program: const int Buzzer = 3;// buzzer connected to pin 3 void setup() { pinMode(Buzzer, OUTPUT); } void loop() { tone(Buzzer, 1000);// send 1 KHz signal to pin 3 where buzzer is connected delay(1000); tone(Buzzer, 3000);// send 3 KHz signal delay(1000); tone(Buzzer, 5000);// send 5 KHz signal delay(1000); tone(Buzzer, 10000);// send 10 KHz signal delay(1000); noTone(Buzzer);// stop the sound.. delay(2000); // for 2 seconds } Tone function automatically generates a square wave signal(50% duty cycle) with frequency defined by you. 3. Fading an LED using PWM
48 www.eTechnophiles.com If you look closely, you will find the '~' symbol on digital pins 3,5,6,9,10, and 11. These are digital pins having an additional feature called PWM. Hence these pins are called PWM pins. What is PWM? Pulse width modulation is a technique that is used to generate analog signals from digital devices. This technique is commonly used with microcontrollers. It is a method of reducing the average power delivered by an electrical signal by effectively chopping it up into discrete parts. For example, Suppose you want a DC motor to run at a certain analog voltage between 0 and 5 V. This is not possible because digital pin of Arduino can only generate either a High or Low signal. Thus, to attain the desired output, we can create an analog signal by switching the digital output on and off very quickly with a certain frequency. The created analog voltage value varies with a varying pulse width(duty cycle). See the image below: As the duty cycle increases, the analog voltage value increase. This is the concept behind the working of PWM pins. Note: Remember that this is not a pure analog signal. PWM can only mimic and simulate the effects of a pure analog signal; it can never perform pure digital-to-analog conversion (which generally requires some active components like capacitors and inductors). How to use Arduino PWM Pins? To generate a PWM signal you need to vary the duty cycle of the digital output signal. There is a predefined command for that: analogWrite(PIN NUMBER, PWM Value); PWM Value should be anywhere between 0 and 255. 0 means 0 duty cycle, and 255 means 100 % duty cycle. Pin Number takes the value PWM pin number(3,5,6,9,10, and 11). Components: Arduino uno LED 100 Ohm resistor Circuit Diagram: Create the circuit diagram as shown below. You can also copy mine from here.
49 www.eTechnophiles.com Fading an LED using PWM Program: Go to the Code section and type the following program: How to use Serial Monitor and Plotter? Serial Monitor and Serial Plotter are two powerful tools in TinkerCad that allow users to interact with the virtual circuits they are designing. Here's a step-by-step guide to using them: 1. Open TinkerCad and create a circuit. 2. Make sure the circuit includes an Arduino board, and connect sensors or other components as needed. 3. Click the "Code" button to open the code editor. 4. Write a sketch that outputs data from the sensors or other components to the Serial interface of the Arduino board. This can be done using the **`Serial.println`** or **`Serial.print`** functions. 5. Click the "Circuits" button to return to the circuit view. 6. Click the "Serial Monitor" button to open the Serial monitor. This displays the data that's being sent from the Arduino board to the TinkerCad simulation.
50 www.eTechnophiles.com 7. To use the Serial plotter, click the "Serial Plotter" button. This displays a graph of the data, which is updated in real-time as the circuit runs. Here is an example on how to use serial monitor and plotter in TinkerCad. 4. Using Potentiometer with Arduino Components: 1. Arduino Uno 2. Potentiometer Circuit Diagram Create the circuit diagram as shown below. You can also copy mine from here. Potentiometer with Arduino 1. First, we need to connect the Potentiometer signal pin to the A0 pin of Arduino. 2. The VCC pin of the potentiometer goes to +5v of Arduino Uno. 3. The GND pin of the potentiometer goes to the GND of the Arduino Uno. After connecting the potentiometer, paste the code(given below) in the code section. You can see the Serial Monitor button at the bottom left corner of the code dashboard. Upon clicking it, you can see the Serial plotter option(toggle graph) in the lower right corner. Open both the serial monitor and plotter to visualize the potentiometer data. Note: You have to simulate the circuit first to see the real-time data on the serial monitor and plotter.