Saturday 25 March 2017

Momentary push buttons - Turning a LED on with a button - arduino

Momentary push buttons
These are really common in arduino projects
 
  It's a basically 2 switches
 
The vertical leads are internally connected prior to pressing the button.
 
Pressing the button will close those switches at the top & bottom of the switch.
 
 
 
 
 
 

The circuit below, doesn't use any coding. The arduino board is simply a power supply.
 
 

 
 Code 1
This is a basic code using pulldown resistors and a switch.
It's a complicated way of turning a LED on.
The idea is to use software, rather than hardware to turn the switch on.
 
Black is ground.
Red is +5V
When the button is not pressed, the pin 2 is connected to ground (via the resistor).
Pin 2 thus reads LOW
When the button is pressed, Pin 2 is connected to +5V, and thus reads a HIGH.
 

Here is the code:
//----------------..........................................
/*
  Button

  Turns on and off a light emitting diode(LED) connected to digital pin 13,
  when pressing a pushbutton attached to pin 2.

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached to pin 2 from +5V
  - 10K resistor attached to pin 2 from ground

  - Note: on most Arduinos there is already an LED on the board
    attached to pin 13.

  created 2005
  by DojoDave <http://www.0j0.org>
  modified 30 Aug 2011
  by Tom Igoe

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Button
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

 // -----------



Basic program # 2
Slightly different way to do the same thing.
 
 

The pushbutton is part of the pulldown resistor detection system

pin 9 reads either a HIGH or LOW signal. 
Its the input
When the button is not pressed, pin 9 is connected to 5V through the resistor... Thus HIGH.
Digital pins are instructed to stay LOW and don't put out 5V to light the LEDs.

When the button is pressed, the opposite occurs.

Here is the code
// ---------------------------------------------------------

int buttonstate = 0;

void setup()
{
  pinMode(9, INPUT);
  pinMode(13, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop()
{
  buttonstate = digitalRead(9);
  if (buttonstate == HIGH) {
    // when not pressed the pull up resistor is on
    digitalWrite(13, LOW);
    digitalWrite(6, LOW);
  } else {
    // helpful single-line comment here
    digitalWrite(13, HIGH);
    digitalWrite(6, HIGH);
  }
  delay(10); // Delay a little bit to improve simulation performance
}
// -------------------------------------------------------

No comments:

Post a Comment