Hidden light control using Arduino

I’ve bought recently some cheap dual axis magnetic sensors at SureElectronics (http://www.sureelectronics.net/goods.php?id=944), I’ve tried first to use them as straight compass.

Annoying problem : it’s impossible to accurately compensate the tilt of the sensor without using a gyro sensor. On the other end, if you stay on the same horizontal plan, the measures are pretty accurate even if you consider their low price (6$), I was really surprised. Three axis sensor can simplify the problem but they are more expensive …

Arduino Hidden light control

Usage of the sensor is pretty simple : the communication is based on I2C and you can read new data 50 times per second. Furthermore, there is a reset command that put the sensor back in its initial state, which is necessary when it has to handle strong magnetic fields.

After a few tries and some code, I was able to get the angle of the compass. For that, I had to calibrate it first to know the minimal and maximal values on each axis. The calibration procedure is easy : I’ve placed the sensor on the edge of my workbench, red the x and y values, then I’ve rotated it by 90°, then 180° and finally 270°. After that, I’ve taken the minimal and maximal values along both axis from the different orientations.

The calibrated values are computed this way :

cal_x=(raw_x-((max_x+min_x)/2))/((max_x-min_x)/2)
cal_y=(raw_y-((max_y+min_y)/2))/((max_y-min_y)/2)

The calibrated values varies more or less between -1 and 1, the angle can be determined thanks to some trigonometry :

if (( cal_x ==0) && ( cal_y <0))
       angle=PI/2;
else if (( cal_x ==0) && ( cal_y >0))
      angle=-PI/2;
else if (cal_x<0)
      angle=PI-atan(cal_y/cal_x);
else if ((cal_x>0) && (cal_y<0))
      angle=-atan( cal_y/cal_x );
else if (( cal_x >0) && ( cal_y >0))
      angle=(2*PI)-atan( cal_y/cal_x );

While playing with some magnets borrowed from my fridge, I’ve noticed that the sensor can detect accurately the orientation of the magnet as well as its presence. The sensed values increase strongly but the angle calculus is still totally valid. The magnetic field intensity can be computed like this :

intensity=sqrt(cal_x*cal_x+cal_y*cal_y)

If there is no magnet, the intensity stays under 1.5 (unless I tilt the sensor) and it moves to higher values which depends on the distance between the magnet and the sensor and on the strenght of the magnet. Above a certain threshold, the sensor has to be reset as it gives totally random values. This is not a real problem for my specific usage as there will always be a minimal distance of an inch of wood between the sensor and the magnet, I just have to avoid strong magnets.

Arduino Hidden light control

But there is a small data stability issue when you stay on the same orientation. To reduce that jitter, I’ve dampened the data depending on their variation : if the offset between the previous read and the new one is small, the data is slightly taken into account, on the other hand, if the offset is bigger, the data has more weight. This has the advantage of stabilyzing the data while ensuring a good reactivity when bigger changes are done.

Once all of this has been validated, I’ve prototyped a controller for an RGB led strip I’ve bought at SureElectronics as well (http://www.sureelectronics.net/goods.php?id=1223). That led strip runs on 12V and can pump up to 15w, so I’ve used IRLZ34N mosfets which can be driven directly from an Arduino pin without any extra components and can handle far more than my needs. The other advantage of using those mosfets is that they can handle the PWM mode, which allow me to control the intensity of each color channel of my strip. Here is the schematic of the project :

I’ve not made a dedicated PCB for that project, I’ve just used a round prototype pcb instead and build it as an Arduino shield.

[box color=”#985D00″ bg=”#FFF8CB” font=”verdana” fontsize=”14 ” radius=”20 ” border=”#985D12″ float=”right” head=”Major Components in Project” headbg=”#FFEB70″ headcolor=”#985D00″]Arduino[/box]

For more detail: Hidden light control using Arduino


About The Author

Ibrar Ayyub

I am an experienced technical writer with a Master's degree in computer science from BZU Multan University. I have written for various industries, mainly home automation and engineering. My writing style is clear and simple, and I am skilled in using infographics and diagrams. I am a great researcher and am able to present information in a well-organized and logical manner.

Follow Us:
LinkedinTwitter
Scroll to Top