Introduction: Magnetic Suspension Demonstration Control by Arduino

Magnetic Suspension Demo System is a device that can suspend an object by magnetic force. A set of parameters to maintain an object in steady suspension state can be obtained via PID algorithm.

Step 1: Hardware

Please open the pictures.

Step 2: Hardware Preparation

a.Selection of Windings

The windings for this test are sourced ready-made from (the Chinese ebay), but can also be self-made with enameled wire and winding hoop. Please be noted that there’re around 800 turns in one winding, and enameled wire with iron core shall not be selected for winding making.

b.Floater Selection

The floater shall be made of high-intensity neodymium iron boron (NdFeB) magnet. If possible, a layer of plastic bag shall be wrapped all over the floater during testing so as to protect it from damages caused due to collision between floater and baseplate magnet.

c.Circuit ConnectionSince the linear Hall sensor is easy to damage during testing, it’s suggested to prepare several more for backup.

Step 3: Circuit Connection

During welding, it’s deemed easy to connect main control panel to amplifying circuit, while it’s a key step to fix the windings.

1) The ends with the same number in the two windings are connected to form a pair, and the other two ends are connected in the same way; as a result, the two sets of corresponding windings are in head-to-tail series connection, leading to repelling and attracting effect.

2)The Hall sensor shall be fixed on the axle of the winding, and right over the center point as much as possible.

3) One pair of windings will be connected to OUT1 and OUT2 of L298, and the other pair to OUT3 and OUT4 of L298N.

Please ensure that the specific windings connected to OUT1/2 and OUT3/4, and the specific leads to OUT1 or OUT2 are numbered and noted clearly, so that the matching will remain the same after disconnection and re-connection. In case the connections are mixed, you would have to adjust and do it again.(Marking numbers on windings will avoid to make mistakes- picture 3)

Step 4: Application of Mega168

There’re four types of IO interfaces in Android Mega, that is, analog input/output and digital input/output.

The analog input is marked as ANALOG IN, and can measure voltage at 0 – 5V; the reading range in the corresponding code is 0 – 1023; and the example code is shown below:

1. int readValue1 = analogRead(read1 Pin);

The analog output is actually the output of square waves on string, which generates
average voltage by means of duty-cycle of hi-and-low voltages; it’s marked as PWM on the circuit board; please be noted that though the output voltage is still 0 – 5V, the numeric value range is set to be 0 – 255; the example code is shown below:

1. analogWrite(power1Pin, Pid1.power);

For digital input and output, the mode of
base-pin shall be set up first; the example code is shown below:

1. pinMode(Pin1, OUTPUT); //Set to be output base-pin

2. pinMode(Pin2, INPUT); //Set to be input base-pin

3. digitalWrite(Pin1, HIGH); // Output hi-voltage

4. int v = digitalRead(Pin2); //Read the voltage of Pin2 and the returned result is either 0 or 1

All the 0 – 53 interfaces can be used for digital input/output interfaces, while only 2 – 13 are suitable for PWM analog output, and 0 – 15 for analog input, which are independent from the numbers stated above and will not mix up. It’s strongly suggested the connection numbers are noted at the beginning of the program collectively, so as to facilitate users to understand the connections straightward.

1. int adjust1Pin = ; // For regulating potentiometer on A direction

2. int adjust2Pin = 2; // For regulating potentiometer on B direction

3. int read1Pin = 4; // For connecting input A potentiometer

4. int read2Pin = 3; // For connecting input B potentiometer

5. int i1Pin = 36; // For connecting I1 interface on motor drive-plate

6. int i2Pin = 37; // For connecting I2 interface on motor drive-plate

7. int i3Pin = 39; // For connecting I3 interface on motor drive-plate

8. int i4Pin = 38; // For connecting I4 interface on motor drive-plate

9. int power1Pin = 5; // For connecting EA interface on motor drive-plate

10. int power2Pin = 6; // For connecting EB interface on motor-drive-plate

There’re four interfaces I1 to I4 in the above codes, which will be set to be digital output.

Application of L298N

L298N is directly connected to 20V power source, and supplies a 5V voltage to circuit by means of inboard power-connection. The circuit board has two current driving circuits in symmetry. Take I1, I2 and EA for an example,

1. In case of positive voltage output, when EA range is 0 – 255, the corresponding output voltage is 0 - +20V

2. I1=1;I2=0; //In case of negative voltage output, when EA range is 0 – 255, the corresponding output voltage is 0 - -20V

3. I1=0;I2=0; //Output voltage is always 0

4. I1=1;I2=1; //Output voltage is always 0

The digital outputs I1 and I2 can be used to control the voltage direction of windings, and analog output EA to control voltage. The functions of I3, I4 and EB are of the same. Moreover, please ensure that the grounding lines of Arduino, L298N and welding circuit shall be inter-connected as regulated.

A complete set of codes

int readValue1 = analogRead(read1 Pin);

analogWrite(power1Pin, Pid1.power);

pinMode(Pin1, OUTPUT);

pinMode(Pin2, INPUT);

digitalWrite(Pin1, HIGH);

int v = digitalRead(Pin2);

int adjust1Pin = ;

int adjust2Pin = 2;

int read1Pin = 4;

int read2Pin = 3;

int i1Pin = 36;

int i2Pin = 37;

int i3Pin = 39;

int i4Pin = 38;

int power1Pin = 5;

int power2Pin = 6;





Step 5: PID Parameter Adjustment

The PID control can be briefly understood as a regulator control by means of P(proportional)-I(integral)-D(differential).

During adjustment, the regulation of P will lead to left-right swinging of floater in increasing magnitude. Some Kd can be added at beginning; when Kd is less than normal, the floater will look like vibrating, and while Kd is greater than normal, the swinging will not be stable and the floater might slip from side.

The principle of PID is not that complicated, but it could be a time-consuming process to adjust the PID parameters during practical testing. It’s suggested to add proper weight to floater when approaching the correct parameters, so as to achieve a remarkable stable effect.

Step 6:


MohsenJ5 (author)2017-01-22

hi there.

step 6 is empty!

can you just upload it again.

i stuck in programming the Arduino board.

thank you.

goodzila (author)2017-01-22

why did you remove step 6 and some parts of source code ?

Prady_35 (author)2016-10-24

Hi EmmaSong,
Its a wonderful project !!! Definitely a amazing one to build,
Please can you share the firmware to or provide the link to download.
Thank you

UlrichW3 (author)2016-08-04

Hi EmmaSong,

very nice instructable and it is what i am looking for.

But i miss the Arduino code.


WinstonT4 (author)2016-05-25

hi emmasong, great work on this levitating project! i'm wondering could you tell us the algorithm in driving the coils in plain english?

here is what i think you did: you pump in current (only in one direction) to one coil at a time then rotate it either clockwise/counter-clockwise to the other 3 coils as fast as you can in order to make the magnet float. the amount of current to drive the coil depends on the strength of the magnetic field sense by the hall sensor. all the coils will have the same polarity when they're driven.

am i right? your reply is greatly appreciated! :)

hyperbass (author)2015-11-03

Hi EmmaSong,

I have some issues with opening the link with the source codes attached.

Can you please check it ?

Thank you!

EmmaSong (author)hyperbass2015-11-08

I've uploaded again:)

Knochi (author)EmmaSong2016-02-01

Hi EmmaSong,

the download link is missing again. Can you please check?

I would also like to ask if there is a schematic?

Thanks in advance

lneves (author)2015-11-11

I didn't undestand what/why are you measuring back, I mean, you use the 3503 sensor to read magnet density, but you should be measuring the height of the floating object instead, shouldn't you?

labdude (author)lneves2015-12-14

The Hall effect sensor measures the magnetic field from the floating
magnet as well as the coils. The effect of the field of the floating
magnet is a function of the height of the magnet. The goal is to
maintain a steady field strength: if the field gets too high, more
current is sent to the coils to repel the magnet. If it gets too low,
less current is sent, allowing the magnet to fall. Since you know how
much current is being sent to the coils it is easy to factor that out.

lneves (author)labdude2015-12-15

Ah, ok, got it! By the way, this project is very interesting indeed!

Slime Eel (author)2015-11-09

I've been meaning to do something like this for a long time. Very cool.

mero55 (author)2015-10-29


I was just gathering stuff to do a very similar project (what a coincidence!) but i was wondering are you using DC or AC current? How much electricity is it using? because from my research i understood that i need plenty of Amps to get a decent magnetic field, and why can"t i have an iron core winding?

Thx in advance! Cool project.

EmmaSong (author)mero552015-11-01

I used DC current here otherwise the direction of magnetic force will always be flipping around. For the current, it may various depends on your device setup (such as how many turns your coils have and how large the voltage is). I can’t get you an exact value if you are using other components different from mine. So my recommendation would be just playing around the voltage and turns of the coil and see if any of these combination gives you a better result.

alcurb (author)EmmaSong2015-11-09

Why coils are air core instead of iron core?

redrooster (author)2015-11-08

Imagine what you could do with a DIY 10,000 Farad graphene ultracapacitor? To make it check out, YouTube: Robert Murray Smith.

saidmsbah (author)2015-11-08

Nice work

nerd7473 (author)2015-10-31

can it be done with an uno?

EmmaSong (author)nerd74732015-11-01

Mega is used 168 just in case of a fairly large amount of computing may be involved in this demo. Haven't tried UNO but I'd say it will give you a similar result.

nerd7473 (author)EmmaSong2015-11-01

thanks! :)

Magnelectrostatic (author)2015-10-28

Very impressive! I knew bottom-up levitation required a different setup from top-down magnetic levitation, but I hadn't considered the use of multiple coils.

Do you know why it is that the magnet rotates slightly? I assumed since the coils are in series the only oscillations would be up and down.


The rotation motion is not meant to be related to the magnatic force..... no vertical torque can be provided by the coil so we cant really control its orientation, may just be some random oscillation as the friction is extremely small.