Other Arduino Codes and Videos by Robojax

Arduino code to use NJK-5002C hall sensor

Arduino code to use NJK-5002C hall sensor

This is the Arduino code to use NJK-5002C hall sensor to control DC or AC load with and without Arduino


Fig. 1 Wiring of NJK-5002C Hall Proximity Sensor


Fig. 2 wiring diagram of NJK-5002C proximity sensor with 5V Relay module

Resources for this sketch


 /*
 * This is the Arduino code to use NJK-5002C hall sensor to control  DC or AC load
 * In the video I have explained that this can be used without Arduino and with Arduino 
 * watch video instruction on video :https://youtu.be/7RQ8QoJWhpY
 * 
 * 
 * Written by Ahmad Shamshiri for Roboja Video, www.Robojax.com
 * Date: Nov 10, 2018 on Saturday at 09:34 in Ajax, Ontario, Canada

 * Please keep this note with the code.
 * This code is available on Robojax.com
 * 
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.* 
 * This code has been download from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#define sensorPin  2 // pin 2 
#define relayPin 8 // pin

const int wait = 3000;// after switched ON, wait for this amount of time

void setup() {

  // Robojax.com 20181110 NJK-5002C hall sensor
  pinMode(sensorPin, INPUT_PULLUP);// define pin  as input for sensor
  // watch video explaining the INPUT_PULLUP
  //http://robojax.com/learn/arduino/?vid=robojax-push_botton_resistor
  
  pinMode(relayPin, OUTPUT);// defind pin as output

  Serial.begin(9600);// initialize serial monitor with 9600 baud.
}

void loop() {
  // Robojax.com 20181110 NJK-5002C hall sensor
  int sensed = digitalRead(sensorPin);// read pin 2 and put the result in the "sensed" variable
  if(sensed == LOW){
    Serial.println("Sensed");
    digitalWrite(relayPin, LOW);// if sensed turn relay ON
    delay(wait);// keep the relay ON for the "wait" amount of time
  }else{
    digitalWrite(relayPin, HIGH);// else turn the relay OFF
    Serial.println("====");
  }
 delay(300);// delay for 0.3 seconds
  // Robojax.com 20181110 NJK-5002C hall sensor
}
   

If you found this tutorial helpful, please support me so I can continue creating contents like this.