Ingo Lohs
Published © LGPL

Magnetic Sensor / Switch brick

Give a Magnet-Switch a try

BeginnerProtip30 minutes250
Magnetic Sensor / Switch brick

Things used in this project

Hardware components

Itead Magnet-Switch
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
3 pieces - Signal (orange D2), + (red 5V), - (brown GND).
×1
Arduino Nano R3
Arduino Nano R3
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

Example Magnet-Switch

C/C++
/* Electronic brick of magnetic switch can be connected to digital I/O port to obtain
   the current status of magnetic switch by reading the current digital signal. 
   Generally speaking, output is high level;
   when the sensor encounters magnetic objects (such as magnet), output is low level. 

   https://www.itead.cc/wiki/Magnetic_Sensor/Switch_Brick
   https://www.itead.cc/electronic-brick-magnetic-sensor-switch-brick.html
   
   Electronic Brick - Magnetic Sensor/Switch Brick
   Model: IM120710016 - EBS003
*/

int magnet=2; //connect tilt to D2
int LED=13;   //internal LED

void setup()
{
 Serial.begin(9600);
 pinMode(LED, OUTPUT);
 pinMode(magnet, INPUT);  
}
 
void loop()
{
 if(digitalRead(magnet)==HIGH)   //when the digital output value of tilt is high, turn on the LED.
 {
   digitalWrite(LED, HIGH);   
   Serial.println ("kein Magnet erkannt - no magnet available");
 }
 if(digitalRead(magnet)==LOW)    //when the digital output value of tilt is low, turn off the LED.
 {
   digitalWrite(LED, LOW);
   Serial.println ("Magnet erkannt - magnet available");
 } 
}

Credits

Ingo Lohs

Ingo Lohs

174 projects • 156 followers
I am well over 40 years and come from the middle of Germany.

Comments