It's a Switch

Educator notes

Introduction


What is a switch?

Parts


Do this

Collect these parts and get ready to make the next circuit!

PartImageDescription
Mehackit Board or Arduino UNOA minicomputer you can program to control light, sound and motors. Connect sensors or switches to make interactive devices.
USB cable (A/B)You upload the program you have written on the computer to Arduino through the USB cable. Arduino also gets power through it.
Jumper wiresYou'll need plenty of thin wires of different colors when working with Arduino!
BreadboardBreadboards are great for making temporary circuits.
LED

LEDs conduct electricity to one direction only. The longer leg is connected towards a numbered, digital pin (0-13).

Resistor 330Ω (or 220Ω)

Resistors resist the flow of an electric current.  The value of the resistor is measured in ohms (Ω). The resistance value is coded into the colored stripes (330Ω: orange, orange, brown and gold). You can replace the 330Ω resistor with a 220Ω one.

Button

The button in the Mehackit Maker Kit is white, not brown.

Pick up at least a pushbutton. If you find other switches, you can use those too! Check out these examples: 

PartImageDescription
Tilt switch 

Here's two examples of what a tilt switch might look like. You'll learn in a moment how it works!
Reed switchA reed switch may also look different - sometimes it has a black plastic casing instead of glass
Different pushbuttonsYou can connect all kinds of buttons to Arduino. If you use buttons from discarded electronic devices, you may need crocodile clips or other kinds of extension wires to connect them. 
Slide switchThese switches have three pins, but you only need to connect the center pin and one of the side pins to Arduino. 

Circuit



Do this

Make a circuit according to the diagram below!

Connect:

  • the long leg of the LED to pin 10 of your Arduino or Mehackit Board
  • a resistor between the short leg of the LED and the blue row at the side of the breadboard
  • a long row on the side of the breadboard to any GND pin
  • one side of the button on the blue row
  • the other side of the button to pin 2

What does pin 2 do in this circuit?

Educator notes


Programming 1: Setting Things up

Transcript


Do this

Write a new program for Arduino:

Check your code by pressing the Verify-button:  

You can upload the code to Arduino, too! It just won't do much!


Where do you configure the Arduino pins you're using?

Educator notes


Programming 2: Make the Button Work


Do this

Continue your code

  • Upload the code to Arduino
  • Test the program by pressing the pushbutton

Extra Try this if you want:

  • Change the code so that the LED lights up only when the button is not being pressed!
  • Upload the code to Arduino and test it.

Does the light turn on when you press the button and turn off when you don’t?
What is the state of pin 2 when the button is pressed down?

Educator notes


Test the Switches


Do this

If you have more than 1 switch:

Test different switches!

  • Replace the button with other switches (for example tilt and reed switches) and see how they work.
  • Hint: you will need a magnet to use a reed switch.

If you don't have any other switches than the button:

Make the LED blink when the button is being pressed!

Remember how you made an LED blink earlier? Continue your pushbutton code and make the LED blink when the button is being pressed down!

Hints:

  • You need to use the delay command and the digitalWrite command
  • These commands must go inside the curly brackets of an if statement. Which of the if statements? Find out yourself!

EXTRA

What kind of switches can you find in the room you’re in? Name at least two different switches!


Which of the following statements is true?

Learn More: Input Pullup


Want to know more?

Check out this detailed tutorial by Sparkfun!


Why do you need to define the button pin as an INPUT_PULLUP and not just INPUT?

Educator notes