This session introduces the fundamentals of electronics and microcontroller programming through Arduino, covering the three major parts of robotics (mechanics, software, and electronics), Arduino Uno development board components (microcontroller, pins, regulators), digital and analog IO pins with PWM support, sensor integration (IR sensors for line following, ultrasonic sensors for distance measurement), communication protocols (UART, I2C, SPI), and practical programming concepts including setup/loop functions, debugging techniques, and LCD display interfacing.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Session 5 - Introduction to Electronics | iBot Summer School 2026
Added:It started.
Hello everyone.
um waiting for 5 hours after the section.
Yes.
You guys need to know anything else and let me know in the chat.
It's the audio Okay.
If you can let me know if the audio Hello. Hello everyone. Um, let me know if my voice is clear or not or if it's still breaking.
It's still low. I will some additional settings. Just let me know.
>> Yeah. So, I'll start in like another five minutes.
Okay. There's one more mic volume. Okay.
Um, give me a minute.
Okay. How about now? Is it better?
Okay, next. Yeah, I'll start the session hopefully by 7:15.
Um before that let me show a quick overview of what we will be going through.
So, Yeah, you guys can hear me now.
Hello.
Give me a give me one.
Hello.
Uh the No.
Okay.
I just Oh, having some.
So, let me let me fix the audio issues and Take care.
Um, what not. No, that's cuz I haven't enabled it. But you can see here Yeah. Hello.
this channel.
Okay.
Okay, can you guys hear me now? Um, if it's still breaking, give me a few moments. I'm sorry for the delay. I'll try to get it fixed.
Is it better now as in um you guys can now actually listen Or is it still breaking? But it's just it's just more audible.
Hello.
Uh guys, can you hear me? I continue trying to 7:30. Let's hope that you can speak.
Yeah. Is the audio or is it too?
Hello.
Uh, can someone just tell me that if the audio is still um still? Okay, give me a few minutes, guys. Sorry.
fine now.
I doubt it's a network issue.
Um, is it good now? Finally. Okay. Nice. Uh, is the audio fine?
Uh, as in the volume. Is the volume fine?
Okay. Nice. Nice. Okay. Um so I think it's about time. Yeah. Okay.
Yeah. Sorry for the delay guys. Um slide scale issue but okay. Um so let's get started with um the role of electronics.
So so I would say electronics uh I mean robotics you can classify into three major parts right. mechanics, software and electronics. So the mechanics part is like the um like the actuators and like designing the structure of the robot itself.
That's the mechanics part. The software part is like um like the core logic of the robot like the brain of the robot you can say. And then comes the um electronics part. So the electronics part is all the sensors um the motors themselves how to control the motors and the software part actually runs on the hardware that the electronics part provides. So uh for basic stuff it's microcontrollers. So the software runs on the microcontrollers and we'll be looking more into um the microcontrollers today and then how to actually program them and use them today. So first I'll introduce you guys to something called Tinkercat. So I did see from the poll that a good number of you did uh use um you know Arduino and all before but even I have been using it for a long time and I didn't know about the existence of this website. So Tinkercad is a website that uh you can log into and you can make these nice looking circuits and you can simulate these circuits and uh by doing this you have a few advantages and I I'll I'll show you those. So the first one is that you know you can actually save your components. So this is a very simple example. Um so here I've just connected a LED via resistor uh to the Arduino's 5 volt and ground. And now usually at least when I was doing it I used to just put a nice big resistance but that need not be the case. Sometimes you may or may not mistake it and put a small resistance in which case you will actually exceed the current limit for this uh resistor and uh Tinkercat warns you about this. Uh here it says you know the maximum current is supposed to be 20 milliamps but you're providing a 184 milliamps right. So uh Tinkercad is useful in this way. So, it's showing me that I will probably burn this LED if I connect it like this with this resistor, which by the way, um, is 1 ohm. 1 ohm is way too less for this. Now, yeah. So, in this one, this was like a very simple case, right? Is it's like very simple to spot that you know your resistance is very less. But in cases where uh you have much more complex circuits, it might be quite hard. and Tinkercad can uh help you identify you know that uh this this is drawing too much current or this might burn or something beforehand. So it's good in that way. The other way oh yeah and this is some memes I found. So here is a rare case of where you're passing way through much current through a resistor and somehow it didn't burn yet. So it literally became a light emmitting resistor and uh this is what happens when you let's say burn a component. So you can see here the ash remarks uh on the component.
Um but yeah the second thing about Tinkercad that's useful is that it's very organized. You can see how organized this circuit is here and you can see the same not the same but a similar circuit here and you can see how like more cluttered it is. Right? So Tinkercat can help in this way.
Any questions as of here?
So to summarize, Tinkercad is helpful in two ways majorly. One is to make your uh circuit look very nice so that you can visualize what you're doing beforehand before actually going to the hardware and actually putting all the connections. The second one is that it can help uh prevent shots. So okay, very nice. All clear then. Good.
Um now the basics of using development boards. So here is the Arduino Uno. This is an image of the regular Uno R3. And you can see uh how much stuff is on here. So this right here, this uh like I think yesterday it was described as a centipede looking thing. This right here is the ATmega and this is the microcontroller. This is where all our logic and everything runs. This is the microcontroller and this is where everything happens. Everything around it are accessories to make this microcontroller more easily accessible by us. So for example, this microcontroller has a lot of pins and you route all of these to uh these output pins. So these are all digital output pins we call them. I'll tell more about this later. These are all analog output pins and you can see here this is a 5V output. This is a 3.3 volt output.
This is the ground pins. So technically all the devboard is doing is actually helping you interface with this microcontroller very easily and it uh allows you to do this for a more wide range of stuff like with the same Arduino. You can probably do 100 or 150 many many projects interface it with many different sensors. So it's not specialized to a specific use case and that's basically what a development board is. It allows you to use a microcontroller in a nice and easy way and in like a generalized fashion. So some other components here you guys are not required um like to know this but I find it cool that uh you know there's so much just to use this one microcontroller nicely. So this is a barrel jack. So if you don't want to provide power through this USB connector you'll use this. And speaking about that the USB connector is what you connect um like your laptop will have one end of the USB cable and the other end will be connected to this port. Now uh only via this connection we are going to program the Arduino and uh this TTL converter I'll get to later. These are just fuses.
Uh this is a 5V regulator. So in case your input is not a nice 5 volts, this will convert it to a nice 5 volts. One thing about microcontrollers is that they require a nice and stable um um what is it? Like they require a nice and stable voltage input. So if your output if your like uh voltage input to the Arduino or the microcontroller is not a nice and stable 5 volt it can cause issues and that's a 3.3 volt regulator.
So see here we're getting 5 volts but we also have a 3.3 volt right so this converts that 5 volt to a 3.3 volt and all of these are IC by the way like this is an IC this is an IC uh even this is technically an IC and yeah um so but the base point is the microcontroller is actual brain and the development board just allows us to interface with it in a very nice way. Um apart from that there is this thing I would like to point out.
So if you look at this right here, this is something called an ICSP. It's basically uh another way to program the microcontroller. So I don't know if it was taken yesterday, but you can program microcontrollers using uh different methods. One of them is via this. So this will go via this converter and then that will go and then that will program the uh the microcontroller. The other way is you connect these pins directly and then you program it. So this is two ways to um program the Arduino. Uh anyways, moving on. Um so Arduino Uno is like a it's like a very good starting point. So the documentation there's a lot of documentation online. It's a very good starting point for people who have not used um you know the Arduino and stuff like that beforehand. So in in case you are one of those people who have not used a development board before, I would suggest the Arduino is a very good starting point. It has a very nice uh community and it has a lot of good um tutorials online as well. So you can probably get started from there. And yeah this again so when we want to program the Arduino we will basically use this port and connect it. So there will be a connector and we connect that to our laptop. So let's assume that this is a laptop. So we connect this to our laptop. Then we have something called an Arduino ID. So in this IDE we can write code or software um and then we program that onto the chip. So when you program something onto a chip it's like once you program it it's stored on the chip. So next time you don't actually need to power it like this. You can power it via the barrel jab. What I'm trying to say is like you can have um like once you program something we say that it's flash on. So there is a flash chip here and what that does is or it's an internal flash it holds the code that you're uh that you send to it and even after you power it off it stays there. So the next time you power it on by default the Arduino starts executing the code that you had entered last time. Uh and yeah so in some projects like if you want to put this on a robot you don't really want a cable running to your laptop all the time. So what you do is you would uh like by robot I mean like a car or something that moves. So you would program it and then you would connect the barrel jack so that it just runs on its own with power that's on the robot itself. Um so next slide. Uh yeah okay so every pin so you can see here there are a lot of pins here. These are all pins that we can use. Um yeah so there are these pins which are digital IO pins and there are these pins which are analog input pins or analog IOPS whatever these are analog the distinction is these are analog and these are digital um I'll tell more about these in like two slides but so these are all ION and spec uh specifically these uh digital ION we can configure in a in a few ways. So the first method is an input. So let's say we have our Arduino and we want to connect it to something that's going to be giving us a reading. So let's say some very basic sensor is here. So we have the Arduino and then we're going to connect it to a sensor and this sensor will give a digital output, right? So in that case what we'll do is we'll connect this sensor to a digital port on the Arduino and now uh we'll configure it to be an input. So the sensor the sensor will give an output and the Arduino will take that data as an input. So usually when you have stuff like sensors you'll configure uh the port it's connected to as an input port and uh when you have stuff like uh servos or actuators or motors that you want to control that case you'll use them as output. So input means the microcontroller will be reading the voltage or it'll be getting an input. Let's say that's an analogy.
And output means it will be writing the voltage or it will be giving the output.
Okay. And there's this third case called an input pullup. So this is kind of specific. So one use case of it would be let's say you have a button. So what a button does is very simple. You have two wires on either side of the button. So one button is here. Uh I mean there's a button there. There's one wire there and there's one wire there. And when you press the button, it shorts these two wires. So it creates a link, right? So that's the button. But what happens when the button is actually not pressed? So if it is not pressed, then this uh like this uh short never happens. So both sides of the wire are technically disconnected. And what this leads to is something called floating voltages.
Floating voltages are basically when the microcontroller doesn't know what voltage uh it's supposed to be reading like as in the voltage will fluctuate which is not something we want. Um so in those cases we use these things called input pullup or input pull down resistors. So yeah like you can see this is the button. This is a button. Okay these two are buttons. Um and so what you do here uh like you'll be connecting this to the IC's input pin and instead of the ground here uh you'll have like the sensor or like this is the button.
So whatever the other side of the button is connected to. Um so what will happen here is that by default when this button is not connected you'll be reading 5 volts because you know like there's nothing to pull this down. There's a resistor here but there's no current flowing. So this will simply read 5 volt. And similarly when this is disconnected, this is connected to ground. There's no um current flowing through. So this will basically be 0 volt. And so this will be reading 0 volt. So this is a pullup resistor where it will default read + 5. And this is a pull down resistor where by default it will read oh wait no my bad. Uh is the exact inverse. Uh oh no I'm correct. So a pull down restor by default it will read 0 volt. Whereas a polar presser by default it will read plus five. Default as in when the button isn't pressed like a button is a specific use case but it's the easiest one to understand.
Right? So any doubts till now. I feel like I'm going a bit fast. Um if you guys need me to slow down or go through something again let me know.
Oh okay fine if it's fully clear then uh then it's good let's go on so now I told about um the IO pins right so there are these things called digital IO pins and uh concept Yeah. Okay. So, there are these things called digital IO pins. So, if you go back Yeah. So, these right here, these are the digital IO pins of the Arduino.
There are a total of 14 of them. And these two have very special uses. And you see these um like these symbols here. This is like a tilda. If you zoom it in, it will look something like this.
And this stands for something called PWM. I'll come back to this a bit later.
But um so this uh within these IO ports there are these two which are a bit special. Uh they're used for something called UAT communication that also I'll cover later in the session. Um and there are these uh within the remaining ports there are some ports which have a tilda symbol which mean they support PWM. um which again I'll tell about later but yeah like I said pins 0 and one so if you see here pins 0 and 1 are used for u communication I'll tell that later the pins with the tilda uh support PWM and okay and when I say these are digital pins that means that when you configure them to an output they will uh like only you can only write 5 volts or 0 volts and nothing in between obviously I'm excluding PWM. PWM I'll get to a little bit later. Um and uh if you put them as an input pin, you can only read um the approximate voltage. So if it's 4 volts, then it'll read logic high. And if it was 1 volt, it would read logic low. So that is a that is like a distinction uh that happens. So if it's I think I don't know the exact values. These are like completely approximate. But if the voltage is lesser than uh 2.3 volt then it's logic low. Again this is all approximate point is like it's not 2.5 greater it's high 2.5 lesser it's low.
There is a in between range where it's completely like you don't know what uh logic it is but uh let's say if it's lesser than 2.3 then it's logic low. If it's greater than 2.7 then it's logic high. Um so point being uh you can't read like the in between voltages here like if you if the pin is getting like a 3V input it will assume that that is logic high and it'll uh all you can read in the software is logic high you can't read anything else. Um so that's about digital IO pins. Next is the analog uh input pins. So see you can configure them as an output pin but um let's say so there's something called a DAC and a ADC. So a DAC is a digital analog converter and an ADC is a analog to digital converter.
The Arduino has these. So when you get an analog voltage as input, so in this case you can get those vol uh that and you can convert it to digital signals for the microcontroller to read. But when you have um a digital signal, the Arduino doesn't have these by so this is a digital to analog converter. So you cannot convert digital pins to a analog voltage. So when you get a that's why these pins are labeled as analog in. Uh so yeah see the analog in is because these have ADCs. So you can convert the analog voltage into a digital method that the uh Arduino can read. Um but when you're going to write outputs right so uh when you want to write an analog output like I think many of you would have heard about the dimmable uh LED so that is a very popular example that's used so in that case you'll use um these tilda pins these pins with the tilda here like look here 9 10 and 11 uh 6 5 3 all of these pins they support PWM So since the Arduino doesn't have the DAC, you would have to do something called PWM to get these analog signals. So what you do is you have uh let's say this is 0 volt right here. And let's say that this is 5 volts right here. But I want I want to output 2.5 volts. So instead of outputting uh let me say green is the actual 2.5, right? Green is the actual 2.5. rather than outputting that what uh these pins do is that they alternate between 0 volts and 5 volts with a 50% duty cycle. A 50% duty cycle basically means that half of the wave they're at uh low and half of the wave they're at high. So now if you see uh if you take this as a wave I don't know how I'm going to highlight that. Okay, let's say this is the wave. Um right, this is the wave. Let's say so now for half of this time it's at 5 volt and the other half it's at 0 volt. And so since all waves are just uh like a repetition of this if you do the math you're basically doing 5 vol * tx 2 + 0 * tx2 by t. Right? So what this average comes out to is 2.5 volt which is the voltage we want. But it's not a true analog voltage. It it is a it is okay. PWM is known as um like if you expand it it's pulse modulated uh width or something like that. But point being basically by changing this duty cycle you can arrive at different analog values. So let's say you want to get uh I don't know 3 volts as the analog output like you want to write 3 volts to a pin then you will make a pulse width of Yeah, pulse with modulation. My bad. Um, so you will make this to be I think 60% and then uh so when it's high so this is 5 volts and then this is ground 0 volt. So yeah I think you'll yeah will you make it to be 60%.
Uh yeah so this will be 60% of the um periodic layer and then ground will be the 40%.
And then it will recur like this.
So by doing this you're creating like pseudo analog voltages let's say um but yeah these aren't true analog voltages they are pseudo analog voltages. also this um the A1 pins. So like um like the digital pins here we had these two which are dedicated for UART. There are these two pins as well which are used for um something called as I2C communication.
I'll talk about this also later but yeah so you can um these two are also kind of dedicated for I2C but that's only if you decide to use them. If you don't decide to you can use them as normal analog input pins as well. Okay, now that we're done with that, let me make a quick blinking LED uh in Thinkercat.
So, I wanted to show you guys just how nice uh this website is. I think you guys should try it. So, once you log in, you'll get a page like this and you go here to this create button and you can create a circuit.
Now, once that's done, you'll get to a screen like this. So this is a project name and here are a bunch of components that you can use. These aren't even all of them. This is the number of components that you can use. So this has a variety like some of the IC's if you want you can try this is a 555 timer.
You can try make a a clock with that clock signal with that. And there's a bunch of other um really cool IC's here.
But I want to just stick to the basics and I will use an Arduino. So here this is the Arduino in Tinkercat and you can have an LED and a resistor. So point is to make a simple blinking LED code and by doing this example I hope any of you who don't know how to code an Arduino you know you will learn that as well. So the anode we can connect to some pin here. I'll just take um pin number nine or why not pin number 10 also. I'll take the pin doesn't really matter. We'll configure that. Um, and then here you'll connect the cathode and then you'll connect that to ground. Right?
So the circuit is basically this. All you're doing is a LED, a resistor, and then that's going back to ground. Now to actually code the Arduino, you click on the component and then you click on code.
So this is a block kind of way of coding. It's for people who are not um introduced to C yet. And I'd say this will be somewhat intuitive. You can try this as well. Uh but I will be using the text mode for this. So when you're coding the Arduino, there are these two loops that you start with. There's something called a setup loop and there's something called a uh um I mean there's a setup function, sorry, and there's a loop function. So setup function is what will run the moment you connect your Arduino and then you program it. So the moment your program starts running, this will run exactly once. So this function will run only once and at the start of the program. Okay. Now this loop function the moment this setup function is done all the uh microprocessor I mean the microcontroller will do is loop through this function again and again and again. So once it's done with this it'll again come back here. So it'll be stuck in this infinite loop and all of your logic and everything will go here and here you'll do the setups like right here. So here this is like a default code that um Tinkercat provides for blinking uh the built-in LED. I will remove that and I will start from scratch. So let me let me just remove this.
Yeah. So I always like defining um the pins I'm using here. So here I'm using pin number 10 to drive this LED. And here I am going to be writing voltage here to this pin. I am writing voltage.
So I will configure that pin as an output. So first let me define it. LED pin is 10. Also I'm assuming you guys know like the basics of C. uh and int is a data type and this semicolon is required at the end of every line. Uh this is the assignment operator and this is the name of the variable. I'm hoping that most of you know um most of you know this. Don't worry about attendance. I will get to that uh soon.
There is an attendance slide. Um I will get to that soon. Uh it'll be there um in this meet itself. Anyway, so I'm hoping you guys know the basics of C.
Like the semicolon is there. Uh the code we're writing is mainly C with a bunch of custom functions. Let's say so one of those is pin mode. So pin mode, like I said before, you can configure all of these pins to be one of like three things. Input, output, and input pullup.
So you can configure this to be any of those. So the way to do that is you write pin mode and then the pin. So here I'm going to be uh doing that for LED pin and I'll be configuring it to be an output right. So this is that um so all this line does is basically tells the microp processor we are the ones who will be writing a voltage to this. You don't need to read from it. We will be writing voltage to this. And the reason we're doing that is because once we write a voltage to this it will go here and then there'll be a current. We can configure this resistor. By default, it's 1 kiloohm and then you go back to ground. So that creates a completed circuit and the LED will glow. Now to actually make the LED glow, we'll use something called digital right. Okay. Um I spelled that wrong. Yeah. So um digital write is a function which basically writes either a high or a low uh a low voltage. High low high voltage being plus 5 volts, low voltage being 0 volt. So it will write that voltage to the pin that we select. So here we do LED pin and we do high. So my point being now this line what it will do when it's executed it will make this voltage high. Now when we want to blink it we want it to be high for some time. Then we want this to be low for some time. So the LED will uh glow for some time. Then when this is um returned to low or this is um connected to ground it will turn off. For that we need to delay for some time, right? So there is a delay function. And here the number you're entering is the time delay in milliseconds. So I will delay for 1,000 milliseconds which is just 1 second.
Right? And now you can just copy this code and instead of writing it to be high, you can write this to be low.
Right? So let's go through this once again. I'm defining which pin I'm going to use. So I'm using the 10th pin. So that's [snorts] uh LED pin is defined to be 10. I'm using pin mode to define that this is going to be an output pin. Then I'm writing a voltage. First I'm writing a high voltage. So it will write this to be high. This will become + 5 volt.
Current will flow. This will glow. Then it waits for one full second. Then after that second is over. It will write this pin to be low again. So now this is ground and this is also ground. So there's no voltage flow. So the LED won't glow. And then it will delay another 1 second before it goes to high.
So then you click start simulation here, right? And as you can see, it's a blinking LED.
Okay?
So that's the very b this is the most basic uh thing that you can do with an Arduino. Um we will later go on to using sensors and stuff. Uh it will still be very basic but with the Ardino you can do a bunch of stuff. Usually this is the first thing any beginner does. So any questions guys that you guys have on this Okay. If there are no questions, then let us get back.
Yeah.
Okay. Now, now that the blinking LED is done, um let us go to a quick question.
Okay. This is something I wanted to show. Um, but yeah, so like I mentioned before, Thinker CAD can show when a component is going to be fried. But you can see both of these circuits are doing basically the same thing. They're making an LED glow. But in one case, it's showing that the LED is the one that's frying. And then in the other case, it's showing that the Arduino itself is what is going to be uh broken. So the difference um is basically that here you're using this a pin that the microcontroller controls to power this LED and in this case when the resistor is too small it causes too much current to like flow via the microcontroller and thus this will uh the microcontroller will what be um the microcontroller is what actually has the excess current flow in this case the 5 volt isn't actually like these power rails aren't routed via the microcontroller.
they are routed via um you know like I told that's a 5 volt regulator right this is a 5 volt regulator so the output from this is what goes to this so you know having two less uh voltage I mean too much current drawn here will only damage this it won't really damage this right so that's why here it's saying this is uh the LED is what will fry and here it's saying that the microcontroller is what will fry um but yeah that's a like a interesting distinction between the two. Okay, now we get into um No, I believe the 3.3 is here if I'm not wrong. Yeah, see this is the 5 volt regulator. There's another 3.3.
You're not wrong. Uh but that's here.
The 5 volt is actually here. Um anyway, so now to get um readings from sensors.
Um okay. So let's go let's start using um uh readings from sensors.
So, okay. So, there are two sensors that I will be showing you guys. The first one is an IR sensor. You can get it in a module like this. And the other one is an ultrasonic sensor. Uh you can get it in a module like this. I don't have the IR sensor with me, but I'll try show a demo of the ultrasonic sensor working.
Now, um let's get started. So, the way an IR sensor work, um like I like to know how the sensors work internally. So I'll tell you guys about that right now.
So IR sensors basically they send an infrared signal. So these are signals uh infrared wave. So these are waves that we cannot see by ourselves but it basically goes and then it hits a surface and then uh based on what color the surface is or the type of the surface some or like most of the wave may not be transmitted back. So one of these is a transmitter IR transmitter the other one is a receiver. So based on how much um of like the wave that it transmitted it receives it it can determine what type or what color um the surface is. So specifically dark surfaces uh they absorb a lot of of the infrared and because of this u most of the infrared that it transmits it doesn't receive it back and uh based on that there's this potentiometer here. Uh for anyone who doesn't know a potentiometer is basically something where if we turn this uh screw let's say it's a screw uh if you turn the screw then we can change how much resistance that has. So by turning this we can change how much resistance it is and internally it is configured so that this uh changing this resistance will change how sensitive it is. So by modifying this resistance we can um we can basically adjust the sensitivity of this sensor. Okay. So where will this sensor be used? You can use it for line following robots. So here you have a black color line in basically what is a white colored background. So these sensors can easily once they're like properly uh calibrated. So to calibrate you again change this. Uh but once they're calibrated you can easily find the distinction between what is a black line and what isn't. And because of that they are widely used in uh these line following robots. Um yeah so this is basically what I just told. Um yeah and also this is a digital output.
So it doesn't really tell us how much of the wave it's receiving. It filters that out and it only gives us a digital output. So based on how we calibrate this it will either give us a high or a low. um it'll it'll only give us digital outputs is a part.
So this is a circuit that you'll use. So you're providing it 5 volts and ground for it to function. And then the output you're connecting it to a digital pin, right?
Um and yeah, that's about it. So wiring is very very simple. Uh and yeah, you have to configure this pin, the pin number nine on the audio, you have to configure it as an input pin because this is what is giving the output. So the flow of the signal will be from here along the wire and then into the Arduino. So you want to accept the val uh the vol the or like you want to input the voltages that it's giving. So you'll configure it as an input pin. Um yeah anyway so yeah you could connect it to an analog pin and it'll still work just that it will probably read zero or 5 volt. So that's the only difference anyway. So now connecting this alone won't be enough. you'll have to actually code it. So you'll have to code the Arduino to understand that oh I want to read the values that it's given. So how you'll do that is this. So you'll get a um like I said the pin that you're connecting is pin number nine right? Pin number nine you're using it's behind all of this. Um but yeah you're connecting this to pin number nine. And so you um um you write a variable which is a constant like you a constant is basically something that won't change throughout the duration of the program.
And this pin you're not going to change it while the middle in the middle of while it's running. So it's a constant and that's configured to be pin number 9. Then this is something called a serial. Um I'll tell about this in a little bit. Um and then there's uh this you're configuring this to be an input because you know we discussed just now I'm sorry. Anyway um so now this is a new function digital read. Before we use digital write which basically writes this voltage to be 0 or 5 volt. Now digital read does basically the same thing but instead of writing it it reads the voltage. So if it's a + 5 voltage it will return this to be high right high as in logic high and if it's a zero voltage or ground then this will be low.
So that's all the digital read does and uh the way this sensor is configured is that by default it will output a high voltage and when the moment it doesn't receive it back like whenever it's receiving it will uh whenever it's receiving the wave that it transmitted it will give a high voltage and whenever it stops receiving that it will give a low voltage and because of that we want to actually check for low voltages right because only when the wave isn't returned like valid explained yeah so it doesn't get back the fully transmitted wave.
So that's when it hits a black surface, right? So we want it um we want the low voltages because that's when it has detected a black surface. So I wrote obstacle because you know it could be any surface like different surfaces have different properties based on that it will change but generally like you know you check if the for like a line following robot you'll say that yeah we're on the line that's what you'll do and otherwise you'll print that u okay serial.print line is just like if you've used C before it's just like a print statement or like a if you use Python it's a print statement. I'll get to exactly what the serial does in the next few slides. Uh but right now just assume that it's a print statement that you can visualize, right? And this delay is just so that you know uh you don't want to sample every uh single time that gives you some form of unreliability. So usually you add a very small delay. So this is like 200 milliseconds. You probably won't notice it, but you know for robots this could be a small like a lengthy enough delay. Uh but yeah, so this delay is just there.
Anyway, next is the ultrasonic sensor.
So, the ultrasonic sensor uh which is also known as the HR HCSR04 is the name of the component. Uh but yeah, it's used to read the distance.
So, the um the IR sensor you can only tell if like this surface is black and if this surface is white. You cannot tell how far you are from that surface or anything like that. All of that you take care by adjusting the sensitivity to. So instead you use the ultrasonic sensor when you want to measure distance.
So what it does is it fires um ultrasonic pulses and then it measures the time taken for the wave to return right and you use this to calculate the distance. So what it does is first it fires um a sound pulse and then it uh the sound pulse propagates it hits a object it returns back right uh I think you guys would have studied echoes uh in class 11 or 12 so it's basically that the sound pulse is getting um it's like getting rebounded from the object which is assume like a wall and then it's coming back to the sensor and you can measure the time delay between these sensors. So that's what the ultrasonic sensor will actually return. It won't return the distance. It will return the time taken for the wave that it sent to return back to itself. So coding this is um a bit more interesting. Uh so because of the way this works, we have a few pins. So there's a ground pin uh there's a ground pin here, a 5 volt like VCC pin here. Now there's a pin also called as trig and echo. So there are two new pins. So the trigger pin is what will allow us to trigger the pulse. So once you en like once you set the trigger to high it will uh the ultrasonic sensor will start sending pulses right and then the echo echo pin is what will be receiving and it will check if like the pulse that you send. So after some time it will propagate like this. It will hit a wall and then it will propagate back.
Right? So the echo pin is what will output high when it receives this uh wave back. So that's why we have two pins. One is to actually trigger sending uh the waves and the other is to notice the echo from them. Right? Uh but there is one issue with the ultrasonic sensor.
The sensor doesn't track the wave itself. It just knows when it sent it and it knows when it returns. It doesn't know what like the wave that's returning. It doesn't know where it's from. It only assumes that it's from the wave that it generated. But let's say we have two sensors like this. So we have one ultrasonic sensor. Assume the box is an ultrasonic sensor and we have another ultrasonic sensor here and both are pointed towards each other. Then in that case the sensor's uh wave from here can be input here like that can interfere with the wave here and moreover the echo can actually pick up this wave and assume that that's the wave that has returned like how do I explain this let's try again so this is the ultrasonic sensor first one second one so this one let's say it triggered and then it output a signal but before that itself this one has already been triggered And this signal is propagating and let's say that the wall is somewhere here. So what will happen is that let's say that the blue color is one time instant. By the next time instant the wave that this one has actually propagated is here. But the wave that this ultrasonic sensor has propagated has already reached this. And because of that it assumes that this is the time delay between the signals when in reality the signal it sent has still not even reached the wall. So this is an issue that can happen with multiple uh ultrasonic sensors. Uh but usually that's like you know you can avoid that by you know placing them on different sides or something like that. Um anyway um so again the circuit itself is very simple to wire. Both of these are digital pins. So the trigger is you are going to so the trigger will be taking an input like it needs to be fed the voltage. So the trigger pin you'll uh you'll make that as an output pin.
Whereas the the other one the echo pin is what will be sending a signal. It will be giving us the results. So you want to make the echo pin as an input pin. Right? So that's all. But both are digital is the point.
[clears throat] So the code for this is a lot more involved. So let's start with um explaining the code. So the echo pin and the trigger pin. So if I erase this, you can see that the echo, if you follow the line, it goes to pin number two. And the trigger, if you follow the line, it goes to pin number three. So the echo and the trigger uh are defined here. Again, they're constants because we're not going to change them while we're running the program. Then there is this time delay and then there's a distance. Okay, this is actually wrong. Um this is supposed to be unsigned long. Uh okay, this can be long also but let's say that this is unsigned long. Unsigned basically means that there isn't a signed bit. Um I don't know if you guys learned that in the last session but uh in binary there are different ways to represent numbers. One is unsigned, one is signed. Oh and distance is supposed to be a float. Uh the reason why distance is supposed to be a float, I'll tell you guys in in some time. Uh but yeah, this is a change I should have made. Anyway, um so pin mode, uh you like I said, you want the trigger to be an output because we are going to be writing a voltage through the trigger pin and you want echo to be an input because you know echo is going to give us a signal that we want to read. And then again, okay, so serial serial is basically our way of communicating with the Arduino. So if you let me go back to Tinkercad I guess um so let's say here let me quickly explain about um the serial monitor.
So I still won't go into how the serial monitor works as of yet but I'll tell you what it does. So serial dot begin and then there's a number here. This number is called the B rate. What this number is I'll tell you towards the end of the session. But serial begin basically initializes something we call as a serial communication between the two. What it basically does is that you know when you're connecting the USB, you can also um communicate with the Arduino while uh it's connected. So the serial is basically uh the wires within uh the USB that are being connected and that will communicate with the microprocessor and that will give us valuable data of what the microp processor is doing. So for example here I can do serial.right um or print ln. So there's two main functions. There's serial.print and then there serial.print ln.
Serial.print ln will print whatever you're saying and then add a new line at the end. print will just uh like I said it will print and then it won't add a new line so it'll continue in the same line anyway so I'll print um blinked once right and then here also I can write serialprint lm um started serial monitor so one thing you guys will have to be careful of is the number you're entering here known as a ba rate you'll have to configure here as well as the serial monitor so I'm going to start this Right. And here in the serial monitor uh okay in the Arduino you can configure it. Here I'm assuming it's already configured but there will be an option to set the border and you'll have to set it. But anyway um as you can see uh at the start right it printed um started serial monitor and then every time it reaches this line it says blinked once. So this is our way of communicating with um you know the Arduino and this is very useful when it comes to debugging cuz you can print out the values of variables and other stuff.
Anyway um let's get back. So that's like a very quick overview on what the serial monitor does. I'll come back to explain how it works later uh toward the end of the session. Um anyway so yeah um [clears throat] no uh yeah my bad.
Uh yeah so let me explain what this does. This basically just prints that you know uh we're going to start uh measuring distance. Uh this is a delay does not need to be here but we just added it. Let's say now here this is the main part. So first you are setting the trigger pin to low. This is just a safety check. Make sure that it's not already high. Otherwise it's going to get skewed. The data is going to get skewed. Now delay microconds is another delay function. The normal delay this number is in milliseconds. This is in microsconds. So you know a full magnitude smaller.
So this will be 2 into 10^ - 6 seconds.
This is a very small time but that's all it takes for the wave to get propagated.
So now after this small delay, you trigger it high. Uh the trigger pin is set to high. So now it will start blasting signals and you wait for another 10 seconds to make sure that at least a few signals have been sent and then you set the trigger pin back to low. Now the data is being sent and after it's set to low it stops sending and then these signals just propagate until they hit a wall.
Then there is this very important function called pulsen. So pulsen basically what it does is it measures the time taken between this command and the time till echo pin is set to high right. So till now echo pin is low but then now it starts a timer internally it counts and then it will give the time in microsconds when uh echo pin is set to high. That's why before I was stressing on why this should be unsigned.
unsane long integers can hold bigger numbers. So it'll give a more accurate um time delay. And the reason why distance is float is very simple. So you can see this calculation here and you can tell two things about it. Uh one is that it's the float uh it will return a floating value because you're multiplying it with the floating number and you're dividing by two. Now what this number is is the speed of sound in cm/s, right? So [clears throat] um yeah so this is the speed of sound in cime per I mean it's not seconds it's the proper units but it's set so that whatever number you multiply this with uh this will return the distance in centm right um and why you're dividing by two is also simple. So let's say once you propagate it hits the wall and then then you have the signal has to travel the same distance back to return back to the signal right so you're actually me measuring twice the time delay that is actually taken so this is t this will also take t like to return from the wall back to the sensor so you are measuring twice of the time you actually want and that's why you divide by two the rest is just printing the distance uh so like I this will um this will not add a space like a new line after this and so it'll basically print distance and then the variable like what distance it's measuring and then the distance in centime so while I get the tinkercat set up um here is the attendance link and a small break so if you guys have any questions and um yeah you can ask it in the chat meanwhile you can also fill the attendance form for this um session We started a bit late so it will probably go on till 6:45 maybe a bit beyond that. Uh but I'll try keep it before 7. We started only at 5:30.
I'll wait till like I think um 6:20 maybe 6:22 5 minutes I'll be uh you guys can fill the form and if you have any questions you can ask in the chat.
If I did go through anything too quickly or like something you guys didn't understand, let me know. I'll I'll go through it alone.
So, I did have uh a setup for um showing you guys this working in real life. So, I have a ultrasonic sensor setup, but right now I'm too scared to mess with the the volume. So, I will let it be for now. And toward the end of the session, I'll show you guys um a working LCD and um the ultrasonic sensor. So, is there any questions?
I'll wait for another 2 or 3 minutes for you guys to fill the form. And yeah, still if you guys have any doubts, you can ask it in the uh in the chat and I will respond.
Oh, input pullup slide. Okay. Yeah.
Yeah. Okay. Oh, pulse in it returns in.
Okay. Um, so pulsen should I uh do you guys want me to change the slides?
>> Yeah. Yeah. So, okay. Let me let me clarify pulson quickly. Uh, I will Okay, give me 2 minutes. If you have any more questions, put it in the chat. I will clarify that when I restart Okay, I'm hoping most of you guys have taken attendance. I will uh switch the slide now. And uh in case you guys didn't take attendance, it's fine. Again towards the end of the session, I think I will show it again if need be. Anyway, so one of you guys asked me about um this constant and how it comes out to be. So pulsen returns the time delay in millisec uh sorry microconds my bad not milliseconds this returns it in microconds. So that will be 10^ - 6 seconds right. And if you take the speed of sound as 344 m/s. So that will turn out to be because you want to convert meter to centimeter first. That will be 344 uh like 34,400 cm/s. Correct? Um but this is returning in microsconds. So if this is in microsconds then we want this unit to be in cm per microcond. Right? And if you want to do that, you have to multiply this because in 1 second it will transfer uh it will um like it'll measure 34,400 cm but in 1 microcond it will measure 34,400 * 10^ - 6 cm right so this is per microcond and if you do this math it comes out to be 0.0344.
So I'm hoping that clarifies the doubt about this. Um another doubt I saw was um the input pullup, right? So let me go to that slide.
Yeah. Okay. So let's say that this is a button, right? And let's say that this is let it not be connected to ground.
Let it be connected to some source or like some uh some sensor, right? And we want to read this value only when um this button is pressed. Right? It sounds simple but then when the button is actually not pressed this is floating.
So when it's floating it will read random um random voltages or we call it floating voltages and this is bad because the Arduino or like your microcontroller doesn't know that the button is pushed up like you are controlling this button. Okay. So the Arduino doesn't know that you've actually lifted the button according to it. It's still getting values from a sensor. So rather what you would do is you would add this resistor and a 5 volt here. Basically what this does is when the button is pulled up the 5 volt is what is connected to the input uh pin of the IC because you know there's nothing to drive it. So the current will be zero. So the voltage drop across this resistance will be zero. Right? And let's say that this button is pressed and let's say that the sensor gives 0 volt then in that case what happens. One sec. So if this does give 0 volt like it shown here then this is a direct shot right like it's directly connected to the input pin. So the IC will read whatever value is here which just so happens to be 0 volt. And to make sure that there is not too much current flowing this resistor is there so that you know the current goes like this and it's a manageable current. It's not too much that will damage anything. Okay, I hope that explains um the doubt that someone had about input pull-up. Okay, uh if you didn't understand it again at the end of the session, I'll again pause. So if you guys have any doubts, then I can explain it then. Um anyway, this is a very small uh intro to debugging uh the Arduino. So that's a lot of stuff that can happen in a project and uh you know these errors will like you know you can read the errors that the Arduino ID gives you. Uh sometimes they might help but sometimes they may not. So there was some memes I found about this. So this is something unironically that a lot of people do.
You add a lot of print statements and sometimes it does magically work. That always happens. Uh but do take this seriously. um you add a lot of uh printer statements. It's in the official Arduino ID uh like community that you usually add a lot of uh serial print line statements uh to see what value is actually messing up and this is a a very simple meme but it was something I found anyway. Um so debugging the first example. So this one should be very simple. Uh this is the code and you can see that when I tried compiling it, it gave me this error. Expected a semicolon before the closed brackets. So if you look at all the lines of code, there's a closed bracket here and a closed bracket here. Before the closed bracket, this statement already has a semicolon. This statement does not. So this is the error, right? So it's a very simple error. But once you guys start coding, I don't know how many of you have experienced this before. I certainly have a lot of times this happens. You will try find out the error and it will be a missing semicolon. Unironically this will happen and sometimes there are these weird compiler bugs that it says that the error is here in this line because it's missing a semicolon in like this line. It can happen. Stuff like this happens. Yeah. So anyway um next um problem. So this is an example of a blinking LED code. So this code its purpose is to blink an LED on pin number nine. But I'm not doing the digital right high low directly. Instead I'm doing this. This is a different logic that I've used. But if you actually run this circuit the LED won't blink. In fact it will always stay on. And uh but you can see that you know the code is actually compiled. So the compiler think there's nothing wrong with it. But your code is not doing what it's supposed to.
It's not blinking the LED. So this is an example. Uh I'm rushing through this because we're actually running a little bit of uh out of time. But this is an example of um what is called logical errors, right? And syntax errors a compiler can see, but logical errors compilers won't see like like this, right? Um anyway so in this code what I'm trying to do is when the state is high you write high and when the state is low you write low.
But if you actually notice the state is declared here and it's never being updated.
[clears throat] So the actual way to implement this is shown here. This is the correct one. So this additional line is what makes this code works. Um reason being let's say first it's true right?
So the first time it's being executed it's true. So it'll be set to high. So first execution it'll be set to high. It will wait 110th of a second and then when it comes back to this state is basically not of state. Right? This is what this line does. State is not of state. So state will be assigned whatever value it wasn't before. Before it was true. So now it will be false. So now the LED will be low. Then when it comes back again, before it was false, so now state will become true. So it'll become high again. So you know this is another way to write a blinking LED code. But this one if you don't add this one line, it won't work. So this is a logical error. And uh these are much harder to spot. And um yeah, so this one I've already covered. Uh that's why I'm not going to stand on this for too long.
But if you see this is not what distance you expect to measure. Okay, for context this code is basically the code that we wanted for um the sensors u this ultrasonic sensor. You look at this code and then you look at the code I just showed you guys. Yeah.
uh you look at this code, it's almost the same except now when you're measuring distance, you're getting some very bad answers and you're getting only integer values. This can be explained very easily by looking at this slide. So this is another specific case of uh logical errors. The compiler didn't find a single issue with it because according to it, yeah, everything is fine. There's no syntax error. But if you look at this, pulsen expects to return an unsigned long type. This however is an integer, right? So it's not having the capacity to hold the number that pulsen returns. Okay, quickly integers have a fixed capacity. It's capacity is four bytes. This not required. I'll just tell you quickly. And this returns an unsigned block long which is more than four bytes. So when you put that data here, it causes what is called an overflow. And that is probably what happened here. Okay, I probably moved the wall bit too far away and that caused overflow and caused this number to be negative. Anyway, that's beyond the scope. I think that you guys might have learned in the digital systems class yesterday, but okay. Uh but yeah, and then the second issue on why all of these are integers is very simple. We have defined this uh distance to be an integer. It's supposed to be a float. So that's why all of these issues uh it was a uh number. It wasn't like a floating value that we want to see. Um, so again towards the end of the session, I'll show you guys this working in real life. Uh, I'm a little bit scared to set up the camera right now after all the audio issues. At the end of the session, I'll see if I can set it up. Um, yeah.
Anyway, so like I said, this is supposed to be an unsigned long and supposed to be a float data type. Uh, yeah, here here I also wrote the time variable is measured in microsconds. So, pulsen returns microconds. Anyway, so now this is um some new and interesting stuff.
This is um some of the basing uh sorry basic communication protocols.
Uh so when we want to communicate between two devices so we have our laptop here. So this is this will be our laptop and we have uh an Arduino here, right? So this is an Arduino. Let's say how are we writing code here?
Technically the Arduino all it can read is binary and even your computer all it can read is binary yet you're sending code over just like a few wires inside the USB that's only four wires like this extra data but it's fine there's like VCC there's ground and then there are two lines called data plus and then data minus but is it not interesting that you're writing a full program worth of data you're transferring that over just two wires right before and all when we were using two wires just to get two different values uh in the ultrasonic we were using two wires just for two bits basically like bit is either one or zero right so we were using two wires for two bits and now we're writing thousands and thousands of bits over I mean it'll be even bytes or megabytes uh but you're writing a lot of bits over just two wires right so how this works is through communication protocols so I'm Pretty sure you guys would have read about um sequential circuits yesterday. So the basic idea is this um you sample like instead of using one wire for a bit in the same wire you send data at set intervals. So there's something called as a clock pulse. So let's let me try draw a clock pulse. Clock pulse itself is very simple. A clock like in our home a clock ticks every 1 second. Right?
Instead this has a fixed time period which you know like it'll be in a lot of like it'll be in megahertz usually or sometimes even for processes it's gigahertz but you see that this will be a constant time and this is very reproducible that's all that a clock signal is anyway so what we do is we send data so this is a clock line this is a clock and you sample whatever is in a data line so this is a data Okay. And you sample this data at fixed intervals.
Right? So if you do this, you will sample every time at something called a positive edge. A positive edge is basically when it goes from 0 to 1. When the clock grows from 0 to 1, that is called a positive edge. Like right here, you represent it like this. But whenever it whenever you reach a positive edge on the clock, you will take a sample of the data line. Right? So right now if you take a sample this will be one and the next time that happens you take a sample it's again one next time that you you take a sample again it's one but then the fourth time it becomes zero. So like this you can send data over just two wires and you can send this data sequentially and it's stored in stuff called registers and then what happens after that is way beyond the scope of this session but uh it's very interesting and um you know by like this is technically just a binary representation of a number right so this number if you translate this this is 2 + 4 + 8 so that will be this is just you're transmitting 14 so you've just transmitted the number 14 between two devices over two wires. Um, and when you're transmitting characters and stuff like that, you'll use something called as an ASKI number. So for A, it is 0x41.
This is hexadimal.
Uh, if you convert this, this will be 65. So A is assigned the number 65. So but for this to happen you both the sides need to know that you're receiving an ASKI number right so you send 65 it will receive 65 but it will interpret that okay I've received 65 in as key that represents a so I've I've received a that's what happens anyway so that's basically what I've told right now see send uh each bit in fixed intervals and you can basically this allows um data transfer between two devices very efficiently I will only be covering these three but there are a lot more interesting ones like uh CAN, USR and USB. I will be covering only UART, I2C and SPI in this session. Okay. Uh and each of these has their own pros and cons. So to start with UART okay uh so if you expand this this is universal. So this is universal asynchronous uh receive and transmit. Uh so R&T is receive and transmit but point being it's asynchronous meaning there is no clock pulse. Instead what happens is both the devices should be configured before send to sample at a specific rate. So instead of transmitting a clock signal you are basically transmitting one is to transmit data like okay so T is for transmit and R is for receive. So the transmit of device one will be connected to the receive of device two and the transmit of device two will be received uh will be in the receive of device one. So this way at the same time you can transmit data uh sorry you can transmit data from device one to device two and you can transmit data from device 2 to device one. This can happen simultaneously and the reason why it's asynchronous is because the clock pulse isn't like you know you're not sending it. It's supposed to be fixed. So before you're sending this data both of these devices like you know they'll be told okay receive data at this fixed frequency. And uh that's why it's asynchronous. Uh I'll tell you about this later like what uh the synchronous ones are. But yeah and the data itself is sent bite by bite. A bite is 8 bits.
So every time you send 8 bits okay uh and then you send headers, right? So you send something called a par bit like a lot of stuff is there. Uh like if I go into this properly it will take way too long. A parity bit I'll just tell you it's checking for errors in the data that is being sent. So in this eight bytes if there's an error the parity bit will become high. Uh there'll be also more information about what data is being sent but that is for later. Uh so that's all about UART. Okay. Now I2C I2C um okay so this is another communication protocol but rather than using um you know one wire for transmitting from one device to another and the other wire for transmitting like rather than using this configuration you use a configuration like this where there's something called a master and there's something called a slave right now I'm not going to tell what those are I mean see master uh it gives like the slight misconvention that you know only this form of data can happen like only from master to slave data communication can happen that's not true you can transmit from slave to master also uh the reason why we decide master and slave is because this is what gives the clock signal so before the issue was you can transmit only at one fixed frequency right now let's say you want to transmit at different frequencies at the same time right not at the same time as in first you want to transmit transmit data at I don't know 16 MHz and then after that you want to transmit data at 4 MHz right different uh like readings you're getting at different times so you're transmitting at different rates so to do that you control the clock and then you set the data bits accordingly so this is what I explained in the first slide right so that will be one line for setting the clock and then one line for data this is basically what I2C does um and now you might ask like before there are only two devices you know that if you are sending from TX bit you know that it's going to device 2 but when you have a configuration like this all of these are connected to the same clock and the same data lines how do you know if I send data from here how do I know which one will be receiving uh receiving the data so to take care of that there is something called an address so I don't know if I wrote about that here ah yeah so that's something called an address so the each of these devices is like when you're writing code for these devices, you'll say that okay this is my address.
Uh let's say that this address is zero or let's say that let's say that um this address is 2, this master's address is four, this address is six and this address is one. Okay. So when you're transmitting data before you send the actual data you will um you will send the address. So before you send you know let's say that this is the data that you're transmitting like 0 1 1 0 0 you will send a 7 bit address 7 bit is very arbitrary don't worry about that now but u you will send the address of where um this data is supposed to be received so if I set the data like this address to be like this which by the way is two in binary right so if I set two in binary to the address then the data will be read only by this. So in the protocol, every device checks if the address matches its built-in address and if it doesn't, it doesn't receive that address. Uh it doesn't receive that data, right?
Anyway, um sorry for that. Um but yeah, uh and similarly, if I want to send data to this slave, then I will write six, which is 0 like 1 0. Um but yeah that's basically how uh this works uh how I2C works but you can notice one downside see one upside we gain that is like a pro of using I2C is you have only two wires for communicating between multiple multiple devices you can have like many many devices here this I put only four but you can have a lot more if you're going to do the same thing with UAT like let's say you have a third device here right and you want to communicate between all of them you will need two wires here and two more wires here. So for just three device communication using UR between all the three you will need six wires right and if you make it fourth device that will be another two wires here another two there and then another two there so that will become 12 wires so as the the number of uh devices increase the number of wires you need increases exponentially so that's why I2C is very nice because it keeps it tidy and it keeps only two wires right um wait till Is this any question? I feel like I've sped through this a bit.
Any question.
Uh if you guys have any questions, enter it in the chat.
Oh okay. I2C full form is inter integrated circuit. It's like it's fine.
Um other than that uh there's like SD and SL which is uh serial data and then serial clock like point being the name doesn't matter. Sccl is what is going to transmit the clock. Let me show. Socc this is what will be sending the clock signal. The C is for clock and here the D is for data. So this will be sending like the data bits right. Um but yeah uh and then I2C I don't know why it's named like that but yeah it's inter integrated circuit. Um where exactly are communication protocols used? Um that is a good question. So let's say you have um like a complex robot right? Most complex robots will have like you'll have multiple dev board itself like you'll have an Arduino purely for um like motor control and then you'll have a Raspberry Pi. So you want to communicate data between these two devices, you'll use stuff like this. And moreover, if you go into some more complicated sensors, they will also use um like these protocols to communicate. So, I don't want to get too deep into this, but there are stuff like uh barometers and um there's stuff like my voice is breaking.
Um I have no idea why. Is it better now?
Okay, fine. It's if it's good now, then fine. Uh but yeah, there's stuff like u barometers and there's other like internal IMUs, we call it internal measurement units or whatever, but those all communicate data via these protocols. So you know knowing these protocols will be very good cuz yeah you will uh you'll use it to not only interface with sensors but if you have like a lot of development boards together then you'll have to interface like you'll have to communicate between them probably using one of these. Okay.
Um anyway so there is a downside to I2C and that is that there's only one channel for data transfer right like the same upside is the downside. You have only one data line. So if you want to communicate at the same time between like two different devices that's not possible like in UAT you can communicate simultaneously between the two devices but in I2C that's not possible because when one device is using the data line that data line is occupied you cannot like if this master is sending data to this layer at the same time not only like can the reverse not happen two other devices also can't communicate with each other because this data line is now used right so these um like it it slows down the speed a lot and like you can't communicate simultaneously um so to fix that there's something called SPI this is uh it's like much faster let's say than I2C so let's say one thing is the protocol waste time sending the address like I told before you send address uh like you send the data you send the addresses So that actually is a waste of time because you're sending seven bits for no reason, right? So to take care of that, you have something called slave select, right? So this slave select will basically decide what um slave actually gets the data, right?
Sorry. Um anyway, and then there are uh the other thing is that it doesn't allow for parallel communication. Okay. So here um you know there are two lines there is mosi and then miso. So moi is master out slave in. So output from the master to the slave right that's moi. So from the master you're going to slave and then that's miso. So from the slave you're going to the master. So now like a master and a slave can communicate parallelly. So that is one benefit. And then the clock line is there again. uh we don't want asynchronous because then we can't change the data rate as we're uh running it. So we use a clock uh clock line between all of these, right?
Um so I don't think I'm going too much over SPI. Uh it is a little bit more complicated than this but yeah um let me just go through these alone though. So in the snare select if you send a high voltage then like if you set um like let's say you want to communicate with this slave then you set SS1 alone to high and these two to low in which case only this slave will receive the data and uh here you're using two lines of data transfer. So you're go you're allowing back and forth communication.
So the master can communicate with the slave parally the slave can also communicate with the master. Um so here again the wires is the lot more than I2C right so here you can see again four configuration like four things want to communicate yet the number of wires is what 1 2 3 4 5 6 that's six wires but at least out of the master but here it's only two so it's more wires but still better than UAT right so for UAT I think it was 12 wires right that we calculated so it's still better than UAT and um yeah that's all I'm going to do about SPI Okay, for now there's a lot more to it. If you guys are interested, read about it. It's a very interesting protocol. Uh, but that's about it for now. And before we go on to using the LCD, a quick note on serial monitors.
So, like I said before, these two pins are special in a way that they allow for UAT communication. Um, and the thing is how does serial actually communicate?
like USB protocol is different from UART and the the chip itself can only communicate in UART. So that's where this chip here comes into play. this uh is like I don't know I forgot the name of this but it's used to convert the serial data into data of a format that you are can and the reverse is also so it allows for um data from the Arduino to communicate with this and then converts it to a form that can be sent to the USB. So this small chip is technically what allows uh the serial monitor to operate. Okay. and the B rate that I was like avoiding before. So like I said um all of this technically operates on UART like the serial monitor is just UR technically and um so the B rate is the like the transfer speed. You remember how I said UR doesn't have a clock line. This is why you have to configure both devices to use the same uh data speed beforehand. And that is what the B rate is. It's in a unit called bits per second. But that will basically determine how fast you're sending data back and forth. And um yeah, so the first device here here in our analogy the like the first device will be the Arduino and then the second device will be the serial monitor. Let me clear this. So this is the Arduino Uno and this will be our serial monitor.
So I will show you guys after this towards the end uh on how to use um or like how to set the border in serial monitor. That will be a nice box. I will show that later. Um anyway, so that's a quick note about this. Yeah. So the B rate is basically the frequency at which you're sending data uh to and from like to and fro from the Arduino and then uh to the computer and so on. Anyway, that's about the basics of communication protocols. I feel like I rushed it. Any questions? Um, any questions?
If so, send it in the chat.
uh if there are any questions let me know. I will try wind up in the next 15 20 minutes.
Um, will we have that demo? Uh, can you be a bit more specific? What demo do you refer to? Um, as in the LCD and the ultrasonic sensor working that I will do after this session. So, this is the last section. I will go through this somewhat quickly. Yeah. Yeah, I will do that soon. Um, okay. So, if there's no other questions, I'll get started with this.
So, till [clears throat] now we have no visual output from the Arduino. Say that you have like a robot or like a car or something that you've made and the Arduino is here in the car, but you don't want to connect your laptop to it to see what values it's reading, right?
So, rather you'll have something like a LCD here. Uh, and you'll use this LCD to display some of the data like the speed at which it's going and stuff like that.
It's it's just a cool add-on. Um but it can be um used in some very nice use cases as well. Um so this is having a lot of pins, right? So there are eight data pins. There is an LED plus and minus pin. Uh there is also a separate uh VSSs and VCC. So that's another ground and 5 volts. There is a contrast control and there's a register select readr enable. So I'll tell you this quickly. um all of these are used to program um the LCD. So the LCD has a set of commands that you can send it via the register select, read, write, enable and these data pins and that programming is actually very difficult and you'll have to read the full data sheet. Um but luckily there are people online who make libraries. So we will write a single line but in the back end a lot of uh processing will be done and the LCD will be sped the data so that it prints whatever we want it to print. Um anyway so [clears throat] so yeah like I said it is possible to program the LCD without an MCU which is how you like you read the data sheet you know every command but you need but like now we'll just use external libraries because doing this programming it without the MCU is slightly beyond the scope of this session right now. Um so now notice that we have so many pins here. connecting this to an Arduino will basically take up most of the pins of the Arduino for this LCD itself rather we use a a communication not a communication interface let's say uh this board is an I2C board so what it does is it gets data in the form of I2C and then it converts that to data that uh you know it converts that to data of these pins so that it can read it right so this is basically something that goes on the back of the LCD and then you can send data via the I2C protocol and it will read it. So like I think someone was asking right where will we use this?
So you know this is a very beginner friendly use case here. We'll be using I2C simply to communicate with the LCD.
So instead of using I don't know I think it was 12 wires uh yeah to only four wires. So you know it's a good save. I think it's more than 12 wires actually.
So you're saving a lot of space on the Arduino. Um so to actually install the library okay again I will show this demo I think at the end um which is like in like 5 10 minutes. So uh there's something called a library manager which is this icon. It will be on the left hand side of the Arduino. You go there um and then you type in liquid crystal I2C in this search box and then this one by M Martin whatever this is I can't read it. Um and then you click install.
So this will set up or install the liquid crystal I2C library in your computer. So after that there [clears throat] are a few commands that we use. So the first one is the liquid crystal I2C um like this basically defines um that you know it tells the compiler that okay there is an LCD this is an address. Okay. So you know how I was telling I2C requires an address. So this module has an address and to communicate with it we need its address.
So there are programs online. I'm not going to show that now but for mine it was 0x27 and for most of them I think it's 0x27 or it might be 0x33. You'll have to run a program and check but yeah so you'll use a communication um like you'll use a address. Sorry. Um and then you'll use this is the size of the LCD.
So the LCD itself has a grid kind of pattern. It has 16 columns and it has two rows. So there are 16 columns and then there is two rows. So that's the data that we're entering here, right? Um and yeah, that's the data we're entering here and then the address. Yeah, again it changes based on the I2C LCD.back. So I can't go through how the LCD functions because of the time constraint. Um but the LCD functions uh you for the LCD to function you need something called a backlight and to enable this backlight there's this command called LCD.back which turns it on. LCD.init is something you generally run uh at the start of the program uh to tell it to basically clear the display of whatever was there previously. It resets the cursor and it also does a few other things which I'm not going to mention.
Um so after this we use a set cursor. So if you have a grid like this right I'm going to draw a smaller grid.
Yeah. So this is a 4 +2 grid right. So each of these have a position an x y coordinate. Basically this is 0 comma 0.
Like in programming you usually always start with zero. Uh this is 0 comma 1.
It's row comma column by the way. Uh 0a 2 0a 3. And then this will be 1 comma 0 and so on. And then this will be 1 comma um 3. Yeah. So basically you can set the cursor to a position. For this example I set it to 0, 0. And then you use LCD.print text, right? So the text I sent was hello smiley face. So that is the text that it prints. So that's all that it does. Um again the code for this like I said so the code for doing this example is this you define the I2C before that you have to import these two modules this module is what actually does the I2C communication uh it it was used I think to get the address it might be required for this example as well uh LCD.init in it so it clears everything.
Make sure the back backlight is on. This you don't really need. I just added it.
It's serial is generally something you use very commonly. Um then LCD.
Uh so clears okay this function basically clears whatever was there before. I know init does it already but you know these two are helper functions you almost always use. Then you set the cursor to 0, 0, and then you print this text. Right?
So another way to make it more interactive. So this will be the session I mean the demo I'll show later or like in 2 minutes. So you use something called LCD.Right right? So LCD.Right it basically prints one character at a time. LCD.print prints all of these at a time. LCD.Right will do exactly one character print and it moves the cursor to the right. So this uh code I'll walk through what it does and then I'll show what it does as well. So this code it turns the LCD on turns on the back begin serial. So for this example serial communication is actually needed. Now this command basically checks if the serial is like available. Sometimes you won't actually um you might have configured it wrong uh so that you know the serial monitor is actually not available. Um so this simply checks if the serial monitor is available. Then this is a small delay we add. We clear everything and while it's available you print whatever the serial has read. Okay. So whatever the serial has the serial read like if you enter let's say hi. Okay. So it will read it and then it will print one by one. So it will first read H. It'll read H and then it will write H. then it will read I.
Then it will write I. And as they'll show show you um when you press enter, this doesn't know that enter isn't like a printable character. So it will actually go and try write the escape sequence which is printed like this. Now I will try um so while I get this set up, if anyone missed the attendance, you guys can scan it quickly. Um meanwhile, let me try get the camera set up.
Yeah. So, is my voice still for anyone who wants to stay for the demo?
Uh I hope you guys are able to see this Right.
I will try rather just hopefully my voice isn't breaking.
Um but yeah, I'll try set it up so that you guys can see. Um yeah. Okay.
So um this is not required but when you're on Linux there will be this slight issue where you guys will have to manually add u read and write permissions for the USB itself. But once you do that it should be fine.
Okay. So, I hope you guys can see this.
Um, so currently it's reading a distance. I will try show you guys my wall, I guess. Um so my phone if you can see it in the camera is my wall and you can see that the distance drops to around 5.4 or something. It keeps on oscillating. So that's another issue with um the ultrasonic sensor. You can see that it's actually oscillating um the reading the reading doesn't stay constantly. It could also be that you know the wall that is my phone is moving but yeah that's an issue. Anyway, let me move my phone back and you can slowly see the value increase.
So, it's 8 9 and yeah, now it's 14. So, it's back to just measuring the water.
Um, so this is one example of the ultrasonic sensor working. You can see like if you I suggest you guys go and then try do this on Tinkercad. Again, on Tinkercad, it won't be as um easy to see. uh like you will actually set the value there and um here it's much easier to see. Right.
Wait one second.
Can you guys hear me?
Is if my audio was bad for the last 5 minutes, please let me know.
>> [snorts] >> I'm going to assume that somehow the audio was fine and I will continue. So, I'll try to show how the um how the LCD works. In case the audio wasn't fine, it's fine. I'll take another 5 minutes and I'll show you guys how the normal I2C1 uh sorry, ultrasonic works. So, let me connect um Okay. So, this is going to be quite hard to see. Um, I will probably have to move the camera around a lot, but um, okay.
So, let me clear the output. Yeah, by the way, this was the B rate that I was telling you about. So, in the serial monitor, the button to open it is up here. Um, once you enter that, that will be a B rate here. So, you'll have to send this. Um, you'll have to set this to whatever you set here. So, in serial begin, I set 9600. That's why I'm setting 9600 here as well. Anyway, um, I will try enter. Hello.
Right. Um and I will try to show you guys what is actually being shown here.
So hopefully you guys can see it. The last character is what I was saying. Um it is the um escape character. So in AS key every character has a number. The escape character also has a number and it's trying to print the escape character itself which is why it's just trying to print lines. Anyway, now I'll send something else like hi. And you can see that it gets updated. Um, so this is what I meant by oh, sorry, this is the wrong code. Uh, this goes back to me showing that uh, you know, once you flash something, um, it stays flashed.
So, as you can see, yeah.
So hello as you can see this has been updated to hello and yeah so that is the um LCD working. Hopefully that was fun.
Um if you guys have any more questions then um yeah feel free to ask me. I'll I'll answer them. But yeah that was the session guys. Thank you for attending.
Uh any more questions you can ask. I think one of you asked a question. How Arduino is able to communicate via I2C?
Isn't it support UAT? Okay, so Arduino supports UAT and I2C. I mentioned it beforehand. Um one second, let me remove the camera.
Yeah. So um the Arduino supports um a few uh modes of communication. Uh I have not used SPI with the Arduino personally but it definitely supports I2C and UART because let me go to that slide. Ah so these two are the ones that support um UART. These two are technically dedicated uh or like the default assigned value for I2C communication. But any of these pins can be configured to do to use I2C. So let me make this clear. You are like the microcontroller is programmed to take UART as like the form of communication by default. However, you can um like program pins so that it communicates via I2C. I2C itself is just a protocol.
Everything else is a bunch of wires. As long as you can send clock pulses with the correct frequency and data um like data pulses with the correct um you know properly any two digital OS can be used as I2C. I2C itself is a protocol. When [snorts] I said the Arduino communicates via UART internally it's programmed so that it accepts UART as a form of communication. So whenever I'm using the serial monitor I am forced to use UART.
However, it doesn't mean that I can't use any other two pins as UA. So if I can use these two pins and I can connect it to another device and somehow these two have the capability to transfer data at the correct rates then these two can also be used as UR. Um so I hope that clears that doubt. Um any other question?
So any other question you guys would have had otherwise you can um end the session.
Uh if there are no more questions then I will end the session.
Now, Yeah. Um, heat Um, yeah, I'm going to assume no more questions. So,
Related Videos

Setting up a curved screen with Immersive Calibration Pro 4 and multiple cameras (P3D v4)
FlyerOneZero
23K views•2019-07-21

Robot Learning with Sparsity and Scarcity
allenai
379 views•2025-10-14

Jorge Mendez-Mendez: Unlocking Lifelong Robot Learning With Modularity (2023-10-05)
umassmlfl
237 views•2024-01-06

Northwestern’s MS in Robotics: Student Robotics Projects, 2023
NorthwesternEngineering
1K views•2024-05-31

"Perfect" Turns: Turning by the Gyro - FIRST LEGO League (FLL) SPIKE Prime + EV3 RePlay Programming
ZacharyTrautwein
94K views•2020-10-02

Gorkem Secer: TSLIP-based Deadbeat Running Control of Bipedal Robot ATRIAS
DynamicWalking-wv6qm
298 views•2018-06-22

Self-Driving Cars Need Lessons On Human Drivers | Maddie About Science
skunkbear
26K views•2018-08-21

Milrem Robotics’ THeMIS UGVs used in a live-fire manned-unmanned teaming exercise
MilremRobotics
99K views•2021-05-20
Trending

2.4 BILLION Records Got Leaked...
DeepHumor
15K views•2026-07-22

Playstation NO DISC/NO BUY Fight Is Over...
DavidJaffeGames
4K views•2026-07-23

Should I buy a Sawmill?
essentialcraftsman
29K views•2026-07-22

Americans Confused in Australia for 17 Minutes Straight
IWrocker
17K views•2026-07-23