wiki:ControlSystems/Electrical/Training/FallTraining/ProjectThree

Materials Needed

  1. You will need: An Arduino, LED(s), a push button, wires, resistors, and a USB cable.

Steps

  1. This project can be done on both TinkerCAD and with the real Arduino kit.

  1. Let's start with a resistor that is between 370 and 500 ohms. You can use a resistor chart online (or from the previous projects) to find the ohms of real life resistors. In TinkerCAD, you can just type in the numbers with the correct units.
  1. Next, take out a push button, and connect the resistor to terminal 2a (you can hover over the pins of the push button to see the names). If you are using a real push button, find the side of the push button with a little divot (looks like a square hole made onto the plastic).
  1. Connect the resistor to that side of the push button with the divot.
  1. After that, connect the LED’s cathode to the 2b terminal (side without a divot) of the push button.

  1. Then, connect the resistor to the GND of the Arduino, and the anode of the LED to the 5v of the Arduino.
  1. And that finishes it! This requires no code as it only needs a power supply, which is in the Arduino. The push button acts as a digital pin in the previous projects, telling when electricity can travel due to making a connection between 2 metal sides of the push button when pressed.

Explanation of how it works

  1. When the push button is pressed, it makes an electrical connection, creating a path from the 5v to the GND of the Arduino.
  1. This causes electricity to flow through the resistor (limiting current for the LED) and the LED as well.
  1. This lights up the LED whenever the push button is pressed.

There is an alternative way to do this.

  1. Alternatively, you can use code to power the LED whenever the push button is pressed, and this way will also teach you about pull down resistors.

  1. We’re going to start off with a resistor, that is around 370-500 ohms of resistance. Again, use a resistor chart if you are using a real resistor. If you are using TinkerCAD, just type in the number with the correct units.
  1. Next, connect that resistor to the anode of the LED (longer leg), and connect the cathode of the LED to the GND next to digital pin 13.
  1. That finishes the LED part of the circuit, now onto the push button part. Let's start with a resistor. Choose a resistor from 1000 ohms, to 10000 ohms (again use a resistor chart to find the resistance of a real resistor).
  1. Connect that resistor to the 1b of the push button (side with the divot if you are using a real push button).
  1. Connect the terminal with the same side with the resistor to digital pin 2.
  1. By this point, you should have 2 terminal pins on the same side connected to something, and 2 terminal pins not connected to anything, and on the same side as each other.
  1. Pick a vacant terminal pin and connect that to the 5V of the Arduino.
  1. Finally, copy and paste the code below into your code option for TinkerCAD, or Arduino software:

const int LED = 13;

const int pushButton = 2;

void setup()

{

pinMode(pushButton, INPUT);

pinMode(LED, OUTPUT);

}

void loop()

{

if (digitalRead(pushButton) == 1){

digitalWrite(LED, HIGH);

}

else{

digitalWrite(LED, LOW);

}

}

Explanation of how it works

  1. The pushbutton when left open, will leave the terminal pins that are connected to GND, and the digital pin, connected to each other. So, there is only one path between the digital pin and the GND.
  1. The 5v is left alone on the other side of the push button, so no current is flowing through.
  1. This makes the digital pin not have any power going to it, so it has a “Low” state, telling the Arduino that the digital pin receives no power. We can use this information in our code to know when to turn on the LED or not.
  1. Now, when the pushbutton is pressed, it allows the 5v to flow, but to which terminal does the electricity flow to?
  1. Electricity takes the path of least resistance, and since the path that connects to the digital pin is only a wire, it has no resistance (theoretically), so the electricity flows to the digital pin, making it have a “high” state. On the other hand, the path with the resistor has a lot of resistance in comparison to the path without a resistor, so no electricity flows through there.
  1. Code can be used to turn on the LED connected to digital pin 13 when power comes into digital pin 2 when the push button is pressed.
  1. The resistor serves as a barrier, herding the electrons away from the GND pin, and into the digital pin, so that we know when the push button was pressed.
  1. You might be asking, why can’t we just put the digital pin on 1 side of the push button, and the 5v on the other, and when the push button is pressed, the digital pin gets power?
  1. If we were to do that, the digital pin will turn on and off randomly even when the push button is left alone. The reason why is because there are a lot of waves of energy happening in the environment, like radio waves, infrared waves, etc, which can enter the push button and give power to the digital pin. We solve this by connecting the wire from the digital pin to GND, and add a resistor connected to GND so that when the push button is pressed, the 5v won’t just travel to GND and totally ignore the digital pin.
Last modified 4 months ago Last modified on Jan 9, 2021, 3:34:34 PM

Attachments (2)

Download all attachments as: .zip