I am currently working on a magnetic levitation project using a Hall effect sensor for distance measurement. I drive the coil using two 12 V car batteries in series to form a 24 V DC source, a MOSFET and a 3900 Hz PWM signal from my Arduino.

Here is my circuit:

Enter image description here

I have to map the field strength (-512 to 511) created by the coil to the input signal (0 to 255). I expected this to be a linear relation, but my readings were quite noisy:

Enter image description here

I reduced it a lot by adding a bypass capacitor to the Hall effect sensor output (C1):

Enter image description here

However, I don't think it's clean enough to get a stable magnetic levitation.

My questions are:

  1. Why does the signal behave like that? Is it measurement noise, or is it the field generated by the coil messy?

  2. How can I improve this?

Here is the code I use for the measuments:

int field[256];
int val;

void setup() {

    setPwmFrequency(MAGNET_PIN,8); //Sets the PWM frequency to 3900 Hz

    for (int i = 0; i < 256; i++){
        analogWrite(MAGNET_PIN, i);
        val = analogRead(SENSOR_PIN)-512;
        field[i] = val;
        Serial.print(", ");
    analogWrite(MAGNET_PIN, 0);

void loop() {


Here are some pictures of my setup, as requested:

enter image description here

enter image description here

enter image description here

Here is a picture of the signal after adding a 240 Ohm resistor to the LED:

enter image description here

Later today i encountered a new problem. The output now looks like this:

enter image description here

I am a bit confused, because I have not really made any changes. Could it be that my batteries are running low?

  • \$\begingroup\$ Like Will Dean says: Fix the LED, and synchronize the sampling, ... and if you haven't already: add decoupling to the supply. \$\endgroup\$
    – HKOB
    Feb 12, 2015 at 12:11
  • \$\begingroup\$ Your ADC may have these non-linearities but without scales on the graph how can anyone tell? \$\endgroup\$
    – Andy aka
    Feb 12, 2015 at 12:41
  • \$\begingroup\$ Like I said in my original post, the first axis is an 8-bit, 5V PWM signal to the gate of my MOSFET. So the values between 0 and 255 represents 0 to 24 volts across the coil. The second axis represents a 10-bit analogRead from the sensor. Originally, the values outputted by the sensor ranged from 0 to 1023, where 512 is the null output. I subtracted 512 so that the range is -512 to 511 and the null reading is 0. The sensitivity of the sensor is 3.125 mV/G. A reading of +/- 512 is represents a field of strength +/- 7812.5 Gauss. \$\endgroup\$
    – Torben
    Feb 12, 2015 at 15:39
  • 1
    \$\begingroup\$ I don't see any fuses in your circuit, so as far as I'm concerned, you might have burned down your house already :( \$\endgroup\$ Feb 12, 2015 at 16:37
  • \$\begingroup\$ Where is the coil? Where is the sensor? It seems to me that your current breadboard setup is very problematic, with very large signal loops that can pick up all sorts of junk. The entire coil circuit (buffer, mosfet, decoupling capacitor, etc.) needs to be right at the coil. \$\endgroup\$ Feb 12, 2015 at 22:32

2 Answers 2


There are several problems, some of which are mentioned in other answers. I'll reiterate them for completeness's sake:

  1. The Arduino's output is loaded with the mosfet's gate capacitance. That can be several nF. Given that Arduino's outputs switch in ~10ns, so you're creating large current spikes with that load. The path of those spikes is not controlled and pollutes the rest of your circuit.

  2. The gate input is clamped to the LED's forward voltage. The mosfet might not be fully turned on.

  3. The output pin's driver is likely driving its full output current into the LED. Since that current is very likely to share lots of circuitry with the analog reference signal, you're corrupting your ADC reference voltage.

  4. The car battery can push hundreds of amperes through your circuit. With no fuse, you will burn your lab/house down.

  5. The inductor will likely need a snubber for EMI minimization.

  6. You have no slew rate control for mosfet turn-on/turn-off. You're likely switching the mosfet way too fast. You need to trade off some heat dissipation for improved electromagnetic compatibility.

  7. The fast switching PWM current path needs to be kept as short as possible: you need to isolate the inductor-switch loop from the battery and the rest of the circuit.

  8. There may be capacitive coupling between the inductor and the Hall sensor.

Below is an attempt at addressing all of the shortcomings.

Let's say that we want to keep the mosfet switching times roughly at 2% of the PWM period. The mosfet should switch in approximately 2.5us. The low-pass filter formed by the gate drive resistance and the gate capacitance should have a time constant of, say, half that value. Thus, assuming a 1nF gate capacitance, we need an equivalent 200 Ohm series resistor in the gate drive circuit.

Ensure that the fast-switching current loop, drawn in thick line, is kept as short as possible. The decoupling C1 needs to be a low-ESR electrolytic. The snubber C2/R3 can be designed following this procedure. The buffer U2 can be 74HC1G125 or similar. It needs to have its power supply decoupled with a 47nF capacitor, and have its output enabled (OE# input driven low). U2 needs to be close to M1. To ensure fastest turn-off, the D2/D3 is a pair of back-to-back 27V Zeners. F1 should be sized to accommodate the power consumption of L1. The GND of U2 needs to be tied to the star point at M1's drain. Ideally you'd also have ferrite beads between U2's VCC and the 3.3V supply rail, as well as between the star point at top of L1 and the output of the fuse. The FB1 is a ferrite bead - choose the largest impedance you can find that will handle 100mA.


simulate this circuit – Schematic created using CircuitLab

To minimize the Hall sensor's capacitive coupling to the inductor, there should be a non-magnetic, conductive shield around it. It won't hurt to make an explicit low-pass filter on the sensor's output, as well as decouple any high-frequency content with a ferrite bead FB2. Choose the largest impedance you can find that will handle 15mA.


simulate this circuit

I have added a representation of how the circuit might look on a breadboard. I've paid particular attention to minimizing the coil circuit's loop area. I've used 74HC14 inverters as a buffer, and I'm paralleling the outputs that drive the LED and the gate.

Due to 123D Circuits' limitations, I had to:

  • Represent the 24V battery and fuse by an AA battery holder and a resistor.

  • Represent the Hall sensor by a potentiometer.

  • Represent FB1 by a small inductor.

Breadboard layout

  • \$\begingroup\$ Thank you for another great answer! What is the purpose of U2? I have never worked with buffers before, and I do not have one at hand. Is it essential? \$\endgroup\$
    – Torben
    Feb 12, 2015 at 19:44
  • \$\begingroup\$ The buffer output can provide more current than Arduino's max output current. The buffer does not change the logic signal (IN=OUT). \$\endgroup\$
    – Triak
    Feb 12, 2015 at 19:51
  • \$\begingroup\$ @Torben The buffer's purpose is to redirect the gate charging current away from the Arduino. Remember that in your use case, Arduino is an analog, noise-sensitive part. \$\endgroup\$ Feb 12, 2015 at 20:00
  • \$\begingroup\$ @Triak The maximum current through R4, given 5V logic, is 25mA, so Arduino could provide it, but you don't want it to, since it will couple into the analog signals. \$\endgroup\$ Feb 12, 2015 at 20:07
  • \$\begingroup\$ Okey, that makes sense. I will order one of them then. What does FB1 and FB2 represent in the new diagram? \$\endgroup\$
    – Torben
    Feb 12, 2015 at 22:09

I'm not sure what's in the Arduino on that PWM output, but it looks very odd to apparently have no gate resistor into a MOSFET, and no current limit resistor on the LED.

At the point you start to turn onto the MOSFET, you have a short-circuit to ground in the form of the gate capacitance - on a big MOSFET that's quite a lot of capacitance, and while that's charging, the Arduino's working very hard indeed to supply that current.

Once the gate capacitance charges enough to reach the forward voltage (Vf) of the LED, that turns on and represents a short-circuit to Vf.

I couldn't say whether either of these things is causing noise on the analogue input, but they're unlikely to help, as they're probably causing a lot of noise on the Arduino.

Separately, and I don't know if you're already doing this, but in this type of system I would usually try to arrange to take the analogue measurement synchronously with the PWM, so that I could consistently avoid the noise around the switching of the load. If you're sampling asynchronously to the PWM, that may well be the cause of occasional big spikes.

  • \$\begingroup\$ - Great Answer!! \$\endgroup\$ Feb 12, 2015 at 13:21
  • \$\begingroup\$ Thank you very much for a great answer. I am quite new to electronics, so I had not considered these problems. I will add a 220 Ohm resistor to my LED. If I add a resistor to the gate of my MOSFET, will this reduce the power to my coil? What value should I use for this resistor? I got some of the ideas from this project: forum.arduino.cc/index.php?topic=89241.0. It does not use such a resistor. I did not quite understand what you meant avout taking the analogue measurement synchronously with the PWM. Could you elaborate a little? \$\endgroup\$
    – Torben
    Feb 12, 2015 at 15:54
  • \$\begingroup\$ I added the code I used for the measurements to my original post. \$\endgroup\$
    – Torben
    Feb 12, 2015 at 15:59
  • 1
    \$\begingroup\$ The gate resistor will not directly affect how much current flows through the MOSFET - what it does is affect how quickly it switches on, because it limits the current that can flow into the gate capacitance, and it's charging-up that 'capacitor' which turns on the MOSFET. Because the MOSFET turns on and off more slowly, it will dissipate more power while it's turning on and off, which make it run hotter - this might or might not become a problem. There are too many variables here to give a general answer. \$\endgroup\$
    – user1844
    Feb 12, 2015 at 16:54
  • 1
    \$\begingroup\$ Taking a measurement synchronously would imply that you trigger the ADC to take a sample or do a conversion, using code or hardware which is linked to the PWM output in some way. This means that your samples exactly line-up with PWM pulses in some fixed way. I'm not an Arduino expert, so I don't know if you can do this easily using the very high-level tools you're using at the moment. It might be worth asking this specific question or searching for existing Arduino answers, because it's a pretty common requirement. \$\endgroup\$
    – user1844
    Feb 12, 2015 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.