Introduction: Magic Wand Relay
In this article we will setup a Particle Photon module to monitor compass baring through the MAG3110 Three-Axis Digital Magnetometer Electronic Compass I²C Mini Module. Based on readings from this sensor we will turn relays on and off using the Relay Shield for Particle Photon I²C 8-Channel SPDT 10-Amp.
This Library is intended to be used in conjunction with the Control Everything article Compass_Relay. The intention of this library is to make use of the MAG3110 Three-Axis Digital Magnetometer Electronic Compass I²C Mini Module and the Relay Shield for Particle Photon I²C 8-Channel SPDT 10-Amp with Particle development web IDE as simple as possible for users.
What you need
- MAG3110 Three-Axis Digital Magnetometer Electronic Compass I²C Mini ModuleRelay Shield for Particle
- Photon I²C 8-Channel SPDT 10-Amp
- Particle Core/Photon module
- Basic knowledge base for developing and programming with Particle Core/Photon modules.
Step 1: Setup Your Particle Photon and Account
Using I2C cables connect the two mini modules to the I2C interface board and insert the Particle Photon module into the I2C Interface board. Then connect the Particle Photon module to a USB power source such as your computer.
Follow instructions from docs.particle.io to setup your Particle Photon module for your WiFi network and associate it with your Particle Account so we can interface with it and flash in firmware.
Step 2: Flash Some Firmware
Go to build.particle.io/build
Click the Target Icon on the left and make sure your Particle Photon module is connected and selected here. Click on the Libraries Icon on the left, then search Community Libraries for Compass_Relay. Make sure the application.ino tab is selected, then click the Use this Example button.
Now we need to import another Library to the application so go back to Libraries and search for NCD8Relay under Community Libraries. Once found click it, then click the button on the left that says Include in App. Then select the newly created application to include the Library there.
Click the Flash button on the left to flash the firmware into your controller.
Source for this firmware for Compass_Relay available on our Github repo here:
Step 3: Configure Settings in Controller Using the Mobicle.io App
Go to mobile.io and sign in with your Particle Credentials.
Click on the controller from the Device List.
Here you can see the current heading for the compass sensor which is not yet accurate.
First we need to calibrate the sensor. Click the function Calibrate and enter Start as the Argument and click Send. This puts the controller into a calibration mode. At this point you can open a serial monitor to the controller and it will output a line each time a new minimum or maximum value is found on any of the 3 xyz axes. Move the controller around in every possible orientation. The longer you do this the better the accuracy of the sensor will be. Once finished click the Calibrate function again and enter Stop as the argument and click send again. Calibration of the compass sensor is now complete.
The Heading variable viewable in the Mobicle app should now be accurate.
Now click the SetMinD function, move the compass to the east most point you want for the range, enter 0 for the argument, then click send.
Now click the SetMaxD function, move the compass to the west most point you want for the range, enter 0 for the argument, then click send.
The controller is now configured and should switch relays on and off as you move the compass between the east and west maximum directions. Be careful not to tilt the sensor much as this will cause switch as well. More development of the library will be required to take sensor tilt into account.