Example Code for Arduino-Test Self-Locking Switch Function

This is a simple test code, to test button pressed or not.

Hardware Preparation

  • UNO x1
  • Self-Locking Switch x1
  • Dupont cable xSome

Software Preparation

Wiring Diagram

DFR0423 Gravity: Digital Self-Locking Switch The Wiring Diagram

Sample Code

/*!
 * @file  selfLockingSwitch.ino
 * @brief  This example light the LED when press the button.
 * @copyright  Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
 * @license  The MIT License (MIT)
 * @author  linfeng([email protected])
 * @version  V1.0
 * @date  2016-6-21
 */

int ledPin = 13;
int inputPin = 4;

void setup() {
  pinMode(ledPin, OUTPUT);
  pinMode(inputPin, INPUT);
}

void loop(){
  int val = digitalRead(inputPin);
  if (val == HIGH) {
    digitalWrite(ledPin, HIGH);
  } else {
    digitalWrite(ledPin, LOW);
  }
}

Result

When you pressed the button , the LED of the pin 13 on the main board is on; Button again, the LED is off.

Was this article helpful?

TOP