Semi-Active Magnetic Levitation System for Education

This paper describes how to construct a low-cost magnetic levitation system (MagLev). The MagLev has been intensively used in engineering education, allowing instructors and students to learn through hands-on experiences of essential concepts, such as electronics, electromagnetism, and control systems. Built from scratch, the MagLev depends only on simple, low-cost components readily available on the market. In addition to showing how to construct the MagLev, this paper presents a semi-active control strategy that seems novel when applied to the MagLev. Experiments performed in the laboratory provide comparisons of the proposed control scheme with the classical PID control. The corresponding real-time experiments illustrate both the effectiveness of the approach and the potential of the MagLev for education.


Introduction
Known in the literature simply as MagLev, the magnetic levitation system comprises an actuator, usually a coil, producing an electromagnetic force that actuates upon an object. The object, usually containing either metal or a magnet, levitates according to the actuator's electromagnetic force. Magnetic levitation systems have been intensively studied due to their wide range of applications, such as in magnetically levitated vehicles [1,2], electrodynamic suspension devices [3,4], magnetic bearings, levitating high-speed rotors [5][6][7], and flywheel energy storage systems [8][9][10]. Levitating an object through an electromagnetic force is usually accompanied by a feedback loop. In this case, a sensor measures the object's position and sends the position information to the controller, which is responsible for regulating the current passing through the coil. The regulated current then produces a corresponding electromagnet force that keeps the object levitating at the desired position. As largely documented in the literature, controlling the object's position is difficult, mainly because the MagLev shows a nonlinear, unstable behavior (e.g., [11][12][13]).
Many attempts have been made to control the MagLev system. For instance, some studies have verified the effectiveness of the classical PID control in levitating an object [14][15][16]. A subsequent study has shown that the sliding-mode control outperforms the classical PID control [17]. Another study has also improved the traditional PID by introducing the socalled fractional-order PID control with a soft computing approach [11]. In [18], the authors present a comparison between the sliding-mode control and the fractional-order sliding mode control, emphasizing the benefits of the latter. A different study raises the concept of generalized PI controller, showing clear advantages with respect to the traditional PID [19]. These investigations together indicate that controlling the MagLev is difficult, motivating the development of a plethora of control strategies. This paper also contributes to this development, as detailed below.
Promoting the study of the MagLev in an educational environment has been the practice of many instructors [20], motivated by the challenge associated with students' learning of how to deal with electromagnetic forces to levitate an object [21]. A famous learning experiment was carried out with the participation of undergraduate students at the Massachusetts Institute of Technology, USA; the instructor asked students to construct their own magnetic levitation kit under the instructor's supervision. This experiment suggested a positive learning experience from the involved students [21,22]. It seems this educational experiment has motivated other researchers to pursue similar investigations, striving to construct their own low-cost magnetic levitation systems [23][24][25].
This paper's main contribution is to show how to construct a novel low-cost magnetic levitation system for educational purposes. Although recognizing many MagLev models in the literature, we present a novel design that employs easily found, low-cost spare parts. Using the components and source code described here, instructors and students can construct their own low-cost MagLev devices. By doing so, instructors and students can enrich the learning experience and practice hands-on training regarding relevant electronic topics, such as sensors, actuators, electronic circuit building, microcontroller programming, electromagnetism, and control systems. This paper describes the step-by-step necessary to construct a MagLev from scratch-all the diagrams and source code are freely available (see Remark 1). Understanding how the electromagnetic force works under feedback control is an important component in the electrical engineering curriculum, as quoted in [21,22]. The main implication of this paper is providing instructors and students with access to that knowledge, thus improving the learning experience. As a by-product, this paper discusses some of the difficulties researchers may face when handling electromagnetic forces. For instance, we bring the reader's attention to the fact that the hysteresis in the electromagnet complicates the task of controlling the MagLev-experimental data illustrate this assertion. Additionally supported by experimental data, we show how ineffective the classical PID control is, thus confirming other investigations [11,17]. In [23], the authors use a position sensor, like us, but also a current sensor, increasing the cost. Moreover, from a control design point of view, using only the Hall effect sensor position is a challenge that has not yet been solved. In addition, to enhance the control algorithm, the authors considered a semi-active system: the input is only activated to face the gravitational force; i.e., the electromagnetic force only works to attract the levitated object. To overcome such difficulties, we present an anti-windup control. Even though the anti-windup control is not novel in the literature, its application in the MagLev seems useful, as the experimental data suggest.
The contributions of this paper can be summarized as follows: • The construction of a low-cost MagLev device for education. • A semi-active PID-like controller for the MagLev, which is an unstable system. The controller uses only the object's position for feedback (no other information is available to the controller).
The remainder of this paper is as follows: Section 2 describes the devices used to assemble the MagLev, and presents some of the device's limitations, highlighting the pros and cons; the control strategy implemented in the Maglev is detailed in Section 2, and the corresponding experimental data, as well as discussions are shown in Section 3. The concluding remarks are stated in Section 4.

Material and Methods
Before presenting the details about the implementation of the magnetic levitation system (MagLev), we recall how a MagLev prototype should work [21,22]. The MagLev contains a coil, which is assembled as an electromagnet. The electromagnet represents the MagLev control system's actuator. By regulating the current passing through the coil, the MagLev controls the electromagnetic force the electromagnet generates. The MagLev employs an algorithm to control the current in the coil. The algorithm is responsible for regulating the induced electromagnetic force, which in turn counteracts the effect of the gravitational force acting on the object. The algorithm requires real-time measurements from the position of the object being levitated. This task is accomplished through a Hall-effect sensor, as detailed below. From the practical viewpoint, the low-cost magnetic levitation prototype consists of an electromagnet, a Hall-effect sensor model A1324 from Allegro(c), an Arduino Uno microcontroller, and a power amplifier, as depicted in Figure 1. It is necessary to use a computer to program the algorithm into the Arduino Uno; in this project, we use a small single-board computer known as Raspberry-Pi (see Figure 2). Figure 3 presents a schematic that displays the construction of the prototype. The Hall-effect sensor monitors the position of the object being levitated. This sensor's analog voltage signal is measured by the Arduino Uno microcontroller (MC) through an analogto-digital converter (ADC). The control algorithm running into the microcontroller then calculates the necessary control value that will be supplied to the electromagnet by using a pulsed-width-modulated output pin. This process is accomplished in the discrete-time domain with a sampling rate fixed at ten milliseconds. Step 2 What you will need Which Raspberry Pi?
There are several models of Raspberry Pi (https://www.raspberrypi.org/products/), and for most people Raspberry Pi 4 Model B is the one to choose. Raspberry Pi 4 Model B is the newest, fastest, and easiest to use.
Raspberry Pi 4 comes with 2GB, 4GB, or 8GB of RAM. For most educational purposes and hobbyist projects, and for use as a desktop computer, 2GB is enough.
Raspberry Pi Zero, Raspberry Pi Zero W, and Raspberry Pi Zero WH are smaller and require less power, so they're useful for portable projects such as robots. It's generally easier to start a project with Raspberry Pi 4, and to move to Raspberry Pi Zero when you have a working prototype that a smaller Raspberry Pi would be useful for.
If you want to buy a Raspberry Pi, head to rpf.io/products (https://rpf.io/products).

A power supply
To connect to a power socket, all Raspberry Pi models have a USB port (the same found on many mobile phones): either USB-C for Raspberry Pi 4, or micro USB for Raspberry Pi 3, 2, and 1.
You need a power supply that provides:

Actuator
The MagLev's actuator is an electromagnet (i.e., a solenoid coil). The electromagnet is handmade, as detailed below. A bolt with dimensions of M20 × 250 mm (hexagon head) is used as the core of the solenoid coil, as suggested in [22,25]. The wire is closely wound around the bolt-the wire used is the enameled copper wire with a diameter of 1 mm (110 m of wire). After it was built, the electromagnet had both the inductance of 15 mH and the resistance of 2.4 Ω. The electromagnet was manually manufactured by wrapping a coil with epoxy around a ferrite core. The circuit that drove the electromagnet in the experiments is depicted in Figure 4. As can be seen, the Arduino Uno generates the PWM signal that controls the current flow through the coil.
The object being levitated is a neodymium disc magnet N42 with a weight of 5.02 g and dimensions of 1/2" × 1/8". The Neodymium disc is attached to a transparent plastic for the sake of a better visual appreciation of the controlled levitation experiment (see Remark 1).

Sensor
The MagLev's sensor chosen to measure the levitated object's position is a Hall-effect sensor; see Figure 5. The motivation for using this kind of sensor stems from the fact that it gives analog measurements, not to mention its low-cost acquisition. An option would be using an optic sensor [26] since it has higher measurement accuracy; however, this sensor seems to be not only more sensitive to disturbances from either light or dust [20] (p. 198) but also more expensive than the Hall-effect sensor [27].

Microcontroller
Arduino Uno is a low-cost, open-source microcontroller board, widely accepted by the research community [28]. In the MagLev, the Arduino Uno implements the control algorithm. Its additional task is to send the corresponding experimental data to a Raspberry Pi board (see [29] for further details about the Raspberry Pi board). Note that any computer can be used in place of the Raspberry Pi board, yet we opt for it because it represents the cheapest solution. Table 1 details the cost of the MagLev, excluding the computer, monitor, keyboard, and mouse, these being necessary to program the source code into the Arduino Uno. It is worth mentioning that the total cost seems compatible with a low-cost device since its assemblage depends only on open-source technology. The components in Table 1 were selected according to what the market had to offer, paying particular attention to the task of minimizing the overall cost.

Control Algorithm
The proposed PID-like controller is model free and does not depend on the system model. However, Appendix A presents the mathematical model to understand the experiment better. The proposed control law aims to enable the system output d to track the ideal output x re f under a semi-active approach. This means that the control is only activated in one direction, opposite to gravitational force, introducing a challenge control design, in contrast with, for instance, [14,18,23,[30][31][32], where the active approach is employed. Moreover, the maximum voltage is stated at 5 V; however, in the literature, its maximum is usually 12 V [19]. Considering that the Arduino Uno works with voltage ranging from 0 to +5 V, we must introduce hard saturation to the input: v = min(max(u k , 0), 5). (1) By recalling that x k represents the distance, and considering that the control strategy requires the speed of the object, we can use the Euler approximation method to compute the object's speed, for example, δx k , as where h > 0 denotes the board sampling time. In the equipment, h = 0.01 seconds is used.
The integral term reads as Parameter a must be introduced to stabilize discrete integration. To avoid the drift of the element I k when the controller becomes saturated (i.e., integrator windup), we impose that If I k > T, then I k = 0, where T > 0 represents a threshold value to be chosen. Additionally, we consider T < 5, since the Arduino Uno converts the voltage representation of 0-5 V into a PWM signal of 0-255 units (8 bits). First, we recall the classical PID control for the sake of experimental comparisons: Appendix A presents the mathematical model for the MagLev system, along with the linearized representation, allowing one to select the PID parameters in (5) using, for instance, pole placement. We suggest that the control algorithm to the MagLev is a straightforward adaptation of the well-known anti-windup PID control [33], which, in our case, reads as The control parameters K p , K d , and K i are positive constants. To perform the comparison, we use the same parameter values in (5) and (6).
It was observed in the laboratory that hysteresis was the main difficulty to achieve the levitation, despite the control method. It is well known that the magnetic flux does not disappear completely, as the electromagnetic core still retains some of its magnetism, even when the current has stopped flowing in the coil [34] (Ch. 1.6). Moving the input signal to the linear region of the hysteretic loop by adding a constant DC current is the simplest method to solve this problem, but with the disadvantage of adding noise, see [35]. Thus, the input voltage must be modified by a constant D > 0 in (5) and (6) to diminish the hysteresis produced by the electromagnet [36]. Thus, the real implemented control is U PID k and U k defined as where the integral term I k is subject to (4). Note that D is added to mitigate the variability of the residual magnetic flux due to the semi-active magnetic control scheme. The term D > 0 then has a practical motivation and it comes from trial and error experiments. A video was made to show this phenomena: https://youtu.be/giaySAGlwAY (access on 7 May 2021).

Limitations
The designed prototype faces some limitations. Some of them are due to the low-cost solution, while others are due to the difficulties posed by handling the electromagnetic forces, as detailed below.

1.
Arduino Uno microcontroller. Despite being a low-cost solution, the Arduino Uno limits the speed of numerical evaluations. In addition, it works only with positive voltage (i.e., u k ≥ 0). To handle this situation, we develop a semi-active control law to compensate the gravitational force, as described in the following.

2.
The electromagnet can produce hysteresis, a phenomenon documented in the literature [34] (Ch. 1.6). Moreover, the Hall-effect sensor may become inaccurate due to the corresponding hysteresis [36]. In this case, the sensor measures an unreal distance d, depending on whether the object moves in the sensor's direction or not- Figure 6 illustrates this phenomenon through experimental data. Additionally, the Hall-effect sensor is sensitive to the magnetic field generated by nearby electrical wires.

3.
The anti-windup PID controller (6) is model free, so it was tuned empirically for the best results possible, minimizing the position error. In addition, the semi-active approach implies a difficulty in obtaining a 'clean' levitation, as shown in the experiment; see https://youtu.be/giaySAGlwAY (access on 7 May 2021).

4.
The PWM signal driving the electromagnet creates a certain level of noise in the circuit, leading to instability. The capacitor attached to the output of the Hall-effect sensor diminishes the amplitude of that noise.

Results and Discussion
Experiments are carried out in the laboratory to check the effectiveness of the proposed MagLev. The experimental data are measured and recorded (see Remark 1 in connection). The goal is to levitate an object at the position x re f = 1.32 cm. For a comparison between the anti-windup control (6) and the classical PID control (5), we consider the statistical mean value error as C N = 1 N ∑ N k=0 |x k − x re f |, where N represents the quantity of steps used in the experiment.
First, the control parameters {K d , K p , K i } in (5) must be defined. The proposed controller is model free and does not depend on the system model. However, Appendix A presents the linearized system, allowing one to select the PID parameters in (5) using, for instance, pole placement. Table 2 presents the control parameter nominal values. Then, experiments are carried out to perform these values minimizing C N [19]. Notice that the hard input saturation (1) limits the trial of these parameters. Appendix B presents the Arduino Uno source code used in this paper.  (5) and (6). Table 2 shows the nominal and empirically optimized control parameters for both controllers, U real PID (7) and U real (8). As shown in Table 3, the proposed control strategy in (8) outperforms the classical PID control. The data are also illustrated in Figures 7 and 8. A visual inspection confirms that the control (8) greatly diminishes the error of the object's position when compared to the error produced by the classical PID control. This finding represents a contribution of (8) in controlling the MagLev. Table 3. Statistical mean absolute error corresponding to the control in the MagLev.

Remark 1.
A video showing the experiment was recorded, and it is freely available at https: //youtu.be/giaySAGlwAY (access on 7 May 2021). All the data and source code used in this manuscript are also freely available on GitHub at github.com/labcontrol-data/MagLev and archived in Zenodo [37] (access on 7 May 2021).

Discussion
From the educational viewpoint, learning how to deal with electromagnet forces represents an exciting challenge. In particular, constructing a MagLev device from scratch is an enriching experience [22].
The experimental data led us to some conclusions. First, building a low-cost MagLev requires specific skills developed during the assemblage process. Unavoidably, the trial and error attempts push the designer's learning process forward. Undoubtedly, a hands-on approach helps to improve their learning curve [22]. Second, designing a control strategy working upon an electromagnet may instigate the designer's curiosity even more. As an example, we developed the control (6), which seems to be novel, despite being a direct adaptation of the anti-windup PID control [33]. However, the main challenge of our proposal is to face the semi-active control system with a hard input saturation.
Certain project limitations are observed, such as the presence of noise, oscillations, and hysteresis (see a discussion in Section 2.6). Some of those limitations come as a side effect of the low-cost strategy, yet overcoming them encourages creativity, which is beneficial from the educational viewpoint.

Concluding Remarks
This paper shows how a prototype of a magnetic levitation system (MagLev) can be built. All the steps necessary to construct the MagLev from scratch are detailed. Creating the MagLev from scratch can be an enriching learning experience, helping instructors and students learn about essential components covered in the curriculum of electrical engineering [22]. Thus, this paper presents a contribution to education, covering a wide range of topics, such as electronics, electromagnetism, and control systems.
The experimental data in particular suggest that the MagLev is a tool that instructors and students can use to learn about control systems in practice. Even novel control strategies can be checked in practice, observing the pros and cons, as documented in Section 3. These features, associated with the low-cost solution, represent an essential step towards facilitating engineering education.

Conflicts of Interest:
The authors declare no conflicts of interest.

Abbreviations
The following abbreviations are used in this manuscript:

ADC
Analog-to-digital converter; GND voltage reference ground; MagLev magnetic levitation; MC microcontroller; PID proportional integral derivative controller; PWM pulse width modulation. Figure 4 shows the electromagnetic system model. The force applied by the electromagnet on the levitating magnet can be closely approximated as

Appendix A. MagLev Mathematical Model
where k is a constant that depends on the geometry of the system [34]. The parameters of the electromagnetic levitation system are determined as in Table A1. It follows from Newton's second law that where d(t) is the vertical position of the levitating magnet measured from the bottom of the coil, i(t) is the current through the electromagnet, m is the mass of the levitating magnet, and g is the acceleration due to gravity. Moreover, from Kirchhoff's voltage law, we have We assume that the current wave will not be dramatically affected by the induced polarization, so we can use the slowly varying amplitude approximation [38], where it is assumed that the envelope of a forward travelling wave pulse varies slowly in time compared to a period or wavelength. Thus, we can consider that di(t) dt 0, and the relation between the control input (voltage v = u(t)) and the current i(t) is as follows: (A4) The MagLev system Equation (A2) is then be the state of the system, the standard state description can be written as (we omit the time variable t): (A6) The equilibrium points of the system are at: where u e = gmR k x 4 1e is the required voltage to suspend the levitating magnet at x T e = [x 1e , x 2e ] = [x re f , 0]. Consider ∆x = x − x e and ∆u = u − u e . Then, Jacobian linearization of the system about the equilibrium point is A PID controller (5) can be designed for this system using the pole assignment method [39], paying special attention to optimizing the disturbance response and providing good damping to the closed-loop response. From (5), U(s) = K p + sK d + K i s , and R(S) is the Laplace transform of the reference. Then, the transfer function of the closed-loop system is ∆(s) Using the experimental parameter value in Table 2, we obtain A = 2.973 × 10 3 and B = −78.34. By pole placement, for instance, the nominal values for control parameters can be found. Then, the PID controller can be tuned by repeated experiments accounting for the control error.

Appendix B. Arduino Uno Code
This section presents the Arduino Uno code used to implement the proposed controller. Note that on the Arduino floating point, mathematical operations are slow, so variable must be carefully defined as float, double, or integer, as needed. An interesting test, especially for graduate students, is to change the storage of the variables and observe the behavior dynamic of the closed-loop system. Additionally, note that Arduino only supports 32bit IEEE754 floats with approximately seven significant digits. s=-1; } // Integral part (discrete evaluation and reset) g=-0.8*gn+h*(xref-x); if (abs(g)>120){ g=0; } // Satured control input u=min(max(-300*(x-xref)*s-0.7*vel-70*g+100,0),255); // Send to Arduino Uno through PIN 3 analogWrite(3,u); // Store last valued variables, needed to // evaluate the velocity and integral factor xn=x; gn=g; }