Arduino magnet sensor

Arduino Add comments

Plug it and Arduino can sense magnet!

The magnetic reed switch module is a wonderful tool for designers who would like to turn a circuit on and off based on proximity. A digital “high” value is given when approached by magnet in around 1 inch (depends on strength of the magnet)

Building interactive work is as easy as piling bricks, just plug it to our Sensor Shield with a buckled cable, and make it looks professional and neat.




Building interactive work is as easy as piling bricks, just plug it to our Sensor Shield with a buckled cable, and make it looks professional and neat.

Example code to turn off LED when approached by a magnet:

int ledPin = 13;               // choose pin for the LED
int inputPin = 2;               // choose input pin (for magnetic switch)
int val = 0;                   // variable for reading the pin status

void setup() {
  pinMode(ledPin, OUTPUT);      // declare LED as output
  pinMode(inputPin, INPUT);     // declare magnetic switch as input
}

void loop(){
  val = digitalRead(inputPin);  // read input value
  if (val == HIGH) {            // check if the input is HIGH
    digitalWrite(ledPin, LOW);  // turn LED OFF
  } else {
    digitalWrite(ledPin, HIGH); // turn LED ON
  }
}
Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists

Leave a Reply

WP Theme & Icons by N.Design Studio
Entries RSS Comments RSS Log in