Toy Levitation From Solenoid Coil

by tuenhidiy in Circuits > Arduino

4175 Views, 20 Favorites, 0 Comments

Toy Levitation From Solenoid Coil

TITLE 2.jpg
20210701_171407.jpg

Magnetic levitation has always been in my intention. Summer is here, I am ready to build a toy for my daughter. It's a toy levitation that can make small toys float in the air. My daughter is very naughty and hyperactive so she needs to be trained on concentration with this toy.

Please check my testing below before getting started.

Things We Need

The main materials are used in this project:

Tools:

Schematic

LAVITATOR_bb.jpg

The project schematic is in picture above.

Hall Module Selection

There are many magnetic sensor modules sold in the market and according to description they can output both digital and analog signals but that is not true. I bought a few modules but it really has only digital signal and in the end I chose the module called “KY-024 linear magnetic Hall module”.

The KY-024 linear magnetic Hall sensor reacts in the presence of a magnetic field. It has a potentiometer to adjust the sensitivity of the sensor and it provides both analog and digital outputs. This module consists of a 49E linear Hall-effect sensor, a LM393 dual differential comparator, a potentiomenter, leds and resistors.

The 49E is a linear hall-effect sensor. It can measure both north and south polarity of a magnetic field and the relative strength of the field. It works as follow:

  • If NO magnetic field is present the 49E will output a voltage around half of the source voltage, around 2.5V.
  • If the SOUTH pole of a magnet is placed near the labeled side of the 49E, then the output voltage will linearly ramp up towards the source voltage (go up to around 4.2V).
  • If the NORTH pole of a magnet is placed near the labeled side of the 49E then the output voltage will linearly drop down toward the ground voltage relative to the strength of the magnetic field (drop to around 0.86V).

Magnetic Coil

My magnetic coil is taken from a magnetic lock. I removed the metal housing and core, just keeping the coil. Its operating voltage is 24VDC.

I used a multimeter to measure coil resistance value, it is about 15.0 ohm. In this project, I use 12VDC to power this coil.

Soldering Work

1. KY-024 Hall module modification.

  • Cutting 3 pins of Hall sensor 49E on the module.

  • Soldering a 3-core wires connecting the sensor and module.

2. Main control board

Following the schematic on STEP 2, I soldered all the connections and components on a PCB prototype board size 50x 70mm.

The component arrangement is described as follow.

  • Top view

  • Bottom view

If you have a PCB project, please visit the NEXTPCB website to get exciting discounts and coupons.

Here are mid-summer sales at NextPCB:

  • Up to 30% off for the PCB orders.
  • Up to 20% off for the PCBA orders.

Determine the Polarity of Magnetic Coil and Neodymium Magnets

Before assembling, we need to determine the polarity of magnetic coil and neodymium magnets for correct arrangement later, otherwise it will make us confused.

Firstly, I used a simple program to read analog value from Hall sensor module.

int HallValue = 0;
void setup() 
  {
    Serial.begin(9600);
  }
void loop() 
  {
    HallValue = analogRead(A1);
    Serial.println(HallValue);
  }

Or we can use a multimeter to measure the sensor output voltage according to the STEP 3.

Then I connected my magnetic coil to a 12VDC power supply and plugged Hall module to Arduino Nano. Step by step, I placed two ends of coil slowly close to the label side of 49E Hall sensor and read the "HallValue" on Arduino IDE serial monitor.

  • If NO magnetic field, the reading value is around 512.
  • If the reading value ramp up from 512, this is SOUTH pole.
  • If the reading value drop down from 512, this is NORTH pole.

I also did the same with some neodymium magnets and marked their polarities for easy identification later.

Assembly Work

My levilator frame is made from PVC pipe. I and my son can easily cut and drill the PVC pipe, as well as, assembly the PVC's elbow and tee together, just like puzzle game.

Firstly, I did a frame like this. It is assembled from pipes, tee, elbows of pipe Ø42mm.

Secondly, I drilled one hole on the end cap pipe 42 so that a hex nipple (pipe fitting) can be tightly fixed on one side of PVC end cap. I placed & tighten one nut M10 on the other side of hex nibble.

It look like this picture.

I screwed an M10 hanging bolt into the hex nibble, then measured and cut a short piece of the bolt, about 150mm length.

The coil and magnet arrangement should be in the following order, pay attention to their poles which I identified in the previous step.

Then I glued one nut M10 on top of magnetic coil. By this way, I can adjust the length of hanging bolt section which is located in the magnetic coil. This is very useful for me to do this project.

Hall sensor was also glued at coil bottom, its nameplate pointing downward towards the magnet. As you can see in the picture, the steel core has been adjusted with a gap with hall sensor, around 5mm.

Magnetic coil was covered and mounted to PVC frame. I cut a groove on the pipe so that magnetic coil can dissipate heat.

Two reducing tees Ø60mm to Ø42mm were used as a levitatior footbase. They also contained control board, KY-024 module. All cables were threaded inside the pipe frame.

I mounted two power plugs (12 & 5VDC) on the PVC end cap Ø60mm and drilled one hole for Arduino programing cable. They're all located at levitator back.

Done. For future upgrade, one potentiometer was mounted on levitator front.

Here is the back side.

Programming

The Arduino Nano code is as below:

#define outMin              0     // Low limit for P.I.D output
#define outMax              255   // High limit for P.I.D output
#define Hall_Input_Pin      A1    // Arduino Pin A1 - Read analog value from Hall Module.
#define Coil_Output_Pin     5     // Arduino Pin D5 - Control magnetic coil.
#define DEBUG               1     // Turn ON/ OFF debugging

// PID parameteres

int Hall_Input = 0;
int Coil_Output = 0;

double Setpoint = 330.0;
double Input = 0.0;
double Previous_Input = 0.0;
double Output = 0.0;
double Integral = 0.0;
double Derivative = 0.0;
double Error = 0.0;
double dInput = 0.0;

double timeChange = 0.1;
double KP = 0.6;
double KI = 0.4;
double KD = 0.01;

unsigned long millisecond = 0;

void setup()
{
  Serial.begin(115200);
  pinMode(Coil_Output_Pin, OUTPUT);
}

void loop()
{
  // Read Input - Hall Sensor
  Hall_Input = analogRead(Hall_Input_Pin);
  Input = Hall_Input;

  // Calculate the Error
  Error = Setpoint - Input;
  dInput = (Input - Previous_Input);
  
  // PID Algorithm
  Integral +=  Error * timeChange;
  Derivative = dInput / timeChange;  
  Output += - KP*Error - KI*Integral + KD*Derivative;  
  
  // Write to Ouput
  if(Output > outMax) Output = outMax;
  if(Output < outMin) Output = outMin;  
  Coil_Output = Output;
  analogWrite(Coil_Output_Pin, Coil_Output);  

  // Remember the previous Input
  Previous_Input = Input;
     
  // Show debugging values
  if (DEBUG)
  {
    if((millis()- millisecond) > 500)
    {
      // Show the Setpoint
      Serial.print(Setpoint);
      Serial.print("/");
   
      // Show the Input
      Serial.print(Input); 
      Serial.print("/");
      
      // Show the Error
      Serial.print(Error); 
      Serial.print("/");
      
      // Show the Output;
      Serial.print(Output);
      Serial.println(";");
      millisecond = millis();
    }
  }
}

Calibration

The P.I.D controller will act to reduce errors by applying correction at its output so it is important to get the correct direction of control action. The P.I.D process is divided into two groups: direct and reverse acting. And magnetic levitation is a reverse acting P.I.D controller, that means its output tends to decrease as the measured signal (input) increases.

I’ve had a lot of fun to figure out how this levitator works. My P.I.D code was referenced from Brett Beauregard, I especially interested in what he mentioned, like a phenomenon known as “Derivative Kick” at link:

http://brettbeauregard.com/blog/2011/04/improving-...

In the P.I.D formula, instead of adding (Kd * derivative of Error), we subtract (Kd * derivative of Input) because my levitation setpoint is constant (330).

Output += - KP*Error - KI*Integral + KD*Derivative;

"Derivative" part is calculated as below:

dInput = (Input - Previous_Input);
Derivative = dInput / timeChange;

I calibrated my toy levitator manually, and my final P.I.D parameters as follow:

double timeChange = 0.1;
double KP = 0.6;
double KI = 0.4;
double KD = 0.01;

To fine-tune and see how the levitator works, we can turn on debugging mode and read data in IDE serial port.

The chart below describes the operation of reverse acting P.I.D. I used Pivot function in Excel to generate the graph from IDE serial data.

Chart annotation:

  • Red line - Setpoint ( 330 in my case).
  • Green curve - Input (Feedback from Hall sensor).
  • Blue curve - Output (PWM signal to magnectic coil).

I did the following tips to get my toy lavitator working properly:

  • Adjusting potentiometer on KY-24 module for its sensitivity.
  • Adjusting the length of core bolt section which is located inside the magnetic coil. This tip has been very helpful for me in this project.
  • There is an effect on the Hall sensor when the magnetic coil is powered by PWM pulse and generates magnetic field. If it is too big, the P.I.D controller will apply wrong correction and floating operation will be unstable or unachievable. In this case, we can adjust the Hall sensor mounting position plus core bolt position as above mentioned.
  • Calibrating the set point and P.I.D coefficients.
  • Selecting correct floating loads plus magnet weight. If it's too light or too heavy, levitator couldn't work well. And the best load should be in symmetrical shape.

Conclusion

Finish 1.jpg
Frame 11.jpg
Finish 4.jpg
Frame 13.jpg
TITLE 1.jpg

My daughter has a lot of vibrating led ball toys that flash blue & red within 25 ~ 30 seconds when shaken vigorously. Due to the COVID pandemic, my whole family was able to play this game by shaking them to be glowed, keeping them floating in the toy levitator then counting the number of times the leds flashed. Whoever had the most number of blinks while floating, that person won.

I also tested with other loads, such as bolts. If it is heavier than led ball toy, I reduce the setpoint from 330 to 320, for example.

Thank you very much for reading my work and hope you enjoyed my post this time!!!