Day 1
Summary of This Lesson
This lesson will be a light one. This lesson will deal with limit switches, which are used to detect when something hits it. We can use limit switches to create a limited range of motion, or to detect when something hits something else. We will see how it works and how to apply it. The next few lessons will introduce components like different sensors that can be used with an Arduino or the roboRio, which the team uses.
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.
So, what is it?
As the name suggests, it is a type of switch. Looking at the picture on the right, you will see a physical picture of a limit switch, similar to the one the team uses, and an internal diagram of how things are connected. The picture shows that the metal lever at the top of the picture is not pressing down on the red button. The picture also shows that there is a connection being made between COM (called common), and NC (called normally closed). So, from the picture, we can see that when the lever is not pressing on the red button, COM connects to NC. We can then imagine that if the lever is pressing on the red button, then the diagonal white line would rotate to the bottom white line to make a connection to NO (called normally open). So, a limit switch is able to change its state depending on if the red button is pressed or not.
Here is a video on limit switches which can help:
So how do we Use it?
The COM (common) pin is what will be the one connected to a DIO pin of an Arduino, or microcontroller, which is what the Arduino is. NC and NO would be connected to a voltage and ground separately, like shown in the picture. In the picture, 3V3 means 3.3 volts, and GND means ground, or 0 volts. So when the lever is not pressing down on the red button, then COM will be connected to 3.3V in this picture. If NC was connected to 5V, then COM would be connected to 5V when the red button is not pressed by the lever, so you can use any voltage as long as it isn't a huge voltage. If the lever presses down on the red button, then the opposite happens. COM connects to ground through NO, so COM would be connected to 0V. So we have two different voltages going into the DIO pin through the COM pin depending on the two states of the lever. Let's see an example of it in Tinkercad.
Note: The in person people will get to use a limit switch just like the one in the picture, but Tinkercad has a substitute, which isn't a limit switch, but a sliding switch, which can still be used to show how connections are made within a limit switch.
Here is how a slide switch, which will represent a limit switch, makes electrical connections inside of it. Click on the imbedded link below to see a demo.
You will notice that when the sliding part of the switch is on the left side, then the left LED lights up. If the sliding part is on the right side, then the right LED lights up. The sliding part has a piece of metal that connects to only two pins, which are the ones under it. The middle pin, which if you hover over the end of it, actually says common which a limit switch will also have, will always connect to the sliding piece of metal, while the other two pins will connect to it if the sliding piece of metal comes to it. Notice how the middle pin connects to the positive of the battery, and the sides connect to the negative. This is to ensure that the metal sliding piece will bridge the negative side of the battery and the positive side of the battery together to have a flow of electrons. So yeah, that is basically what happens inside of a slide switch. This is pretty similar to a limit switch, except in a limit switch, there would be a different mechanical movement like rotating a lever, or a forward and backward motion to change states, but other than that, the electrical connections being made are similar.
Let's try to use it in an Arduino Circuit!
Now, the demonstration above showed how to power two separate parallel circuits, but now let's try to use it to power the common pin with 5V in 1 state, and power it with 0V (or ground) in the other state, then connect the common pin to a DIO pin so that we can use that information to light up an LED or not light up an LED with a separate DIO pin. Go ahead and create a new Tinkercad project and see if you can create that situation. Remember that the common pin, which is the middle pin of the slide switch, will always connect to the metal sliding piece.
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()
{
pinMode(3, OUTPUT);
pinMode(8, INPUT);
}
void loop()
{
if (digitalRead(8) == LOW){
digitalWrite(3, LOW);
}
if (digitalRead(8) == HIGH){
digitalWrite(3, HIGH);
}
delay(100);
}
In the code, you will see that there is "pinMode(3, OUTPUT);", which means that you will use DIO pin 3 for powering the LED since it will output power, as stated in the code. You will also see "pinMode(8, INPUT);", which means you will use DIO pin 8 to detect the changes in voltage caused by the switch moving from side to side. DIO pin 8 would connect to the common pin of the slide switch.
Make the circuit, and copy and paste the code into the Arduino.
If you've got it right, you should see the LED light up when the slide switch was in one position, and when the slide switch went to the other position, it should not light up. Now you might be asking why we can't just use the slide switch in series with the LED to make it light up like that, with no Arduino needed? This is to demonstrate that we can use a slide switch, which in this case represents a limit switch, to let the Arduino know what is happening around it. There are applications in which a limit switch is used, not to turn something on or off, but to know if an object has hit the lever of a limit switch or not. In that case, we used it to get information, not get power for a component. We are getting into components that don't just power things on, but we are getting into components that can help microcontrollers, or just circuits in general, think and gather information.
If you are having trouble, continue to tinker around and figure out what can work. Remember to use DIO pin 8 for the switch, and DIO pin 3 for the LED.
If you are stuck, you can look at this example here:
What's going on in the Arduino Circuit
So, in the example above, and in your circuit if you managed to get it to work, the limit switch connects DIO pin 8 to either 5V, or to ground, depending on the position of the slider. The code in the Arduino will turn on the LED connected to DIO pin 3 if DIO pin 8 connects to 5V, and will turn the LED off if DIO pin 8 connects to 0V. That is essentially what is going on in the circuit. Technically, we can swap the logic, and have the Arduino turn the LED on if DIO pin 8 is connected to 0V, and turn it off if DIO pin 8 connects to 5V, all we have to do is change the code. That is the beauty of programming, you can change things pretty easily, because all you have to do is change characters on a screen.
Anyways, if you are still confused about how the switch can connect DIO pin 8 to either 5V or 0V, then take a look below.
The left picture shows how the slide switch connects DIO pin 8 to GND, or 0V in the current slide switch position. The blue line traces the path. In the right picture, it shows how the slide switch connects DIO pin 8 to 5V, which is also traced, but in red this time. It also shows electricity flowing through the LED connected to pin 3 with a red trace.
That's basically it!
You can continue to tinker around with slide switches in Tinkercad, which represent limit switches that the team uses. Since I can't predict what you are going to make, try to tinker around with limit switches without using the Arduino so that code isn't required. If you want to use an Arduino when tinkering around, you can use the one in the example demo above, and just use the output pin 3 to power another component other than the LED. You can also switch around the connections of the limit switch, but still connect the common pin of the limit switch to DIO pin 8 to see how the logic swaps.
Day 2
Summary of This Lesson
This lesson will cover a magnetic reed switch, which is another kind of switch. This lesson will cover how it works, and how to use it/apply it to circuits.
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 it and How does it Work?
To the right, you will see an example of a magnetic reed switch. Inside, you can see two metal rods close together, almost touching. As the name suggests, the magnetic reed switch works with a magnetic field. When a magnetic field, like one in a permanent magnet or a powered inductor, is near the magnetic reed switch, the two metal rods made of ferrous material (ferrous means having some iron, which is able to be magnetized) touch each other and make an electrical connection. So it works like a simple switch that physically connects pieces of metal together to allow electricity to flow through, but the way it does that is with a magnetic field. Pretty simple.
How can we Use it?
A magnetic reed switch can be used to power a small component like an LED, or to provide a voltage for a DIO or even analog pin to let a microcontroller or some other controller know that there is a magnetic field around the magnetic reed switch. You might question why this can be useful, like why is sensing a magnetic field useful? You can use it in clever ways, for example, you can attach a magnet to a moving part of an arm, and attach the magnetic reed switch to the non-moving part of the arm. Then, when the arm, along with the magnet hovers near the magnetic reed switch, it makes an electrical connection. That electrical connection could be made between a DIO pin, and a pin that provides 5V. So in effect, when the moving part of the arm moves close to the magnetic reed switch, the DIO pin receives 5 volts since the magnetic reed switch closes due to the magnetic field. We can use this signal, the 5 volts, to tell the moving part of the arm, which would be attached to a motor, to stop moving. Other applications would be sensing if a door is opened, or if some other object is passing by. So all in all, a magnetic reed switch can be used to detect a change in motion, or things passing by, etc., as long as the object it is trying to detect has a magnetic field.
Unfortunately, Tinkercad does not have a magnetic reed switch. Since this component is fairly simple, you can just substitute it with a normal pushbutton. Now, here's a picture that shows how a pushbutton works.
The jagged line is a schematic symbol that represents a resistor. In this case, it is meant to show the very high resistance of the DIO pin in the input state, as we've seen before since it allows very little current through itself, implying a large resistance. The 1a, 1b, 2b, and 2a labels refer to the Tinkercad labels. So if you hover over the pins on a pushbutton in Tinkercad, it would show you one of those labels. So above is a diagram of how a pushbutton works, and how it can make, or not make, an electrical connection. In this instance, we are trying to connect 5V when the button is pressed, which would be represented as the diagonal line bridging the gap in this diagram, or not connect 5V when the button is not pressed, which is shown in the picture. The diagonal line in the middle represents the metal piece that the pushbutton has that goes down and connects the two sides when the pushbutton is pressed. This is similar to a magnetic reed switch, which is why we are using the pushbutton in Tinkercad, the only difference is that the magnetic reed switch would have its "diagonal line" go down when a magnetic field is near, not when a hand presses on it. The only problem, is that when you try to do this configuration in Tinkercad and in real life, you will not get the result you want, which is to have an "on" represented by 5V when it is pressed, and "off" represented by 0V when it isn't. Try it in Tinkercad. Connect a pushbutton to DIO pin 8 and 5V as shown in the picture above. 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()
{
pinMode(8, INPUT);
Serial.begin(9600);
}
void loop()
{
Serial.println(digitalRead(8));
delay(100);
}
Press the "start simulation" button and "Serial monitor" button and notice how it only shows "1", which represents high, despite you pressing the button and releasing it. So what gives? Take a look at the picture below, and continue on.
Now what are the voltages with the squiggly lines for? They represent voltages that are produced by the environment from electromagnetic fields, like radio waves, or microwaves. Electromagnetic fields are essentially made up of electricity, and magnetic fields. Remember how magnetic fields from things like inductors can create electricity and vice versa due to their interrelatedness? That is a reason why we have these outside voltages. When the switch is open, meaning it doesn't connect our 5 volts to our DIO pin, it would connect our DIO pin to the mentioned outside voltages, which can be random. We don't want that inconsistency, since it would serve no use if an open switch gives us a million different voltages, and therefore, give us a random readings. These random readings creates something called "floating", where the DIO pin "floats" between a 1 or 0, switching between them. What we want, is when the switch opens, it connects to 0V, making it read a digital low, and when the switch closes, the DIO pin connects to 5V, reading a digital high, so that we can do what we want with that consistent info to stop a motor or something. Ok, so just have a GND (0V) pin connect to the DIO pin when the switch opens to prevent current from going into the DIO pin, so basically in the picture below, right?
In this case, we are one step closer, but not at our destination yet. The thing is, since there is so little resistance in the path towards GND, all of the current would want to flow to GND. Keep in mind that the DIO pin has more resistance by quite a bit compared to a GND pin. If you think back to Ohm's law, you will know that there would be a lot of current flowing through if there is very little resistance, since I = V/R. The path that has very little resistance is the path to GND, not the DIO pin since the DIO pin has a lot of resistance (as we've seen since it only allows very little current through in the input mode, implying it has a lot of resistance, again using ohm's law to figure it out). So what happens? The GND pin would take in all the current in both situations: when the switch is open, or closed. When it is open, it would make the DIO pin read low, since no current flows through that pin, but instead, the current now has a path to ground, and using Ohm's law, V = 0*R, we get 0 volts through that DIO pin, cool. That's what we wanted. If the switch was closed, then all hell breaks loose, since the connection is 5V directly to ground. What happens when there is very little resistance between the positive side and the negative side of a battery? The wire gets really hot and burns due to very high current through the wire from the very low resistance of the wire, so I guess yeah, all hell breaks loose. We definitely do not want that. So what do we do? Well, if we add a sizable amount of resistance to the GND path, then we fixed the problem of shorting, by reducing the current by a lot. So, we can use a resistor, say a 1K ohm (1000 ohm) resistor right in front of GND. This is what we end up with:
So, when the switch is open, the outside voltages would cause most of the current produced by those voltages to flow through the resistor to the ground pin, causing no current to go through the DIO pin, and thus, no voltage drop (0V), making it read 0V, or low. That is what we want so far. Then, when we close the switch, 5 volts flows to both the ground and DIO pin. This time, it has enough continuous current to have enough current flow through the DIO pin to create a sizable voltage drop close to 5V, causing the pin to read 5V, or high. What we have just used, is called a pull-down resistor, which pulls the DIO pin down to a low reading when the button is not pressed. There is also a pull-up resistor, which would do the opposite, pull the DIO pin up to a high reading, which would be made by simply just swapping the positions of 5V and GND (0V). These pull-up or pull-down resistors are nice, but if there is an outside voltage of 5 volts, which can be found in areas of a lot of electromagnetic waves like a radio station, then the resistors won't do anything since the outside 5 volts imitates the 5V in the circuit. All of this explanation is good and all, but it might not make sense entirely, so here's a video for you. When it gets to the coding part, don't worry about that.
After that, you can also use this Tinkercad demonstration to play around. To see the output from the serial monitor in each Arduino, press the Arduino, then press "Serial Monitor". Every time you click an Arduino, it would show the output of that Arduino. You will notice that the output for the left Arduino is always "1", despite you pressing the button or not, so it doesn't work properly. On the other hand, the right Arduino has an output of "1" when pressed, and "0" when not pressed, so it works fine.
That's basically it
In today's lesson, you learned about a pushbutton, but the concept largely applies to a magnetic reed switch, in that there is an open circuit/connection in both components when not activated. This means that yes, you will need a pull-up or pull-down resistor for both components. We are just using a pushbutton as a substitute.
The members in the in-person meets would get to experiment with activating the magnetic reed switch, so that is basically what you would be missing out on. They would also be able to use the magnetic reed switch with other components.