Quantcast
Viewing latest article 8
Browse Latest Browse All 11

Controlling motors with an MSP430 (or Arduino)

John asked a question about how to control motors from an MSP430, so I thought I’d make a post about it. This is a post-in-progress, since I’m at home looking after my baby boy and he won’t let me write much continuously. If you have suggestions to where it should be improved, let me know and I’ll add stuff. 

Quick overview

First choose motor type. If you use modified servos, you can just connect them to your microcontroller (MCU) and upload the right code. If you use plain DC motors you need a motor driver chip or board, such as the L298. You probably want geared DC motors, since they aren’t as fast as non-geared ones. Connect the driver to your MCU and upload the right code. The speed of the motors is controlled using pulse-width-modulation.

Software-wise you can either choose Arduino (or the MSP430-equivalent called Energia) or go for the C-approach. C is more difficult, but maybe more powerful. Arduino/Energia works great for many robots and I used this for several years before I moved to C as a programming platform.

Read on if you’re interested. More details and instructions below.

Firstly, PWM.

There is an important word that we need to cover first. PWM means pulse-width-modulation. It’s basically a digital pulse that has a certain time on and off. The important thing is either the percentage of time it’s on and the percentage of time it’s off OR in some cases it’s important how long the positive pulse is). Let me do that again.

When you’re controlling a servo, you send the servo a PWM signal and the length of time the pulse is high (1 or positive), decides which angle the servo will have. A typical servo requires a pulse betwen 1-2ms wide, where 1ms will give you 0 degrees and 2ms will give you 180 degrees. 1.5ms will turn it 90 degrees, or to the middle. For a full-rotation-modified servo, you’ll want the signal to be around 1.5ms for the servo to stay still and move it slightly to control the speed.

Image may be NSFW.
Clik here to view.

PWM is also being used to control DC motors through a motor driver, but then in a different way. The PWM signal is simply used to turn on and off the voltage across the motor. So if your signal is 1 all the time, then the motor has the power supply connected all the time, but if your signal only is high 25% of the time, then the motor only runs at 25% of it’s capacity/power. This is very important for controlling the motor and making it run slowly. The frequency at which you shift between 1 and 0 is important. You don’t want it too high because this will make your motor driver inefficient and you don’t want it too low since then you notice it turning on and off.

I’ll get back with code for an MSP430 for generating PWM signals. In the meantime, you can go to  43oh.com and search for PWM. There are many good examples. The timer of the chip is typically used to create a PWM signal. If you are using Arduino or Energia, these have good libraries for creating PWM signals both for servos and DC motors. If you haven’t done any MCU coding before, I suggest you start with one of these. Arduino being for one type of chip (AVR) and Energia being for the MSP430. Your choice, they’re both great platforms. If you want some example code for Energia/Arduino, post a question below and I’ll add some. There is some info on PWM on Arduino/Energia here: “A call to analogWrite() is on a scale of 0 – 255, such that analogWrite(255) requests a 100% duty cycle (always on), and analogWrite(127) is a 50% duty cycle (on half the time) for example.” The analogWrite command in Arduino/Energia is a really useful and simple way of implementing a PWM signal, though you may sometimes have to configure different PWM frequencies for better efficiency.

Motors

Secondly, there are four ‘families’ of motors that are controlled in different ways:

  • Plain DC Motors. These come with and without gears and in all sorts of siizes and shapes. For robots it’s often useful to have gears since the ones without move very fast. You cannot connect a DC motor directly to a microcontroller (MCU) because it draws too much power. A transistor controlling a motor will only allow you to move it in one direction, which is boring. So you may want to use something called an H-Bridge instead. I’m coming back to that later in the post. There are many geared motors that are good to use for robots.
  • Servos. These are also typically DC motors, but come with a motor driver embedded. They typically rotate from 0 to 180 degrees, which is useful for controlling robot arms and turning wheels. But they can be modified quite easily to support full rotation and work just like a geared DC motor with a motor driver. You can find many youtube videos with instructions, but the modification typically has two parts to it. There is a physical notch on one of the gears that stops it from rotating. This has to be removed and since the gear often is made from plastic, it’s quite easy to remove. Also, one of the gears is attached to a potentiometer (a circular variable resistor) which tells the driver which position the motor is in. You have to lock this pot in place so that it’s always in center, and at the same time make sure that the gear it’s attached to isn’t attached to it any more. This modification gives you an ok motor that can be controlled directly from an MSP430 or other microcontroller, without additional electronics. It’s often the cheapest and simplest solution, but may not give you the flexibility you’re after.
  • Brushless motors. These are really fast motors often used in radio-controlled vehicles. They are much more powerful than the brushed DC motors (which I plainly call DC motors above). To control these motors you really need a motor driver called an ESC. Look at the hobbyking.com site for examples of both ESCs and brushless motors. These are crazy fast, very efficient and use huge amounts of current, so you need a powerful battery.
  • Stepper motors. I would say these are not so useful for robots, but they might be useful for certain applications. While with a DC motor you apply a voltage, and the motor moves around, the rotation of the stepper motor (similar to brushless motors) is more controlled. You can choose to rotate it 45 degrees only for example. It’s used a lot in 3d-printers, CNC routers etc where you need to control the position of something very carefully. For these you also need an external motor driver, the simplest being two h-bridges as discussed below.

DC Motor drivers and H-Bridges

You can’t run a motor directly from the microcontroller. You need a driver, which is either a simple transistor (for one-direction) or an H-Bridge (for bi-directional).

The simplest way of connecting the DC motor is to use a transistor such as a MOSFET, which is an ‘electronic switch’ with 3 pins. One pin goes to the MCU, one goes to ground and one goes to to the motor (with the other side of the motor connecting to your positive power supply).

An H-Bridge is a configuration of transistors that help you control a DC motor bi-directionally. They are shaped on the diagram as an H, which is the reason for it being called an H-Bridge. These are now supplied in single chips, often with two H-Bridges on one chip, and are very useful for controlling DC motors.

I typically use an H-Bridge called L298. This can run two motors (or one stepper motor) and is large enough to handle 2A continous current for each motor. Here is a cheap board based on that chip that might be good. I haven’t tried this board though. If you’ve tried it, let me know how it works. The L298 isn’t really for 3volt operation, but in my experience it still works fine. Here is an example circuit diagram using the L298, but the diagram misses protection diodes that should really be there to protect the circuit from the motors as they stop quickly and send back current the wrong way. The pins could have been different, but this diagram is from a board I made that uses the other pins for different things (separate post). There is a post with some very simple example code and a bit more info on this layout here.

Image may be NSFW.
Clik here to view.

And one diagram below from the L298 datasheet showing the diodes that should have been in the circuit above. It shows only one of the two H-Bridges on this chip, to keep things simple. Your motor likes to move and hates to stop, so when you stop it quickly it sends a strong electrical pulse which can destroy your circuit. For this reason it is often good to put a diode across the motor also, which removes that electrical pulse. Diodes are devices that allows current one way and not the other, so you need to make sure you connect the diode the right way. (Google flyback diode and motors).

Image may be NSFW.
Clik here to view.

A smaller alternative which is slightly simpler to prototype with is the L293. It typically works fine up to 1A. It is very similar in operation to the L298, and is a bit easier to place onto a breadboard or prototype board because of it’s standard pins. There is some example code for the L293 here. That code can also be used on L298. I have blown a few L293, but never an L298. Remember the diodes and you should be fine.

Another alternative (one way only) is to use a brushless ESC from Hobbyking (example). These often only move one way (no reverse), but it handles a very high current. These are controlled with a servo signal (PWM with a pulse between 1 and 2ms width). I think they also exist in bi-direction types, but are then more expensive.

Voltages and batteries

The MSP430-chip should not be used with more than 3.6V, as higher voltages will destroy the chip. This is the same as 3 AA NiMH batteries (1.2V each), but Alkaline batteries have 1.5V each, so then you need 2.

Alkaline batteries are generally great, but do not work well with motors and robotics because they are not able to supply enough current. If you’re using Alkaline batteries, you’ll often experience the electronics resetting itself because when the motor turns on, the voltage drops, and the MCU resets. This is a very common starter-problem with DIY robots. There are two solutions to it:

  1. Use separate batteries for your motors and electronics. Remember to use the same ground.
  2. Use batteries that can give enough current. If your battery is above 3.6V, you must use a regulator to supply only 3.6 to your MCU. Often it’s ok to use 3.6V for the MCU and an higher voltage for the motor driver (check the datasheet), but then the motor driver needs to accept 3.6V as the logical voltage for a HIGH signal (it often does). 
Remember to use decoupling capacitors on your electronics circuit. Use two, one larger one (1uF for example) and one smaller one (0.01uF). The reason for using two is that the smaller one is better at removing higher frequencies and the larger one is better at lower frequencies.
The motors often introduce noise, which can actually also be reduced by putting a capacitor across the motor-leads. 
It might be a good idea to put a diode between your positive and negative power supply, but put it the right way. It should be pointing from the negative supply to the positive supply. There are all sorts of ways of preventing damage if the battery polarities are swapped, and this is one. Using a fuse and a diode is even better, since the diode will essentially short circuit your battery if you connect it the wrong way. Here is a suggested circuit showing the power-supply with decoupling capacitors, a fuse (or PTC, which is a resettable fuse) and a protection diode:
Image may be NSFW.
Clik here to view.
The frame itself
This is the time to get creative. It’s easy to build your own frame, but you can of course buy one also. Building one can be as easy as gluing two motors onto a board and attaching something round to the motors as wheels. This one from dfrobot is a simple platform with fast and accurate motors that I like quite a lot. Sparkfun, iteadstudio, Seeedstudio, DFRobot and many more have great kits.
If you’re feeling really adventurous, get a remote controlled car kit from HobbyKing such as this one and attach an MCU and sensors instead of the remote control. That’ll give you an extremely fast robot! Put a GPS on it and the send it off Image may be NSFW.
Clik here to view.
:-)
Sensors
There are a number of sensors you can attach to make this a robot. The most common is a proximity sensor, which you can either make yourself or buy.

Viewing latest article 8
Browse Latest Browse All 11

Trending Articles