Introduction: Controlling Light Bulb in Your Room Using MC-38 Wired Magnetic Door Sensor

About: I am interested in Arduino and looking forward to discover more.

This is my first IoT project and I have made a prototype. Arduino Uno R3 and MC-38 Wired Magnetic Door Sensor are the main components and I am using Blynk application by Blynk.Inc to arm/disarm the sensor.

As this is my first project, I did not use mains electricity to connect with my relay module. Instead I used my power bank and LED to represent mains electricity supply and the electric bulb.

When you execute this project for longer term usage, connect the Arduino Uno R3 to a 20000mAh power bank and when not in use, remove the power bank and recharge it.


You will need:


  • Arduino Uno R3
  • Ethernet Shield 5100
  • MC-38 Wired Magnetic Door sensor
  • Relay module - 5V (1 channel)
  • Light Emitting Diodes (LEDs) - 03 (Green, Red and Any other colour you like)
  • Resistors - 03 (one 10kΩ and two 220Ω)
  • Solderless Breadboard
  • Jumper cables (3 male-to-female and 5 male-to-male)
  • Screwdriver
  • Power Bank - 10000mAh
  • USB Cable
  • Ethernet Cable


  • Arduino IDE -

  • Blynk app

Now let's start this project!

Step 1: Setting Up Your Circuit (HARDWARE)


Relay Module:

To Arduino

  • S ----> D8 (Digital I/O pin)
  • + ----> 5V
  • - ----> GND (Ground)

To LED (Prototype): This is a model to show how the connection with mains supply and bulb works.


  • Mains Supply - Power Bank (10000mAh)
  • Bulb - LED

Magnetic Door Sensor:

One wire is connected to GND and the other connects to D7.


The anode (+, long leg) of the LED is connected to the digital I/O pin while the cathode (-, short leg) of the LED is connected to the Ground.

  • Red - D6
  • Green - D5

Step 2: Setting Up Your Software

Step 01: Installing Arduino IDE

You will need Arduino IDE software to program your Arduino Uno.

Download the software by using the link below:

Choose the compatible format to download the suitable software for your PC (or laptop).

Step 02: Installing the Blynk app by Blynk Inc.

You can get the app from either Play Store or App Store ( for Apple iOS).

I have used Play Store to install this app.

In this project, I have used Blynk app to arm/disarm my magnetic door sensor.

Considering the aim of this project, you can arm the sensor when it is dark or at night and disarm when the function of the sensor is not required.

Step 03: Setting up Blynk app

  1. First, you will need to login using your email address or by Facebook account.
  2. Then you must create a new project by tapping the '+' button on the top.
  3. Name your project. I have named mine as 'Door Switch'.
  4. Choose your device. In this case you must choose Arduino UNO.
  5. Select your connection type. I have selected connection type as Ethernet.
  6. Choose the theme (optional)
  7. And finally, tap on 'Create Project'.
  8. You will receive an Auth token via email.

Step 04: Creating Button to Arm/Disarm magnetic door sensor

  1. Tap on the '+' button on the top.
  2. The Widget Box will appear and you will have an energy balance of 2,000.
  3. Tap on 'Button' option from CONTROLLERS section.
  4. Tap on the button created and its settings will open up.
  5. Change the title to 'ARM/ DISARM'.
  6. Select the OUTPUT pin to D7 as the magnetic door sensor is connected to that pin.
  7. Change the MODE from PUSH to SWITCH, by simply tapping on the black circle.
  8. Change the design as you desire.

The final look must be similar to that in the photo above.

Step 3: Coding

Programming your Arduino Uno to perform the task and connect to the Blynk app

As you can see in the image from step 1, the circuit consists of two LEDs connect to the solderless breadboard. The Green LED indicates that the Arduino Uno R3 is connected to a power supply and that the door is closed(when the sensor is armed). The Red LED indicates that the door is opened (when the sensor is armed).

You will need to install Blynk library in your Arduino IDE software. You can do this by:

  • Going to Tools and select 'Manage Libraries...' option.
  • The Library Manager opens up and type 'Blynk' in the search bar.
  • You can see the first option: 'Blynk by Volodymyr Shymanskyy'.
  • Click on that option, select the latest version and install the library.


The codes used for connecting the Arduino Uno R3 to Blynk app was taken from the Blynk Example Browser. I modified the codes to make it compatible with my model and added some codes to make my Arduino Uno R3 perform the task. For the '#include' functions, you must go to 'Sketch' and include the suitable library.

// Project by Rucksikaa_R

// For Instructable Sensor Contest

#define BLYNK_PRINT Serial

#include <SPI.h>

#include <Ethernet.h>


char auth[ ] = " "; // You must include the Auth token(sent by Blynk) with the quotes.

// You should get Auth Token in the Blynk App.

// Go to the Project Settings (nut icon).

#define W5100_CS 10

#define SDCARD_CS 4

int prevState = -1;

int currState = -1;

long lastChangeTime = 0;

const int RELAY = 8;

const int GREEN = 5;

const int RED = 6;

int val=0;

void checkPin() {

// Invert state, since button is "Active LOW"

int state = !digitalRead(7);

// Debounce mechanism

long t = millis();

if (state != prevState) {

lastChangeTime = t;


if (t - lastChangeTime > 50) {

if (state != currState) {

currState = state;

Blynk.virtualWrite(V1, state);



prevState = state;


void setup() {

// Debug console



digitalWrite(SDCARD_CS, HIGH); // Deselect the SD card


pinMode(7, INPUT_PULLUP); //Door Sensor is connected to D7





void loop() {;


val = digitalRead(7);

if (val==HIGH){










Step 4: Testing Your Model

Everything is complete now. All you have to do is test your model.

To connect your Arduino UNo to Blynk application, tap on the play button and shortly afterwards you will see the message, 'Device is connected'.

How my model works:

  • The Green LED will be lit up as long as the Arduino Uno R3 is connected to a power supply even when the magnetic door sensor is disarmed.
  • When the magnetic door sensor is armed, and the door is opened, the Red LED lights up and the relay module is switched on.
  • When the magnetic door sensor is disarmed while the door is opened, both the Red LED and Relay module are switched off, and the Green LED lights up.

I hope you enjoyed this project!

Sensors Contest

Participated in the
Sensors Contest