Categories
COMPUTER HARDWARE ELECTRICAL SCIENCE ELECTRONICS EMBEDDED HARDWARE PROGRAMMING TUTORIALS

Shift Register Modes, Use, Advantages & Disadvantages

So in this article, we will look at a piece of hardware called the shift register and their modes of operation. But we wont stop there. Because we will also look at their use case, advantages & disadvantages.

So if you are new to computer hardware or learning about what goes inside a microprocessor, then brace yourself. As this is going to be an eye opener for you on how computer works!

So are you ready? Great! Then let us go!

To start with, let us ask ourselves the basic question – What is a shift register? Because only when we know what it is, does it make sense to learn about shift register modes of operation. Right? Let us answer that first!

What Is A Shift Register?

A shift register is a piece of circuit that you will find inside a microprocessor. It is used to store and modify data. Now this is one of the simplest explanation I can give for a 2nd grade student.

But if you are some one who is studying about computers, this is just not sufficient. So I will have to explain it in a bit more detail. Alright? I will be getting a bit technical here. But it is how you will get a solid understanding of shift register. So you will have to bare it. Okay?

Technical Explanation

Technically, a shift register is made up of a bunch of Flip Flops. So as you know, a flip flop is a circuit that can store information. It can store data that is in one of the two states – 0 or 1. Right? So what happens when you connect a bunch of these flip flops together? You get a “register“.

So a register is a circuit made up of a bunch of flip flops that can store data having values in 0s or 1s. Now, if that is the definition of a register, then what is a shift register?

A shift register is a type of register where data is shifted from one flip flop to another within the register. But this shift of data does not happen by itself. Instead, it needs a clock signal to do so. So for every input clock cycle, the data get shifted from one flip flop to another. Hence the name “Shift Registers”. Aha! That name now makes so much sense. Right?

How Shift Register Works
How Shift Register Works

The above pic shows how it works. So as you can see here, for every clock cycle, a new input bit enters Bit0. But at the same time, existing Bit0 value shifts to Bit1, Bit1 to Bit2 and so on. But what happens to current value of Bit7? The register will just throw it out! Removed forever!

Now there is one thing for you to notice in the above pic. It is that the bit value here is shifting left. Right? Because of this, we call it a “Left Shift Register“.

But does that mean we also have a “Right Shift Register”? You bet! We do have a right shift register where input is fed to Bit7 & Bit0 will thrown out for every clock cycle!

Where Do We Use A Shift Register?

So now that we know how a Shift register works, let us see where we can use it.

Using Shift Register For Multiplication

When you shift bits in a byte to the left, the value of the byte is multiplied by 2! So we can use a left shift register whenever we want to multiply a byte by 2.

Using Shift Register For Division

When you shift a byte to the right, you are dividing it’s value by 2. So we can use a right shift register whenever we want to divide a byte by 2!

So with that, let us now look at the different modes of operation of a shift register.

Shift Register Modes Of Operation

A shift register will work in one these four modes:

  • Serial In Serial Out (SISO) Mode
  • Serial In Parallel Out (SIPO) Mode
  • Parallel In Serial Out (PISO) Mode
  • Parallel In Parallel Out (PIPO) Mode

So let us take a look at each of these modes one by one. Alright? Here we go!

Serial In Serial Out (SISO) Mode

In this mode of operation, the data is fed into the shift register serially for every clock cycle. That is, for every clock cycle, the data is shifted either to the right or left serially. The output is also taken out one bit at a time. So both inputs and outputs are serial here. Hence the name SISO. So then how does the Flip Flop connection look like? Take a look at it below:

SISO Mode Of Operation
Shift Register In SISO Mode Of Operation

Serial In Parallel Out (SIPO) Mode

When we use the shift register in SIPO mode, we feed the input data serially but take the output data out in parallel. But again, this happens at every clock cycle. So how does that look like? Take a look at it yourself!

SIPO Mode Of Operation
Shift Register In SIPO Mode Of Operation

So as you can see here, we are still feeding the input data serially. But the output is no more serial. We are taking all the output bits at the same time, in parallel. So what this means is that we will get full 4 bit output every clock cycle!

Parallel In Serial Out (PISO) Mode

In the case of shift register in PISO mode, we feed the input data in parallel but take the output data serially. So what this means is that we will be feeding multiple data bits as inputs for every clock cycle. But will be taking only one output bit for each clock cycle.

So then what will happen to the output of each flip flop? Well, even that will be fed as an input! So, we will be feeding two inputs after multiplexing them together. So how does that connection look like? Take a look at it below:

PISO Mode Of Operation
Shift Register In PISO Mode Of Operation

Parallel In Parallel Out (PIPO) Mode

And finally, we have the shift register working in PISO mode. So can you guess what in this mode? Yes. In this mode of operation, you have both input and output data running in parallel. So how does that work? Take a look at it first!

Shift Register In PIPO Mode Of Operation
Shift Register In PIPO Mode Of Operation

So as you can see above, there is a major change in the way we connect the flip flops. In that, you do not see them connected to each other at all. So each input bit goes to a flip flop and it’s output is directly taken out. The only connection that is common to all these flip flops are the clock and clear signal!

So there you have it. Those are the different modes in which we can design a shift register to work. In the next section let us take a look at different types of shift registers that we can use.

Types Of Shift Registers

Based on the way the data is shifted, we have 5 different types of shift registers. They are:

  • Left Shift Registers
  • Right Shift Registers
  • Bidirectional Shift Registers
  • Circular Shift Registers &
  • Linear Feedback Shift Registers

Now let explain what each of these registers work like:

Left Shift Registers

We have already talked about the left shift register. So I think you are familiar with it by now. If not, let me re-iterate. So in the case of a left shift register, the data is shifted to the left on each clock cycle.

Right Shift Registers

This is just like the left shift register. But here it is shifting the data to the right on each clock cycle.

Bidirectional Shift Registers

In the case of a bidirectional shift register, we can shift the data in both the directions. So you can shift the data either to it’s left or right!

Circular Shift Registers

In the case of a circular shift register, the last output is connected back as input. So your data will not be thrown out. Instead, will be shifted either left or right in a circular fashion!

Linear Feedback Shift Registers

In this type of shift register, the input of one flip flop will be linear output value of the previous flip flop.

Advantages & Disadvantages Of A Shift Register

It is now time to talk about the advantages & disadvantages of using a shift register. So what are they?

Advantages Of A Shift Register

  • They are very fast to use.
  • Very quick when you want to convert data from serial to parallel or vice versa. They are faster than normal serial to parallel converter circuits.
  • They are very simple in design. So you can easily rig up a circuit to create a shift register.
  • We can use them to encrypt or decrypt the data.
  • We can use them to a delay signal.
  • It is used in CDMA to generate Pseudo Noise Sequence Number.
  • We can use them to track our data!

Disadvantages Of A Shift Register

While we could see that it has major advantages, shift register has one major disadvantage. That is:

  • The strength of the output current coming from a shift register is not so strong!

So there you have it. Those where some of the advantages & disadvantages of using a shift register.

And with that, I will end this article now. But if you have any doubts, do let me know. Because I will be more than happy to answer them! Alright? So see you and take care until next time! 🙂

Categories
ELECTRONICS EMBEDDED HARDWARE PROGRAMMING TUTORIALS

What Are Hexadecimal Numbers & Why Do We Use It?

So in this article, we will take a look at what Hexadecimal numbers are. But we wont stop just there. We will also learn why we need to use them. We will take a look at few examples of hexadecimal numbers to know it better.

So does that sound like something you want to know more about? Great! Then strap yourself to your seat and read along. Because you are in for a treat with a world of numbers!

So first thing first, let us answer the basic question we have.

What Are Hexadecimal Numbers?

So to answer this, we first need to ask ourselves what a decimal number is. Alright? Because the numbers we use in our daily lives are based on decimal number system. So looking at hexadecimal numbers after analyzing decimal number will be so much easier. Right?

Alright then.

Decimal numbers

They are the number system that has the digits 0 to 9. Right? But what do we do when we want to go beyond the number 9?

What Are Hexadecimal Numbers?
What Are Hexadecimal Numbers?

We use 2 digits to represent the next number. And these digits will now start with a 1 followed by another digit between 0 to 9.

So the next set of numbers will be 10, 11, 12, 13, 14 …… Correct? But what happens when we reach the number 19? We again start with our next number 2 and repeat the process again.

So it will now be 20, 21, 22, 23 ….

So what we see here is that we can only use digits between 0 to 9. But they can be grouped together in to multiple digits to count any number we want. Right?

Alright. I can now hear you asking me what does this have to do with Hexadecimal number?

Well this concept is very much related to hexadecimal numbers as well. How, you ask? Let me explain!

Hexadecimal Numbers

So just like we have digits between 0 to 9 for decimal, we use digits between 0 to F in hexadecimal system!

What? So how does the digits look like? They look like below:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E & F

These are the digits we use in a hexadecimal number system!

So as you can see, the numbers between 0 & 9 are the same as decimal system. However after that, 10 is represented by A. 11 by B, 12 by C and so on. This goes on until the letter F which is used to represent the number 15.

So in hexadecimal system, we have symbols to count from 0 to 15!

Why Do We Use Hexadecimal System?

Now that we know what hexadecimal system is, let us learn why we need it.

If you take a look at a computer, we know that it works in binary right? So the only numbers it can understand is 1s & 0s. Because of this, a large number like 10 is represented in binary as 1010b right?

But as you can see, it is not so readable. Correct? So there is a very good chance that we can read or write a binary number wrong! So to avoid that, we use hexadecimal numbers when working with computers!

Hexadecimal Number Examples

Decimal NumberHexadecimal Number
1000x64
120xC
2120xD4

Example Of Hexadecimal Numbers

So you can see few examples of hexadecimal numbers in the above table. Right? But did you notice something? We have prefixed hexadecimal numbers with “0x”. Why? Because that is a convention that will tell anyone to treat it as a hexadecimal number!

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Plot Histogram In Python Using Matplotlib

So in this article, we will take a look at how we can plot histogram in Python using Matplotlib library. Now this is some thing quite different from the basics of line plotting we have seen so far. So I know you will need some time to get through it. So what I will do is to go through it in an easy to understand way. Alright?

So relax, take a cup of coffee if you want to. As we will now look into the plot of histogram in Python using Matplotlib!

Plot Histogram In Python Using Matplotlib – The Basics

To get started, let us first learn a bit about what Histogram plot is. And then we will look at other questions. Like where it is used and how to draw it using Matplotlib. Okay? Great! So here we go!

What is an Histogram?

A histogram is a way to display frequencies of some thing. So how does it look like? In simple words, it is drawn using bars.

Oh wait a second! So does that mean that it is a kind of bar graph? Yeah you are right. Kind of!

So what happens is, the data that you want to show in an histogram is grouped together. But it does not mean that they are grouped randomly. But instead, similar data items are grouped together. Alright? Does that make sense? So when you plot, you will be plotting these grouped data on the chart. Okay?

Now there is one other thing. In Matplotlib, we call these groups of data as bins.

What are Histogram bins?

So a histogram bin is nothing but a group of similar data. That is all it is. So there is nothing really confusing about it!

Alright. So now that we know what a histogram bin in Matplotlib is, it is time for us to see an example of it. So how do we go about creating a plot of histogram in Python? Here is an example of it.

Plot Histogram In Python Using Matplotlib – Example

So we all know that to start a plot of something, we need data. Right? So how do we get this data? Since histogram is used to plot a lot of data, we cannot create it by hand. So what do we do then? We will have to take help of a library. Of course!

And what better library than NumPy to get a set of random numbers. Right? So that is what we will do. We will use Numpy to generate a bunch of random numbers.

But how many random numbers shall we use? 10, 50 or 100? Naah! We can surely go more than that. Right? So how about using 1000 random numbers? 😉

So here is the piece of code we will use to generate 1000 random numbers using Numpy!

import numpy as np
y = np.random.randn(1000)

That is it! That is all the code we need to create 1000 random numbers using Numpy! So easy. Right?

So now that we have our data ready, let us see how we can plot it as a Histogram using Python’s Matplotlib.

So the code to plot a histogram using Matplotlib looks like this:

import matplotlib.pyplot as plt
import numpy as np

y = np.random.randn(1000)
plt.hist(y);
plt.show()

That’s it! We just import pyplot module and call it’s hist( ) function with our data. And the Matplotlib library does the rest. It will go ahead and plot a Histogram in Python for us!

This is very easy right? And that is the beauty of Matplotlib library. The modules and functions are so well written that you can create beautiful histogram plot in Python easily!

So then how does the final output plot of the Histogram look like? Well, you see it for yourself!

Plot of Histogram Drawn In Python Using Matplotlib
Plot of Histogram Drawn In Python Using Matplotlib

Matplotlib Histogram Bins

Woah! What happened here? We gave it 1000 input data points right? What happened to all of it then? Well let me explain. Here is what Matplotlib has done.

It has taken our 1000 data input and grouped them together into 10 bins. And then it created the above histogram!

So why 10 bins? Why not 12 or 15 or any other number? Now that is a valid question for you to ask. So let me tell you why the number 10.

It is because that is the default number of bins Matplotlib will create for any number of input data you give to it. Okay? Does that make sense?

So in simple terms – Matplotlib took our 1000 data & grouped closer numbers together into 10 bins. It then went on to create the above histogram plot!

So that is all there is to it! But what if we want to have more than 10 bins? Well, we will come to that soon, but not now. Because it is going to need it’s own article that I will write next!

So see you in the next article!

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Use Different Line Color And Marker Color In Matplotlib

In this article, we will learn how to to set different line color and marker color in Matplotlib plot. But if have seen my earlier article, I showed you how we can set colors to markers. Right? So this will be a follow up on that article. Alright?

So what is the problem we are trying to solve here? Well you see, we want to have a plot with lines connecting markers. But then main thing is that we want different line color and the marker color in it! So how can we do that? Let me explain!

So first, let us take a look at our earlier plot. This is how it looked like, right?

Matplotlib plot with colored markers
Matplotlib plot with colored markers

So as you can see in the plot above, we had changed the color of the triangle markers to magenta color. Right? So what was the code we used to generate this plot? Let us take a look at it as well:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '*')
plt.plot(x, [xi*2 for xi in x], '+')
plt.plot(x, [xi*3 for xi in x], 'm^')
plt.show()

So we used the color code ‘m‘ in the third plot to get the color. Right? But what is missing here? Well, you can see that there are no lines drawn. Correct? So how do we go about fixing that? And more importantly, how can we add lines with different colors?

Well, to do that we will need to use certain keywords in the plot( ) function! That is the key to solving this problem! Does that make sense? Great!

So then what keyword do we need to use? Well you see, Matplotlib gives us a lot of keywords to use when plotting. So there are special keys for setting line color as well as marker color!

But how do these keywords look like? Let me explain.

Keywords To Use For Different Line Color And Marker Color In Matplotlib

There are three keywords we can use to set the color of a line and the marker. They are:

color or c – So by using a color or c keyword in our plot( ) function, we can set the line color of a plot.

markeredgecolor – By using this keyword, we will tell Matplotlib what color to use to draw the edges of our marker.

markerfacecolor – By using this keyword, we can tell Matplotlib what color to use for the face of our marker.

These are the 3 keywords than we can use to set different line color and marker color in Matplotlib. So now that we know what to use, let us next see how we can use it. Alright?

What better way than to use our previous plot and to change it’s color? Right? So let us do just that!

Let us change the color of our plot line to be Yellow while the triangles to be Red with a green border. Alright? So how will our plot then look like? Any guess?

Well, take a look at it yourself below:

Different Line Color And Marker Color Plot image
Different Line Color And Marker Color Plot image

Woah! That is nice, right? We now have total control over the colors we can use in our plots, right? So what is the code change we did to get this? Take a look at the code for yourself!

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '*')
plt.plot(x, [xi*2 for xi in x], '+')
plt.plot(x, [xi*3 for xi in x], color='yellow', marker='^', markeredgecolor='green', markerfacecolor='red')
plt.show()

So there is a tiny little change we have done to get this working. As said earlier, we simply used the keywords to set the color like we want. And it did work as we wanted. Right?

Conclusion

So that is all there is to set different line color and marker color in Matplotlib. You just need to use the right keyword and it will work like a charm!

So with that, I will end this article now. But if you have any doubt about it, do let me know in the comment below. I will be more than happy to help!

So until next time, take care! 🙂

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Change Marker Color In Matplotlib

So in this article, we will learn how we can change the marker color in a Matplotlib plot. We will first see how we draw these markers and then see what we can do to change their colors.

Does that sound good? Great! Then let us start right away!

But before we start looking on how to change the Marker color of a plot, we need a plot. Right? But where do we get one?

Well, how about we make use of the plot we got from our previous article:

How To Change Marker Style In Matplotlib

That plot should be good enough. Right? So we will use just that!

Here is how the plot then looks like:

Change Marker Style In Matplotlib
Matplotlib plot with different marker styles

But what do we see here? We are seeing that each of the line marker in the plot already has different color. Right? But how did that happen? Who set the color for these markers?

Well the answer to that lies in the default behavior of the Matplotlib library. Because, even if we did not set those color, the library did it by itself. It made sure that each of the line markers got a different color.

That is cool! right? Because in that case, we will not have to worry about setting color ourselves. Isn’t it?

Well yes. That is true for most of the time. But there are times when we want to set the markers with a specific color. So having an option to change the marker color In Matplotlib is still needed. Right?

So then how can we do that? Well that is when the parameters of plot( ) function once again helps us! Here is how we can use it to change the marker color.

Change Marker Color In Matplotlib

So before we look at how to change the marker color in Matplotlib, let us look at current code. The code that is responsible for the plot created above. This is how that code looks like:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '*')
plt.plot(x, [xi*2 for xi in x], '+')
plt.plot(x, [xi*3 for xi in x], '^')
plt.show()

So using the above code we got three sets of markers in the above plot, right? And each set had a different color set to it. But what if I want the triangle in the first set to be in the color of magenta?

Well, luckily we can do that! How? By passing our desired color value to the plot( ) function. So the code for that will then look like this:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '*')
plt.plot(x, [xi*2 for xi in x], '+')
plt.plot(x, [xi*3 for xi in x], 'm^')
plt.show()

Notice the addition of the color value “m” to our third plt.plot( ) function call? That is what will do the trick for us! Here the alue “m” stands for the color “magenta”. This tells the plot( ) function to draw the triangles using magenta color!

So then how does our final plot look like then? Take a look at it for yourself!

Change Marker Color In Matplotlib
Change Marker Color In Matplotlib

So what do you see?!

As you can see, the color of the triangles have changed from green to magenta. And that is what we wanted. Right? 😉

But then you must be asking what are all the available colors that you can use? Right? Well, they are the same set of colors that you used while changing line color in Matplotlib earlier! So it is quite easy then. Isn’t it?

So there you have it! That is how you change the marker color In Matplotlib. With this, I will end this article now. But if you have any questions, do let me know in the comments below.

So until next time, take care!

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Change Marker Style In Matplotlib

So in the previous article, we say how to change line style in Matplotlib. But in this article, we will take a look at how we can change the marker style in Matplotlib. So let us start with some idea about what these markers are in the first place. Shall we?

What Are Markers In Matplotlib Plots?

So for us to learn what markers are in Matplotlib plots, let us take a look at our previous example. Alright? Show this is the plot that we had in our previous example:

An example Matplotlib plot with 3 lines drawn from 3 sets of data
An example Matplotlib plot with 3 lines drawn from 3 sets of data

So as you can see here, we have 3 lines drawn using Matplotlib. Right? But how did we get these lines in the first place? Any guess?

To understand that, we need to take a look at the piece of code that generated this plot. So here is what that code looks like:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '-.')
plt.plot(x, [xi*2 for xi in x], '--')
plt.plot(x, [xi*3 for xi in x], ':')
plt.show()

So as you can see, the three lines we have plotted are drawn using these 3 lines of code:

plt.plot(x, [xi*1 for xi in x], '-.')
plt.plot(x, [xi*2 for xi in x], '--')
plt.plot(x, [xi*3 for xi in x], ':')

And what is the data set used to plot these lines? There are 3 sets of data and they are:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
[3, 6, 9, 12, 15, 18, 21, 24, 27, 30]

So as you can see, the three data sets shown above is what resulted in the 3 lines above.

But here is the thing. The lines that we have drawn in the plot are nothing but connections to these points. Right?

So in Matplotlib, we call these points as Markers and the lines joining them as Segments.

Aha! Now you know what these markers are, right? But there is another important thing you should notice here. The plot we have in the above pic is made up of both Markers & Segments. So the above Matplotlib plot has both Markers & Segments in it!

Woah..! We didn’t knew that the plot had markers in it right? But where is it then? Why can’t we see it?

Well it is because the markers in this plot are drawn using their default style and hence it is not clearly visible. Wait, what is the default style of a marker then?

What Is The Default Style Of A Marker In Matplotlib Plot?

The default style of a Matplotlib Marker is to draw it as a point. And this is the reason why we are not able to see it. Because we are then connecting them by lines!

But then this begs us the next question:

What can we do to make the markers in Matplotlib visible?

So how are we going to show clearly then? Well, the answer to that once again lies in the parameters we pass to the plot( ) function.

Wait, so how will that look like then?

Well, instead of using the plot( ) function to draw default markers and lines, we do something else. We will ask Matplotlib not to use default markers and also not to draw the lines!

So then, how will that code look like? Well, take a look at it yourself!

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '*')
plt.plot(x, [xi*2 for xi in x], '+')
plt.plot(x, [xi*3 for xi in x], '^')
plt.show()

So when we run this piece of code, what do you think will the plot look like? Any guess?

Change Marker Style In Matplotlib
Change Marker Style In Matplotlib

Woah! Where did this nice little plot come from?! How did we get those cool looking stars and triangles in the plot? Huh?!

How To Change Marker Styles In Matplotlib

Well here the thing. When we used the marker style ‘*’, ‘+’ and ‘^’ symbol in our code, it got translated to that. Now that is one nice little feature that Matplotlib library is giving to us. Right?

So how do we know what all Marker styles are made available to us by Matplotlib then?

Well that is when this nifty table below will help you out! Take a look at it. It is showing you all the marker styles you can draw on your Python plot to make it look pretty!

Marker SymbolMarker Style
^Triangle Up
vTria. Down
<Triangle Left
>Tria. Right
1Tripod Down
2Tripod Up
3Tripod Left
4Tripod Right
pPentagon
sSquare
hHexagon
HRoated Hexagon
*Star
+Plus
Horizontal
DDiamond
dLean diamond
|Vertical Line
Available Marker Styles To Draw Using Matplotlib

And this is how you can get some nice little pretty plots using Matplotlib Python library. So I hope this was quite easy to understand for you. But if you still have any questions around it, do let me know. Because I will be more than happy to help! 🙂

Categories
DATA MINING DATA SCIENCE DATA VISUALIZATION MACHINE LEARNING PROGRAMMING PYTHON TUTORIALS

How To Change Line Style In Matplotlib

If you have been working with Matplotlib to plot lines, you might be looking for how you can style lines. Right? So if that is the case, they you have come to the right place. Because in this article, we will learn how to change line style in Matplotlib.

But before we do that, I hope you already know how to plot multiple lines on Matplotlib. But if you do not know, then take a look at the article linked above. That should get you going. Alright?

Okay then. With that, it is time for us to get started on changing the style of our lines now! Are you ready?

Drawing Multiple Lines Using Matplotlib

So as I said in the earlier section, we first need to have the code to draw multiple lines. Here is that code we have from our earlier article:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x])
plt.plot(x, [xi*2 for xi in x])
plt.plot(x, [xi*3 for xi in x])
plt.show()

So this is the same piece of code we have taken from our earlier article. When we run this code, we get the following output:

Matplotlib multiple lines same graph
Drawing Multiple Lines Using Matplotlib

So as you can see from the above pic, we have 3 different lines plotted on the same graph. But what is even more important is that all these lines are in different color. That is good. Right?

But what we want to do now is to change the line style in this Matplotlib output. Correct? So how do we go abut doing that?

How To Change Line Style In Matplotlib?

With the basic multi line plot ready, let us now see how we can change the style of these lines.

So how do we go about doing this? Any guess? I want you to think a little bit to see if you can make a guess before you look for the answer. Alright? Just take a minute or two to think it over before you read further.

Okay. Here is what we can do to change the line style that we draw using Matplotlib:

Just like we could change the color of our lines ourselves, we can also change the style of these lines. So how did we change the color of our lines using Matplotlib to what we want, earlier? Yes, we passed in a new parameter to our plot( ) function. Right?

So in the same way, we can change the line style in Matplotlib as well. By passing our desired style as a parameter to our plot( ) function!

So how does that look like? Well, we just need to pass in the style we want as another plot( ) function parameter. So we modify our code in the plt.plot( ) function as shown below:

plt.plot(x, [xi*1 for xi in x], '-.')
plt.plot(x, [xi*2 for xi in x], '--')
plt.plot(x, [xi*3 for xi in x], ':')

So what happens when you do that? You have just changed the line style of the plots. So here is how it now looks like:

Change Line Style In Matplotlib by passing in the style as a parameter to the plot() function.
Change Line Style In Matplotlib by passing in the style as a parameter to the plot() function.

Now that looks like a perfect plot, isn’t it? But this begs us to ask the next question:

What Are The Options Available To Change The Line Style In Matplotlib

So there are 4 line styles that Matplotlib provides us to choose from. They are:

Style SymbolStyle Type
Solid Line
Dashed Line
-.Dash Dot Line
:Dotted Line
4 line styles available for us to choose from in Matplotlib

So if you choose from any one of these styles, you can get some pretty looking plots out there!

Conclusion

So that is all there is to changing the line style of a Matplotlib plot! You just have to add a new parameter to the plot( ) function with the style information. That is it and then it just works!

Pretty easy way to achieve what we want then, right? 😉 That is the beauty of Matplotlib library. It has so many interesting features with such a simple API!

So then what does our final code look like? Here it is:

import matplotlib.pyplot as plt
x = range(1, 10)
plt.plot(x, [xi*1 for xi in x], '-.')
plt.plot(x, [xi*2 for xi in x], '--')
plt.plot(x, [xi*3 for xi in x], ':')
plt.show()

So with that, I will end this article now. But if you still have any questions, do let me know in the comment below. I will be more than happy to help! Alright?

So until next time, take care guys and gals out there! Have a great day! 🙂

Categories
ELECTRONICS EMBEDDED TUTORIALS

What Is A Light Dependent Resistor (LDR) & How Does It Work?

So in this article, we will take a look at what a Light Dependent Resistor (LDR) is. But we will not stop just at that. Because we will also learn how an LDR works.

Now if you are some one who is getting into electronics, this will be very helpful for you. But if you are someone who has already worked with electronics, this will still be helpful to read. Why? Because we will be going through this sensor step by step. In detail.

So give yourself some time to read it all. Because this will get you to know everything there is to know about LDR. Alright? Cool, then let us get started!

What Is A Light Dependent Resistor (LDR) Sensor?

To answer this, we must first take a look at what a sensor is and what it does. Because only when we know why we use them in general, can we look at LDR in particular. Does that make sense? Great! So let us start with that.

What is a sensor?

A sensor is a device that we use in our electronics to measure a physical quantity. But why do we need to use these sensors? Because they can sense any change in these physical quantity and convert it into electrical signal.

What Is A Light Dependent Resistor
What Is A Light Dependent Resistor

Wait, that was quite a mouthful to read. Right? But what does it really mean?

So to put it in simple words, a sensor is a type of device that can convert a physical quantity to an electrical signal. It is as simple as that!

Okay. Now that makes sense. But why do we need them in our circuits?

Why do we use sensors?

So here is why we need them – Just think about some sensual thing you can feel. Can you tell me some of them that you feel on a daily basis? Yes, there are these things like smell, sound, weather etc. that our body can feel, right? But what if we wanted our electronics to sense that too? How can we make that happen?

So that is when these sensors will come into picture!

We can use sensors for smell, sound etc. And one such sensor is the Light Dependent Resistor (LDR) sensor! But what does an LDR sensor do?

What Is A Light Dependent Resistor and What Does It Do?

A Light Dependent Resistor (LDR) is a sensor whose resistance will change with the changing temperature. It is as simple as that!

So how does this LDR work? What is it made up of? Let us answer these queries now.

How Does A Light Dependent Resistor Work?

A light Dependent Resistor (LDR) is made of those materials which has these unique properties – In darkness, these materials do not conduct electricity but will do so when there is light falling on them!

So as a result, you can say that they have high resistance when it is dark but have low resistance when light falls on them!

Alright! So that is the principle behind the working of an LDR. But then this begs us to ask our next question:

What is the range of resistance of an LDR?

So as you know by now, the resistance of an LDR changes between darkness & when in light. But what is this range? How much of a resistance change is happening here?

So the answer is, it fluctuates between 100 Ohms to 1M Ohm!

So that is quite a big change, isn’t it? And that is why we can use this sensor for increasing or decreasing the flow of current to a circuit based on light!

So there you go! That is all there is for you to know about a Light Dependent Resistor. But if you have any more questions about it, do let me know in the comments below. I will be more than happy to help!

With that being said, I will end this article now! I hope you have a great day! 🙂

Categories
ELECTRONICS EMBEDDED TUTORIALS

PT100 Temperature Sensor Table & How To Use It

In this article, we will be taking a look at the PT100 Temperature Sensor Table. So using this table, we can see how the resistance of PT100 changes with temperature.

But before we do that, we first need to know what a PT100 temperature sensor is and how it works. Right? Because having a clear idea about what this sensor is will help us know how to use it. Does that make sense? Great! Then let us start from there!

What Is PT100 Temperature Sensor?

PT100 temperature sensor is a sensor that that is made from Platinum. But what does it do? Well, we can use PT100 sensor to measure the temperature around it. So in a way, it can act like a thermometer!

Wait a second. How does a sensor made of Platinum work as a thermometer?

How does it work?

So here is the thing. A pure metal like platinum has a unique feature in that it’s resistance increases with an increase in temperature. So by just measuring the resistance across it, we can map it to it’s corresponding temperature!

Now that sounds great right? Because now just my checking the resistance of this sensor, we can tell what the temperature is. How cool is that!

But why does the Platinum behave this way? What makes it change its resistance based on the temperature?

Well, to answer this question, we need to know a little bit of the chemical structure of Platinum.

First thing first, we need to know that Platinum is a pure metal. Because of this, an electric current can flow easily through it. Alright?

So everything should work fine when you are passing current through it then, right? Well, not really!

Why? Because this free flow of electrons in Platinum is affected by the temperature around it.

You see, a metal like Platinum is made up of crystals. And these crystals starts vibrating as the temperature increases. So this increase in vibration in turn will slow down the flow of electrons!

So there you have it! This is the reason why Platinum will conduct less current as temperature increases.

So PT100 temperature sensor that is built using Platinum will take advantage of this feature. And hence, we can use it to measure resistance across it to determine the temperature!

Now that is a very nifty way to take advantage of this feature right? I mean you are measuring temperature around you by just checking resistance of a sensor. How cool is that!

So now that we know how a PT100 temperature sensor works, it is time for us to look at the PT100 Temperature Sensor Table.

What Is A PT100 Temperature Sensor Table

So as we learnt, PT100 temperature sensor’s resistance increases with increase in temperature. Right? So just by measuring the resistance across it, we can tell what the temperature around it is. Right?

But what is the formula we need to use to convert a resistance to temperature? That is when the temperature sensor table comes in handy!

So simply put, this PT100 temperature sensor table will help us map between a resistance value and it’s respective temperature. No formula required then! That is great right.

So how does this PT100 temperature sensor table look like then? Well, take a look at it for yourself:

Temperature (In Degree Celsius)Resistance (In Ohms)
-20018.52
-10060.26
0100
100138.5
200175.85
300212.05
400247.10
500281
600313.72
700345.29
800375.71
850390.49
PT100 Temperature Sensor Table

So as you can see, there is a simple mapping between the temperature and resistance. You can then use this table as a reference to measure the current temperature across a PT100 sensor.

But before we go, did you notice one important thing in the PT100 Temperature Sensor Table? Can you spot it?

PT100 Temperature Sensor Table
PT100 Temperature Sensor Table Image File

So if you did find that the resistance is at 100 Ohms when the temperature is at zero degree Celsius, then yes! You got hat right!

Now that is an important feature of the Pt100 temperature sensor. So its resistance is always at 100 Ohms when it is working at a temperature of 0 degree centigrade. It is a good thing to keep that in mind when working with this sensor!

Conclusion

So there you have it. Because that is all there is for you to know about PT100 sensor.

But if you still have any questions about it, do let me know in the comment section below and I will be happy to help!

So with that, I will end this article now. Have a great day you all! 🙂

Categories
ELECTRONICS TUTORIALS

What Is Leakage Current? How To Stop It?

So if you have been reading about electrical equipment, you might have come across the term “leakage current”. But what is this leakage current? Do you know how it is caused and what steps you can take to prevent it? If you are not sure, do not worry! Because we will be answering these questions here in this article.

But before we do that, let us just start with learning about a few basic things. Alright? Because without an idea about these, it will be difficult for us to get our head around what leakage current is. Okay? Does that make sense? Great! So then here we go!

If you take a look at any electrical equipment, you will find a few important components in it. So this is irrespective of what this device is or what it does. It will for sure have these components in them. So what we are talking about are those generic components that you will for sure see in any electrical equipment. Now what are they?

What Is  A leakage Current?

Any electrical equipment will have a power supply, electrodes, conductors & insulators in them.

Learn what is the difference between conductors & insulators

Wait a second, an electrical equipment having conductors is fine. But why do we need insulators? Can you take a guess?

We use insulators in those areas where we do not want electric current to flow through! Now where will that be? It is usually those areas where the user would touch the equipment, right? But not just that, equipment uses insulators even inside the circuitry where we do not want current to flow.

Alright, very well. But what has that got to do with the leakage of current? Well, there is a strong connection between them. Let me explain it to you!

What Is Leakage Current?

So what does a leakage current got to do with insulator? Well, one of the main reason we use insulator is to prevent current from flowing through it right? But the thing is, even a strong insulator can let some amount of current to flow through it.

And this is only going to increase with an increase in voltage. Also, there comes a point in voltage called the breakdown voltage where an insulator will lose all it’s properties and just allow current to flow through it.

Now this type of current flow is not happening by design, right? It is something that we do not want. But still happening. So you can say that this is non functional current. Or just unwanted current in simple words. This is what is called a leakage current.

So in other words, we can define leakage current as:

Any current that is not flowing by the design and non functional is called a leakage current

So this is what a leakage current is in terms of an electrical equipment. But is that all there is to it? I mean does it happen only due to insulator breakdown? Well actually NO!

Why Does It Happen?

It can happen due to any of the following reasons:

  • Break down of insulator properties due to break down voltage.
  • Breakdown of capacitors in trying to block DC current. This happens due to an impure dielectric material used in a capacitor. As a result, some amount of DC current will continue to flow even after power is removed from the device.
  • Reverse leakage current in a diode due to semiconductor impurities.

How Can we Prevent It?

Now that we know what a leakage current is and why it occurs, it is time to see how we can prevent it. So what are the best ways we can avoid it? Well, the simple and easiest thing to do is to detect the deterioration of the components that causes it.

So what does that mean? It means to detect any leakage current happening and replacing the parts that are causing it. But then we have this next question. How can we detect it? Is there an easy way to do it? Can anyone do it?

And the answer for this is, YES! There is an easy way to detect leakage current. How? By using a leakage current detector!

What Is A Leakage Current Detector?

A leakage current detector is a device that we can use to see if there is any leakage current in a device. So you can say that it is a type of leakage current tester.

Now there are many types of leakage current testers out there. So it needs its own article which I will be writing soon. But just know that using these leakage current testers, we can find any leakages and then fix it!

Sometimes these testers are also called as Megger.

Why Should We Prevent A Current Leakage?

Now that we know how we can detect the leakage of current we should ask ourselves why we do want to prevent it?

Well the are many reasons why we want to prevent it from happening. But here are some of the important reasons for it:

  • A leakage current can be dangers as it can get you electrocuted!
  • In a handheld device running on battery, leakage current can drain your battery quickly.
  • Any leakage current is just wasted current. This is just waste of resources.
  • Leakage current can result in device heating up quickly & frequently.

So there you have it. A brief introduction of what it is, why it is caused and how you can prevent it. With this, I will end this article here and continue on this topic in a future article again. But in the mean time if you have any questions, do let me know in the comment below!

So until next time, have a great day! 🙂