Magnetic Levitation: Using Arduino Mega 2560 to levitate magnets

Chaps Pornprinya
6 min readNov 28, 2019

--

Nowadays, magnetic levitation trains are extremely fast because the trains’ body do not make contact with the rail itself, reducing friction and increasing speed. I am very interested in Maglev trains so I decided to create a miniature magnetic levitation device.

In this project, I designed a device using an electromagnet to levitate a small magnet. I used 24 individual magnets to form a ring around the coil to attract the small magnet. Then, I placed a coil (electromagnet) at the center of the magnets to attract this small magnet. To stabilize the small magnet, I stuck the hall sensor on the top of the coil to detect the magnetic field of tiny magnet and send signals to Arduino Mega 2560. This board will then use the signals to control the strength of the magnetic field from the coil.

There will be two parts to this experiment. The first part is testing out the hall sensor to test the response of the hall sensor with different polarities of the magnet. The second part is constructing the levitation system.

Part 1: Testing the Hall Sensor

In this part, I test the effect of hall sensor in different ways to see how the hall sensor reacts to the change in magnetic field by connecting the hall sensor to the input and the LED connected to the output of the Arduino Mega 2560. When the hall sensor senses a magnetic field, it will send a signal to the Arduino Mega 2560 which will output a current to turn the light bulb on.

Figure 1, Magnetic hall sensor

Components

  1. 3mm LED ⨉ 1
  2. 300 Ω resister ⨉1
  3. Hall sensor UGN3503 ⨉1
  4. Arduino Mega 2560 ⨉1

Circuit

Figure 2, the green wires are the output wires, the black wire is the ground, and the orange wire is the input wire.

As shown in figure 2, the power pin of the hall sensor is connected to the 5V supply of the Arduino, the output is connected to the A2 input port, and the ground pin of the hall sensor is connected to ground port of the Arduino. The LED’s anode is connected to D24 and the cathode is connected to ground.

The strength of the magnetic field determines the Hall voltage produced by the sensor. When the south pole moves closer to the sensor, the Hall sensor will create a positive Hall voltage, which will be read as 1 digitally by the Arduino. If the north pole moves closer to the sensor, it will create a negative voltage, which will be read as 0.

In my implementation, the LED will turn on when the reading is 1, and off when the reading is 0.

Here’s the code:

int halls = 2;
int led = 24;
int data = 0;
void setup() {
Serial.begin(10);
pinMode(halls, INPUT);
pinMode(led, OUTPUT);
digitalWrite(led,LOW);
}void loop() {
data = analogRead(halls);
delay(10);
if(data > 500 )
{
digitalWrite(led, HIGH);
}
if(data < 500 )
{
digitalWrite(led, LOW);
}
else
delay(100);
}

Results and Discussion

Test the magnetic sensor system by using neodymium magnets.

I connected a voltmeter, to check the voltage of the hall sensor which started at 2.44 volts.

Part 1: 0.02s to 0.12s |Moving south towards the hall sensor. The LED was lit the whole time and the voltage increased.

Part 2: 0.13s to 0.18s |Moving south back and forth. The LED lights up when the south pole moves close to the hall sensor and the voltage increased.

Part 3: 0.22s to 0.24s |Moving north towards the hall sensor. The LED does not light up at all and the voltage decreased.

Part 4: 0.25s to 0.36s |Moving north back and forth. The LED lights up when the north pole is far enough away and the voltage decreased.

The hall sensor is suitable for detecting magnet’s movement and can be used for the project that I am currently conducting.

Part 2 : Making the Magnetic Levitation System

Figure 3 The Neodymium magnet (dimension) levitates on the the air core coil.

In this part, I construct the magnetic levitation system as shown in the fig. 3. In this case, the small magnet is able to levitate because of the cancelation of two opposing magnetic forces: the force induced from the coil and the force from the surrounding magnets.

Component

1. Hall sensor UGN3503 x 1

2. transistor BD241C x 1

3. Arduino Mega 2560 x 1

4. Neodymium Magnets

4.1diameter 1.20cm, thickness 0.5cm x 32

4.2 diameter 1.4 cm, thickness o.3 cm x 1

5. Coil diameter 4.22 cm, height 2.5 cm, made from copper wire diameter of 0.7 mm, resistance about 2.6 Ω

6. resistor 500–1000Ω

7. power supply 5V 2A

Procedure

  1. Create air core coil with inner diameter 0.5 cm, outer diameter 4.0 cm, and height 2.5 cm. There is a trick to making it yourself: I used a nut, a bolt and cardboards to make the axis for the coil as shown in figure 4. When finished, I removed the axis.
Figure 4, setup of bolt, nut and cardboard to make an air core coil by myself.

Wrap the wire around the axis until the diameter is around 4 cm. When finished, cut the wire and leave 5 cm for soldering.

2. Solder coil, hall sensor, transistor, and resistor and connect them to Arduino Mega 2560 as shown in figure 5

Figure 5, the schematics of the magnetic levitation system.

3. Writing the code and then upload it onto the Arduino Mega 2560

Code for the entire program

int set_point = 250;
int sensorPin = A2;
int output_pin = 24;
int sensorValue = 0;
void setup() {
Serial.begin(9000);
pinMode(output_pin, OUTPUT);
}
void loop()
{
sensorValue = analogRead(sensorPin);
if(sensorValue <= set_point)
{
digitalWrite(output_pin, LOW);
}
else
{
digitalWrite(output_pin, HIGH);
}
}

4. Before I aligned the magnets, I drew a circle with a diameter of 10 cm and split it into 8 parts. After that, I stacked three magnets on top of each other and put them on the cross sectional point between the circumference and the lines drawn to split the circle. Fix the position of each magnet stack by with the south pole facing up by using another magnet underneath the platform so it is easy to move while also staying in place during testing.

Figure 6

5. Put the coil in the center of the circle with the south pole facing up. Then, put the hall sensor in the center of the coil and tape it.

6. Turn on the power supply and the Arduino Mega 2560 and place the magnet diameter xx , height xx on top of the coil with the north pole facing downwards. Adjust the magnets around the coil if needed until the center magnet is stable.

Trick : to stabilize the center magnet, I put a bolt onto the south pole of the magnet to see when the magnet tilts, if the bolt is tilting to the left, this means that the magnets on the side the bolt is tilted towards is not close enough to the center magnet. The diameter of the center magnet also affects the stability of the magnet when levitating. The diameter of the magnet needs to be big enough to reach the point at which magnetic field is the strongest from the coil.

The magnetic levitation device was made in the simplest way, using the simplest parts and equipment. Therefore, it might not be as capable as commercialized machines. But in the future, I will try to make it more efficient and make it capable of lifting heavier objects.

--

--