What's A Switch?
What's A Switch?
You're probably familiar with switches, there's tons of them in your house. One kind of switch you use every day is a light switch. A light switch is a simple device with two positions, on and off. When on, two wires are connected inside, which allows current to flow. When off, the two wires are disconnected.
On the left, the switch is open and no current flows. On the right, the switch is closed, current flows and the light turns on.
(thanks wikipedia!) In this photo, you can see the internals of a light switch. The two wires connect to the top and bottom. The flat bar that goes verically down the middle is what is physically moved to connect or disconnect. Light switches are great but we need something smaller. We'll be primarily using 6mm tactile button switches.
These little switches are a 1/4" on each side, cost about 25 cents, and can plug directly into a breadboard. These mechanical devices have 4 legs, which may make you think that there are 4 wires that are switched on and off, but in fact, two on each side are actually connected together inside. So really, this switch is just a 2-wire switch.
Normally, the two wires are disconnected (normally open) but when you press the little button on top, they are mechanically connected.
To get the buttons to sit better in the protoshield, you may want to straighten out the legs (just squish them with a pair of pliers) so that they look like the button on the left.
Quick Quiz! Find 5 things around the house that have switches. Whats the average number of switches per device?
Light switch
We're going to make our first test of the pushbutton by having it turn on and off an LED light
Fig 5.1 You'll note that the schematic symbol for a pushbutton switch is a little bit different than the one above Get out your red LED and 1.0K resistor, as well as the tiny pushbutton and build the schematic onto your protoshield:
Power up the Arduino and try pressing the button. The LED should light up when the button is held down (current is able to flow) and go dark when it's released (current is not able to flow).
Switch capability Before you try to turn a 100W lightbulb on and off using a pushbutton switch, be aware that switches have ratings that will tell you the maximum amount of current and voltage they can switch. The little switches are only rated for a few volts and milliAmps. Big switches such as wall light switches are rated for 120V and many Amperes. Make sure you choose the right switch for the job or you may accidentally cause a small fire! Quick Quiz! What does this wiring setup do? (The LED is connected to ground, but its kind of hidden in this photo) Make a guess and then build it and test your guess.
Highlight the text below to see the answer The switch is oriented so that the LED is always on!
These switches have the part number B3F-1000, here is a datasheet webpage for the part. There's a lot of information, but learning how to navigate these sorts of pages is rather important. Use your detective skills to figure out the follwing: What is the maxiumum amount of current this button can switch? Highlight the text below to see the answer 50 mA What is the maximum voltage you can use this switch for? Highlight the text below to see the answer 24V What is the recommended Operating Force (how hard the button is pressed) for the B3F-1000? Highlight the text below to see the answer 0.98 Newtons (100 gf)
DigitalRead
Switches are great for controlling current, as shown by our little light switch demo. But they're even better as input devices! In previous lessons we set a pin on the microcontroller (say pin 13) to HIGH (5V) or LOW (ground, 0V) using the DigitalWrite procedure. Now we get to do the opposite. We will set the voltage on a pin to 5V or ground and then use DigitalRead to inquire whether that pin is HIGH or LOW For our first test, we will use a wire as our switch. Turn on the Arduino and run this little sketch
/* * Switch test program */ int switchPin = 2; void setup() { Serial.begin(9600); pinMode(switchPin, INPUT); switch } // Switch connected to digital pin 2 // run once, when the sketch starts // set up Serial library at 9600 bps // sets the digital pin as input to read
void loop() // run over and over again { Serial.print("Read switch input: "); Serial.println(digitalRead(switchPin)); // Read the pin and display the value delay(100); }
You'll note that we have to tell the Arduino to set the pin as an input. This is pretty easy, use pinMode() but use INPUT instead of OUTPUT
pinMode(switchPin, INPUT); switch // sets the digital pin as input to read
We also use the new digitalRead() procedure, which just takes as an input the pin to examine.
Serial.println(digitalRead(switchPin)); the value // Read the pin and display
The digitalRead() procedure returns a result when its done. That result is either 0 (LOW) or 1 (HIGH) depending on what it saw when it looked at the pin's voltage. In this case, we read the pin and then pass the result as an input to another procedure, println(). Sure we could use a variable to hold the result fromdigitalRead() and then use that variable as input to println() but this is much more succinct.
var = digitalRead(switchPin); Serial.println(var); // read the pin and save it into var // print out the value stored in var
Now use a wire to alternate between connecting Pin 2 to 5V and Ground through a 100 resistor, and watch the serial monitor.
Fig 5.2
You should see it print out two messages depending on whether a the wire jumper connects the input to HIGH (5V) or LOW (ground) voltage. Dont forget, in digital binary land, HIGH is another word for 1 and LOW is another word for 0.
Valid inputs The best way to completely destroy a microcontroller such as an Arduino is to feed it voltages that are much too high. Make sure your input voltages are between 0 and 5V! Never connect a 9V battery directly into an input pin, it will fry the pin for good and possibly destroy the Arduino microcontroller! Whats this 100 resistor all about? There's a 100 resistor we use to connect the input pin to either HIGH or LOW voltage. Why is it there? Well, lets say you accidentally set P2 to be an OUTPUTtype pin, but then you connected it to 5V. If you write a LOW to the pin (0V) but its connected to HIGH (5V), you've basically caused a short circuit at that pin. This isn't very good for the pin and could damage it! The 100 resistor acts as a buffer, to protect the pin from short circuits.