NodeMCU Hall Effect Sensor

Intro: NodeMCU Hall Effect Sensor

This tutorial will show you how to use NodeMCU with an AH180 Hall Effect Sensor for transmitting data to the readiness.io service. The nodeMCU uses the ESP8266 Chip from Espressif. It has built in Wifi and is compatible with the Arduino IDE. If you haven't used a NodeMCU before check out this quick start guide.

The digital hall effect sensor that we've used essentially behaves like a switch. When a magnet is detected it closes the switch. So you can change the sensor to another switch, like a reed switch or push button and the code will continue to work.

This example is quite generic but the applications are quite wide. Some include - checking a door/gate is open or closed, if the water level on a tank is low. You can even use it for things like Rain Gauges or Pulse Flow Meters

Step 1: Components

Step 2: Wiring It Up

The wiring diagram shows you how to connect the sensor and an LED. Firstly we connect the digital Hall effect sensor up to D5 and connecting the 10K resistor between the Vcc and signal pins on the sensor. For the LED it is connected to D3 with a 1K resistor.

However, if you are using a Hall effect sensor - like the one we've linked in the components list - the LED and resistors are already included, so you just need to connect the three pins to the ground (- pin), Vcc (middle pin) and D5 (S pin).

Step 3: Source Code

We are going to be posting to the readiness.io service every time we see a change in state on the sensor. We'll be using the readiness.io library to manage the wifi and service connections.

In order to reduce the number of false positives - we've include a software debounce with a 200ms window, which you can change in the config file.

To read when the Hall effect sensor changes state we are connecting D5 up as an interrupt so as to keep the loop free from any unnecessary work. Once a change it detected we set a flag in the INTERRUPT variable and then send the state back to the readiness.io service.

Sending data back to the readiness.io service should happen inside the loop and not in the interrupt function as it will time out before it has had a chance to complete the action.

You can find the full source code here

config.h

const String CHANNEL_ID = "XXXXXX"; // The Readiness.io channel ID
const String SENSOR_ID = "XXXXXX"; // Your AgriWebb or made up sensor ID
const String TOPIC = "switch-generic"; // The type of sensor or name of the data your sending
const String VERSION = "1";
const String FORMAT = "";
constchar* WIFI_SSID = "XXXXXX"; // Your WiFi SSID / name
constchar* WIFI_PASS = "XXXXXX"; // Your WiFi password
constuint8_t TIMEZONE_OFFSET = 10; // The timezone the sensor is located in (eg. 10 for GMT)
constuint16_t DEBOUNCE_TIME = 200; // How long before the next gate reading can take place (ms)
view rawconfig.h hosted with ❤ by GitHub

genericHallEffect.ino

#include<readiness_io.h>
#include"config.h"
constint LED_PIN = 4; // The pin connecting the LED (D3)
constint INTERRUPT_PIN = 14; // The pin connect the test button (D5)
volatile byte interrupt = 0;
staticunsignedlong last_interrupt_time = 0;
readiness_io client(CHANNEL_ID, TOPIC, SENSOR_ID, VERSION, FORMAT);
/* Interrupt for assessing a change in the hall effect sensor (both high and low) */
voidhalleffectInterrupt() {
unsignedlong interrupt_time = millis();
if (interrupt_time - last_interrupt_time > DEBOUNCE_TIME)
{
interrupt++;
}
last_interrupt_time = interrupt_time;
}
voidsetup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED, HIGH); // internal LED is switched on when low - so we have to switch it off/
Serial.begin(115200);
Serial.setTimeout(2000);
while(!Serial) { } // Wait for serial to initialize.
Serial.println("Device Started");
Serial.print("Connecting to ");
Serial.println(WIFI_SSID);
client.wifiConnection(WIFI_SSID, WIFI_PASS);
pinMode(INTERRUPT_PIN, INPUT_PULLUP); // Set the interrupt pin for the reed/hall effect
attachInterrupt(digitalPinToInterrupt(INTERRUPT_PIN), halleffectInterrupt, CHANGE); // Attach the interrupt.
client.testConnection();
}
voidloop() {
if(interrupt>0){
interrupt=0;
// GATE_STATE 0=open 1= closed
bool GATE_STATE = !digitalRead(INTERRUPT_PIN);
digitalWrite(LED_PIN,GATE_STATE);
client.publishData(GATE_STATE);
}
}

Step 4: The Finished Product

Here is the final board. As you can see we've used a Hall effect sensor attached to a breakout board - which includes the LED and resistors. You simply need to raise the magnet up to the sensor and you will see the LED light up. It will then transmit the state to readiness.io.

Share

    Recommendations

    • Plastics Contest

      Plastics Contest
    • Optics Contest

      Optics Contest
    • Halloween Contest 2018

      Halloween Contest 2018

    Discussions