Home Creators Posts Import Register Favorites Logout

Downloads

Content

Hi folks! This month's tutorial is about a very common type of sensor used in escape room puzzles - magnetic sensors - i.e. Hall sensors and reed switches.

Whether it's returning a dragon's egg to a nest, placing a  missing book back in the correct position on a shelf, or moving a chesspiece to a certain square on a chessboard, a simple magnet embedded in almost any portable prop component can be detected and trigger a response - activating a  relay, opening a maglock, or firing a smoke machine. Like many other ER  sensors, if done well, this simple technology can create "magic" as far  as the player is concerned!  

While it's not difficult to find code examples of how to use a magnetic sensor with an Arduino, I couldn't find anything that compared different properties of sensors, or answered important questions like "how close does a magnet have to be detected?" or "will this work the same for both the North and South pole of a magnet?". So that's what I'll try to do in this tutorial: by showing you first how to understand their datasheets, and then giving a practical demonstration of some common readily-available magnetic sensors - the Honeywell SS49E,  Allegro  A3144, A1324, and A1389 - giving a live demonstration of their output and sensitivity to different magnets, and particularly focussing on which ones are most suitable to use in an escape room context.  

I'll then show you how to write the Arduino code for an all-purpose magnetic prop controller which automatically calibrates to its environment on startup, has adjustable  sensitivity, and fires separate triggers to respond to either a North pole or South pole of a magnet. This makes it possible to use not only for object detection, but also basic object identification/differentiation if a pattern of magnets of different polarities are presented to an array of readers (which could be used as an alternative to RFID, say).

I hope you find this a relatively comprehensive guide, providing you with all the details you require to create your own magnetically-activated puzzle props. But, as always, if there are remaining questions which you have, or any comments or suggestions I'd love to hear them!

I'll put the Arduino code download, the STL of the 3d-printed Hall sensor mount, and wiring diagram etc. in a separate post for Patreons shortly. 

UPDATE

I've also now attached a modified version of the code provided by Todd Child designed to accommodate 5 sensors - thanks Todd!

Timings

00:00 - 01:23 Introduction

01:24 - 04:20 Selecting a magnet

04:21 - 04:45 Selecting a magnetic sensor

04:46 - 05:47  -Reed switches

05:48 - 13:42  -Hall sensors (SS49E, A1324, A1389, A3144)

13:43 - 14:32 Practical sensor comparison 

14:33 - 18:49  -Wiring

18:50 - 19:51  -Arduino code

19:52 - 24:15  -Live demo 

24:16 - 25:55  -Analysis of findings

25:56 - 29:27 Creating a magnetically-activated ER prop controller

29:28 - 41:11  -Arduino code

41:12 - 43:12  -Live demo

43:13 - 44:08 Wrapup

Files

Magnetically-Activated Escape Room Prop Tutorial

Magnetic sensors are one of the most commonly-used inputs in escape room tech puzzles. Whether it's returning a dragon's egg to a nest, placing a missing book back in the correct position on a shelf, or moving a chesspiece to a certain square on a chessboard, a simple magnet embedded in almost any portable prop component can be detected by a microprocessor such as an Arduino to trigger a response - activating a relay, opening a maglock, or firing a smoke machine. Like many other ER sensors, if done well, this simple technology can create "magic" as far as the player is concerned! There are two common types of magnetic sensors - Hall sensors, and reed switches, but there are many varieties with different properties and characteristics, so which should you choose? In this video, I'll show you some of the important properties of a component datasheet to look out for, and give a practical demonstration of a variety of common readily-available magnetic sensors - the Honeywell SS49E, Allegro A3144, A1324, and A1389 - comparing their output and sensitivity to different magnets. Based on an analysis of those findings, I'll then show you how to write the Arduino code for an all-purpose magnetic prop controller which automatically calibrates to its environment on startup, has adjustable sensitivity, and fires separate triggers to respond to either a north pole or south pole of a magnet. Timings --- 00:00 - 01:23 Introduction 01:24 - 04:20 Selecting a magnet 04:21 - 04:45 Selecting a magnetic sensor 04:46 - 05:47 -Reed switches 05:48 - 13:42 -Hall sensors (SS49E, A1324, A1389, A3144) 13:43 - 14:32 Practical sensor comparison 14:33 - 18:49 -Wiring 18:50 - 19:51 -Arduino code 19:52 - 24:15 -Live demo 24:16 - 25:55 -Analysis of findings 25:56 - 29:27 Creating a magnetically-activated ER prop controller 29:28 - 41:11 -Arduino code 41:12 - 43:12 -Live demo 43:13 - 44:08 Wrapup For more resources on this, or any of my other escape room puzzle projects, please check out my Patreon at https://www.patreon.com/posts/26349661

Comments

Todd Child

Successfully built a multi-dimensional array system for using multiple sensors in the same code based on Alister's. Not sure if its bad form to post the entire sketch here, but any are welcome to it.

Zaphod

I have 10 reed switches in series. They *all* have to be closed for the puzzle to be solved. I bought ten switches including magnets at aliexpress. The magnets were to weak so i bought bigger ones. It turns out that one reed switch didn't work. That's 10 percent failure. Don't know if i can trust the rest of the reed switches. Now i am thinking of replacing them with hall switches. What do you think? Will the end result be fast enough with ten hall switches???

Prince

i am confused, from where do i download the code for this project?

Big Booty

How many magnetic sensors are there in your patron puzzle?

Óliver Novelles

Hi, Alastair. I'm trying to make a three sensors game but I've just start with simple LEDs and that's something wrong because it only recognise the firs one. I leave you my sketch, hope you or anyone can help me. Thank you so much! /**Magnetic Sensor Controller This is the basis for a escape room prop controller that is activated by a magnet. It assumes use of a linear, analogue Hall sensor (e.g. A1324, A1389, SS49E), which is able to detect both proximity and polarity of a magnet - corresponding to the magnitude and sign of an analogRead of the signal pin. The puzzle defines different two different states that can be triggered - one by a positive magnetic field (S pole, which causes the reading to rise), and one by a negative magnetic field (N pole, which causes reading to fall). This allows not only object detection, but (limited) object differentiation too. Input readings are smoothed using a rolling average function with a configurable number of samples, and an initial base reading is calibrated in the setup function to allow for background environmental noise. */ // The GPIO pin to which the linear, analogue Hall sensor (e.g. A1324, A1389, SS49) is attached const byte sensorPin1 = A0; const byte sensorPin2 = A1; const byte sensorPin3 = A2; // When the sketch first starts up, an average will be taken from a number of sample readings from // the sensor, and this value will be updated to reflect the "base" state int baseReading = 512; int LEDv = 2; int LEDb = 3; int LEDV = 4; // By how much must the average value deviate from the baseReading to trigger a state change? // Higher values = less sensitive. int sensitivity = 20; // Define three states for the puzzle, based on whether sensor is triggered by a positive magnetic field, // negative magnetic field, or neither. enum State {Negative, Default, Positive}; State state = State::Default; // How many samples to include in the average. Increasing this will lead to a smoother value, eliminating // more noise or erroneous spikes in the output. However, it will also increase the time taken for the // value to reflect any changes detected by the sensor - too high will make the response feel "sluggish" const int numSamples = 10; // An array of readings from the sensor. When the array is full, new readings will replace the oldest readings, // acting like a circular buffer int readings[numSamples]; // To calculate the average, we'll keep track of the total of all sample values currently in the array int sampleTotal = 0; // n is a counter to help us advance through the array and know which is the oldest reading (and hence next to // nbe overwritten when a new reading is received) int n = 0; // Adds a new sample to the rolling average int AddSample(int newSample) { // Take off the oldest reading, and add the new one to the total sampleTotal += (newSample - readings[n]); // Add the reading to the array itself readings[n] = newSample; // Increment the counter n = n + 1; // If we've just hit the limit for the numnber of readings that can be stored, // start again to overwrite oldest values at the beginning of the array if (n >= numSamples) { n = 0; } // Return the average of the newly-updated data return GetAverage(); } // Returns the current moving average from the array int GetAverage() { return sampleTotal / numSamples; } void setup() { // Begin a serial connection to use for monitoring Serial.begin(9600); pinMode (LEDv, OUTPUT); pinMode (LEDb, OUTPUT); pinMode (LEDV, OUTPUT); // Fill the sample array with sample readings for (int i = 0; i < numSamples; i++) { AddSample(analogRead(sensorPin1 & sensorPin2 & sensorPin3)); } // Assign the baseReading variable to the average value obtained during sampling baseReading = GetAverage(); // Write output to the serial monitor Serial.print(F("Base Reading: ")); Serial.println(baseReading); } void loop() { // Add the current sensor reading onto the array of samples int val1 = AddSample(analogRead(sensorPin1)); int val2 = AddSample(analogRead(sensorPin2)); int val3 = AddSample(analogRead(sensorPin3)); // If the average has risen above the base reading by more than the specified tolerance if (val1 > baseReading + sensitivity) { // ...and it wasn't previously if (state != State::Negative) { // Call the trigger function to act upon the state change onHighTrigger1(); } // Update the current state state = State::Negative; } // If the average has fallen below the base reading by more than the specified tolerance else if (val1 < baseReading - sensitivity) { // ... and it wasn't previously if (state != State::Positive) { // Call the trigger function to act up on the state change onLowTrigger(); } // Update the current state state = State::Positive; } if(val2 > baseReading + sensitivity) { // ...and it wasn't previously if (state != State::Negative) { // Call the trigger function to act upon the state change onHighTrigger2(); } // Update the current state state = State::Negative; } // If the average has fallen below the base reading by more than the specified tolerance else if (val2 < baseReading - sensitivity) { // ... and it wasn't previously if (state != State::Positive) { // Call the trigger function to act up on the state change onLowTrigger(); } // Update the current state state = State::Positive; } if(val3 > baseReading + sensitivity) { // ...and it wasn't previously if (state != State::Negative) { // Call the trigger function to act upon the state change onHighTrigger3(); } // Update the current state state = State::Negative; } // If the average has fallen below the base reading by more than the specified tolerance else if (val3 < baseReading - sensitivity) { // ... and it wasn't previously if (state != State::Positive) { // Call the trigger function to act up on the state change onLowTrigger(); } // Update the current state state = State::Positive; } else { // Current average reading is within tolerance of the base reading, so do nothing state = State::Default; } // No need to check sensor every single frame, so we'll conserve power by adding a slight delay delay(10); } // This function is called when the average reading drops by more than the specified sensitivity tolerance // from the base reading. You can use it to light an LED, trigger a relay, unlock a maglock etc. void onLowTrigger() { Serial.println(F("Low trigger activated!")); } // This function is called when the average reading rises by more than the specified sensitivity tolerance // from the base reading. You can use it to light an LED, trigger a relay, unlock a maglock etc. void onHighTrigger1() { Serial.println(F("High trigger1 activated!")); digitalWrite (LEDv, HIGH); delay (2000); digitalWrite(LEDv, LOW); } void onHighTrigger2() { Serial.println(F("High trigger2 activated!")); digitalWrite (LEDb, HIGH); delay (2000); digitalWrite(LEDb, LOW); } void onHighTrigger3() { Serial.println(F("High trigger3 activated!")); digitalWrite (LEDV, HIGH); delay (2000); digitalWrite(LEDV, LOW); }