I’m working on a project to levitate a magnet using a linear hall effect sensor (SS494B) as feedback, so the closer the magnet gets, the larger the output voltage is. Using the code below I made this video:
the coil is my inductor with the sensor below reading the magnet below that http://mekonik.files.wordpress.com/2009/03/setup.png?w=125&h=147
and my coil driver is set up similarly but instead I’m using a mosfet instead of a bjt and the gate is connected to digital pin 5 http://mekonik.files.wordpress.com/2009/03/driverscheme.png?w=227&h=197
The problem I’m having (as you can probably see in the video) is the magnet bounces around and then will settle but the oscillations build back up and I would like to keep the magnet in one place.
What my code does is I have 3 while loops. The first is a standby loop that is waiting until the sensor reaches an arbitrary int of 630 (with no magnet and pwm output at 0 the hall output reads about 530).
Once the sensor passes 630, it will increase the pwm by 15 each iteration until the sensor’s output is 700 and then the program moves to the last loop decreasing the pwm by 15 each iteration until the sensor output drops below 700.
Here’s the code I have so far:
const int hall = A1; //hall=analog pin 1
TCCR0B = TCCR0B & 0b11111000 | 0x02; //Change pwm output frequency to 7.8kHz
pinMode(5, OUTPUT); //sets digital pin 5 as Output
analogWrite(5, 30); //start pwm output at low Duty
while(analogRead(hall)<630) //standby loop waiting for the presense of a strong magnet
while(analogRead(hall)<700) //increase pwm value until hall reads 700
if (pwmVal>110) break; //exit loop if pwm becomes too high
analogWrite(5, pwmVal); //updates pwm output
while(analogRead(hall)>700) //decreases pwm value until hall reads 700
if (pwmVal<15) break; //exit loop if pwm becomes too small
analogWrite(5,pwmVal); //updates pwm output
Does anyone know how I can smooth out the oscillations to keep it in one place? I’m not sure if the map() function would help or be a waste of time.
One of my other ideas was I need the sensor to read the levitating magnet’s intensity while ignoring the inductor’s magnetic intensity. My plan was to create an array storing the value of the sensor at each pwm output from 0-255 in set up (using a global array) and apply the following equation:
Mag=analogRead(Hall)-HallValue[pwm]+Hallvalue or the Magnet’s effect on the sensor is equal to the sensor output minus the sensor’s output from the inductor at the current pwm output plus the offset voltage.
Problem is however it slows everything down and it won’t levitate. Any ideas to read the value of the magnet only as it’s levitating?
Thanks for the help and if you need more info let me know.