Day 1
Summary of This Lesson
So far, we've gone over some components as well as different topics that pertain to electricity and magnetism. Now, we can get into more exciting stuff. We will be adding logic into our circuits with the Arduino, which is an electrical component that can be programmed, meaning you can write to it what you want to do in a programming language. You won't be responsible for programming the Arduino board, since it is prewritten. You will get to see how the programming works with the circuit however, but just in a general sense.
Lessons for Electrical Training on the website requires a Tinkercad account at www.Tinkercad.com, so if you haven't already, make an account on that website. Also, you can press the "Try Circuits" button in the circuits tab of Tinkercad to get an introduction of how to use Tinkercad's circuit simulation feature.
What is the Arduino?
The Arduino is a component that can essentially think. You can program it in a programming language to do things that a normal circuit without something like the Arduino can do. For example, if there needs to be a way to turn an LED on and off at a specific time in the day, or at an interval. These things can be possible with normal electronic components, in the last example, it could be a capacitor to store electricity temporarily with different resistance levels to have the correct current so that it can get to the max or minimum voltage at the right time. But that sounds complicated right? The Arduino itself is a very complex piece of hardware, but to use it is fairly simple. Let's see how. Click the imbedded link below. Press simulate and see what happens.
This circuit looks familiar and also different in a way. One, you see an LED connected to a resistor. We've seen this kind of thing many times before. What's different is the "power source". This time, it is the Arduino (specifically Arduino Uno) that is powering the LED. Another thing that is different is that the LED is blinking every second! This is not because it is malfunctioning, but instead, it is what we wanted it to do thanks to programming.
How is it Blinking?
Let's look at how the LED is able to blink. The picture on the right highlights what are called digital input/output pins, or DIO for short. Now what is digital? Digital is simply saying that there can only be two results, a yes or a no, 1 or 0, true or false, you get the point. These pins are able to output voltage, or input voltage, hence the name digital input/output. It is called digital because it can only output 0V, or 5V, and will only take in voltages a little over 0V or under 5V to receive it correctly. The 0V and 5V means 0 and 1 respectively in digital terms. We'll talk about what input means later. You might've guessed what output is, it is what comes out of the pin, which in this case, can be 0V or 5V. When the Arduino is meant to turn the LED on for a second, it turns the pin connected to the resistor and LED, which is pin 13, into a state called a high state, or a 1 in digital terms. A high state makes that pin provide 5V. You might guess what a low state is. A low state provides no voltage, or 0 volts. The Arduino makes pin 13 go into a low state when it is told by the program to turn the LED off. You might ask what was the other side of the LED connected to? That pin is called ground, which was written as GND on the Arduino. Ground can be treated like the negative side of the battery. The DIO pins can be treated as the positive side of a battery, except that it can be turned on or off through programming, and can also act as a multimeter by measuring voltage when it is programmed to be an input.
So all in all, the digital pins are like the positive side of switchable batteries that are controlled by programming, which can also be an input, and the ground pins are essentially like the negative side of the battery, where the positive charge (using conventional flow) returns to the battery.
What about the Other Pins?
In the picture to the right, it highlights pins called analog pins. What makes analog different from digital? Well, analog has many possible input forms or values, compared to digital which only has two, 1 or 0, on or off, true or false, which are all different names for the same two things. Basically, analog has everything in between on or off, like kind of on, or kind of off. Sort of like 70% on, or 30% on, etc. The analog pins are able to take in a variety of voltages between 0V and 5V. This of course, can be programmed by programming language. Analog pins on the Arduino can't provide a voltage in between 0V and 5V even though it is called analog, but it can provide 0V or 5V similar to how a digital pin would. Analog and digital pins have different uses. We'll see those uses later.
The Last Set of Pins on the Arduino, Power
The picture on the right highlights the last set of pins we will go over. Those pins are responsible for things dealing with, you guess it, power! The 3.3V and 5V essentially act as a 3.3V or 5V battery. It can provide 3.3V or 5V to a circuit or component if the Arduino is powered. The two GND (ground) pins next to them serve the same function as the GND pin on the top of the picture, and in the blinking LED circuit, which is to act as the negative side of a battery, returning the positive charges to the battery once it flows through a circuit. The Vin pin is for the voltage input, hence the reason why there is V for voltage, and the in for input. This is where you can connect the positive side of a battery into it to power it, and the negative side into a GND pin. In this case, it is like how an LED has a positive and a negative side. The positive side connects to the positive of the battery, and the negative side connects to the negative side. The Arduino is similar. If you are using a battery and want to power the Arduino with wires, you connect the positive side of the battery into Vin, and negative side into GND. What this does, is it connects other GND pins to the battery, allowing for the other GND pins to have 0V. The positive side of the battery connects to Vin. That allows other pins like the DIO pins, or analog pins, to output a max of 5V. The battery can be 7-12 volts, but you may be wondering, how did that 7-12 volts turn into 5V max at the DIO and analog pins? The answer is voltage regulators, which we've covered earlier. If all this sounds confusing, then just know that you can power an Arduino with GND connected to the negative side of a battery (7-12 volts) and Vin to the positive side.
Mini Project
Let's try and make some circuits to use and learn these different pins. Below, you'll find a picture of two circuits. I challenge you to try and make those in Tinkercad. These circuits have a potentiometer in them, which is an analog electrical component, and we've tinkered with these before. In essence, they are resistors that can have their resistance changed at the turn of the handle, due to more or less distance made between pins when turning the handle. For simplicity reasons, you can make one circuit at a time in different projects on Tinkercad. This removes the need to name the Arduinos, copy and paste the correct program into the right Arduino, etc., so just make one circuit in one project at a time to make things simpler.
Once you get that setup, copy and paste the code below into that Arduino by clicking on it, pressing the "code button", pressing the drop down menu that says "blocks" then choosing "text", then replace the text to the right with the code you've copied. Here's the code:
void setup()
{
Serial.begin(9600);
pinMode(2, INPUT);
}
void loop()
{
Serial.println(digitalRead(2));
delay(300);
}
Now what I want you to do is press the button on the top called "Code", then go to the drop down menu from the tab that pops out, and press "text". Then, highlight everything in there and paste the code that you've copied. Press simulate, then press the button called "Serial monitor" at the bottom of the screen. This should allow you to see numbers starting to pop up. Then, see what happens when you rotate the handle of the potentiometer. Turning the handle of the potentiometer only gives out a 0 or 1 reading. This is what a digital pin is. It can only give you a 1 or 0/high or low/true or false result, even though the potentiometer is analog.
Create the second circuit at the bottom of the picture in another project on Tinkercad. Then, like you've done for the first circuit, copy and paste this code:
void setup()
{
Serial.begin(9600);
pinMode(A0, INPUT);
}
void loop()
{
Serial.println(analogRead(A0));
delay(300);
}
into the code block with text, press simulate, press the serial monitor button, then turn the handle of the potentiometer in different directions.
You'll notice this time that you can see a variety or numbers, from 0, to 1023. Why 1023? 1023 is the maximum number the Arduino would give you when the voltage going into an analog pin is 5V. The Arduino in this case has to use numbers to represent the variety of voltage levels going into it, which is why there are numbers from 0 to 1023. So 0 would mean 0V, and 1023 means 5V. To find other voltages, you can take the input number and divide it by 1023 to get a percentage, then multiply that by 5 to get the voltage amount between 0 and 5. This is what analog is, a variety of values other than 1 and 0.
What's Going on here?
The potentiometer has a resistive material inside it, and it changes resistance as the slider/handle increases the distance it has with a pin. As resistance changes, the voltage drop also changes, as you might know from V = IR, "V" being the voltage drop, "I" being the current through the component, and "R" being the resistance of the component, in this case, the potentiometer.
For the first circuit, the reason why it only displays 1 or 0 is because the middle pin of the potentiometer connects to the digital pin of the first circuit, and gives a voltage level from 0V to 5V depending on the direction that the handle is in. You still might not understand what a potentiometer is, or how it creates different voltages, or both. We can demystify this by using a very familiar component, the resistor. Click the imbedded link below.
Here, we see two circuits. One with three resistors, and one with a potentiometer and a resistor. These two circuits are supposed to demonstrate the same concept: voltage drop across different resistances.
Here is a diagram showing how a potentiometer works. Essentially, there is a slider that slides over a material that has some resistance. In any resistive material, including wires, the resistance increases with the length/distance that the electrons have to move through, since electrons have to go between atoms that aren't going to easily allow electron flow more often. When the tip of the slider is far away from a terminal, there is more resistance between that same terminal, and the tip of the slider. In that same position however, there is less resistance between the OTHER terminal and the tip of the slider since the tip is closer to it now
So how does the Tinkercad demo show this in action? To start, change the resistances of the left or right resistors in the left circuit. The combined resistances of both resistors needs to be equal to 250k ohms. This is to show how the left circuit is similar to the right circuit, since the potentiometer has a total resistance of 250k ohms as well. From the previous point made above, you know that a potentiometer has a resistive material inside it. Can you guess what that value is inside of a 250k ohm potentiometer? Well, it's simply 250k ohms. So essentially, that slider is essentially sliding on top of a 250k ohm resistor.
Once you've changed the resistances of the resistors, you will notice how the voltage drops of both resistors will add up to 9 volts. If you play around with the potentiometer in the other circuit, you might be able to get the same voltage readings on those multimeters. So you might be able to see how the resistor circuit is the same in terms of voltage drop if the resistances are done in a way to match the voltage drops in the potentiometer circuit.
That is what a potentiometer is. On both sides of the slider, it is like there is a resistor with some value, but both resistors' resistances add up to a common value, in this case, 250k ohms. The resistance between a pin and the slider increases as distance increases, and decreases as distance decreases. With the increase in resistance, there is also an increase in voltage drop, since V (voltage drop) = I (current) * R (resistance).
Now, you might be wondering what the middle resistor is on both circuits. That represents the digital IO pin. The digital IO pin only allows around 1 microamp, or 0.000001 amps, into itself in the input mode. Very small. We can use a giant resistor to simulate that, since a lot of resistance results in little current. Without that resistor, there will be a lot of current going through it, resulting in virtually no current moving through the other resistor. What happens to the voltage drop of a resistor if current is 0? Well, V = 0*R. Anything multiplied by 0 is 0, so if you removed that middle resistor, that would mean that one of the resistors, probably the one on the right of the middle resistor, would have no voltage drop.
So how does the digital IO pin detect the changing voltage of the potentiometer? After every resistor, there is a voltage drop, so if a resistor drops 5 volts in a circuit that provides 9 volts, that means that after the resistor, there is 4 volts (4 + 5 = 9). The voltage after the first resistor (in the left circuit, it is the left resistor, since it is closest to the positive side, again using conventional flow, and in the right circuit, it is the left pin of the potentiometer which connects to the potentiometers resistor), which is the voltage after the first voltage drop, is used by the DIO pin, which in both circuits is represented by the middle resistor. At the start of the middle resistor, the voltage is what came out of the first voltage drop. This similar configuration in both circuits, which is the use of two resistors to create voltages that get divided amongst the resistors is called a voltage divider. You can probably guess where the name came from.
All this is a lot to read through, but it is best explained if you just play around with the circuit and see how the concept works. A picture is 1000 words, but I guess a simulation is 10000 words.
This same concept applies to the second Arduino circuit which used the potentiometer that connected to the analog pin.
The only difference is that the DIO pin will take in the varying voltages from the potentiometer, and will only display a 1 or a 0, whereas the analog pin will take the varying voltages, and display any number between 0 and 1023, 0 correlating to 0V, and 1023 correlating to 5V.
Ok, Input is Cool, but what about Output?
Output for DIO pins is basically the same in terms of voltage levels, except instead of taking in voltage, it outputs it. As stated before, analog pins cannot actually output an analog voltage, meaning it cannot output a voltage between 0V or 5V, like 2.5V, but it can output 0V or 5V similar to a DIO pin would. Though, there is a difference for DIO output. Let me just show you what I mean.
Wiring this circuit up was pretty simple, so you don't have to wire it yourself so that we can get to the point. There are three LEDs, the top left powered by a DIO pin, the top middle LED powered by a DIO pin as well, (except this is different, and we'll talk about the difference later), and the last LED powered by an analog pin that is imitating a DIO pin (it delivers 5V, or close to it, like a DIO pin would).
The top left and bottom LED have around the same brightness, but what about the LED in the middle? It is a little dimmer than the rest. This is because of something called Pulse Width Modulation, or PWM. Below is what a PWM signal looks like on a graph.
You might be asking how a DIO pin can output a lower voltage to make the LED dimmer even though it can only output 0V or 5V? Well, it is true that a DIO can only output 0V or 5V, but it can do it in a clever way as to lower the average voltage given to the LED. The DIO pin is basically outputting 5V and 0V in an alternating fashion as to create that lower average voltage. In the picture above, you can see that the length of tH is longer than tL. tH stands for the length of time it was high, tL stands for the length of time it was low, and T stands for the length of time for both of those combined. You might recall earlier that high means 5V and low means 0V. So, putting that together, the graph above shows us that the time it was on is longer than the time it was off. This pattern on the graph continues. Simply, there is a higher average voltage if the time it is high is longer than the time it was low. If each high "mountain" is longer than each low "valley" on the graph, then you have a higher average voltage, probably above 50% of the max voltage. How do we know the percentage of the maximum voltage that the average voltage is at? You just take the time it takes for 1 "mountain", or high, and divide it by the time it takes for both 1 "mountain", or high, and 1 "valley", or low. This creates this equation: percentage of max voltage = tH/(tH + tL). In the graph, you can see an uppercase "T". That represents the sum of the time it takes for both the high and low section, so we can simplify the equation above to just say: percentage of max voltage = tH/T. So the jist is, divide the time it was high by that same amount + the time it was low.
So, this cycling of on and off, and the ratio between the on time and off time is how the LEDs brightness changes, and this process is called Pulse Width Modulation. On the Arduino, there are some pins that have this function. The pins that have this function have a "~" symbol next to it to let you know.
That's essentially how the outputs and inputs of the DIO and analog pin works!
We'll get to use the Arduino more in the future, and will get some practice with using it as well. I hope this has been a good introduction to the Arduino. If want more info on this, here is a video for you. This video also goes over the programming which is worth looking at.
Relating it to Robotics
Below is what is called a roboRio. We use roboRios in robotics to control the robot. It is like the brain of the robot, and is used to control different components. The Arduino is similar in a way, in that it can also control components. If you look at the left, right, and bottom side, you will notice that there are DIO, PWM, and analog pins, similar to the Arduino, and they have the same functions. There is also an input power section on the top left of the roboRio. This is where you would connect the dedicated power section for the roboRio on the PDP to. The Arduino can be connected by a battery through its Vin pin, so in a way, it is similar to the roboRio.
Day 2
Summary of This Lesson
This lesson will allow members to create different circuits, some being an LED circuit, a LED dimmer circuit, and a circuit that can control a motor, all with the Arduino. There will be some new components introduced, namely, the Bipolar Junction transistor, the MOSFET and the diode. This is essentially an application of what was covered in the last lesson.
Lessons for Electrical Training on the website requires a Tinkercad account at www.Tinkercad.com, so if you haven't already, make an account on that website. Also, you can press the "Try Circuits" button in the circuits tab of Tinkercad to get an introduction of how to use Tinkercad's circuit simulation feature.
Make it Light up and Blink
I challenge you to make an LED circuit in Tinkercad and use the code provided to make it blink. Don't worry if you don't know any coding, as we'll cover how the programming works with the circuit.
Here is the provided code which you will paste into the Tinkercad project by pressing the "code" button, dropdown menu and pressing "text", then copy and paste the code below to replace the default code in there.
void setup()
{
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(6, HIGH);
delay(1000); // Wait for 1000 millisecond(s)
digitalWrite(6, LOW);
delay(1000); // Wait for 1000 millisecond(s)
}
Now, in order for you to make the correct connections to have the LED blink, you need to understand a part of the code above. Looking at "pinMode(6, OUTPUT);", you'll see the number 6. That indicates the DIO pin 6 on the Arduino. The whole thing means that the DIO pin 6 will have the mode set to output, which is a mode that allows that pin to provide voltage to whatever is connected to it. This is where you would connect your LED. Keep in mind that GND is like the negative of the battery, and a DIO pin is like the positive side, so use this to connect the LED the right way around. If nothing happens, then you probably connected it backwards, so just swap the connections. Also don't forget to use a resistor. If not, then you would see the LED blow up. Use Ohm's law to limit the current to around 15 mA or so.
Cool, you got it to blink! Now let's try dimming
Last lesson, we went over how a DIO pin can alternate its voltage pretty quickly, which is called Pulse Width Modulation, or PWM. We will use that here. It would be boring to just change the code to dim it, so why not use a potentiometer? The potentiometer's middle pin connects to the analog pin A0, and one of the outer pin connects to GND, and the other to 5V. The potentiometer can create varying voltages from its middle pin depending on the position of the handle, which is what we want the analog pin to see so that we can use that info to dim the LED according to that voltage level. I basically just explained what the code will look like in English, which is found below. Once you get that setup, copy and paste the code below into that Arduino by clicking on it, pressing the "code button", pressing the drop down menu that says "blocks" then choosing "text", then replace the text to the right with the code you've copied. Here's the code:
float LED_Brightness = 0.00;
void setup()
{
pinMode(6, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
LED_Brightness = (analogRead(A0)/1023.00*255);
analogWrite(6, int(LED_Brightness));
delay(100);
}
Again, we will be using DIO pin 6, which also has a PWM function, for the LED. You can use the same circuit from last time. Now, you will connect a potentiometer as described above. In the code, it says "analogRead(A0)". This means that the Arduino will take in analog voltage values from analog pin A0. So, you should connect the middle pin of the potentiometer to analog pin A0. Copy and paste the code like you did before, and play around with it. You should see the LED get brighter or dimmer as you turn the handle.
Now, there is an even simpler way to do this, but this is just to combine analog inputs with analog outputs. The simpler way would be to just use a potentiometer in series with the LED, since a potentiometer is basically just a resistor that can change its resistance. There was a demo of this in the first lesson, so you can check that out.
Same Concept with Motors... But Wait!
The past two circuits can be applied to motors to have the motors be powered by a DIO pin and can use PWM to control the speed. The only problem is, motors take too much current from the DIO pins. Normally, the max current from a DIO pin can only be 20 mA, but a motor can draw upwards of 40 mA. This can cause the DIO pin to get too hot and can get damaged. So how do we use a motor with a DIO pin? The answer is in transistors, specifically the Bipolar Junction Transistor (BJT). A transistor is like a switch, but it can allow electricity to flow through it using electricity, instead of the mechanical force of your hand on a lever or slider. How does it work? Well, here is an excellent video that not only explains it, but also shows you the details of how it works. It also talks about diodes, which we will also need to use in the next two circuits.
To recap the video above, transistors only allow electricity to flow through it if there is a voltage at the base of the transistor, and that there is an overall voltage difference across the transistor, meaning the emitter and collector have a voltage difference, making electrons flow in one way, from negative to positive. The base is like a gatekeeper, it allows or prevents electricity flow through the transistor depending on if there is current flowing through it. Now, in the video, it showed an NPN transistor, which we will use. There is another transistor called a PNP transistor, which is basically an NPN transistor, except that the emitter and collector are now positively doped, and the base is negatively doped. For now, focus of the NPN transistor, which is more widely used as well.
To get you more familiar with NPN transistors, play around with them. Connect the pins to a battery to power a component connected to the emitter side of the NPN transistor. Imagine the electrons going through the circuit and through the transistor. If you are having trouble, you can watch the video again. If the transistor isn't allow electricity through, look at the video and see what needs to be changed to allow electricity through.
A transistor may seem complicated, but just remember that its purpose is essentially just a switch, which is activated with electricity. It is also an amplifier of electrical signals, but then again, that is just a switch, because the electrical signal is used allow electricity through the transistor, like a switch, making it appear like the signal was made bigger, when in reality, the signal just allowed a larger current through.
Now Let's get on with Application
If you've gotten a component like an LED powered by electricity that flowed through a transistor, then congrats! Now, there is a case where it can be done accidentally, which means that you can power an LED with a transistor without using all of the three pins. That being connecting the negative side of a battery to the collector or emitter (they are essentially the same, both negatively doped but with slight differences), and the positive side of the battery to the base. Below is what I mean by that. You will also see an example of how to use the transistor. Now I strongly recommend you to not just look at how things are connected and call it a day. Play around with it and see what happens, for example, if you connect an LED on the collector or emitter side of the transistor. It does actually make a difference, specifically the current is different between those two because the current from the base combines with the emitter pin (using conventional flow), but not the collector pin since it is "downstream" of the flow of electricity, meaning it doesn't flow against the flow of electricity.
Using transistors with motors
Now that you know how we can use a smaller current to switch a transistor on to activate a larger current, we can apply this to the Arduino to turn a motor on. A motor takes in a lot of current, which the Arduino DIO pins cannot provide. If you tried to only power a motor with a DIO pin in real life, it would malfunction and be unusable, obviously not good. If you used a transistor, then the transistor can deliver the higher current for you, which is activated by the small current given by the DIO pin, so the DIO pin won't break. However, there is one last thing before we can use a motor with the Arduino. We need a diode for the motor. You have seen how the diode and a transistor are similar in that one area is positively doped, and the other is negatively doped. The diode is meant to only allow electricity to flow through it in one direction, whereas a transistor only allows electricity to flow through it when current goes into the base pin.
So what is the diode for? Well it is used for a number of reasons. In this case, it is to prevent sparking in switching devices like mechanical or electrical switches that are connected to an inductor. You know that an inductor stores magnetic energy after current flows through it after a while. The energy from the electricity had created this magnetic field, which that magnetic field can be used to create electricity (the flow of electrons). Electricity and magnetism are interrelated like that, they can affect each other. That is why we say that the energy from the magnetic field is stored, since its energy can be used to make electrons move. When the switch is open after the switch has been closed for a while, the electrons group up on one side of the switch. The reason why the electrons don't just stop like in a normal circuit without an inductor is because in this case, we have an inductor that has stored energy from the magnetic field. This energy cannot disappear randomly, which follows the law of conservation of energy. So, the energy is turned into electrical energy, or electricity. This is why the electrons group up on one side of the switch, since the energy from the magnetic field tries to pull it forward, but of course, the switch is open, so the electrons stay on one side, whereas the electrons on the other side of the switch continue on. So what does this do? Well, there are a lot more electrons on one side of the switch compared to the other, so a voltage is created. In this situation with an inductor, this creates a huge voltage, which gives the electrons a LOT of energy, which causes a spark. Sparks are bad in circuits, as they can cause fires if it is big enough, or just damage the switch, mechanical or electrical, in general. So what can we do to prevent those sparks? The answer, flyback diodes. If we can redirect the current, the electrons, that are on the inductor side of the switch to go to the switch, where the other set of electrons are grouped up, then the voltage difference won't be that large, and therefore, no spark, or at least not a very big one. If you need a visual, here is my very wobbly mouse-drawn depiction of what I just said. This depiction uses schematic symbols, which are used when designing electronics. I have put labels on each symbol for you to learn what each symbol means. There are many schematic symbols; those aren't the only ones used in electronic designs.
Both the pictures show open circuits with inductors that had current flowing through it for a while, and thus a magnetic field had resulted from it. Both pictures use conventional flow, but even though I explained the concept above using electrons, the concept still holds, so just swap the charges, or just imagine the positive charges in the picture to be electrons. The left picture doesn't have a diode, while the right one does. As you can see in the left picture, the positive charges group up at the switch, whereas after the switch, the positive charges leave that area, creating a large voltage difference, which is bad. In the right picture, the positive charges still group up at the beginning of the switch, but the positive charges at the other side of the switch keep being redirected to the end of the switch, causing a small voltage difference. Now, the positive charges will keep on cycling around and will eventually stop. This is because the energy from the magnetic field turns into heat energy since the circuit has resistance, and that will deplete the magnetic field energy, causing the flow of electrons to stop. Why can't we just use a normal wire to redirect the electrons? Well in that case, the electrons wouldn't flow through the inductor at all, since the wire creates a short, or a direct and easy path, towards the other side of the battery when the switch is closed, making the inductor useless. The diode is placed in a way as to have no electricity flow through itself when the current flows from positive to negative, but when the switch opens, there are a lack of electrons at the end of the switch, so electrons would flow through the diode in the opposite direction that it would've gone when the switch was closed in order to go towards the end of the switch and balance out the electron difference.
If you are still confused or need a video, here is a video for you explaining how sparks are made with switches connected to an inductor.
To use a diode with a motor, you would place the diode in the reverse direction of conventional flow, in parallel to the inductive device, in this case, a motor. To know what direction the diode would allow electricity through when looking at a diode, look for a white line on the edge of the diode. That line represents the same vertical line in the schematic picture above, which also tells you the direction that electricity can flow through. Here's an example below:
You can see that the vertical line on the diode (the black rectangular component next to the motor) faces towards the positive side of the circuit/battery, meaning it won't allow electricity to flow through it when the current moves from positive to negative, but will allow electricity through itself when the switch opens, and the current goes in the opposite direction.
Finally, we can combine what you know into a circuit that uses an Arduino to control a motor. Using the two new components, the transistor as a switch/amplifier, and the diode as a spark preventer, create a circuit that can turn a motor on using a DIO pin. The next circuit after that will add onto that circuit with a potentiometer section to control the motor speed with PWM, similar to how you added onto your previous LED circuit with a potentiometer to control the brightness of the LED with PWM. Use the first code for the first motor circuit:
void setup()
{
pinMode(6, OUTPUT);
}
void loop()
{
digitalWrite(6, HIGH);
}
and the second code for the second motor circuit:
float Motor_Power = 0;
void setup()
{
pinMode(6, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
Motor_Power = (analogRead(A0)/1023.00*255);
analogWrite(6, int(Motor_Power));
}
For the DIO pin that you will be using, use DIO pin 6, just like you did for the LED circuits previously since it has the PWM function. As for analog, use A0.
And Finally...
I'm sorry I had to do this to you, but if you had trouble creating some of the circuits, there was a tinkercad project this whole time with all of the circuits that you had to make above in there. I didn't want you to show you the answer right away, since that would ruin the feeling of achievement and would take away the tinkering of tinkercad. Also, it is better to be confused sometimes and learn from it. Confusion is like the sweat of learning. Anyways, here it is:
Similar to the transistor, a MOSFET is a switch that allows electricity through when something else happens. This something that happens is different compared to a transistor however, since instead of using current moving into the base pin to allow electricity through, a MOSFET uses voltage at a pin called the gate pin, which is equivalent to the base pin on a transistor. Another difference is that it has very low resistance when the MOSFET is on and allowing electricity through. This means that MOSFETs can handle much more current while being less hot compared to a BJT transistor, since less resistance means less of a voltage drop (V = I*R, if R is lower, then V is lower). This ultimately leads to lower power dissipation, since P = I*V. With the voltage drop being lower, power is also lower. Since this lesson had covered transistors and since MOSFETs are similar in a way to BJT transistors, there will only be two videos explaining MOSFETs. You can search for MOSFETs on Tinkercad and simulating MOSFETs there.
As always, tinker around with components and go on google, youtube, etc. to figure out any mysteries that you have. Confusion is the sweat of learning.