Analog read between pwm pulses

Hello, im trying to make a electromagnetic levitator with an arduino nano, and i have a problem reading the hall sensor to determine the proximity of the magnet i want to levitate, becouse i use PWM to control the electromagnet power, its own magnetic field interfiere the hall sensor reading, so i want to only read the hall sensor when pwm pulses goes low and the electromagnet is powered down, i tried wiring pwm pin to an external interupt pin, and then read the hall sensor inside the interupt routine ,but in doesnt work well, also adding a second hall sensor on top of the magnet and subtracting its values frim the botom one is not a posibility becouse the electromagnet i use doesnt produce singnificant magnetic field on top, somehow i managet to levitate a magnet but the system is too inestable and it fall down in mater of seconds. So is there a way to to get it working using only one hall sensor and eliminate the electromagnet effect from the hall sensor radings ?

If not, what should i do?

Here is a short video of my setup : Arduino Nano electromagnetic levitation - YouTube

Thank you in advance!!!

The simplest way is not to use a hall sensor but something like an IR beam to detect the position of your levitating ball.

I dont like this aproach, there must be a way to do it with the hall sensor.

You will get better responses if you edit your original post and move it to a more appropriate forum like Project Guidance. Installation and Troubleshooting deals with getting the IDE up and running.

1 Like

Search for Arduino and levitation. This has been done before, and that guy used a hall sensor.

which guy ? All i manage to found is levitators based on analog circuits,or levitators using IR sensors, there one guy using STM32 and he told in the coments to mesure the hall sensor between pulses but didnt explain how to do it.

here is a video of his project Magnetic Levitation using a microcontroller (Arduino / STM32) - YouTube

The current continues after that, until the magnetic field has collapsed, until the energy in the magnetic field is down to nothing.
Look for the project done!

Don't search for a guy. Try "Arduino + levitation".

Here's his code:

//=========================================================|
//     Ekobots Innovation Ltda - www.ekobots.com.br        |
//        Juan Sirgado y Antico - www.jsya.com.br          |
//---------------------------------------------------------|
//      Program Magnetic Levitator PID - 2016/10/06        |
//               All rights reserved 2016                  |
//=========================================================|
int anaPin = 1;   // Arduino Analogic Pin 1
int digPin = 5;   // Arduino Digital  Pin 5
int subPin = 7;   // Arduino Digital  Pin 7
int addPin = 8;   // Arduino Digital  Pin 8

int anaVal = 0;   // Analogic Valie
int digVal = 0;   // Digital Value
//
int levVal = 262; // Levitation Value
int dlyVal = 10;  // Delay Value
//
float timVal = 0; // Time Value
//---------------------------------------------------------|
// PID Values
float setpoint = 0;
float measured_value = 0;
float output = 0;
float integral = 0;
float derivative = 0;
float error = 0;
float previous_error = 0;
float dt = 0.1;
float Kp = 1.0;
float Ki = 0.1;
float Kd = 0.01;
//---------------------------------------------------------|
void setup()
{
   // Levitator initialization Begin; 
   Serial.begin(9600);// was 57600
   Serial.println("Levitator by JSyA");
   Serial.println("Starting...");
   // Digital Pins Work Mode Setup;
   pinMode(digPin, OUTPUT);
   pinMode(addPin, INPUT_PULLUP);
   pinMode(subPin, INPUT_PULLUP);
   //
   timVal = millis();
   setpoint = levVal;
   // Levitator initialization End; 
   Serial.println("Started.");
}
//---------------------------------------------------------|
void loop() // PID
{
   // Hall Sensor Read (Magnetic Field Intensity);
   anaVal = analogRead(anaPin);
   // PID calculations
   measured_value = anaVal;
   error = setpoint - measured_value;
   integral = integral + error * dt;
   derivative = (error - previous_error) / dt;
   output = (-Kp * error) + (-Ki * integral) + (-Kd * derivative);
   previous_error = error;
   // Final value setup
   digVal += output;
   // Check the value for levitation point;
   if (digVal < 0) digVal=0;
   if (digVal > 255) digVal=255;
   // Increase/Decrease the value for Electromagnet;
   // With Base on Sensor Value and the Levitation Point;
   analogWrite(digPin, digVal);
   // Show log values for debug;
   if((millis()-timVal) > 500)
   {
      value_log();
      timVal = millis();
   }
   // Increase The Value Of Levitation Point;
   if (digitalRead(addPin) == LOW) 
   {
      levVal++;
      value_log();
      delay(250);
   }
   if (digitalRead(subPin) == LOW) 
   {
      levVal--;
      value_log();
      delay(250);
   }
   // Time between electromagnet state changes;
   delayMicroseconds(dlyVal);
}
//---------------------------------------------------------|
void value_log()
// Analogic/Digital/Levitation Values Print;
{
   // Show the Hall Sensor Value;
   Serial.print("anaVal=[");
   Serial.print(anaVal); 
   Serial.print("]-");
   // Show the Electromagnet state On=1/Off=0;
   Serial.print("digVal=[");
   Serial.print(digVal);
   Serial.print("]-");
   // Show the Levitation Point Value;
   Serial.print("levVal=[");
   Serial.print(levVal);
   Serial.println("];");
}
//=========================================================|

That looks similar but it's not the project I saw.

I will try it, but i dont understand how this will work asi it will run on the same problem as i, becouse it reads the hall sensor without turning the electromagnet off first and then it will feed bad values to the pid controller.

Try this link. Arduino + levitation - Sök (bing.com)

The code I posted is from the project I saw. That guy used a + and a - button to adjust some parameter in the PID algorithm to balance the setup..

I found the order of the electromagnet I bought almost 3 years ago but it's not available.

I don't see why Your setup fails just from the code. Posting schematics could bring some answers.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.