Back EMF

As promised, here are some more details on how I implemented back-emf to control the wheel’s rotational speed.

First, what is back-emf exactly?

When you apply voltage to a DC motor, it turns. But also when the DC motor is turning, voltage appears on its pins, like a dynamo. This voltage is the back-emf I’m using here.

The back-emf voltage is proportional to the speed at which the motor turns, so it can be used to measure the motor speed. But this can only be measured when the pins are not already supplied with voltage from the h-bridge. So we need to “cut power” for short time intervals to do the measurement. For more details you can visit this site.

The controller I used is an Arduino Duemilanove and an L298 based H-bridge from solarbotics.

Here is a schematic of the circuit I used:

BackEMF schematic

You’ll notice the h-bridge has one enable pin (E1/E2) for each input pin (I1/I2). I need separate enable pins when I measure the back-emf, because one output has to be set to ground, while the other is on hi-Z.

On most h-bridges I1 and I2 share the same enable pin. So to get around this limitation, I only use of the h-bridge. Then combine two halves to make a full h-bridge with separate E1/E2 pins. Luckily the L298 has 2 full h-bridges on the same chip. So I one L298, with its two h-bridges, controls a single DC motor.

Both motor wires are connected to analog input 0 through identical resistors. But since one of those 2 wires will always be set to the ground (exactly which one depends on motor direction), the other wire will receive the back-emf voltage from the motor. So effectively the three resistors are a voltage divider by 3. Since the theoretical maximum back-emf voltage is 12V, the analog would see a maximum of 4V, which is fine for its 5V range.

When the motor is running, the arduino will run the motor for 100ms, by setting one output to the ground and the other to a PWM signal.

When the 100ms are up, the PWM output will switch to hi-Z for 4ms: first 2ms to allow transient currents to flow out, then another 2ms to measure. The average voltage taken during those last 2ms is then compared to our target voltage. Then PWM signal driving the motor speed is increased or decreased depending on the difference between that average and the target voltage.

Arduino code I used for this project is available at http://code.google.com/p/robidouille/

There is room for improvement in the algorithm.

  • the 2ms wait for transient voltage to dissipate is a maximum value. If we autodetected the end of the transient spike, we could start sampling the input voltage sooner
  • the 2ms measuring and averaging voltages could also be shortened
  • shortening the 4ms during which the motor is not driven, but back-emf voltage is instead measured would allow us to measure more frequently, and react more quickly to changing conditions.

While these 3 points are valid, I’ll wait for the motors to be mounted on an actual platform to make tweaks that are really necessary.

Here is the video showing back-emf in action.

measure for

Visit the Robidouille website for the latest updates to this project

19 Responses to Back EMF

  1. […] I started experimenting with wheels and motors, which led to Hawi: a holonomic drive platform, which instead of using omni-wheels, simply rotates […]

  2. Hassan Çakıcı says:

    Hi
    I’m studying at İzmir Instute of Tecnology, Turkey. I have a this project. I didn’t run arduino. I have a problem. May you help me?

  3. […] motor controller based on the SN74410. I then added some back-emf sensing after stumbling upon this post. The post explains the details, but basically on each motor there’s a pair of voltage […]

  4. Widodo says:

    Dear Emil,
    Thank you for a nice post. could you please share the circuit layout of the L298 IC. I could not figure out how you combine two half of L298 to drive one motor only. can I use other driver e.g. TA7291p? Thank you for your reply.

    • Emil Valkov says:

      I use the L298 through the solarbotics board, it exposes the E1 E2 pins I describe. It’s a great little board, I highly recommend it. There is a link for it in the article, y you can even find the schematic of the board with some googling.

  5. web site says:

    My brother recommended I might like this web site. He was
    totally right. This publish truly made my day. You can not believe simply how
    so much time I had spent for this information!
    Thank you!

  6. robomaster says:

    good job but your schematic is a really like pre school children drawing (dont take it rudeness please) it is nonsense

  7. robomaster says:

    I appreciate your works.
    Regards

  8. robomaster says:

    in The second schematic motor 0 and 1 connections are different from motor 2 and 3.

    • Emil Valkov says:

      The connections are the same. In all cases M1 goes to + and M3 goes to -.
      I only drew motor 2 and 3 upside-down to avoid crossing the wires.

  9. Walt Scrivens says:

    Is anyone reading/updating this page? I have a question regarding the E1/E2 pins being combined on the Solarbotics board.

    • Emil Valkov says:

      Yup, they are combined on the L298 itself. As a result you can only use half of the outputs. So a single L298 can only control a single motor instead of the two it can normally do.
      There is a silver lining though, if you combine the outputs that E1 controls together (same for E2), you can increase the amps it can handle. Of course you should make sure the two corresponding inputs agree, or you’ll get a nasty short.

  10. Walt Scrivens says:

    Thank you!

    From the picture in the video, it looks like you have connected the L298 E1-2 to Arduino Digital 3 and E3-4 to Digital 5. Then run the motor from the Motor 1 terminals. That seems to work, but I’m still trying to figure out all the commands in your sketch.
    My ultimate goal here is a constant-speed controller for a model railroad, so I’ll be adding a speed control pot and direction switch to the code.

    Many thanks for all your work on this!

    Walt

  11. Walt Scrivens says:

    Emil,
    I’m still having trouble with this circuit/sketch. Working from the Youtube picture, it looks like you have the H bridge wired as follows:
    Digital pin 2 —> I1
    Digital pin 3 —> E1/2
    Digital pin 4 —> I2
    Digital pin 5 —> E3/4
    and using Motor 1

    Power connections are straightforward, and I’m using the 5v out to VIN on the arduino, and ground-to ground on both.

    With this connection and your backemf.ino sketch, the l command runs the motor at a high speed, but the backemf function does not work as in the video – the LED does not change in brightness when I mnaually slow the motor.

    The s command is very strange – with s followed by any single digit, the motor oscillates forward/backward and both the red and green LEDs flash.

    With the p command, the motor runs, and when I slow it manually, the opposite direction LED begins glowing.

    Any hints you can give would be appreciated. Thanks

    Walt

  12. chas says:

    you stated:
    So effectively the three resistors are a voltage divider by 3. Since the theoretical maximum back-emf voltage is 12V, the analog would see a maximum of 4V…

    did I misunderstand, I’m not following the circuit analysis if the 3 resistors are the same value, it would appear to be a series-parallel combination.

    • Emil Valkov says:

      The 3 resistors connect to an analog input, so the analog input sees a voltage V = (V1 + V2 + V3) / 3, with V1, V2, V3 the respective voltages on the other side of the resistors.
      V1 and V2 are the voltages on motor leads, V3 is set to GND, so V3 = 0.
      Depending on the direction of movement, the H-Bridge connects one of either V1, or V2 to GND, the other pin gets the voltage dependent on the speed of rotation of the motor.
      So if V1 = 0, V = V2 / 3. If V2 = 0, V = V1 / 3, and in both cases V is the motor voltage divided by 3.

Leave a comment